| 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 | our $VERSION = 0.2; |
|---|
| 17 | |
|---|
| 18 | use JSON; |
|---|
| 19 | use List::Util qw(first); |
|---|
| 20 | |
|---|
| 21 | use BarnOwl; |
|---|
| 22 | use BarnOwl::Hooks; |
|---|
| 23 | use BarnOwl::Module::Twitter::Handle; |
|---|
| 24 | use BarnOwl::Module::Twitter::Completion; |
|---|
| 25 | |
|---|
| 26 | our @twitter_handles = (); |
|---|
| 27 | our $default_handle = undef; |
|---|
| 28 | |
|---|
| 29 | my $desc = <<'END_DESC'; |
|---|
| 30 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
|---|
| 31 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
|---|
| 32 | from your sender and mirror them to Twitter. |
|---|
| 33 | |
|---|
| 34 | A value of '*' in any of these fields acts a wildcard, accepting |
|---|
| 35 | messages with any value of that field. |
|---|
| 36 | END_DESC |
|---|
| 37 | BarnOwl::new_variable_string( |
|---|
| 38 | 'twitter:class', |
|---|
| 39 | { |
|---|
| 40 | default => $ENV{USER}, |
|---|
| 41 | summary => 'Class to watch for Twitter messages', |
|---|
| 42 | description => $desc |
|---|
| 43 | } |
|---|
| 44 | ); |
|---|
| 45 | BarnOwl::new_variable_string( |
|---|
| 46 | 'twitter:instance', |
|---|
| 47 | { |
|---|
| 48 | default => 'status', |
|---|
| 49 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
|---|
| 50 | description => $desc |
|---|
| 51 | } |
|---|
| 52 | ); |
|---|
| 53 | BarnOwl::new_variable_string( |
|---|
| 54 | 'twitter:opcode', |
|---|
| 55 | { |
|---|
| 56 | default => 'twitter', |
|---|
| 57 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
|---|
| 58 | description => $desc |
|---|
| 59 | } |
|---|
| 60 | ); |
|---|
| 61 | |
|---|
| 62 | BarnOwl::new_variable_bool( |
|---|
| 63 | 'twitter:poll', |
|---|
| 64 | { |
|---|
| 65 | default => 1, |
|---|
| 66 | summary => 'Poll Twitter for incoming messages', |
|---|
| 67 | description => "If set, will poll Twitter every minute for normal updates,\n" |
|---|
| 68 | . 'and every two minutes for direct message' |
|---|
| 69 | } |
|---|
| 70 | ); |
|---|
| 71 | |
|---|
| 72 | sub fail { |
|---|
| 73 | my $msg = shift; |
|---|
| 74 | undef @twitter_handles; |
|---|
| 75 | BarnOwl::admin_message('Twitter Error', $msg); |
|---|
| 76 | die("Twitter Error: $msg\n"); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
|---|
| 80 | |
|---|
| 81 | if (open(my $fh, "<", "$conffile")) { |
|---|
| 82 | read_config($fh); |
|---|
| 83 | close($fh); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub read_config { |
|---|
| 87 | my $fh = shift; |
|---|
| 88 | |
|---|
| 89 | my $raw_cfg = do {local $/; <$fh>}; |
|---|
| 90 | close($fh); |
|---|
| 91 | eval { |
|---|
| 92 | $raw_cfg = from_json($raw_cfg); |
|---|
| 93 | }; |
|---|
| 94 | if($@) { |
|---|
| 95 | fail("Unable to parse $conffile: $@"); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY"; |
|---|
| 99 | |
|---|
| 100 | # Perform some sanity checking on the configuration. |
|---|
| 101 | { |
|---|
| 102 | my %nicks; |
|---|
| 103 | my $default = 0; |
|---|
| 104 | |
|---|
| 105 | for my $cfg (@$raw_cfg) { |
|---|
| 106 | if(! exists $cfg->{user}) { |
|---|
| 107 | fail("Account has no username set."); |
|---|
| 108 | } |
|---|
| 109 | my $user = $cfg->{user}; |
|---|
| 110 | if(! exists $cfg->{password}) { |
|---|
| 111 | fail("Account $user has no password set."); |
|---|
| 112 | } |
|---|
| 113 | if(@$raw_cfg > 1&& |
|---|
| 114 | !exists($cfg->{account_nickname}) ) { |
|---|
| 115 | fail("Account $user has no account_nickname set."); |
|---|
| 116 | } |
|---|
| 117 | if($cfg->{account_nickname}) { |
|---|
| 118 | if($nicks{$cfg->{account_nickname}}++) { |
|---|
| 119 | fail("Nickname " . $cfg->{account_nickname} . " specified more than once."); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | if($cfg->{default} || $cfg->{default_sender}) { |
|---|
| 123 | if($default++) { |
|---|
| 124 | fail("Multiple accounts marked as 'default'."); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | # If there is only a single account, make publish_tweets default to |
|---|
| 131 | # true. |
|---|
| 132 | if (scalar @$raw_cfg == 1 && !exists($raw_cfg->[0]{publish_tweets})) { |
|---|
| 133 | $raw_cfg->[0]{publish_tweets} = 1; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | for my $cfg (@$raw_cfg) { |
|---|
| 137 | my $twitter_args = { username => $cfg->{user}, |
|---|
| 138 | password => $cfg->{password}, |
|---|
| 139 | source => 'barnowl', |
|---|
| 140 | ssl => 1, |
|---|
| 141 | }; |
|---|
| 142 | if (defined $cfg->{service}) { |
|---|
| 143 | my $service = $cfg->{service}; |
|---|
| 144 | $twitter_args->{apiurl} = $service; |
|---|
| 145 | my $apihost = $service; |
|---|
| 146 | $apihost =~ s/^\s*http:\/\///; |
|---|
| 147 | $apihost =~ s/\/.*$//; |
|---|
| 148 | $apihost .= ':80' unless $apihost =~ /:\d+$/; |
|---|
| 149 | $twitter_args->{apihost} = $cfg->{apihost} || $apihost; |
|---|
| 150 | my $apirealm = "Laconica API"; |
|---|
| 151 | $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm; |
|---|
| 152 | } else { |
|---|
| 153 | $cfg->{service} = 'http://twitter.com'; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | my $twitter_handle; |
|---|
| 157 | eval { |
|---|
| 158 | $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args); |
|---|
| 159 | }; |
|---|
| 160 | if ($@) { |
|---|
| 161 | BarnOwl::error($@); |
|---|
| 162 | next; |
|---|
| 163 | } |
|---|
| 164 | push @twitter_handles, $twitter_handle; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | $default_handle = first {$_->{cfg}->{default}} @twitter_handles; |
|---|
| 168 | if (!$default_handle && @twitter_handles) { |
|---|
| 169 | $default_handle = $twitter_handles[0]; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | sub match { |
|---|
| 175 | my $val = shift; |
|---|
| 176 | my $pat = shift; |
|---|
| 177 | return $pat eq "*" || ($val eq $pat); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | sub handle_message { |
|---|
| 181 | my $m = shift; |
|---|
| 182 | my ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
|---|
| 183 | if($m->type eq 'zephyr' |
|---|
| 184 | && $m->sender eq BarnOwl::zephyr_getsender() |
|---|
| 185 | && match($m->class, $class) |
|---|
| 186 | && match($m->instance, $instance) |
|---|
| 187 | && match($m->opcode, $opcode) |
|---|
| 188 | && $m->auth eq 'YES') { |
|---|
| 189 | for my $handle (@twitter_handles) { |
|---|
| 190 | $handle->twitter($m->body) if $handle->{cfg}->{publish_tweets}; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | sub poll_messages { |
|---|
| 196 | # If we are reloaded into a BarnOwl with the old |
|---|
| 197 | # BarnOwl::Module::Twitter loaded, it still has a main loop hook |
|---|
| 198 | # that will call this function every second. If we just delete it, |
|---|
| 199 | # it will get the old version, which will call poll on each of our |
|---|
| 200 | # handles every second. However, they no longer include the time |
|---|
| 201 | # check, and so we will poll a handle every second until |
|---|
| 202 | # ratelimited. |
|---|
| 203 | |
|---|
| 204 | # So we include an empty function here. |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | sub find_account { |
|---|
| 208 | my $name = shift; |
|---|
| 209 | my $handle = first {$_->{cfg}->{account_nickname} eq $name} @twitter_handles; |
|---|
| 210 | if ($handle) { |
|---|
| 211 | return $handle; |
|---|
| 212 | } else { |
|---|
| 213 | die("No such Twitter account: $name\n"); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | sub find_account_default { |
|---|
| 218 | my $name = shift; |
|---|
| 219 | if(defined($name)) { |
|---|
| 220 | return find_account($name); |
|---|
| 221 | } else { |
|---|
| 222 | return $default_handle; |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | sub twitter { |
|---|
| 227 | my $account = shift; |
|---|
| 228 | |
|---|
| 229 | my $sent = 0; |
|---|
| 230 | if (defined $account) { |
|---|
| 231 | my $handle = find_account($account); |
|---|
| 232 | $handle->twitter(@_); |
|---|
| 233 | } |
|---|
| 234 | else { |
|---|
| 235 | # broadcast |
|---|
| 236 | for my $handle (@twitter_handles) { |
|---|
| 237 | $handle->twitter(@_) if $handle->{cfg}->{publish_tweets}; |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
|---|
| 243 | summary => 'Update Twitter from BarnOwl', |
|---|
| 244 | usage => 'twitter [ACCOUNT] [MESSAGE]', |
|---|
| 245 | description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.' |
|---|
| 246 | . "\nIf no ACCOUNT is provided, update all services which have publishing enabled." |
|---|
| 247 | . "\nOtherwise, prompt for a status message to use." |
|---|
| 248 | }); |
|---|
| 249 | |
|---|
| 250 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
|---|
| 251 | summary => 'Send a Twitter direct message', |
|---|
| 252 | usage => 'twitter-direct USER [ACCOUNT]', |
|---|
| 253 | description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,' |
|---|
| 254 | . "\nor first service if no default is provided)" |
|---|
| 255 | }); |
|---|
| 256 | |
|---|
| 257 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
|---|
| 258 | { |
|---|
| 259 | summary => 'Send a Twitter @ message', |
|---|
| 260 | usage => 'twitter-atreply USER [ACCOUNT]', |
|---|
| 261 | description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' |
|---|
| 262 | . "\nor first service if no default is provided)" |
|---|
| 263 | } |
|---|
| 264 | ); |
|---|
| 265 | |
|---|
| 266 | BarnOwl::new_command( 'twitter-retweet' => sub { cmd_twitter_retweet(@_) }, |
|---|
| 267 | { |
|---|
| 268 | summary => 'Retweet the current Twitter message', |
|---|
| 269 | usage => 'twitter-retweet [ACCOUNT]', |
|---|
| 270 | description => <<END_DESCRIPTION |
|---|
| 271 | Retweet the current Twitter message using ACCOUNT (defaults to the |
|---|
| 272 | account that received the tweet). |
|---|
| 273 | END_DESCRIPTION |
|---|
| 274 | } |
|---|
| 275 | ); |
|---|
| 276 | |
|---|
| 277 | BarnOwl::new_command( 'twitter-follow' => sub { cmd_twitter_follow(@_); }, |
|---|
| 278 | { |
|---|
| 279 | summary => 'Follow a user on Twitter', |
|---|
| 280 | usage => 'twitter-follow USER [ACCOUNT]', |
|---|
| 281 | description => 'Follow USER on Twitter ACCOUNT (defaults to default_sender, or first service' |
|---|
| 282 | . "\nif no default is provided)" |
|---|
| 283 | } |
|---|
| 284 | ); |
|---|
| 285 | |
|---|
| 286 | BarnOwl::new_command( 'twitter-unfollow' => sub { cmd_twitter_unfollow(@_); }, |
|---|
| 287 | { |
|---|
| 288 | summary => 'Stop following a user on Twitter', |
|---|
| 289 | usage => 'twitter-unfollow USER [ACCOUNT]', |
|---|
| 290 | description => 'Stop following USER on Twitter ACCOUNT (defaults to default_sender, or first' |
|---|
| 291 | . "\nservice if no default is provided)" |
|---|
| 292 | } |
|---|
| 293 | ); |
|---|
| 294 | |
|---|
| 295 | BarnOwl::new_command('twitter-count-chars' => \&cmd_count_chars, { |
|---|
| 296 | summary => 'Count the number of characters in the edit window', |
|---|
| 297 | usage => 'twitter-count-chars', |
|---|
| 298 | description => <<END_DESCRIPTION |
|---|
| 299 | Displays the number of characters entered in the edit window so far. |
|---|
| 300 | END_DESCRIPTION |
|---|
| 301 | }); |
|---|
| 302 | |
|---|
| 303 | $BarnOwl::Hooks::getQuickstart->add( sub { twitter_quickstart(@_); } ); |
|---|
| 304 | |
|---|
| 305 | sub twitter_quickstart { |
|---|
| 306 | return <<'EOF' |
|---|
| 307 | @b[Twitter]: |
|---|
| 308 | Add your Twitter account to ~/.owl/twitter, like: |
|---|
| 309 | {"user":"nelhage", "password":"sekrit"} |
|---|
| 310 | Run :reload-module Twitter, then use :twitter to tweet. |
|---|
| 311 | EOF |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | sub cmd_twitter { |
|---|
| 316 | my $cmd = shift; |
|---|
| 317 | my $account = shift; |
|---|
| 318 | if (defined $account) { |
|---|
| 319 | if(@_) { |
|---|
| 320 | my $status = join(" ", @_); |
|---|
| 321 | twitter($account, $status); |
|---|
| 322 | return; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | BarnOwl::start_edit_win("What's happening?" . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)}); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | sub cmd_twitter_direct { |
|---|
| 329 | my $cmd = shift; |
|---|
| 330 | my $user = shift; |
|---|
| 331 | die("Usage: $cmd USER\n") unless $user; |
|---|
| 332 | my $account = find_account_default(shift); |
|---|
| 333 | BarnOwl::start_edit_win("$cmd $user " . ($account->nickname||""), |
|---|
| 334 | sub { $account->twitter_direct($user, shift) }); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | sub cmd_twitter_atreply { |
|---|
| 338 | my $cmd = shift; |
|---|
| 339 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
|---|
| 340 | my $id = shift; |
|---|
| 341 | my $account = find_account_default(shift); |
|---|
| 342 | |
|---|
| 343 | BarnOwl::start_edit_win("Reply to \@" . $user . ($account->nickname ? (" on " . $account->nickname) : ""), |
|---|
| 344 | sub { $account->twitter_atreply($user, $id, shift) }); |
|---|
| 345 | BarnOwl::Editwin::insert_text("\@$user "); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | sub cmd_twitter_retweet { |
|---|
| 349 | my $cmd = shift; |
|---|
| 350 | my $account = shift; |
|---|
| 351 | my $m = BarnOwl::getcurmsg(); |
|---|
| 352 | if(!$m || $m->type ne 'Twitter') { |
|---|
| 353 | die("$cmd must be used with a Twitter message selected.\n"); |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | $account = $m->account unless defined($account); |
|---|
| 357 | find_account($account)->twitter_retweet($m); |
|---|
| 358 | return; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | sub cmd_twitter_follow { |
|---|
| 362 | my $cmd = shift; |
|---|
| 363 | my $user = shift; |
|---|
| 364 | die("Usage: $cmd USER\n") unless $user; |
|---|
| 365 | my $account = shift; |
|---|
| 366 | find_account_default($account)->twitter_follow($user); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | sub cmd_twitter_unfollow { |
|---|
| 370 | my $cmd = shift; |
|---|
| 371 | my $user = shift; |
|---|
| 372 | die("Usage: $cmd USER\n") unless $user; |
|---|
| 373 | my $account = shift; |
|---|
| 374 | find_account_default($account)->twitter_unfollow($user); |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | use BarnOwl::Editwin qw(:all); |
|---|
| 378 | sub cmd_count_chars { |
|---|
| 379 | my $cmd = shift; |
|---|
| 380 | my $text = save_excursion { |
|---|
| 381 | move_to_buffer_start(); |
|---|
| 382 | set_mark(); |
|---|
| 383 | move_to_buffer_end(); |
|---|
| 384 | get_region(); |
|---|
| 385 | }; |
|---|
| 386 | my $len = length($text); |
|---|
| 387 | BarnOwl::message($len); |
|---|
| 388 | return $len; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | eval { |
|---|
| 392 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
|---|
| 393 | }; |
|---|
| 394 | if($@) { |
|---|
| 395 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | BarnOwl::filter(qw{twitter type ^twitter$}); |
|---|
| 401 | |
|---|
| 402 | 1; |
|---|