source: lib/BarnOwl/Module/Twitter.pm @ 8618438

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 8618438 was 8618438, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Implement receiving messages
  • Property mode set to 100644
File size: 5.2 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
16use Net::Twitter;
17use JSON;
18
19use BarnOwl;
20use BarnOwl::Hooks;
21use BarnOwl::Message::Twitter;
22use HTML::Entities;
23
24my $twitter;
25my $user     = BarnOwl::zephyr_getsender();
26my ($class)  = ($user =~ /(^[^@]+)/);
27my $instance = "status";
28my $opcode   = "twitter";
29
30sub fail {
31    my $msg = shift;
32    undef $twitter;
33    BarnOwl::admin_message('Twitter Error', $msg);
34    die("Twitter Error: $msg\n");
35}
36
37# Don't redefine variables if they already exist
38# This is a workaround for http://barnowl.mit.edu/trac/ticket/44
39# Which was fixed in svn r819
40if((BarnOwl::getvar('twitter:class')||'') eq '') {
41    my $desc = <<'END_DESC';
42BarnOwl::Module::Twitter will watch for authentic zephyrs to
43-c $twitter:class -i $twitter:instance -O $twitter:opcode
44from your sender and mirror them to Twitter.
45
46A value of '*' in any of these fields acts a wildcard, accepting
47messages with any value of that field.
48END_DESC
49    BarnOwl::new_variable_string('twitter:class',
50                               {
51                                   default => $class,
52                                   summary => 'Class to watch for Twitter messages',
53                                   description => $desc
54                                  });
55    BarnOwl::new_variable_string('twitter:instance',
56                               {
57                                   default => $instance,
58                                   summary => 'Instance on twitter:class to watch for Twitter messages.',
59                                   description => $desc
60                                  });
61    BarnOwl::new_variable_string('twitter:opcode',
62                               {
63                                   default => $opcode,
64                                   summary => 'Opcode for zephyrs that will be sent as twitter updates',
65                                   description => $desc
66                                  });
67}
68
69my $conffile = BarnOwl::get_config_dir() . "/twitter";
70open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
71my $cfg = do {local $/; <$fh>};
72close($fh);
73eval {
74    $cfg = from_json($cfg);
75};
76if($@) {
77    fail("Unable to parse ~/.owl/twitter: $@");
78}
79
80$twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
81                              password   => $cfg->{password},
82                              source => 'barnowl');
83
84if(!defined($twitter->verify_credentials())) {
85    fail("Invalid twitter credentials");
86}
87
88sub match {
89    my $val = shift;
90    my $pat = shift;
91    return $pat eq "*" || ($val eq $pat);
92}
93
94sub handle_message {
95    my $m = shift;
96    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
97    if($m->sender eq $user
98       && match($m->class, $class)
99       && match($m->instance, $instance)
100       && match($m->opcode, $opcode)
101       && $m->auth eq 'YES') {
102        twitter($m->body);
103    }
104}
105
106my $last_poll = 0;
107my $last_id   = undef;
108unless(defined($last_id)) {
109    $last_id = $twitter->friends_timeline({count => 1})->[0]{id};
110}
111
112sub poll_messages {
113    return unless ( time - $last_poll ) >= 45;
114    $last_poll = time;
115    my $timeline = $twitter->friends_timeline( { since_id => $last_id } );
116    unless(defined($timeline)) {
117        BarnOwl::error("Twitter returned error ... rate-limited?");
118        # Sleep for 15 minutes
119        $last_poll = time + 60*15;
120        return;
121    };
122    if ( scalar @$timeline ) {
123        for my $tweet ( reverse @$timeline ) {
124            if ( $tweet->{id} <= $last_id ) {
125                next;
126            }
127            my $msg = BarnOwl::Message->new(
128                type      => 'Twitter',
129                sender    => $tweet->{user}{screen_name},
130                recipient => $cfg->{user} || $user,
131                direction => 'in',
132                source    => decode_entities($tweet->{source}),
133                location  => decode_entities($tweet->{user}{location}),
134                body      => decode_entities($tweet->{text})
135               );
136            BarnOwl::queue_message($msg);
137        }
138        $last_id = $timeline->[0]{id};
139    } else {
140        # BarnOwl::message("No new tweets...");
141    }
142}
143
144sub twitter {
145    my $msg = shift;
146    if(defined $twitter) {
147        $twitter->update($msg);
148    }
149}
150
151BarnOwl::new_command(twitter => \&cmd_twitter, {
152    summary     => 'Update Twitter from BarnOwl',
153    usage       => 'twitter [message]',
154    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
155    . "\nOtherwise, prompt for a status message to use."
156   });
157
158sub cmd_twitter {
159    my $cmd = shift;
160    if(@_) {
161        my $status = join(" ", @_);
162        twitter($status);
163    } else {
164      BarnOwl::start_edit_win('What are you doing?', \&twitter);
165    }
166}
167
168eval {
169    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
170    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
171};
172if($@) {
173    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
174    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
175}
176
177BarnOwl::command(qw(filter twitter type ^twitter$));
178
1791;
Note: See TracBrowser for help on using the repository browser.