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