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

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