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