1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Twitter |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Twitter; |
---|
15 | |
---|
16 | our $VERSION = 0.1; |
---|
17 | |
---|
18 | use Net::Twitter; |
---|
19 | use JSON; |
---|
20 | |
---|
21 | use BarnOwl; |
---|
22 | use BarnOwl::Hooks; |
---|
23 | use BarnOwl::Message::Twitter; |
---|
24 | use HTML::Entities; |
---|
25 | |
---|
26 | our $twitter; |
---|
27 | my $user = BarnOwl::zephyr_getsender(); |
---|
28 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
29 | my $instance = "status"; |
---|
30 | my $opcode = "twitter"; |
---|
31 | my $use_reply_to = 0; |
---|
32 | |
---|
33 | sub fail { |
---|
34 | my $msg = shift; |
---|
35 | undef $twitter; |
---|
36 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
37 | die("Twitter Error: $msg\n"); |
---|
38 | } |
---|
39 | |
---|
40 | if($Net::Twitter::VERSION >= 2.06) { |
---|
41 | $use_reply_to = 1; |
---|
42 | } |
---|
43 | |
---|
44 | my $desc = <<'END_DESC'; |
---|
45 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
46 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
47 | from your sender and mirror them to Twitter. |
---|
48 | |
---|
49 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
50 | messages with any value of that field. |
---|
51 | END_DESC |
---|
52 | BarnOwl::new_variable_string( |
---|
53 | 'twitter:class', |
---|
54 | { |
---|
55 | default => $class, |
---|
56 | summary => 'Class to watch for Twitter messages', |
---|
57 | description => $desc |
---|
58 | } |
---|
59 | ); |
---|
60 | BarnOwl::new_variable_string( |
---|
61 | 'twitter:instance', |
---|
62 | { |
---|
63 | default => $instance, |
---|
64 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
65 | description => $desc |
---|
66 | } |
---|
67 | ); |
---|
68 | BarnOwl::new_variable_string( |
---|
69 | 'twitter:opcode', |
---|
70 | { |
---|
71 | default => $opcode, |
---|
72 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
73 | description => $desc |
---|
74 | } |
---|
75 | ); |
---|
76 | |
---|
77 | BarnOwl::new_variable_bool( |
---|
78 | 'twitter:poll', |
---|
79 | { |
---|
80 | default => 1, |
---|
81 | summary => 'Poll Twitter for incoming messages', |
---|
82 | description => "If set, will poll Twitter every minute for normal updates,\n" |
---|
83 | . 'and every two minutes for direct message' |
---|
84 | } |
---|
85 | ); |
---|
86 | |
---|
87 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
88 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
89 | my $cfg = do {local $/; <$fh>}; |
---|
90 | close($fh); |
---|
91 | eval { |
---|
92 | $cfg = from_json($cfg); |
---|
93 | }; |
---|
94 | if($@) { |
---|
95 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
96 | } |
---|
97 | |
---|
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 | } |
---|
113 | |
---|
114 | $twitter = Net::Twitter->new(%$twitter_args); |
---|
115 | |
---|
116 | if(!defined($twitter->verify_credentials())) { |
---|
117 | fail("Invalid twitter credentials"); |
---|
118 | } |
---|
119 | |
---|
120 | our $last_poll = 0; |
---|
121 | our $last_direct_poll = 0; |
---|
122 | our $last_id = undef; |
---|
123 | our $last_direct = undef; |
---|
124 | |
---|
125 | unless(defined($last_id)) { |
---|
126 | eval { |
---|
127 | $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; |
---|
128 | }; |
---|
129 | $last_id = 0 unless defined($last_id); |
---|
130 | } |
---|
131 | |
---|
132 | unless(defined($last_direct)) { |
---|
133 | eval { |
---|
134 | $last_direct = $twitter->direct_messages()->[0]{id}; |
---|
135 | }; |
---|
136 | $last_direct = 0 unless defined($last_direct); |
---|
137 | } |
---|
138 | |
---|
139 | eval { |
---|
140 | $twitter->{ua}->timeout(1); |
---|
141 | }; |
---|
142 | |
---|
143 | sub match { |
---|
144 | my $val = shift; |
---|
145 | my $pat = shift; |
---|
146 | return $pat eq "*" || ($val eq $pat); |
---|
147 | } |
---|
148 | |
---|
149 | sub handle_message { |
---|
150 | my $m = shift; |
---|
151 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
152 | if($m->sender eq $user |
---|
153 | && match($m->class, $class) |
---|
154 | && match($m->instance, $instance) |
---|
155 | && match($m->opcode, $opcode) |
---|
156 | && $m->auth eq 'YES') { |
---|
157 | twitter($m->body); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | sub poll_messages { |
---|
162 | poll_twitter(); |
---|
163 | poll_direct(); |
---|
164 | } |
---|
165 | |
---|
166 | sub twitter_error { |
---|
167 | my $ratelimit = $twitter->rate_limit_status; |
---|
168 | unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') { |
---|
169 | # Twitter's just sucking, sleep for 5 minutes |
---|
170 | $last_direct_poll = $last_poll = time + 60*5; |
---|
171 | # die("Twitter seems to be having problems.\n"); |
---|
172 | return; |
---|
173 | } |
---|
174 | if(exists($ratelimit->{remaining_hits}) |
---|
175 | && $ratelimit->{remaining_hits} <= 0) { |
---|
176 | $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds}; |
---|
177 | die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); |
---|
178 | } elsif(exists($ratelimit->{error})) { |
---|
179 | die("Twitter: ". $ratelimit->{error} . "\n"); |
---|
180 | $last_direct_poll = $last_poll = time + 60*20; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | sub poll_twitter { |
---|
185 | return unless ( time - $last_poll ) >= 60; |
---|
186 | $last_poll = time; |
---|
187 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
188 | |
---|
189 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
190 | unless(defined($timeline) && ref($timeline) eq 'ARRAY') { |
---|
191 | twitter_error(); |
---|
192 | return; |
---|
193 | }; |
---|
194 | if ( scalar @$timeline ) { |
---|
195 | for my $tweet ( reverse @$timeline ) { |
---|
196 | if ( $tweet->{id} <= $last_id ) { |
---|
197 | next; |
---|
198 | } |
---|
199 | my $msg = BarnOwl::Message->new( |
---|
200 | type => 'Twitter', |
---|
201 | sender => $tweet->{user}{screen_name}, |
---|
202 | recipient => $cfg->{user} || $user, |
---|
203 | direction => 'in', |
---|
204 | source => decode_entities($tweet->{source}), |
---|
205 | location => decode_entities($tweet->{user}{location}||""), |
---|
206 | body => decode_entities($tweet->{text}), |
---|
207 | status_id => $tweet->{id}, |
---|
208 | service => $cfg->{service}, |
---|
209 | ); |
---|
210 | BarnOwl::queue_message($msg); |
---|
211 | } |
---|
212 | $last_id = $timeline->[0]{id}; |
---|
213 | } else { |
---|
214 | # BarnOwl::message("No new tweets..."); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | sub poll_direct { |
---|
219 | return unless ( time - $last_direct_poll) >= 120; |
---|
220 | $last_direct_poll = time; |
---|
221 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
222 | |
---|
223 | my $direct = $twitter->direct_messages( { since_id => $last_direct } ); |
---|
224 | unless(defined($direct) && ref($direct) eq 'ARRAY') { |
---|
225 | twitter_error(); |
---|
226 | return; |
---|
227 | }; |
---|
228 | if ( scalar @$direct ) { |
---|
229 | for my $tweet ( reverse @$direct ) { |
---|
230 | if ( $tweet->{id} <= $last_direct ) { |
---|
231 | next; |
---|
232 | } |
---|
233 | my $msg = BarnOwl::Message->new( |
---|
234 | type => 'Twitter', |
---|
235 | sender => $tweet->{sender}{screen_name}, |
---|
236 | recipient => $cfg->{user} || $user, |
---|
237 | direction => 'in', |
---|
238 | location => decode_entities($tweet->{sender}{location}||""), |
---|
239 | body => decode_entities($tweet->{text}), |
---|
240 | isprivate => 'true', |
---|
241 | service => $cfg->{service}, |
---|
242 | ); |
---|
243 | BarnOwl::queue_message($msg); |
---|
244 | } |
---|
245 | $last_direct = $direct->[0]{id}; |
---|
246 | } else { |
---|
247 | # BarnOwl::message("No new tweets..."); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | sub twitter { |
---|
252 | my $msg = shift; |
---|
253 | my $reply_to = shift; |
---|
254 | |
---|
255 | if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { |
---|
256 | twitter_direct($1, $2); |
---|
257 | } elsif(defined $twitter) { |
---|
258 | if($use_reply_to && defined($reply_to)) { |
---|
259 | $twitter->update({ |
---|
260 | status => $msg, |
---|
261 | in_reply_to_status_id => $reply_to |
---|
262 | }); |
---|
263 | } else { |
---|
264 | $twitter->update($msg); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | sub twitter_direct { |
---|
270 | my $who = shift; |
---|
271 | my $msg = shift; |
---|
272 | if(defined $twitter) { |
---|
273 | $twitter->new_direct_message({ |
---|
274 | user => $who, |
---|
275 | text => $msg |
---|
276 | }); |
---|
277 | if(BarnOwl::getvar("displayoutgoing") eq 'on') { |
---|
278 | my $tweet = BarnOwl::Message->new( |
---|
279 | type => 'Twitter', |
---|
280 | sender => $cfg->{user} || $user, |
---|
281 | recipient => $who, |
---|
282 | direction => 'out', |
---|
283 | body => $msg, |
---|
284 | isprivate => 'true', |
---|
285 | service => $cfg->{service}, |
---|
286 | ); |
---|
287 | BarnOwl::queue_message($tweet); |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | sub twitter_atreply { |
---|
293 | my $to = shift; |
---|
294 | my $id = shift; |
---|
295 | my $msg = shift; |
---|
296 | if(defined($id)) { |
---|
297 | twitter("@".$to." ".$msg, $id); |
---|
298 | } else { |
---|
299 | twitter("@".$to." ".$msg); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
304 | summary => 'Update Twitter from BarnOwl', |
---|
305 | usage => 'twitter [message]', |
---|
306 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
307 | . "\nOtherwise, prompt for a status message to use." |
---|
308 | }); |
---|
309 | |
---|
310 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
311 | summary => 'Send a Twitter direct message', |
---|
312 | usage => 'twitter-direct USER', |
---|
313 | description => 'Send a Twitter Direct Message to USER' |
---|
314 | }); |
---|
315 | |
---|
316 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
317 | { |
---|
318 | summary => 'Send a Twitter @ message', |
---|
319 | usage => 'twitter-atreply USER', |
---|
320 | description => 'Send a Twitter @reply Message to USER' |
---|
321 | } |
---|
322 | ); |
---|
323 | |
---|
324 | |
---|
325 | sub cmd_twitter { |
---|
326 | my $cmd = shift; |
---|
327 | if(@_) { |
---|
328 | my $status = join(" ", @_); |
---|
329 | twitter($status); |
---|
330 | } else { |
---|
331 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | sub cmd_twitter_direct { |
---|
336 | my $cmd = shift; |
---|
337 | my $user = shift; |
---|
338 | die("Usage: $cmd USER\n") unless $user; |
---|
339 | BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)}); |
---|
340 | } |
---|
341 | |
---|
342 | sub cmd_twitter_atreply { |
---|
343 | my $cmd = shift; |
---|
344 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
---|
345 | my $id = shift; |
---|
346 | BarnOwl::start_edit_win("Reply to \@" . $user, sub { twitter_atreply($user, $id, shift) }); |
---|
347 | } |
---|
348 | |
---|
349 | eval { |
---|
350 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
351 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
352 | }; |
---|
353 | if($@) { |
---|
354 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
355 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
356 | } |
---|
357 | |
---|
358 | BarnOwl::filter('twitter type ^twitter$'); |
---|
359 | |
---|
360 | 1; |
---|