1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Twitter::Handle |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Contains everything needed to send and receive messages from a Twitter-like service. |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Twitter::Handle; |
---|
15 | |
---|
16 | use Net::Twitter::Lite; |
---|
17 | BEGIN { |
---|
18 | # Backwards compatibility with version of Net::Twitter::Lite that |
---|
19 | # lack home_timeline. |
---|
20 | if(!defined(*Net::Twitter::Lite::home_timeline{CODE})) { |
---|
21 | *Net::Twitter::Lite::home_timeline = |
---|
22 | \&Net::Twitter::Lite::friends_timeline; |
---|
23 | } |
---|
24 | } |
---|
25 | use HTML::Entities; |
---|
26 | |
---|
27 | use BarnOwl; |
---|
28 | use BarnOwl::Message::Twitter; |
---|
29 | |
---|
30 | sub fail { |
---|
31 | my $self = shift; |
---|
32 | my $msg = shift; |
---|
33 | undef $self->{twitter}; |
---|
34 | my $nickname = $self->{cfg}->{account_nickname} || ""; |
---|
35 | die("[Twitter $nickname] Error: $msg\n"); |
---|
36 | } |
---|
37 | |
---|
38 | sub new { |
---|
39 | my $class = shift; |
---|
40 | my $cfg = shift; |
---|
41 | |
---|
42 | my $val; |
---|
43 | |
---|
44 | if(!exists $cfg->{default} && |
---|
45 | defined($val = delete $cfg->{default_sender})) { |
---|
46 | $cfg->{default} = $val; |
---|
47 | } |
---|
48 | |
---|
49 | if(!exists $cfg->{show_mentions} && |
---|
50 | defined($val = delete $cfg->{show_unsubscribed_replies})) { |
---|
51 | $cfg->{show_mentions} = $val; |
---|
52 | } |
---|
53 | |
---|
54 | $cfg = { |
---|
55 | account_nickname => '', |
---|
56 | default => 0, |
---|
57 | poll_for_tweets => 1, |
---|
58 | poll_for_dms => 1, |
---|
59 | publish_tweets => 0, |
---|
60 | show_mentions => 1, |
---|
61 | %$cfg |
---|
62 | }; |
---|
63 | |
---|
64 | my $self = { |
---|
65 | 'cfg' => $cfg, |
---|
66 | 'twitter' => undef, |
---|
67 | 'last_poll' => 0, |
---|
68 | 'last_direct_poll' => 0, |
---|
69 | 'last_id' => undef, |
---|
70 | 'last_direct' => undef, |
---|
71 | }; |
---|
72 | |
---|
73 | bless($self, $class); |
---|
74 | |
---|
75 | my %twitter_args = @_; |
---|
76 | |
---|
77 | $self->{twitter} = Net::Twitter::Lite->new(%twitter_args); |
---|
78 | |
---|
79 | my $timeline = eval { $self->{twitter}->home_timeline({count => 1}) }; |
---|
80 | warn "$@\n" if $@; |
---|
81 | |
---|
82 | if(!defined($timeline)) { |
---|
83 | $self->fail("Invalid credentials"); |
---|
84 | } |
---|
85 | |
---|
86 | eval { |
---|
87 | $self->{last_id} = $timeline->[0]{id}; |
---|
88 | }; |
---|
89 | $self->{last_id} = 1 unless defined($self->{last_id}); |
---|
90 | |
---|
91 | eval { |
---|
92 | $self->{last_direct} = $self->{twitter}->direct_messages()->[0]{id}; |
---|
93 | }; |
---|
94 | warn "$@\n" if $@; |
---|
95 | $self->{last_direct} = 1 unless defined($self->{last_direct}); |
---|
96 | |
---|
97 | eval { |
---|
98 | $self->{twitter}->{ua}->timeout(1); |
---|
99 | }; |
---|
100 | warn "$@\n" if $@; |
---|
101 | |
---|
102 | return $self; |
---|
103 | } |
---|
104 | |
---|
105 | =head2 twitter_command COMMAND ARGS... |
---|
106 | |
---|
107 | Call the specified method on $self->{twitter} with an extended |
---|
108 | timeout. This is intended for interactive commands, with the theory |
---|
109 | that if the user explicitly requested an action, it is slightly more |
---|
110 | acceptable to hang the UI for a second or two than to fail just |
---|
111 | because Twitter is being slightly slow. Polling commands should be |
---|
112 | called normally, with the default (short) timeout, to prevent |
---|
113 | background Twitter suckage from hosing the UI normally. |
---|
114 | |
---|
115 | =cut |
---|
116 | |
---|
117 | sub twitter_command { |
---|
118 | my $self = shift; |
---|
119 | my $cmd = shift; |
---|
120 | |
---|
121 | eval { $self->{twitter}->{ua}->timeout(5); }; |
---|
122 | my $result = eval { |
---|
123 | $self->{twitter}->$cmd(@_); |
---|
124 | }; |
---|
125 | my $error = $@; |
---|
126 | eval { $self->{twitter}->{ua}->timeout(1); }; |
---|
127 | if ($error) { |
---|
128 | die($error); |
---|
129 | } |
---|
130 | return $result; |
---|
131 | } |
---|
132 | |
---|
133 | sub twitter_error { |
---|
134 | my $self = shift; |
---|
135 | |
---|
136 | my $ratelimit = eval { $self->{twitter}->rate_limit_status }; |
---|
137 | warn "$@\n" if $@; |
---|
138 | unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') { |
---|
139 | # Twitter's just sucking, sleep for 5 minutes |
---|
140 | $self->{last_direct_poll} = $self->{last_poll} = time + 60*5; |
---|
141 | # die("Twitter seems to be having problems.\n"); |
---|
142 | return; |
---|
143 | } |
---|
144 | if(exists($ratelimit->{remaining_hits}) |
---|
145 | && $ratelimit->{remaining_hits} <= 0) { |
---|
146 | $self->{last_direct_poll} = $self->{last_poll} = $ratelimit->{reset_time_in_seconds}; |
---|
147 | die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); |
---|
148 | } elsif(exists($ratelimit->{error})) { |
---|
149 | die("Twitter: ". $ratelimit->{error} . "\n"); |
---|
150 | $self->{last_direct_poll} = $self->{last_poll} = time + 60*20; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | sub poll_twitter { |
---|
155 | my $self = shift; |
---|
156 | |
---|
157 | return unless ( time - $self->{last_poll} ) >= 60; |
---|
158 | $self->{last_poll} = time; |
---|
159 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
160 | |
---|
161 | my $timeline = eval { $self->{twitter}->home_timeline( { since_id => $self->{last_id} } ) }; |
---|
162 | warn "$@\n" if $@; |
---|
163 | unless(defined($timeline) && ref($timeline) eq 'ARRAY') { |
---|
164 | $self->twitter_error(); |
---|
165 | return; |
---|
166 | }; |
---|
167 | |
---|
168 | if ($self->{cfg}->{show_mentions}) { |
---|
169 | my $mentions = eval { $self->{twitter}->mentions( { since_id => $self->{last_id} } ) }; |
---|
170 | warn "$@\n" if $@; |
---|
171 | unless (defined($mentions) && ref($mentions) eq 'ARRAY') { |
---|
172 | $self->twitter_error(); |
---|
173 | return; |
---|
174 | }; |
---|
175 | #combine, sort by id, and uniq |
---|
176 | push @$timeline, @$mentions; |
---|
177 | @$timeline = sort { $b->{id} <=> $a->{id} } @$timeline; |
---|
178 | my $prev = { id => 0 }; |
---|
179 | @$timeline = grep($_->{id} != $prev->{id} && (($prev) = $_), @$timeline); |
---|
180 | } |
---|
181 | |
---|
182 | if ( scalar @$timeline ) { |
---|
183 | for my $tweet ( reverse @$timeline ) { |
---|
184 | if ( $tweet->{id} <= $self->{last_id} ) { |
---|
185 | next; |
---|
186 | } |
---|
187 | my $msg = BarnOwl::Message->new( |
---|
188 | type => 'Twitter', |
---|
189 | sender => $tweet->{user}{screen_name}, |
---|
190 | recipient => $self->{cfg}->{user} || $self->{user}, |
---|
191 | direction => 'in', |
---|
192 | source => decode_entities($tweet->{source}), |
---|
193 | location => decode_entities($tweet->{user}{location}||""), |
---|
194 | body => decode_entities($tweet->{text}), |
---|
195 | status_id => $tweet->{id}, |
---|
196 | service => $self->{cfg}->{service}, |
---|
197 | account => $self->{cfg}->{account_nickname}, |
---|
198 | ); |
---|
199 | BarnOwl::queue_message($msg); |
---|
200 | } |
---|
201 | $self->{last_id} = $timeline->[0]{id} if $timeline->[0]{id} > $self->{last_id}; |
---|
202 | } else { |
---|
203 | # BarnOwl::message("No new tweets..."); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | sub poll_direct { |
---|
208 | my $self = shift; |
---|
209 | |
---|
210 | return unless ( time - $self->{last_direct_poll}) >= 120; |
---|
211 | $self->{last_direct_poll} = time; |
---|
212 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
213 | |
---|
214 | my $direct = eval { $self->{twitter}->direct_messages( { since_id => $self->{last_direct} } ) }; |
---|
215 | warn "$@\n" if $@; |
---|
216 | unless(defined($direct) && ref($direct) eq 'ARRAY') { |
---|
217 | $self->twitter_error(); |
---|
218 | return; |
---|
219 | }; |
---|
220 | if ( scalar @$direct ) { |
---|
221 | for my $tweet ( reverse @$direct ) { |
---|
222 | if ( $tweet->{id} <= $self->{last_direct} ) { |
---|
223 | next; |
---|
224 | } |
---|
225 | my $msg = BarnOwl::Message->new( |
---|
226 | type => 'Twitter', |
---|
227 | sender => $tweet->{sender}{screen_name}, |
---|
228 | recipient => $self->{cfg}->{user} || $self->{user}, |
---|
229 | direction => 'in', |
---|
230 | location => decode_entities($tweet->{sender}{location}||""), |
---|
231 | body => decode_entities($tweet->{text}), |
---|
232 | private => 'true', |
---|
233 | service => $self->{cfg}->{service}, |
---|
234 | account => $self->{cfg}->{account_nickname}, |
---|
235 | ); |
---|
236 | BarnOwl::queue_message($msg); |
---|
237 | } |
---|
238 | $self->{last_direct} = $direct->[0]{id} if $direct->[0]{id} > $self->{last_direct}; |
---|
239 | } else { |
---|
240 | # BarnOwl::message("No new tweets..."); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | sub twitter { |
---|
245 | my $self = shift; |
---|
246 | |
---|
247 | my $msg = shift; |
---|
248 | my $reply_to = shift; |
---|
249 | |
---|
250 | if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { |
---|
251 | $self->twitter_direct($1, $2); |
---|
252 | } elsif(defined $self->{twitter}) { |
---|
253 | if(length($msg) > 140) { |
---|
254 | die("Twitter: Message over 140 characters long.\n"); |
---|
255 | } |
---|
256 | $self->twitter_command('update', { |
---|
257 | status => $msg, |
---|
258 | defined($reply_to) ? (in_reply_to_status_id => $reply_to) : () |
---|
259 | }); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | sub twitter_direct { |
---|
264 | my $self = shift; |
---|
265 | |
---|
266 | my $who = shift; |
---|
267 | my $msg = shift; |
---|
268 | if(defined $self->{twitter}) { |
---|
269 | $self->twitter_command('new_direct_message', { |
---|
270 | user => $who, |
---|
271 | text => $msg |
---|
272 | }); |
---|
273 | if(BarnOwl::getvar("displayoutgoing") eq 'on') { |
---|
274 | my $tweet = BarnOwl::Message->new( |
---|
275 | type => 'Twitter', |
---|
276 | sender => $self->{cfg}->{user} || $self->{user}, |
---|
277 | recipient => $who, |
---|
278 | direction => 'out', |
---|
279 | body => $msg, |
---|
280 | private => 'true', |
---|
281 | service => $self->{cfg}->{service}, |
---|
282 | ); |
---|
283 | BarnOwl::queue_message($tweet); |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | sub twitter_atreply { |
---|
289 | my $self = shift; |
---|
290 | |
---|
291 | my $to = shift; |
---|
292 | my $id = shift; |
---|
293 | my $msg = shift; |
---|
294 | if(defined($id)) { |
---|
295 | $self->twitter("@".$to." ".$msg, $id); |
---|
296 | } else { |
---|
297 | $self->twitter("@".$to." ".$msg); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | sub twitter_retweet { |
---|
302 | my $self = shift; |
---|
303 | my $msg = shift; |
---|
304 | |
---|
305 | if($msg->service ne $self->{cfg}->{service}) { |
---|
306 | die("Cannot retweet a message from a different service.\n"); |
---|
307 | } |
---|
308 | $self->twitter_command(retweet => $msg->{status_id}); |
---|
309 | } |
---|
310 | |
---|
311 | sub twitter_follow { |
---|
312 | my $self = shift; |
---|
313 | |
---|
314 | my $who = shift; |
---|
315 | |
---|
316 | my $user = $self->twitter_command('create_friend', $who); |
---|
317 | # returns a string on error |
---|
318 | if (defined $user && !ref $user) { |
---|
319 | BarnOwl::message($user); |
---|
320 | } else { |
---|
321 | BarnOwl::message("Following " . $who); |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | sub twitter_unfollow { |
---|
326 | my $self = shift; |
---|
327 | |
---|
328 | my $who = shift; |
---|
329 | |
---|
330 | my $user = $self->twitter_command('destroy_friend', $who); |
---|
331 | # returns a string on error |
---|
332 | if (defined $user && !ref $user) { |
---|
333 | BarnOwl::message($user); |
---|
334 | } else { |
---|
335 | BarnOwl::message("No longer following " . $who); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | sub nickname { |
---|
340 | my $self = shift; |
---|
341 | return $self->{cfg}->{account_nickname}; |
---|
342 | } |
---|
343 | |
---|
344 | 1; |
---|