source: lib/BarnOwl/Module/Twitter/Handle.pm @ bdb7c26

release-1.10release-1.7release-1.8release-1.9
Last change on this file since bdb7c26 was bdb7c26, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
Only poll after posting if we're configured to.
  • Property mode set to 100644
File size: 10.3 KB
RevLine 
[159aaad]1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Twitter::Handle
7
8=head1 DESCRIPTION
9
10Contains everything needed to send and receive messages from a Twitter-like service.
11
12=cut
13
14package BarnOwl::Module::Twitter::Handle;
15
[0b13bbc]16use Net::Twitter::Lite;
[9876953]17BEGIN {
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}
[159aaad]25use HTML::Entities;
26
[7424a5b]27use Scalar::Util qw(weaken);
28
[159aaad]29use BarnOwl;
30use BarnOwl::Message::Twitter;
[d389947]31use POSIX qw(asctime);
[7430aa4]32
[159aaad]33sub fail {
34    my $self = shift;
35    my $msg = shift;
36    undef $self->{twitter};
[7430aa4]37    my $nickname = $self->{cfg}->{account_nickname} || "";
38    die("[Twitter $nickname] Error: $msg\n");
[159aaad]39}
40
41sub new {
42    my $class = shift;
43    my $cfg = shift;
44
[385dd69]45    my $val;
46
47    if(!exists $cfg->{default} &&
48       defined($val = delete $cfg->{default_sender})) {
49        $cfg->{default} = $val;
50    }
51
52    if(!exists $cfg->{show_mentions} &&
53       defined($val = delete $cfg->{show_unsubscribed_replies})) {
54        $cfg->{show_mentions} = $val;
55    }
56
[efcd223]57    $cfg = {
58        account_nickname => '',
[385dd69]59        default          => 0,
[efcd223]60        poll_for_tweets  => 1,
61        poll_for_dms     => 1,
[26f9e2e]62        publish_tweets   => 0,
[385dd69]63        show_mentions    => 1,
[efcd223]64        %$cfg
65       };
66
[7430aa4]67    my $self = {
[159aaad]68        'cfg'  => $cfg,
69        'twitter' => undef,
70        'last_id' => undef,
71        'last_direct' => undef,
[36546fa]72        'timer'        => undef,
73        'direct_timer' => undef
[7430aa4]74    };
75
76    bless($self, $class);
[159aaad]77
78    my %twitter_args = @_;
79
[0b13bbc]80    $self->{twitter}  = Net::Twitter::Lite->new(%twitter_args);
[159aaad]81
[9876953]82    my $timeline = eval { $self->{twitter}->home_timeline({count => 1}) };
[0948fa0f]83    warn "$@\n" if $@;
[a8a0b0a]84
85    if(!defined($timeline)) {
[7430aa4]86        $self->fail("Invalid credentials");
[159aaad]87    }
88
[a8a0b0a]89    eval {
90        $self->{last_id} = $timeline->[0]{id};
91    };
92    $self->{last_id} = 1 unless defined($self->{last_id});
[159aaad]93
[a8a0b0a]94    eval {
95        $self->{last_direct} = $self->{twitter}->direct_messages()->[0]{id};
96    };
[0948fa0f]97    warn "$@\n" if $@;
[a8a0b0a]98    $self->{last_direct} = 1 unless defined($self->{last_direct});
[159aaad]99
100    eval {
[7430aa4]101        $self->{twitter}->{ua}->timeout(1);
[159aaad]102    };
[0948fa0f]103    warn "$@\n" if $@;
[159aaad]104
[36546fa]105    $self->sleep(0);
106
[7430aa4]107    return $self;
[159aaad]108}
109
[36546fa]110=head2 sleep SECONDS
111
112Stop polling Twitter for SECONDS seconds.
113
114=cut
115
116sub sleep {
117    my $self  = shift;
118    my $delay = shift;
119
[e4951ea]120    my $weak = $self;
121    weaken($weak);
[7424a5b]122
[36546fa]123    if($self->{cfg}->{poll_for_tweets}) {
124        $self->{timer} = BarnOwl::Timer->new({
125            after    => $delay,
[f2adf6c]126            interval => 90,
[7424a5b]127            cb       => sub { $weak->poll_twitter if $weak }
[36546fa]128           });
129    }
130
131    if($self->{cfg}->{poll_for_dms}) {
132        $self->{direct_timer} = BarnOwl::Timer->new({
133            after    => $delay,
[f2adf6c]134            interval => 180,
[7424a5b]135            cb       => sub { $weak->poll_direct if $weak }
[36546fa]136           });
137    }
138}
139
[118d800]140=head2 twitter_command COMMAND ARGS...
141
142Call the specified method on $self->{twitter} with an extended
143timeout. This is intended for interactive commands, with the theory
144that if the user explicitly requested an action, it is slightly more
145acceptable to hang the UI for a second or two than to fail just
146because Twitter is being slightly slow. Polling commands should be
147called normally, with the default (short) timeout, to prevent
148background Twitter suckage from hosing the UI normally.
149
150=cut
151
152sub twitter_command {
153    my $self = shift;
154    my $cmd = shift;
155
156    eval { $self->{twitter}->{ua}->timeout(5); };
157    my $result = eval {
158        $self->{twitter}->$cmd(@_);
159    };
160    my $error = $@;
161    eval { $self->{twitter}->{ua}->timeout(1); };
162    if ($error) {
163        die($error);
164    }
165    return $result;
166}
167
[159aaad]168sub twitter_error {
169    my $self = shift;
170
[82e0f26]171    my $ratelimit = eval { $self->{twitter}->rate_limit_status };
[c9bdb46]172    BarnOwl::debug($@) if $@;
[159aaad]173    unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') {
[36546fa]174        # Twitter's probably just sucking, try again later.
[d69c37c]175        $self->sleep(5*60);
[159aaad]176        return;
177    }
[36546fa]178
[159aaad]179    if(exists($ratelimit->{remaining_hits})
180       && $ratelimit->{remaining_hits} <= 0) {
[d389947]181        my $timeout = $ratelimit->{reset_time_in_seconds};
182        $self->sleep($timeout - time + 60);
183        BarnOwl::error("Twitter" .
184                       ($self->{cfg}->{account_nickname} ?
185                        "[$self->{cfg}->{account_nickname}]" : "") .
186                        ": ratelimited until " . asctime(localtime($timeout)));
[159aaad]187    } elsif(exists($ratelimit->{error})) {
[36546fa]188        $self->sleep(60*20);
[159aaad]189        die("Twitter: ". $ratelimit->{error} . "\n");
190    }
191}
192
193sub poll_twitter {
194    my $self = shift;
195
196    return unless BarnOwl::getvar('twitter:poll') eq 'on';
197
[36546fa]198    BarnOwl::debug("Polling " . $self->{cfg}->{account_nickname});
199
[9876953]200    my $timeline = eval { $self->{twitter}->home_timeline( { since_id => $self->{last_id} } ) };
[c9bdb46]201    BarnOwl::debug($@) if $@;
[159aaad]202    unless(defined($timeline) && ref($timeline) eq 'ARRAY') {
203        $self->twitter_error();
204        return;
205    };
[5da6ed8]206
[385dd69]207    if ($self->{cfg}->{show_mentions}) {
[82e0f26]208        my $mentions = eval { $self->{twitter}->mentions( { since_id => $self->{last_id} } ) };
[c9bdb46]209        BarnOwl::debug($@) if $@;
[5da6ed8]210        unless (defined($mentions) && ref($mentions) eq 'ARRAY') {
211            $self->twitter_error();
212            return;
213        };
214        #combine, sort by id, and uniq
215        push @$timeline, @$mentions;
216        @$timeline = sort { $b->{id} <=> $a->{id} } @$timeline;
217        my $prev = { id => 0 };
218        @$timeline = grep($_->{id} != $prev->{id} && (($prev) = $_), @$timeline);
219    }
220
[159aaad]221    if ( scalar @$timeline ) {
222        for my $tweet ( reverse @$timeline ) {
223            if ( $tweet->{id} <= $self->{last_id} ) {
224                next;
225            }
226            my $msg = BarnOwl::Message->new(
227                type      => 'Twitter',
228                sender    => $tweet->{user}{screen_name},
229                recipient => $self->{cfg}->{user} || $self->{user},
230                direction => 'in',
231                source    => decode_entities($tweet->{source}),
232                location  => decode_entities($tweet->{user}{location}||""),
233                body      => decode_entities($tweet->{text}),
234                status_id => $tweet->{id},
235                service   => $self->{cfg}->{service},
236                account   => $self->{cfg}->{account_nickname},
237               );
238            BarnOwl::queue_message($msg);
239        }
240        $self->{last_id} = $timeline->[0]{id} if $timeline->[0]{id} > $self->{last_id};
241    } else {
242        # BarnOwl::message("No new tweets...");
243    }
244}
245
246sub poll_direct {
247    my $self = shift;
248
249    return unless BarnOwl::getvar('twitter:poll') eq 'on';
250
[36546fa]251    BarnOwl::debug("Polling direct for " . $self->{cfg}->{account_nickname});
252
[82e0f26]253    my $direct = eval { $self->{twitter}->direct_messages( { since_id => $self->{last_direct} } ) };
[c9bdb46]254    BarnOwl::debug($@) if $@;
[159aaad]255    unless(defined($direct) && ref($direct) eq 'ARRAY') {
256        $self->twitter_error();
257        return;
258    };
259    if ( scalar @$direct ) {
260        for my $tweet ( reverse @$direct ) {
261            if ( $tweet->{id} <= $self->{last_direct} ) {
262                next;
263            }
264            my $msg = BarnOwl::Message->new(
265                type      => 'Twitter',
266                sender    => $tweet->{sender}{screen_name},
267                recipient => $self->{cfg}->{user} || $self->{user},
268                direction => 'in',
269                location  => decode_entities($tweet->{sender}{location}||""),
270                body      => decode_entities($tweet->{text}),
[c78d06f]271                private => 'true',
[159aaad]272                service   => $self->{cfg}->{service},
273                account   => $self->{cfg}->{account_nickname},
274               );
275            BarnOwl::queue_message($msg);
276        }
277        $self->{last_direct} = $direct->[0]{id} if $direct->[0]{id} > $self->{last_direct};
278    } else {
279        # BarnOwl::message("No new tweets...");
280    }
281}
282
283sub twitter {
284    my $self = shift;
285
286    my $msg = shift;
287    my $reply_to = shift;
288
289    if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) {
290        $self->twitter_direct($1, $2);
291    } elsif(defined $self->{twitter}) {
[7ec65f5]292        if(length($msg) > 140) {
293            die("Twitter: Message over 140 characters long.\n");
[159aaad]294        }
[118d800]295        $self->twitter_command('update', {
296            status => $msg,
297            defined($reply_to) ? (in_reply_to_status_id => $reply_to) : ()
298           });
[159aaad]299    }
[bdb7c26]300    $self->poll_twitter if $self->{cfg}->{poll_for_tweets};
[159aaad]301}
302
303sub twitter_direct {
304    my $self = shift;
305
306    my $who = shift;
307    my $msg = shift;
308    if(defined $self->{twitter}) {
[118d800]309        $self->twitter_command('new_direct_message', {
[159aaad]310            user => $who,
311            text => $msg
312           });
313        if(BarnOwl::getvar("displayoutgoing") eq 'on') {
314            my $tweet = BarnOwl::Message->new(
315                type      => 'Twitter',
316                sender    => $self->{cfg}->{user} || $self->{user},
317                recipient => $who, 
318                direction => 'out',
319                body      => $msg,
[c78d06f]320                private => 'true',
[159aaad]321                service   => $self->{cfg}->{service},
322               );
323            BarnOwl::queue_message($tweet);
324        }
325    }
326}
327
328sub twitter_atreply {
329    my $self = shift;
330
331    my $to  = shift;
332    my $id  = shift;
333    my $msg = shift;
334    if(defined($id)) {
335        $self->twitter("@".$to." ".$msg, $id);
336    } else {
337        $self->twitter("@".$to." ".$msg);
338    }
339}
340
[5214546]341sub twitter_retweet {
342    my $self = shift;
343    my $msg = shift;
344
345    if($msg->service ne $self->{cfg}->{service}) {
346        die("Cannot retweet a message from a different service.\n");
347    }
348    $self->twitter_command(retweet => $msg->{status_id});
[bdb7c26]349    $self->poll_twitter if $self->{cfg}->{poll_for_tweets};
[5214546]350}
351
[513da71]352sub twitter_follow {
353    my $self = shift;
354
355    my $who = shift;
356
[118d800]357    my $user = $self->twitter_command('create_friend', $who);
[513da71]358    # returns a string on error
359    if (defined $user && !ref $user) {
360        BarnOwl::message($user);
361    } else {
362        BarnOwl::message("Following " . $who);
363    }
364}
365
366sub twitter_unfollow {
367    my $self = shift;
368
369    my $who = shift;
370
[118d800]371    my $user = $self->twitter_command('destroy_friend', $who);
[513da71]372    # returns a string on error
373    if (defined $user && !ref $user) {
374        BarnOwl::message($user);
375    } else {
376        BarnOwl::message("No longer following " . $who);
377    }
378}
379
[8462b38]380sub nickname {
381    my $self = shift;
382    return $self->{cfg}->{account_nickname};
383}
384
[159aaad]3851;
Note: See TracBrowser for help on using the repository browser.