[e54f2fa] | 1 | use warnings; |
---|
| 2 | use strict; |
---|
| 3 | |
---|
| 4 | =head1 NAME |
---|
| 5 | |
---|
| 6 | BarnOwl::Module::Twitter |
---|
| 7 | |
---|
| 8 | =head1 DESCRIPTION |
---|
| 9 | |
---|
| 10 | Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter |
---|
| 11 | |
---|
| 12 | =cut |
---|
| 13 | |
---|
| 14 | package BarnOwl::Module::Twitter; |
---|
| 15 | |
---|
| 16 | use Net::Twitter; |
---|
| 17 | use JSON; |
---|
| 18 | |
---|
| 19 | use BarnOwl; |
---|
| 20 | use BarnOwl::Hooks; |
---|
[8618438] | 21 | use BarnOwl::Message::Twitter; |
---|
| 22 | use HTML::Entities; |
---|
[e54f2fa] | 23 | |
---|
[9bedca0] | 24 | my $twitter; |
---|
| 25 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 26 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 27 | my $instance = "status"; |
---|
| 28 | my $opcode = "twitter"; |
---|
| 29 | |
---|
[e54f2fa] | 30 | sub fail { |
---|
| 31 | my $msg = shift; |
---|
[9bedca0] | 32 | undef $twitter; |
---|
[e54f2fa] | 33 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
[4cf4067] | 34 | die("Twitter Error: $msg\n"); |
---|
[e54f2fa] | 35 | } |
---|
| 36 | |
---|
[d689fc7] | 37 | my $desc = <<'END_DESC'; |
---|
[d1bb4f3] | 38 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
| 39 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
| 40 | from your sender and mirror them to Twitter. |
---|
| 41 | |
---|
| 42 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
| 43 | messages with any value of that field. |
---|
| 44 | END_DESC |
---|
[d689fc7] | 45 | BarnOwl::new_variable_string( |
---|
| 46 | 'twitter:class', |
---|
| 47 | { |
---|
| 48 | default => $class, |
---|
| 49 | summary => 'Class to watch for Twitter messages', |
---|
| 50 | description => $desc |
---|
| 51 | } |
---|
| 52 | ); |
---|
| 53 | BarnOwl::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 | ); |
---|
| 61 | BarnOwl::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 | ); |
---|
[e54f2fa] | 69 | |
---|
[927c186] | 70 | BarnOwl::new_variable_bool( |
---|
| 71 | 'twitter:poll', |
---|
| 72 | { |
---|
| 73 | default => 1, |
---|
| 74 | summary => 'Poll Twitter for incoming messages', |
---|
| 75 | description => "If set, will poll Twitter every minute for normal updates,\n" |
---|
| 76 | . 'and every two minutes for direct message' |
---|
| 77 | } |
---|
| 78 | ); |
---|
| 79 | |
---|
[d775050] | 80 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 81 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[e54f2fa] | 82 | my $cfg = do {local $/; <$fh>}; |
---|
| 83 | close($fh); |
---|
| 84 | eval { |
---|
| 85 | $cfg = from_json($cfg); |
---|
| 86 | }; |
---|
[9bedca0] | 87 | if($@) { |
---|
| 88 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
[e54f2fa] | 89 | } |
---|
| 90 | |
---|
[9bedca0] | 91 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
| 92 | password => $cfg->{password}, |
---|
[104f7d9] | 93 | source => 'barnowl'); |
---|
[e54f2fa] | 94 | |
---|
[51a7fc5] | 95 | eval { |
---|
| 96 | $twitter->{ua}->timeout(1); |
---|
| 97 | }; |
---|
| 98 | |
---|
[e54f2fa] | 99 | if(!defined($twitter->verify_credentials())) { |
---|
[d775050] | 100 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 101 | } |
---|
| 102 | |
---|
[d1bb4f3] | 103 | sub match { |
---|
| 104 | my $val = shift; |
---|
| 105 | my $pat = shift; |
---|
| 106 | return $pat eq "*" || ($val eq $pat); |
---|
| 107 | } |
---|
| 108 | |
---|
[e54f2fa] | 109 | sub handle_message { |
---|
| 110 | my $m = shift; |
---|
| 111 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
| 112 | if($m->sender eq $user |
---|
[d1bb4f3] | 113 | && match($m->class, $class) |
---|
| 114 | && match($m->instance, $instance) |
---|
| 115 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 116 | && $m->auth eq 'YES') { |
---|
| 117 | twitter($m->body); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[927c186] | 121 | our $last_poll = 0; |
---|
| 122 | our $last_direct_poll = 0; |
---|
| 123 | our $last_id = undef; |
---|
| 124 | our $last_direct = undef; |
---|
| 125 | |
---|
[8618438] | 126 | unless(defined($last_id)) { |
---|
[927c186] | 127 | eval { |
---|
| 128 | $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; |
---|
| 129 | }; |
---|
[706ff88] | 130 | $last_id = 0 unless defined($last_id); |
---|
[927c186] | 131 | } |
---|
| 132 | |
---|
| 133 | unless(defined($last_direct)) { |
---|
| 134 | eval { |
---|
| 135 | $last_direct = $twitter->direct_messages()->[0]{id}; |
---|
| 136 | }; |
---|
[706ff88] | 137 | $last_direct = 0 unless defined($last_direct); |
---|
[8618438] | 138 | } |
---|
| 139 | |
---|
| 140 | sub poll_messages { |
---|
[927c186] | 141 | poll_twitter(); |
---|
| 142 | poll_direct(); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | sub twitter_error { |
---|
| 146 | my $ratelimit = $twitter->rate_limit_status; |
---|
| 147 | unless(defined($ratelimit)) { |
---|
| 148 | # Twitter's just sucking, sleep for 5 minutes |
---|
| 149 | $last_direct_poll = $last_poll = time + 60*5; |
---|
[bab79f8] | 150 | # die("Twitter seems to be having problems.\n"); |
---|
| 151 | return; |
---|
[927c186] | 152 | } |
---|
| 153 | if($ratelimit->{remaining_hits} <= 0) { |
---|
| 154 | $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds}; |
---|
| 155 | die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | sub poll_twitter { |
---|
| 160 | return unless ( time - $last_poll ) >= 60; |
---|
[8618438] | 161 | $last_poll = time; |
---|
[927c186] | 162 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 163 | |
---|
[8618438] | 164 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
[5e1876d] | 165 | unless(defined($timeline) && ref($timeline)) { |
---|
[927c186] | 166 | twitter_error(); |
---|
[8618438] | 167 | return; |
---|
| 168 | }; |
---|
| 169 | if ( scalar @$timeline ) { |
---|
| 170 | for my $tweet ( reverse @$timeline ) { |
---|
| 171 | if ( $tweet->{id} <= $last_id ) { |
---|
| 172 | next; |
---|
| 173 | } |
---|
| 174 | my $msg = BarnOwl::Message->new( |
---|
| 175 | type => 'Twitter', |
---|
| 176 | sender => $tweet->{user}{screen_name}, |
---|
| 177 | recipient => $cfg->{user} || $user, |
---|
| 178 | direction => 'in', |
---|
| 179 | source => decode_entities($tweet->{source}), |
---|
[d689fc7] | 180 | location => decode_entities($tweet->{user}{location}||""), |
---|
[8618438] | 181 | body => decode_entities($tweet->{text}) |
---|
| 182 | ); |
---|
| 183 | BarnOwl::queue_message($msg); |
---|
| 184 | } |
---|
| 185 | $last_id = $timeline->[0]{id}; |
---|
| 186 | } else { |
---|
| 187 | # BarnOwl::message("No new tweets..."); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[927c186] | 191 | sub poll_direct { |
---|
| 192 | return unless ( time - $last_direct_poll) >= 120; |
---|
| 193 | $last_direct_poll = time; |
---|
| 194 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 195 | |
---|
| 196 | my $direct = $twitter->direct_messages( { since_id => $last_direct } ); |
---|
| 197 | unless(defined($direct)) { |
---|
| 198 | twitter_error(); |
---|
| 199 | return; |
---|
| 200 | }; |
---|
| 201 | if ( scalar @$direct ) { |
---|
| 202 | for my $tweet ( reverse @$direct ) { |
---|
| 203 | if ( $tweet->{id} <= $last_direct ) { |
---|
| 204 | next; |
---|
| 205 | } |
---|
| 206 | my $msg = BarnOwl::Message->new( |
---|
| 207 | type => 'Twitter', |
---|
| 208 | sender => $tweet->{sender}{screen_name}, |
---|
| 209 | recipient => $cfg->{user} || $user, |
---|
| 210 | direction => 'in', |
---|
| 211 | location => decode_entities($tweet->{sender}{location}||""), |
---|
| 212 | body => decode_entities($tweet->{text}), |
---|
| 213 | isprivate => 'true' |
---|
| 214 | ); |
---|
| 215 | BarnOwl::queue_message($msg); |
---|
| 216 | } |
---|
| 217 | $last_direct = $direct->[0]{id}; |
---|
| 218 | } else { |
---|
| 219 | # BarnOwl::message("No new tweets..."); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
[e54f2fa] | 223 | sub twitter { |
---|
| 224 | my $msg = shift; |
---|
[927c186] | 225 | if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { |
---|
| 226 | twitter_direct($1, $2); |
---|
| 227 | } elsif(defined $twitter) { |
---|
[9bedca0] | 228 | $twitter->update($msg); |
---|
| 229 | } |
---|
[e54f2fa] | 230 | } |
---|
| 231 | |
---|
[927c186] | 232 | sub twitter_direct { |
---|
| 233 | my $who = shift; |
---|
| 234 | my $msg = shift; |
---|
| 235 | if(defined $twitter) { |
---|
| 236 | $twitter->new_direct_message({ |
---|
| 237 | user => $who, |
---|
| 238 | text => $msg |
---|
| 239 | }); |
---|
[c1e5316] | 240 | if(BarnOwl::getvar("displayoutgoing") eq 'on') { |
---|
| 241 | my $tweet = BarnOwl::Message->new( |
---|
| 242 | type => 'Twitter', |
---|
| 243 | sender => $cfg->{user} || $user, |
---|
| 244 | recipient => $who, |
---|
| 245 | direction => 'out', |
---|
| 246 | body => $msg, |
---|
| 247 | isprivate => 'true' |
---|
| 248 | ); |
---|
| 249 | BarnOwl::queue_message($tweet); |
---|
| 250 | } |
---|
[927c186] | 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
[6babb75] | 254 | sub twitter_atreply { |
---|
| 255 | my $to = shift; |
---|
| 256 | my $msg = shift; |
---|
| 257 | twitter("@".$to." ".$msg); |
---|
| 258 | } |
---|
| 259 | |
---|
[4cf4067] | 260 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 261 | summary => 'Update Twitter from BarnOwl', |
---|
| 262 | usage => 'twitter [message]', |
---|
| 263 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
| 264 | . "\nOtherwise, prompt for a status message to use." |
---|
| 265 | }); |
---|
| 266 | |
---|
[927c186] | 267 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
| 268 | summary => 'Send a Twitter direct message', |
---|
| 269 | usage => 'twitter-direct USER', |
---|
| 270 | description => 'Send a Twitter Direct Message to USER' |
---|
| 271 | }); |
---|
| 272 | |
---|
[6babb75] | 273 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
| 274 | { |
---|
| 275 | summary => 'Send a Twitter @ message', |
---|
| 276 | usage => 'twitter-atreply USER', |
---|
| 277 | description => 'Send a Twitter @reply Message to USER' |
---|
| 278 | } |
---|
| 279 | ); |
---|
| 280 | |
---|
| 281 | |
---|
[4cf4067] | 282 | sub cmd_twitter { |
---|
| 283 | my $cmd = shift; |
---|
| 284 | if(@_) { |
---|
| 285 | my $status = join(" ", @_); |
---|
| 286 | twitter($status); |
---|
| 287 | } else { |
---|
| 288 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
[927c186] | 292 | sub cmd_twitter_direct { |
---|
| 293 | my $cmd = shift; |
---|
| 294 | my $user = shift; |
---|
| 295 | die("Usage: $cmd USER\n") unless $user; |
---|
| 296 | BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)}); |
---|
| 297 | } |
---|
| 298 | |
---|
[6babb75] | 299 | sub cmd_twitter_atreply { |
---|
| 300 | my $cmd = shift; |
---|
| 301 | my $user = shift; |
---|
| 302 | BarnOwl::start_edit_win("Reply to \@" . $user, sub { twitter_atreply($user, shift) }); |
---|
| 303 | } |
---|
| 304 | |
---|
[72b61dd] | 305 | eval { |
---|
| 306 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 307 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 308 | }; |
---|
| 309 | if($@) { |
---|
| 310 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 311 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 312 | } |
---|
[e54f2fa] | 313 | |
---|
[5aabe2d7] | 314 | BarnOwl::filter('twitter type ^twitter$'); |
---|
[8618438] | 315 | |
---|
[e54f2fa] | 316 | 1; |
---|