[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 | }; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | unless(defined($last_direct)) { |
---|
| 133 | eval { |
---|
| 134 | $last_direct = $twitter->direct_messages()->[0]{id}; |
---|
| 135 | }; |
---|
[8618438] | 136 | } |
---|
| 137 | |
---|
| 138 | sub poll_messages { |
---|
[927c186] | 139 | poll_twitter(); |
---|
| 140 | poll_direct(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | sub twitter_error { |
---|
| 144 | my $ratelimit = $twitter->rate_limit_status; |
---|
| 145 | unless(defined($ratelimit)) { |
---|
| 146 | # Twitter's just sucking, sleep for 5 minutes |
---|
| 147 | $last_direct_poll = $last_poll = time + 60*5; |
---|
| 148 | die("Twitter seems to be having problems.\n"); |
---|
| 149 | } |
---|
| 150 | if($ratelimit->{remaining_hits} <= 0) { |
---|
| 151 | $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds}; |
---|
| 152 | die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | sub poll_twitter { |
---|
| 157 | return unless ( time - $last_poll ) >= 60; |
---|
[8618438] | 158 | $last_poll = time; |
---|
[927c186] | 159 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 160 | |
---|
[8618438] | 161 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
| 162 | unless(defined($timeline)) { |
---|
[927c186] | 163 | twitter_error(); |
---|
[8618438] | 164 | return; |
---|
| 165 | }; |
---|
| 166 | if ( scalar @$timeline ) { |
---|
| 167 | for my $tweet ( reverse @$timeline ) { |
---|
| 168 | if ( $tweet->{id} <= $last_id ) { |
---|
| 169 | next; |
---|
| 170 | } |
---|
| 171 | my $msg = BarnOwl::Message->new( |
---|
| 172 | type => 'Twitter', |
---|
| 173 | sender => $tweet->{user}{screen_name}, |
---|
| 174 | recipient => $cfg->{user} || $user, |
---|
| 175 | direction => 'in', |
---|
| 176 | source => decode_entities($tweet->{source}), |
---|
[d689fc7] | 177 | location => decode_entities($tweet->{user}{location}||""), |
---|
[8618438] | 178 | body => decode_entities($tweet->{text}) |
---|
| 179 | ); |
---|
| 180 | BarnOwl::queue_message($msg); |
---|
| 181 | } |
---|
| 182 | $last_id = $timeline->[0]{id}; |
---|
| 183 | } else { |
---|
| 184 | # BarnOwl::message("No new tweets..."); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[927c186] | 188 | sub poll_direct { |
---|
| 189 | return unless ( time - $last_direct_poll) >= 120; |
---|
| 190 | $last_direct_poll = time; |
---|
| 191 | return unless BarnOwl::getvar('twitter:poll') eq 'on'; |
---|
| 192 | |
---|
| 193 | my $direct = $twitter->direct_messages( { since_id => $last_direct } ); |
---|
| 194 | unless(defined($direct)) { |
---|
| 195 | twitter_error(); |
---|
| 196 | return; |
---|
| 197 | }; |
---|
| 198 | if ( scalar @$direct ) { |
---|
| 199 | for my $tweet ( reverse @$direct ) { |
---|
| 200 | if ( $tweet->{id} <= $last_direct ) { |
---|
| 201 | next; |
---|
| 202 | } |
---|
| 203 | my $msg = BarnOwl::Message->new( |
---|
| 204 | type => 'Twitter', |
---|
| 205 | sender => $tweet->{sender}{screen_name}, |
---|
| 206 | recipient => $cfg->{user} || $user, |
---|
| 207 | direction => 'in', |
---|
| 208 | location => decode_entities($tweet->{sender}{location}||""), |
---|
| 209 | body => decode_entities($tweet->{text}), |
---|
| 210 | isprivate => 'true' |
---|
| 211 | ); |
---|
| 212 | BarnOwl::queue_message($msg); |
---|
| 213 | } |
---|
| 214 | $last_direct = $direct->[0]{id}; |
---|
| 215 | } else { |
---|
| 216 | # BarnOwl::message("No new tweets..."); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
[e54f2fa] | 220 | sub twitter { |
---|
| 221 | my $msg = shift; |
---|
[927c186] | 222 | if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { |
---|
| 223 | twitter_direct($1, $2); |
---|
| 224 | } elsif(defined $twitter) { |
---|
[9bedca0] | 225 | $twitter->update($msg); |
---|
| 226 | } |
---|
[e54f2fa] | 227 | } |
---|
| 228 | |
---|
[927c186] | 229 | sub twitter_direct { |
---|
| 230 | my $who = shift; |
---|
| 231 | my $msg = shift; |
---|
| 232 | if(defined $twitter) { |
---|
| 233 | $twitter->new_direct_message({ |
---|
| 234 | user => $who, |
---|
| 235 | text => $msg |
---|
| 236 | }); |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
[6babb75] | 240 | sub twitter_atreply { |
---|
| 241 | my $to = shift; |
---|
| 242 | my $msg = shift; |
---|
| 243 | twitter("@".$to." ".$msg); |
---|
| 244 | } |
---|
| 245 | |
---|
[4cf4067] | 246 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 247 | summary => 'Update Twitter from BarnOwl', |
---|
| 248 | usage => 'twitter [message]', |
---|
| 249 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
| 250 | . "\nOtherwise, prompt for a status message to use." |
---|
| 251 | }); |
---|
| 252 | |
---|
[927c186] | 253 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
| 254 | summary => 'Send a Twitter direct message', |
---|
| 255 | usage => 'twitter-direct USER', |
---|
| 256 | description => 'Send a Twitter Direct Message to USER' |
---|
| 257 | }); |
---|
| 258 | |
---|
[6babb75] | 259 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
| 260 | { |
---|
| 261 | summary => 'Send a Twitter @ message', |
---|
| 262 | usage => 'twitter-atreply USER', |
---|
| 263 | description => 'Send a Twitter @reply Message to USER' |
---|
| 264 | } |
---|
| 265 | ); |
---|
| 266 | |
---|
| 267 | |
---|
[4cf4067] | 268 | sub cmd_twitter { |
---|
| 269 | my $cmd = shift; |
---|
| 270 | if(@_) { |
---|
| 271 | my $status = join(" ", @_); |
---|
| 272 | twitter($status); |
---|
| 273 | } else { |
---|
| 274 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
[927c186] | 278 | sub cmd_twitter_direct { |
---|
| 279 | my $cmd = shift; |
---|
| 280 | my $user = shift; |
---|
| 281 | die("Usage: $cmd USER\n") unless $user; |
---|
| 282 | BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)}); |
---|
| 283 | } |
---|
| 284 | |
---|
[6babb75] | 285 | sub cmd_twitter_atreply { |
---|
| 286 | my $cmd = shift; |
---|
| 287 | my $user = shift; |
---|
| 288 | BarnOwl::start_edit_win("Reply to \@" . $user, sub { twitter_atreply($user, shift) }); |
---|
| 289 | } |
---|
| 290 | |
---|
[72b61dd] | 291 | eval { |
---|
| 292 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 293 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 294 | }; |
---|
| 295 | if($@) { |
---|
| 296 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 297 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 298 | } |
---|
[e54f2fa] | 299 | |
---|
[5aabe2d7] | 300 | BarnOwl::filter('twitter type ^twitter$'); |
---|
[8618438] | 301 | |
---|
[e54f2fa] | 302 | 1; |
---|