10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 318 $' =~ /(\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 ) {
250 ¬ice("/twitter_login requires either a username and password "
251 . "or twitter_usernames and twitter_passwords to be set." );
255 %friends = %nicks = ();
257 $twit = Net::Twitter->new(
263 unless ( $twit->verify_credentials() ) {
264 ¬ice("Login as $user failed");
267 &cmd_switch( ( keys %twits )[0], $server, $win );
273 my $rate_limit = $twit->rate_limit_status();
274 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
275 ¬ice("Rate limit exceeded, try again later");
280 $twits{$user} = $twit;
281 Irssi::timeout_remove($poll) if $poll;
282 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
283 ¬ice("Logged in as $user, loading friends list...");
285 ¬ice( "loaded friends: ", scalar keys %friends );
286 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
287 Irssi::settings_set_bool( "twirssi_first_run", 0 );
288 unless ( exists $friends{twirssi} ) {
289 ¬ice("Welcome to twirssi!"
290 . " Perhaps you should add \@twirssi to your friends list,"
291 . " so you can be notified when a new version is release?"
292 . " Just type /twitter_friend twirssi." );
299 ¬ice("Login failed");
304 my ( $data, $server, $win ) = @_;
306 my $loc = Irssi::settings_get_str("twirssi_location");
309 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
314 if ( not -x "/usr/bin/md5sum" and not $data ) {
316 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
323 eval { use Digest::MD5; };
327 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
332 $md5 = get("http://irc.peeron.com/~zigdon/twirssi/md5sum");
336 ¬ice("Failed to download md5sum from peeron! Aborting.");
340 unless ( open( CUR, $loc ) ) {
342 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
347 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
350 if ( $cur_md5 eq $md5 ) {
351 ¬ice("Current twirssi seems to be up to date.");
356 my $URL = "http://irc.peeron.com/~zigdon/twirssi/twirssi.pl";
357 ¬ice("Downloading twirssi from $URL");
358 LWP::Simple::getstore( $URL, "$loc.upgrade" );
361 unless ( open( NEW, "$loc.upgrade" ) ) {
363 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
368 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
371 if ( $new_md5 ne $md5 ) {
372 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
377 rename $loc, "$loc.backup"
378 or ¬ice("Failed to back up $loc: $!. Aborting")
380 rename "$loc.upgrade", $loc
381 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
384 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
385 if ( -e "$dir/autorun/$file" ) {
386 ¬ice("Updating $dir/autorun/$file");
387 unlink "$dir/autorun/$file"
388 or ¬ice("Failed to remove old $file from autorun: $!");
389 symlink "../$file", "$dir/autorun/$file"
390 or ¬ice("Failed to create symlink in autorun directory: $!");
393 ¬ice("Download complete. Reload twirssi with /script load $file");
400 my $friends = $twit->friends( { page => $page } );
401 last unless $friends;
402 $new_friends{ $_->{screen_name} } = time foreach @$friends;
404 last if @$friends == 0 or $page == 10;
405 $friends = $twit->friends( page => $page );
408 foreach ( keys %new_friends ) {
409 next if exists $friends{$_};
413 foreach ( keys %friends ) {
414 delete $friends{$_} unless exists $new_friends{$_};
419 print scalar localtime, " - get_updates starting" if &debug;
422 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
425 ->print( "Can't find a window named '"
426 . Irssi::settings_get_str('twitter_window')
427 . "'. Create it or change the value of twitter_window" );
430 ¬ice("Not logged in! Use /twitter_login username pass!");
434 my ( $fh, $filename ) = File::Temp::tempfile();
438 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
439 } elsif ( defined $pid ) { # child
446 &do_updates( $fh, $user, $twit );
447 foreach ( keys %twits ) {
449 &do_updates( $fh, $_, $twits{$_} );
452 print $fh "__friends__\n";
454 foreach ( sort keys %friends ) {
455 print $fh "$_ $friends{$_}\n";
461 print scalar localtime, " - get_updates ends" if &debug;
465 my ( $fh, $username, $obj ) = @_;
467 print scalar localtime, " - Polling for updates for $username" if &debug;
469 $obj->friends_timeline( { since => HTTP::Date::time2str($last_poll) } )
471 foreach my $t ( reverse @$tweets ) {
472 my $text = decode_entities( $t->{text} );
474 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
476 if ( Irssi::settings_get_bool("show_reply_context")
477 and $t->{in_reply_to_screen_name} ne $username
478 and $t->{in_reply_to_screen_name}
479 and not exists $friends{ $t->{in_reply_to_screen_name} } )
481 $nicks{ $t->{in_reply_to_screen_name} } = time;
482 my $context = $obj->show_status( $t->{in_reply_to_status_id} );
484 my $ctext = decode_entities( $context->{text} );
486 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
487 printf $fh "id:%d [%s%%B\@%s%%n] %s\n",
489 ( $username ne $user ? "$username: " : "" ),
490 $context->{user}{screen_name}, $ctext;
493 print "Failed to get context from $t->{in_reply_to_screen_name}"
498 if $t->{user}{screen_name} eq $username
499 and not Irssi::settings_get_bool("show_own_tweets");
500 printf $fh "id:%d %s[%s%%B\@%s%%n] %s\n",
503 ( $username ne $user ? "$username: " : "" ),
504 $t->{user}{screen_name},
508 print scalar localtime, " - Polling for replies" if &debug;
509 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
511 foreach my $t ( reverse @$tweets ) {
513 if exists $friends{ $t->{user}{screen_name} };
515 my $text = decode_entities( $t->{text} );
517 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
518 printf $fh "id:%d [%s%%B\@%s%%n] %s\n",
520 ( $username ne $user ? "$username: " : "" ),
521 $t->{user}{screen_name},
525 print scalar localtime, " - Polling for DMs" if &debug;
527 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
529 foreach my $t ( reverse @$tweets ) {
530 my $text = decode_entities( $t->{text} );
532 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
533 printf $fh "id:%d [%s%%B\@%s%%n (%%WDM%%n)] %s\n",
535 ( $username ne $user ? "$username: " : "" ),
536 $t->{sender_screen_name},
539 print scalar localtime, " - Done" if &debug;
544 my $filename = $data->[0];
546 print scalar localtime, " - checking child log at $filename" if &debug;
547 my $old_last_poll = $last_poll;
548 if ( open FILE, $filename ) {
552 last if /^__friends__/;
553 if (s/^id:(\d+) //) {
554 next if exists $tweet_cache{$1};
555 $tweet_cache{$1} = time;
557 push @lines, $_ unless /^__friends__/;
566 my ( $f, $t ) = split ' ', $_;
567 $nicks{$f} = $friends{$f} = $t;
570 if ( $last_poll != $old_last_poll ) {
571 print "new last_poll = $last_poll" if &debug;
572 foreach my $line (@lines) {
574 $window->print( $line, MSGLEVEL_PUBLIC );
575 foreach ( $line =~ /\@([-\w]+)/ ) {
582 or warn "Failed to remove $filename: $!"
585 # keep 10 minutes of cached tweets, to make sure we don't show duplicates.
586 foreach ( keys %tweet_cache ) {
587 next if $tweet_cache{$_} > time - 600;
588 delete $tweet_cache{$_};
595 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
599 return Irssi::settings_get_bool("twirssi_debug");
603 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
607 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
609 return unless $linestart =~ /^\/(?:tweet|dm)/;
610 return if $linestart eq '/tweet' and $word !~ s/^@//;
611 push @$complist, grep /^\Q$word/i,
612 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
613 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
616 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
617 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
618 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
619 Irssi::settings_add_str( "twirssi", "twirssi_location",
620 ".irssi/scripts/twirssi.pl" );
621 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
622 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
623 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
624 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
625 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
626 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
627 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
628 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
631 Irssi::command_bind( "dm", "cmd_direct" );
632 Irssi::command_bind( "tweet", "cmd_tweet" );
633 Irssi::command_bind( "dm_as", "cmd_direct_as" );
634 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
635 Irssi::command_bind( "twitter_login", "cmd_login" );
636 Irssi::command_bind( "twitter_logout", "cmd_logout" );
637 Irssi::command_bind( "twitter_switch", "cmd_switch" );
638 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
642 print "twits: ", Dumper \%twits;
643 print "friends: ", join ", ", sort keys %friends;
644 print "nicks: ", join ", ", sort keys %nicks;
645 print "last poll: $last_poll";
652 "Twirssi v$VERSION (r$REV); Net::Twitter v$Net::Twitter::VERSION. "
653 . "See details at http://tinyurl.com/twirssi" );
659 "/twitter_friend <username>",
661 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
667 "/twitter_unfriend <username>",
669 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
672 Irssi::command_bind( "twitter_updates", "get_updates" );
673 Irssi::signal_add_last( 'complete word' => \&sig_complete );
675 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
676 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
678 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
680 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
681 eval "use WWW::Shorten::$provider;";
685 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
690 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
691 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
698 ->print( "Create a window named "
699 . Irssi::settings_get_str('twitter_window')
700 . " or change the value of twitter_window. Then, reload twirssi." );