source: lib/BarnOwl/Module/Twitter.pm @ 1f82df9

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 1f82df9 was 1f82df9, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
oops typo
  • 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
70my $conffile = BarnOwl::get_config_dir() . "/twitter";
71open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
72my $cfg = do {local $/; <$fh>};
73close($fh);
74eval {
75    $cfg = from_json($cfg);
76};
77if($@) {
78    fail("Unable to parse ~/.owl/twitter: $@");
79}
80
81$twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
82                              password   => $cfg->{password},
83                              source => 'barnowl');
84
85eval {
86    $twitter->{ua}->timeout(1);
87};
88
89if(!defined($twitter->verify_credentials())) {
90    fail("Invalid twitter credentials");
91}
92
93sub match {
94    my $val = shift;
95    my $pat = shift;
96    return $pat eq "*" || ($val eq $pat);
97}
98
99sub handle_message {
100    my $m = shift;
101    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
102    if($m->sender eq $user
103       && match($m->class, $class)
104       && match($m->instance, $instance)
105       && match($m->opcode, $opcode)
106       && $m->auth eq 'YES') {
107        twitter($m->body);
108    }
109}
110
111my $last_poll = 0;
112my $last_id   = undef;
113unless(defined($last_id)) {
114    $last_id = $twitter->friends_timeline({count => 1})->[0]{id};
115}
116
117sub poll_messages {
118    return unless ( time - $last_poll ) >= 45;
119    $last_poll = time;
120    my $timeline = $twitter->friends_timeline( { since_id => $last_id } );
121    unless(defined($timeline)) {
122        BarnOwl::error("Twitter returned error ... rate-limited?");
123        # Sleep for 15 minutes
124        $last_poll = time + 60*15;
125        return;
126    };
127    if ( scalar @$timeline ) {
128        for my $tweet ( reverse @$timeline ) {
129            if ( $tweet->{id} <= $last_id ) {
130                next;
131            }
132            my $msg = BarnOwl::Message->new(
133                type      => 'Twitter',
134                sender    => $tweet->{user}{screen_name},
135                recipient => $cfg->{user} || $user,
136                direction => 'in',
137                source    => decode_entities($tweet->{source}),
138                location  => decode_entities($tweet->{user}{location}||""),
139                body      => decode_entities($tweet->{text})
140               );
141            BarnOwl::queue_message($msg);
142        }
143        $last_id = $timeline->[0]{id};
144    } else {
145        # BarnOwl::message("No new tweets...");
146    }
147}
148
149sub twitter {
150    my $msg = shift;
151    if(defined $twitter) {
152        $twitter->update($msg);
153    }
154}
155
156BarnOwl::new_command(twitter => \&cmd_twitter, {
157    summary     => 'Update Twitter from BarnOwl',
158    usage       => 'twitter [message]',
159    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
160    . "\nOtherwise, prompt for a status message to use."
161   });
162
163sub cmd_twitter {
164    my $cmd = shift;
165    if(@_) {
166        my $status = join(" ", @_);
167        twitter($status);
168    } else {
169      BarnOwl::start_edit_win('What are you doing?', \&twitter);
170    }
171}
172
173eval {
174    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
175    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
176};
177if($@) {
178    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
179    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
180}
181
182BarnOwl::filter('twitter type ^twitter$');
183
1841;
Note: See TracBrowser for help on using the repository browser.