10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 326 $' =~ /(\d+)/;
17 authors => 'Dan Boger',
18 contact => 'zigdon@gmail.com',
20 description => 'Send twitter updates using /tweet. '
21 . 'Can optionally set your bitlbee /away message to same',
22 license => 'GNU GPL v2',
23 url => 'http://tinyurl.com/twirssi',
24 changed => 'Mon Dec 1 15:36:01 PST 2008',
34 my $last_poll = time - 300;
38 my ( $data, $server, $win ) = @_;
41 ¬ice("Not logged in! Use /twitter_login username pass!");
45 my ( $target, $text ) = split ' ', $data, 2;
46 unless ( $target and $text ) {
47 ¬ice("Usage: /dm <nick> <message>");
51 &cmd_direct_as( "$user $data", $server, $win );
55 my ( $data, $server, $win ) = @_;
58 ¬ice("Not logged in! Use /twitter_login username pass!");
62 my ( $username, $target, $text ) = split ' ', $data, 3;
63 unless ( $username and $target and $text ) {
64 ¬ice("Usage: /dm_as <username> <nick> <message>");
68 unless ( exists $twits{$username} ) {
69 ¬ice("Unknown username $username");
73 unless ( $twits{$username}
74 ->new_direct_message( { user => $target, text => $text } ) )
76 ¬ice("DM to $target failed");
80 ¬ice("DM sent to $target");
81 $nicks{$target} = time;
85 my ( $data, $server, $win ) = @_;
88 ¬ice("Not logged in! Use /twitter_login username pass!");
92 $data =~ s/^\s+|\s+$//;
94 ¬ice("Usage: /tweet <update>");
98 &cmd_tweet_as( "$user $data", $server, $win );
102 my ( $data, $server, $win ) = @_;
105 ¬ice("Not logged in! Use /twitter_login username pass!");
109 $data =~ s/^\s+|\s+$//;
110 my ( $username, $data ) = split ' ', $data, 2;
112 unless ( $username and $data ) {
113 ¬ice("Usage: /tweet_as <username> <update>");
117 unless ( exists $twits{$username} ) {
118 ¬ice("Unknown username $username");
122 if ( Irssi::settings_get_str("short_url_provider") ) {
123 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
125 my $short = makeashorterlink($url);
126 $data =~ s/\Q$url/$short/g;
131 if ( length $data > 140 ) {
133 "Tweet too long (" . length($data) . " characters) - aborted" );
137 unless ( $twits{$username}->update($data) ) {
138 ¬ice("Update failed");
142 foreach ( $data =~ /@([-\w]+)/ ) {
147 if ( Irssi::settings_get_bool("tweet_to_away")
149 and $data !~ /^[dD] / )
152 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
154 $server->send_raw("away :$data");
157 ¬ice( "Can't find bitlbee server.",
158 "Update bitlbee_server or disalbe tweet_to_away" );
162 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
166 my ( $usage_str, $api_name, $post_ref ) = @_;
169 my ( $data, $server, $win ) = @_;
172 ¬ice("Not logged in! Use /twitter_login username pass!");
176 $data =~ s/^\s+|\s+$//;
178 ¬ice("Usage: $usage_str");
182 unless ( $twit->$api_name($data) ) {
183 ¬ice("$api_name failed");
187 &$post_ref($data) if $post_ref;
192 my ( $data, $server, $win ) = @_;
194 $data =~ s/^\s+|\s+$//g;
195 if ( exists $twits{$data} ) {
196 ¬ice("Switching to $data");
197 $twit = $twits{$data};
200 ¬ice("Unknown user $data");
205 my ( $data, $server, $win ) = @_;
207 $data =~ s/^\s+|\s+$//g;
208 if ( $data and exists $twits{$data} ) {
209 ¬ice("Logging out $data...");
210 $twits{$data}->end_session();
211 delete $twits{$data};
213 ¬ice("Unknown username '$data'");
215 ¬ice("Logging out $user...");
216 $twit->end_session();
218 delete $twits{$user};
220 &cmd_switch( ( keys %twits )[0], $server, $win );
222 Irssi::timeout_remove($poll) if $poll;
229 my ( $data, $server, $win ) = @_;
232 ( $user, $pass ) = split ' ', $data, 2;
233 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
234 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
236 my @user = split /\s*,\s*/, $autouser;
237 my @pass = split /\s*,\s*/, $autopass;
238 if ( @user != @pass ) {
239 ¬ice("Number of usernames doesn't match "
240 . "the number of passwords - auto-login failed" );
243 while ( @user and @pass ) {
251 ¬ice("/twitter_login requires either a username and password "
252 . "or twitter_usernames and twitter_passwords to be set." );
256 %friends = %nicks = ();
258 $twit = Net::Twitter->new(
264 unless ( $twit->verify_credentials() ) {
265 ¬ice("Login as $user failed");
268 &cmd_switch( ( keys %twits )[0], $server, $win );
274 my $rate_limit = $twit->rate_limit_status();
275 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
276 ¬ice("Rate limit exceeded, try again later");
281 $twits{$user} = $twit;
282 Irssi::timeout_remove($poll) if $poll;
283 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
284 ¬ice("Logged in as $user, loading friends list...");
286 ¬ice( "loaded friends: ", scalar keys %friends );
287 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
288 Irssi::settings_set_bool( "twirssi_first_run", 0 );
289 unless ( exists $friends{twirssi} ) {
290 ¬ice("Welcome to twirssi!"
291 . " Perhaps you should add \@twirssi to your friends list,"
292 . " so you can be notified when a new version is release?"
293 . " Just type /twitter_friend twirssi." );
300 ¬ice("Login failed");
305 my ( $data, $server, $win ) = @_;
307 my $loc = Irssi::settings_get_str("twirssi_location");
310 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
315 if ( not -x "/usr/bin/md5sum" and not $data ) {
317 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
324 eval { use Digest::MD5; };
328 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
333 $md5 = get("http://irc.peeron.com/~zigdon/twirssi/md5sum");
337 ¬ice("Failed to download md5sum from peeron! Aborting.");
341 unless ( open( CUR, $loc ) ) {
343 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
348 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
351 if ( $cur_md5 eq $md5 ) {
352 ¬ice("Current twirssi seems to be up to date.");
357 my $URL = "http://irc.peeron.com/~zigdon/twirssi/twirssi.pl";
358 ¬ice("Downloading twirssi from $URL");
359 LWP::Simple::getstore( $URL, "$loc.upgrade" );
362 unless ( open( NEW, "$loc.upgrade" ) ) {
364 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
369 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
372 if ( $new_md5 ne $md5 ) {
373 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
378 rename $loc, "$loc.backup"
379 or ¬ice("Failed to back up $loc: $!. Aborting")
381 rename "$loc.upgrade", $loc
382 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
385 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
386 if ( -e "$dir/autorun/$file" ) {
387 ¬ice("Updating $dir/autorun/$file");
388 unlink "$dir/autorun/$file"
389 or ¬ice("Failed to remove old $file from autorun: $!");
390 symlink "../$file", "$dir/autorun/$file"
391 or ¬ice("Failed to create symlink in autorun directory: $!");
394 ¬ice("Download complete. Reload twirssi with /script load $file");
401 my $friends = $twit->friends( { page => $page } );
402 last unless $friends;
403 $new_friends{ $_->{screen_name} } = time foreach @$friends;
405 last if @$friends == 0 or $page == 10;
406 $friends = $twit->friends( page => $page );
409 foreach ( keys %new_friends ) {
410 next if exists $friends{$_};
414 foreach ( keys %friends ) {
415 delete $friends{$_} unless exists $new_friends{$_};
420 print scalar localtime, " - get_updates starting" if &debug;
423 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
426 ->print( "Can't find a window named '"
427 . Irssi::settings_get_str('twitter_window')
428 . "'. Create it or change the value of twitter_window" );
431 ¬ice("Not logged in! Use /twitter_login username pass!");
435 my ( $fh, $filename ) = File::Temp::tempfile();
439 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
440 Irssi::pidwait_add($pid);
441 } elsif ( defined $pid ) { # child
448 &do_updates( $fh, $user, $twit );
449 foreach ( keys %twits ) {
451 &do_updates( $fh, $_, $twits{$_} );
454 print $fh "__friends__\n";
456 foreach ( sort keys %friends ) {
457 print $fh "$_ $friends{$_}\n";
463 print scalar localtime, " - get_updates ends" if &debug;
467 my ( $fh, $username, $obj ) = @_;
469 print scalar localtime, " - Polling for updates for $username" if &debug;
471 $obj->friends_timeline( { since => HTTP::Date::time2str($last_poll) } )
473 foreach my $t ( reverse @$tweets ) {
474 my $text = decode_entities( $t->{text} );
476 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
478 if ( Irssi::settings_get_bool("show_reply_context")
479 and $t->{in_reply_to_screen_name} ne $username
480 and $t->{in_reply_to_screen_name}
481 and not exists $friends{ $t->{in_reply_to_screen_name} } )
483 $nicks{ $t->{in_reply_to_screen_name} } = time;
484 my $context = $obj->show_status( $t->{in_reply_to_status_id} );
486 my $ctext = decode_entities( $context->{text} );
488 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
489 printf $fh "id:%d [%s%%B\@%s%%n] %s\n",
491 ( $username ne $user ? "$username: " : "" ),
492 $context->{user}{screen_name}, $ctext;
495 print "Failed to get context from $t->{in_reply_to_screen_name}"
500 if $t->{user}{screen_name} eq $username
501 and not Irssi::settings_get_bool("show_own_tweets");
502 printf $fh "id:%d %s[%s%%B\@%s%%n] %s\n",
505 ( $username ne $user ? "$username: " : "" ),
506 $t->{user}{screen_name},
510 print scalar localtime, " - Polling for replies" if &debug;
511 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
513 foreach my $t ( reverse @$tweets ) {
515 if exists $friends{ $t->{user}{screen_name} };
517 my $text = decode_entities( $t->{text} );
519 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
520 printf $fh "id:%d [%s%%B\@%s%%n] %s\n",
522 ( $username ne $user ? "$username: " : "" ),
523 $t->{user}{screen_name},
527 print scalar localtime, " - Polling for DMs" if &debug;
529 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
531 foreach my $t ( reverse @$tweets ) {
532 my $text = decode_entities( $t->{text} );
534 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
535 printf $fh "id:%d [%s%%B\@%s%%n (%%WDM%%n)] %s\n",
537 ( $username ne $user ? "$username: " : "" ),
538 $t->{sender_screen_name},
541 print scalar localtime, " - Done" if &debug;
546 my $filename = $data->[0];
548 print scalar localtime, " - checking child log at $filename" if &debug;
549 my $old_last_poll = $last_poll;
550 if ( open FILE, $filename ) {
554 last if /^__friends__/;
555 if (s/^id:(\d+) //) {
556 next if exists $tweet_cache{$1};
557 $tweet_cache{$1} = time;
559 push @lines, $_ unless /^__friends__/;
568 my ( $f, $t ) = split ' ', $_;
569 $nicks{$f} = $friends{$f} = $t;
572 if ( $last_poll != $old_last_poll ) {
573 print "new last_poll = $last_poll" if &debug;
574 foreach my $line (@lines) {
576 $window->print( $line, MSGLEVEL_PUBLIC );
577 foreach ( $line =~ /\@([-\w]+)/ ) {
584 or warn "Failed to remove $filename: $!"
587 # keep 10 minutes of cached tweets, to make sure we don't show duplicates.
588 foreach ( keys %tweet_cache ) {
589 next if $tweet_cache{$_} > time - 600;
590 delete $tweet_cache{$_};
597 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
601 return Irssi::settings_get_bool("twirssi_debug");
605 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
609 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
611 return unless $linestart =~ /^\/(?:tweet|dm)/;
612 return if $linestart eq '/tweet' and $word !~ s/^@//;
613 push @$complist, grep /^\Q$word/i,
614 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
615 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
618 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
619 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
620 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
621 Irssi::settings_add_str( "twirssi", "twirssi_location",
622 ".irssi/scripts/twirssi.pl" );
623 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
624 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
625 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
626 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
627 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
628 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
629 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
630 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
633 Irssi::command_bind( "dm", "cmd_direct" );
634 Irssi::command_bind( "tweet", "cmd_tweet" );
635 Irssi::command_bind( "dm_as", "cmd_direct_as" );
636 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
637 Irssi::command_bind( "twitter_login", "cmd_login" );
638 Irssi::command_bind( "twitter_logout", "cmd_logout" );
639 Irssi::command_bind( "twitter_switch", "cmd_switch" );
640 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
644 print "twits: ", Dumper \%twits;
645 print "friends: ", join ", ", sort keys %friends;
646 print "nicks: ", join ", ", sort keys %nicks;
647 print "last poll: $last_poll";
654 "Twirssi v$VERSION (r$REV); Net::Twitter v$Net::Twitter::VERSION. "
655 . "See details at http://tinyurl.com/twirssi" );
661 "/twitter_friend <username>",
663 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
669 "/twitter_unfriend <username>",
671 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
674 Irssi::command_bind( "twitter_updates", "get_updates" );
675 Irssi::signal_add_last( 'complete word' => \&sig_complete );
677 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
678 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
680 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
682 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
683 eval "use WWW::Shorten::$provider;";
687 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
692 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
693 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
700 ->print( "Create a window named "
701 . Irssi::settings_get_str('twitter_window')
702 . " or change the value of twitter_window. Then, reload twirssi." );