source: lib/BarnOwl/Module/Twitter.pm @ 1c45137

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 1c45137 was 1c45137, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Don't copy messages from zephyr when publish_tweets => false is specified.
  • Property mode set to 100644
File size: 8.4 KB
Line 
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
16our $VERSION = 0.2;
17
18use Net::Twitter;
19use JSON;
20
21use BarnOwl;
22use BarnOwl::Hooks;
23use BarnOwl::Module::Twitter::Handle;
24
25our @twitter_handles = ();
26our $default_handle = undef;
27my $user     = BarnOwl::zephyr_getsender();
28my ($class)  = ($user =~ /(^[^@]+)/);
29my $instance = "status";
30my $opcode   = "twitter";
31my $use_reply_to = 0;
32my $next_service_to_poll = 0;
33
34my $desc = <<'END_DESC';
35BarnOwl::Module::Twitter will watch for authentic zephyrs to
36-c $twitter:class -i $twitter:instance -O $twitter:opcode
37from your sender and mirror them to Twitter.
38
39A value of '*' in any of these fields acts a wildcard, accepting
40messages with any value of that field.
41END_DESC
42BarnOwl::new_variable_string(
43    'twitter:class',
44    {
45        default     => $class,
46        summary     => 'Class to watch for Twitter messages',
47        description => $desc
48    }
49);
50BarnOwl::new_variable_string(
51    'twitter:instance',
52    {
53        default => $instance,
54        summary => 'Instance on twitter:class to watch for Twitter messages.',
55        description => $desc
56    }
57);
58BarnOwl::new_variable_string(
59    'twitter:opcode',
60    {
61        default => $opcode,
62        summary => 'Opcode for zephyrs that will be sent as twitter updates',
63        description => $desc
64    }
65);
66
67BarnOwl::new_variable_bool(
68    'twitter:poll',
69    {
70        default => 1,
71        summary => 'Poll Twitter for incoming messages',
72        description => "If set, will poll Twitter every minute for normal updates,\n"
73        . 'and every two minutes for direct message'
74     }
75 );
76
77sub fail {
78    my $msg = shift;
79    undef @twitter_handles;
80    BarnOwl::admin_message('Twitter Error', $msg);
81    die("Twitter Error: $msg\n");
82}
83
84my $conffile = BarnOwl::get_config_dir() . "/twitter";
85open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
86my $raw_cfg = do {local $/; <$fh>};
87close($fh);
88eval {
89    $raw_cfg = from_json($raw_cfg);
90};
91if($@) {
92    fail("Unable to parse $conffile: $@");
93}
94
95$raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
96
97for my $cfg (@$raw_cfg) {
98    my $twitter_args = { username   => $cfg->{user} || $user,
99                        password   => $cfg->{password},
100                        source     => 'barnowl', 
101                    };
102    if (defined $cfg->{service}) {
103        my $service = $cfg->{service};
104        $twitter_args->{apiurl} = $service;
105        my $apihost = $service;
106        $apihost =~ s/^\s*http:\/\///;
107        $apihost =~ s/\/.*$//;
108        $apihost .= ':80' unless $apihost =~ /:\d+$/;
109        $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
110        my $apirealm = "Laconica API";
111        $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
112    } else {
113        $cfg->{service} = 'http://twitter.com';
114    }
115
116    my $twitter_handle;
117    eval {
118         $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
119    };
120    if ($@) {
121        BarnOwl::error($@);
122        next;
123    }
124    push @twitter_handles, $twitter_handle;
125    $default_handle = $twitter_handle if (!defined $twitter_handle && exists $cfg->{default_sender} && $cfg->{default_sender});
126}
127
128sub match {
129    my $val = shift;
130    my $pat = shift;
131    return $pat eq "*" || ($val eq $pat);
132}
133
134sub handle_message {
135    my $m = shift;
136    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
137    if($m->sender eq $user
138       && match($m->class, $class)
139       && match($m->instance, $instance)
140       && match($m->opcode, $opcode)
141       && $m->auth eq 'YES') {
142        for my $handle (@twitter_handles) {
143            $handle->twitter($m->body) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets});
144        }
145    }
146}
147
148sub poll_messages {
149    return unless @twitter_handles;
150
151    my $handle = $twitter_handles[$next_service_to_poll];
152    $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles);
153   
154    $handle->poll_twitter() if (!exists $handle->{cfg}->{poll_for_tweets} || $handle->{cfg}->{poll_for_tweets});
155    $handle->poll_direct() if (!exists $handle->{cfg}->{poll_for_dms} || $handle->{cfg}->{poll_for_dms});
156}
157
158sub twitter {
159    my $account = shift;
160
161    my $sent = 0;
162    if (defined $account) {
163        for my $handle (@twitter_handles) {
164            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
165                $handle->twitter(@_);
166                $sent = 1;
167                last;
168            }
169        }
170        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
171    } 
172    else {
173        # broadcast
174        for my $handle (@twitter_handles) {
175            $handle->twitter(@_) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets});
176        }
177    }
178}
179
180sub twitter_direct {
181    my $account = shift;
182
183    my $sent = 0;
184    if (defined $account) {
185        for my $handle (@twitter_handles) {
186            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
187                $handle->twitter_direct(@_);
188                $sent = 1;
189                last;
190            }
191        }
192        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
193    }
194    elsif (defined $default_handle) {
195        $default_handle->twitter_direct(@_);
196    }
197    else {
198        $twitter_handles[0]->twitter_direct(@_);
199    }
200}
201
202sub twitter_atreply {
203    my $account = shift;
204
205    my $sent = 0;
206    if (defined $account) {
207        for my $handle (@twitter_handles) {
208            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
209                $handle->twitter_atreply(@_);
210                $sent = 1;
211                last;
212            }
213        }
214        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
215    }
216    elsif (defined $default_handle) {
217        $default_handle->twitter_atreply(@_);
218    }
219    else {
220        $twitter_handles[0]->twitter_atreply(@_);
221    }
222}
223
224BarnOwl::new_command(twitter => \&cmd_twitter, {
225    summary     => 'Update Twitter from BarnOwl',
226    usage       => 'twitter [ACCOUNT] [MESSAGE]',
227    description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.'
228    . "\nIf no ACCOUNT is provided, update all services which have publishing enabled."
229    . "\nOtherwise, prompt for a status message to use."
230   });
231
232BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, {
233    summary     => 'Send a Twitter direct message',
234    usage       => 'twitter-direct USER [ACCOUNT]',
235    description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,'
236    . "\nor first service if no default is provided)"
237   });
238
239BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); },
240    {
241    summary     => 'Send a Twitter @ message',
242    usage       => 'twitter-atreply USER [ACCOUNT]',
243    description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' 
244    . "or first service if no default is provided)"
245    }
246);
247
248
249sub cmd_twitter {
250    my $cmd = shift;
251    my $account = shift;
252    if (defined $account) {
253        if(@_) {
254            my $status = join(" ", @_);
255            twitter($account, $status);
256            return;
257        }
258    }
259    BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
260}
261
262sub cmd_twitter_direct {
263    my $cmd = shift;
264    my $user = shift;
265    die("Usage: $cmd USER\n") unless $user;
266    my $account = shift;
267    BarnOwl::start_edit_win("$cmd $user" . (defined $account ? " $account" : ""), sub{twitter_direct($account, $user, shift)});
268}
269
270sub cmd_twitter_atreply {
271    my $cmd  = shift;
272    my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n");
273    my $id   = shift;
274    my $account = shift;
275    BarnOwl::start_edit_win("Reply to \@" . $user . (defined $account ? " on $account" : ""), sub { twitter_atreply($account, $user, $id, shift) });
276}
277
278eval {
279    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
280    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
281};
282if($@) {
283    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
284    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
285}
286
287BarnOwl::filter(qw{twitter type ^twitter$});
288
2891;
Note: See TracBrowser for help on using the repository browser.