source: lib/BarnOwl/Module/Twitter.pm @ 4bd9327

release-1.7release-1.8release-1.9
Last change on this file since 4bd9327 was 4bd9327, checked in by Kevin Riggle <kevinr@free-dissociation.com>, 12 years ago
Refactor commands which do something on the default account handle
  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[e54f2fa]1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Twitter
7
8=head1 DESCRIPTION
9
10Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter
11
12=cut
13
14package BarnOwl::Module::Twitter;
15
[159aaad]16our $VERSION = 0.2;
[af3415c]17
[e54f2fa]18use Net::Twitter;
19use JSON;
[efcd223]20use List::Util qw(first);
[e54f2fa]21
22use BarnOwl;
23use BarnOwl::Hooks;
[159aaad]24use BarnOwl::Module::Twitter::Handle;
[e54f2fa]25
[d748296]26our @twitter_handles = ();
27our $default_handle = undef;
[9bedca0]28my $user     = BarnOwl::zephyr_getsender();
29my ($class)  = ($user =~ /(^[^@]+)/);
30my $instance = "status";
31my $opcode   = "twitter";
[d658c29]32my $use_reply_to = 0;
[159aaad]33my $next_service_to_poll = 0;
[188b745]34
[d689fc7]35my $desc = <<'END_DESC';
[d1bb4f3]36BarnOwl::Module::Twitter will watch for authentic zephyrs to
37-c $twitter:class -i $twitter:instance -O $twitter:opcode
38from your sender and mirror them to Twitter.
39
40A value of '*' in any of these fields acts a wildcard, accepting
41messages with any value of that field.
42END_DESC
[d689fc7]43BarnOwl::new_variable_string(
44    'twitter:class',
45    {
46        default     => $class,
47        summary     => 'Class to watch for Twitter messages',
48        description => $desc
49    }
50);
51BarnOwl::new_variable_string(
52    'twitter:instance',
53    {
54        default => $instance,
55        summary => 'Instance on twitter:class to watch for Twitter messages.',
56        description => $desc
57    }
58);
59BarnOwl::new_variable_string(
60    'twitter:opcode',
61    {
62        default => $opcode,
63        summary => 'Opcode for zephyrs that will be sent as twitter updates',
64        description => $desc
65    }
66);
[e54f2fa]67
[927c186]68BarnOwl::new_variable_bool(
69    'twitter:poll',
70    {
71        default => 1,
72        summary => 'Poll Twitter for incoming messages',
73        description => "If set, will poll Twitter every minute for normal updates,\n"
74        . 'and every two minutes for direct message'
75     }
76 );
77
[159aaad]78sub fail {
79    my $msg = shift;
80    undef @twitter_handles;
81    BarnOwl::admin_message('Twitter Error', $msg);
82    die("Twitter Error: $msg\n");
83}
84
[d775050]85my $conffile = BarnOwl::get_config_dir() . "/twitter";
86open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
[159aaad]87my $raw_cfg = do {local $/; <$fh>};
[e54f2fa]88close($fh);
89eval {
[159aaad]90    $raw_cfg = from_json($raw_cfg);
[e54f2fa]91};
[9bedca0]92if($@) {
[159aaad]93    fail("Unable to parse $conffile: $@");
[e54f2fa]94}
95
[159aaad]96$raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
97
98for my $cfg (@$raw_cfg) {
99    my $twitter_args = { username   => $cfg->{user} || $user,
100                        password   => $cfg->{password},
101                        source     => 'barnowl', 
102                    };
103    if (defined $cfg->{service}) {
104        my $service = $cfg->{service};
105        $twitter_args->{apiurl} = $service;
106        my $apihost = $service;
107        $apihost =~ s/^\s*http:\/\///;
108        $apihost =~ s/\/.*$//;
109        $apihost .= ':80' unless $apihost =~ /:\d+$/;
110        $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
111        my $apirealm = "Laconica API";
112        $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
113    } else {
114        $cfg->{service} = 'http://twitter.com';
115    }
[b56f2c3]116
[39dd366]117    my $twitter_handle;
[d748296]118    eval {
[39dd366]119         $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
[d748296]120    };
[39dd366]121    if ($@) {
122        BarnOwl::error($@);
123        next;
124    }
125    push @twitter_handles, $twitter_handle;
[efcd223]126}
127
128$default_handle = first {$_->{cfg}->{default_sender}} @twitter_handles;
129if (!$default_handle && @twitter_handles) {
130    $default_handle = $twitter_handles[0];
[b56f2c3]131}
132
[d1bb4f3]133sub match {
134    my $val = shift;
135    my $pat = shift;
136    return $pat eq "*" || ($val eq $pat);
137}
138
[e54f2fa]139sub handle_message {
140    my $m = shift;
141    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
142    if($m->sender eq $user
[d1bb4f3]143       && match($m->class, $class)
144       && match($m->instance, $instance)
145       && match($m->opcode, $opcode)
[e54f2fa]146       && $m->auth eq 'YES') {
[159aaad]147        for my $handle (@twitter_handles) {
[efcd223]148            $handle->twitter($m->body) if $handle->{cfg}->{publish_tweets};
[159aaad]149        }
[e54f2fa]150    }
151}
152
[8618438]153sub poll_messages {
[d748296]154    return unless @twitter_handles;
155
[159aaad]156    my $handle = $twitter_handles[$next_service_to_poll];
157    $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles);
158   
[efcd223]159    $handle->poll_twitter() if $handle->{cfg}->{poll_for_tweets};
160    $handle->poll_direct() if $handle->{cfg}->{poll_for_dms};
161}
162
163sub find_account {
164    my $name = shift;
165    my $handle = first {$_->{cfg}->{account_nickname} eq $name} @twitter_handles;
166    if ($handle) {
167        return $handle;
168    } else {
169        die("No such Twitter account: $name\n");
170    }
[927c186]171}
172
[159aaad]173sub twitter {
174    my $account = shift;
175
176    my $sent = 0;
177    if (defined $account) {
[efcd223]178        my $handle = find_account($account);
179        $handle->twitter(@_);
[159aaad]180    } 
181    else {
182        # broadcast
183        for my $handle (@twitter_handles) {
[efcd223]184            $handle->twitter(@_) if $handle->{cfg}->{publish_tweets};
[159aaad]185        }
[8618438]186    }
187}
188
[4bd9327]189# do_for_default_account( SUB( HANDLE, ARGS...), ACCOUNT, ARGS... )
190sub do_for_default_account {
191    my $lambda = shift;
192    die ("do_for_default_account has nothing to do") unless defined $lambda && ref($lambda) eq 'CODE';
[159aaad]193    my $account = shift;
194
195    if (defined $account) {
[efcd223]196        my $handle = find_account($account);
[4bd9327]197        $lambda->($handle, @_);
[efcd223]198    } elsif (defined $default_handle) {
[4bd9327]199        $lambda->($default_handle, @_);
[9bedca0]200    }
[159aaad]201    else {
[4bd9327]202        $lambda->($twitter_handles[0], @_);
[927c186]203    }
204}
205
[4bd9327]206sub twitter_direct {
[159aaad]207    my $account = shift;
[4bd9327]208    do_for_default_account( sub { my $handle = shift; $handle->twitter_direct(@_); }, $account, @_);
209}
[159aaad]210
[4bd9327]211sub twitter_atreply {
212    my $account = shift;
213    do_for_default_account( sub { my $handle = shift; $handle->twitter_atreply(@_); }, $account, @_);
[6babb75]214}
215
[513da71]216sub twitter_follow {
217    my $account = shift;
[4bd9327]218    do_for_default_account( sub { my $handle = shift; $handle->twitter_follow(@_); }, $account, @_);
[513da71]219}
220
221sub twitter_unfollow {
222    my $account = shift;
[4bd9327]223    do_for_default_account( sub { my $handle = shift; $handle->twitter_unfollow(@_); }, $account, @_);
[513da71]224}
225
[4cf4067]226BarnOwl::new_command(twitter => \&cmd_twitter, {
227    summary     => 'Update Twitter from BarnOwl',
[159aaad]228    usage       => 'twitter [ACCOUNT] [MESSAGE]',
229    description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.'
230    . "\nIf no ACCOUNT is provided, update all services which have publishing enabled."
[4cf4067]231    . "\nOtherwise, prompt for a status message to use."
232   });
233
[927c186]234BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, {
235    summary     => 'Send a Twitter direct message',
[159aaad]236    usage       => 'twitter-direct USER [ACCOUNT]',
237    description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,'
238    . "\nor first service if no default is provided)"
[927c186]239   });
240
[6babb75]241BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); },
242    {
243    summary     => 'Send a Twitter @ message',
[159aaad]244    usage       => 'twitter-atreply USER [ACCOUNT]',
245    description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' 
[513da71]246    . "\nor first service if no default is provided)"
247    }
248);
249
250BarnOwl::new_command( 'twitter-follow' => sub { cmd_twitter_follow(@_); },
251    {
252    summary     => 'Follow a user on Twitter',
253    usage       => 'twitter-follow USER [ACCOUNT]',
254    description => 'Follow USER on Twitter ACCOUNT (defaults to default_sender, or first service'
255    . "\nif no default is provided)"
[6babb75]256    }
257);
258
[513da71]259BarnOwl::new_command( 'twitter-unfollow' => sub { cmd_twitter_unfollow(@_); },
260    {
261    summary     => 'Stop following a user on Twitter',
262    usage       => 'twitter-unfollow USER [ACCOUNT]',
263    description => 'Stop following USER on Twitter ACCOUNT (defaults to default_sender, or first'
264    . "\nservice if no default is provided)"
265    }
266);
[6babb75]267
[4cf4067]268sub cmd_twitter {
269    my $cmd = shift;
[159aaad]270    my $account = shift;
271    if (defined $account) {
272        if(@_) {
273            my $status = join(" ", @_);
274            twitter($account, $status);
275            return;
276        }
[4cf4067]277    }
[159aaad]278    BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
[4cf4067]279}
280
[927c186]281sub cmd_twitter_direct {
282    my $cmd = shift;
283    my $user = shift;
284    die("Usage: $cmd USER\n") unless $user;
[159aaad]285    my $account = shift;
286    BarnOwl::start_edit_win("$cmd $user" . (defined $account ? " $account" : ""), sub{twitter_direct($account, $user, shift)});
[927c186]287}
288
[6babb75]289sub cmd_twitter_atreply {
290    my $cmd  = shift;
[acdd52e]291    my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n");
292    my $id   = shift;
[159aaad]293    my $account = shift;
294    BarnOwl::start_edit_win("Reply to \@" . $user . (defined $account ? " on $account" : ""), sub { twitter_atreply($account, $user, $id, shift) });
[6babb75]295}
296
[513da71]297sub cmd_twitter_follow {
298    my $cmd = shift;
299    my $user = shift;
300    die("Usage: $cmd USER\n") unless $user;
301    my $account = shift;
302    twitter_follow($account, $user);
303}
304
305sub cmd_twitter_unfollow {
306    my $cmd = shift;
307    my $user = shift;
308    die("Usage: $cmd USER\n") unless $user;
309    my $account = shift;
310    twitter_unfollow($account, $user);
311}
312
[72b61dd]313eval {
314    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
[8618438]315    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
[72b61dd]316};
317if($@) {
318    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
[8618438]319    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
[72b61dd]320}
[e54f2fa]321
[f93b81b]322BarnOwl::filter(qw{twitter type ^twitter$});
[8618438]323
[e54f2fa]3241;
Note: See TracBrowser for help on using the repository browser.