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

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