[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 | |
---|
[a0385ad3] | 96 | my $twitter_args = { username => $cfg->{user} || $user, |
---|
| 97 | password => $cfg->{password}, |
---|
| 98 | source => 'barnowl', |
---|
| 99 | }; |
---|
| 100 | if (defined $cfg->{service}) { |
---|
| 101 | my $service = $cfg->{service}; |
---|
| 102 | $twitter_args->{apiurl} = $service; |
---|
| 103 | my $apihost = $service; |
---|
| 104 | $apihost =~ s/^\s*http:\/\///; |
---|
| 105 | $apihost =~ s/\/.*$//; |
---|
| 106 | $apihost .= ':80' unless $apihost =~ /:\d+$/; |
---|
| 107 | $twitter_args->{apihost} = $cfg->{apihost} || $apihost; |
---|
| 108 | my $apirealm = "Laconica API"; |
---|
| 109 | $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | $twitter = Net::Twitter->new(%$twitter_args); |
---|
[e54f2fa] | 113 | |
---|
| 114 | if(!defined($twitter->verify_credentials())) { |
---|
[d775050] | 115 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 116 | } |
---|
| 117 | |
---|
[b56f2c3] | 118 | our $last_poll = 0; |
---|
| 119 | our $last_direct_poll = 0; |
---|
| 120 | our $last_id = undef; |
---|
| 121 | our $last_direct = undef; |
---|
| 122 | |
---|
| 123 | unless(defined($last_id)) { |
---|
| 124 | eval { |
---|
| 125 | $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; |
---|
| 126 | }; |
---|
| 127 | $last_id = 0 unless defined($last_id); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | unless(defined($last_direct)) { |
---|
| 131 | eval { |
---|
| 132 | $last_direct = $twitter->direct_messages()->[0]{id}; |
---|
| 133 | }; |
---|
| 134 | $last_direct = 0 unless defined($last_direct); |
---|
| 135 | } |
---|
| 136 | |
---|
[b25b5a4] | 137 | eval { |
---|
| 138 | $twitter->{ua}->timeout(1); |
---|
| 139 | }; |
---|
| 140 | |
---|
[d1bb4f3] | 141 | sub match { |
---|
| 142 | my $val = shift; |
---|
| 143 | my $pat = shift; |
---|
| 144 | return $pat eq "*" || ($val eq $pat); |
---|
| 145 | } |
---|
| 146 | |
---|
[e54f2fa] | 147 | sub handle_message { |
---|
| 148 | my $m = shift; |
---|
| 149 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
| 150 | if($m->sender eq $user |
---|
[d1bb4f3] | 151 | && match($m->class, $class) |
---|
| 152 | && match($m->instance, $instance) |
---|
| 153 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 154 | && $m->auth eq 'YES') { |
---|
| 155 | twitter($m->body); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
[8618438] | 159 | sub poll_messages { |
---|
[927c186] | 160 | poll_twitter(); |
---|
| 161 | poll_direct(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | sub twitter_error { |
---|
| 165 | my $ratelimit = $twitter->rate_limit_status; |
---|
[f0550c5] | 166 | unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') { |
---|
[927c186] | 167 | # Twitter's just sucking, sleep for 5 minutes |
---|
| 168 | $last_direct_poll = $last_poll = time + 60*5; |
---|
[bab79f8] | 169 | # die("Twitter seems to be having problems.\n"); |
---|
| 170 | return; |
---|
[927c186] | 171 | } |
---|
[f0550c5] | 172 | if(exists($ratelimit->{remaining_hits}) |
---|
| 173 | && $ratelimit->{remaining_hits} <= 0) { |
---|
[927c186] | 174 | $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds}; |
---|
| 175 | die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); |
---|
[f0550c5] | 176 | } elsif(exists($ratelimit->{error})) { |
---|
| 177 | die("Twitter: ". $ratelimit->{error} . "\n"); |
---|
| 178 | $last_direct_poll = $last_poll = time + 60*20; |
---|
[927c186] | 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | sub poll_twitter { |
---|
| 183 | return unless ( time - $last_poll ) >= 60; |
---|
[8618438] | 184 | $last_poll = time; |
---|
[927c186] | 185 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 186 | |
---|
[8618438] | 187 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
[f0550c5] | 188 | unless(defined($timeline) && ref($timeline) eq 'ARRAY') { |
---|
[927c186] | 189 | twitter_error(); |
---|
[8618438] | 190 | return; |
---|
| 191 | }; |
---|
| 192 | if ( scalar @$timeline ) { |
---|
| 193 | for my $tweet ( reverse @$timeline ) { |
---|
| 194 | if ( $tweet->{id} <= $last_id ) { |
---|
| 195 | next; |
---|
| 196 | } |
---|
| 197 | my $msg = BarnOwl::Message->new( |
---|
| 198 | type => 'Twitter', |
---|
| 199 | sender => $tweet->{user}{screen_name}, |
---|
| 200 | recipient => $cfg->{user} || $user, |
---|
| 201 | direction => 'in', |
---|
| 202 | source => decode_entities($tweet->{source}), |
---|
[d689fc7] | 203 | location => decode_entities($tweet->{user}{location}||""), |
---|
[acdd52e] | 204 | body => decode_entities($tweet->{text}), |
---|
[a0385ad3] | 205 | status_id => $tweet->{id}, |
---|
| 206 | service => $cfg->{service}, |
---|
[8618438] | 207 | ); |
---|
| 208 | BarnOwl::queue_message($msg); |
---|
| 209 | } |
---|
| 210 | $last_id = $timeline->[0]{id}; |
---|
| 211 | } else { |
---|
| 212 | # BarnOwl::message("No new tweets..."); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
[927c186] | 216 | sub poll_direct { |
---|
| 217 | return unless ( time - $last_direct_poll) >= 120; |
---|
| 218 | $last_direct_poll = time; |
---|
| 219 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 220 | |
---|
| 221 | my $direct = $twitter->direct_messages( { since_id => $last_direct } ); |
---|
[f0550c5] | 222 | unless(defined($direct) && ref($direct) eq 'ARRAY') { |
---|
[927c186] | 223 | twitter_error(); |
---|
| 224 | return; |
---|
| 225 | }; |
---|
| 226 | if ( scalar @$direct ) { |
---|
| 227 | for my $tweet ( reverse @$direct ) { |
---|
| 228 | if ( $tweet->{id} <= $last_direct ) { |
---|
| 229 | next; |
---|
| 230 | } |
---|
| 231 | my $msg = BarnOwl::Message->new( |
---|
| 232 | type => 'Twitter', |
---|
| 233 | sender => $tweet->{sender}{screen_name}, |
---|
| 234 | recipient => $cfg->{user} || $user, |
---|
| 235 | direction => 'in', |
---|
| 236 | location => decode_entities($tweet->{sender}{location}||""), |
---|
| 237 | body => decode_entities($tweet->{text}), |
---|
[a0385ad3] | 238 | isprivate => 'true', |
---|
| 239 | service => $cfg->{service}, |
---|
[927c186] | 240 | ); |
---|
| 241 | BarnOwl::queue_message($msg); |
---|
| 242 | } |
---|
| 243 | $last_direct = $direct->[0]{id}; |
---|
| 244 | } else { |
---|
| 245 | # BarnOwl::message("No new tweets..."); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
[e54f2fa] | 249 | sub twitter { |
---|
| 250 | my $msg = shift; |
---|
[acdd52e] | 251 | my $reply_to = shift; |
---|
| 252 | |
---|
[927c186] | 253 | if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { |
---|
| 254 | twitter_direct($1, $2); |
---|
| 255 | } elsif(defined $twitter) { |
---|
[d658c29] | 256 | if($use_reply_to && defined($reply_to)) { |
---|
| 257 | $twitter->update({ |
---|
| 258 | status => $msg, |
---|
| 259 | in_reply_to_status_id => $reply_to |
---|
| 260 | }); |
---|
| 261 | } else { |
---|
| 262 | $twitter->update($msg); |
---|
| 263 | } |
---|
[9bedca0] | 264 | } |
---|
[e54f2fa] | 265 | } |
---|
| 266 | |
---|
[927c186] | 267 | sub twitter_direct { |
---|
| 268 | my $who = shift; |
---|
| 269 | my $msg = shift; |
---|
| 270 | if(defined $twitter) { |
---|
| 271 | $twitter->new_direct_message({ |
---|
| 272 | user => $who, |
---|
| 273 | text => $msg |
---|
| 274 | }); |
---|
[c1e5316] | 275 | if(BarnOwl::getvar("displayoutgoing") eq 'on') { |
---|
| 276 | my $tweet = BarnOwl::Message->new( |
---|
| 277 | type => 'Twitter', |
---|
| 278 | sender => $cfg->{user} || $user, |
---|
| 279 | recipient => $who, |
---|
| 280 | direction => 'out', |
---|
| 281 | body => $msg, |
---|
[a0385ad3] | 282 | isprivate => 'true', |
---|
| 283 | service => $cfg->{service}, |
---|
[c1e5316] | 284 | ); |
---|
| 285 | BarnOwl::queue_message($tweet); |
---|
| 286 | } |
---|
[927c186] | 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
[6babb75] | 290 | sub twitter_atreply { |
---|
| 291 | my $to = shift; |
---|
[acdd52e] | 292 | my $id = shift; |
---|
[6babb75] | 293 | my $msg = shift; |
---|
[acdd52e] | 294 | if(defined($id)) { |
---|
| 295 | twitter("@".$to." ".$msg, $id); |
---|
| 296 | } else { |
---|
| 297 | twitter("@".$to." ".$msg); |
---|
| 298 | } |
---|
[6babb75] | 299 | } |
---|
| 300 | |
---|
[4cf4067] | 301 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 302 | summary => 'Update Twitter from BarnOwl', |
---|
| 303 | usage => 'twitter [message]', |
---|
| 304 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
| 305 | . "\nOtherwise, prompt for a status message to use." |
---|
| 306 | }); |
---|
| 307 | |
---|
[927c186] | 308 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
| 309 | summary => 'Send a Twitter direct message', |
---|
| 310 | usage => 'twitter-direct USER', |
---|
| 311 | description => 'Send a Twitter Direct Message to USER' |
---|
| 312 | }); |
---|
| 313 | |
---|
[6babb75] | 314 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
| 315 | { |
---|
| 316 | summary => 'Send a Twitter @ message', |
---|
| 317 | usage => 'twitter-atreply USER', |
---|
| 318 | description => 'Send a Twitter @reply Message to USER' |
---|
| 319 | } |
---|
| 320 | ); |
---|
| 321 | |
---|
| 322 | |
---|
[4cf4067] | 323 | sub cmd_twitter { |
---|
| 324 | my $cmd = shift; |
---|
| 325 | if(@_) { |
---|
| 326 | my $status = join(" ", @_); |
---|
| 327 | twitter($status); |
---|
| 328 | } else { |
---|
| 329 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[927c186] | 333 | sub cmd_twitter_direct { |
---|
| 334 | my $cmd = shift; |
---|
| 335 | my $user = shift; |
---|
| 336 | die("Usage: $cmd USER\n") unless $user; |
---|
| 337 | BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)}); |
---|
| 338 | } |
---|
| 339 | |
---|
[6babb75] | 340 | sub cmd_twitter_atreply { |
---|
| 341 | my $cmd = shift; |
---|
[acdd52e] | 342 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
---|
| 343 | my $id = shift; |
---|
| 344 | BarnOwl::start_edit_win("Reply to \@" . $user, sub { twitter_atreply($user, $id, shift) }); |
---|
[6babb75] | 345 | } |
---|
| 346 | |
---|
[72b61dd] | 347 | eval { |
---|
| 348 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 349 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 350 | }; |
---|
| 351 | if($@) { |
---|
| 352 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 353 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 354 | } |
---|
[e54f2fa] | 355 | |
---|
[5aabe2d7] | 356 | BarnOwl::filter('twitter type ^twitter$'); |
---|
[8618438] | 357 | |
---|
[e54f2fa] | 358 | 1; |
---|