10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 317 $' =~ /(\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;
37 my ( $data, $server, $win ) = @_;
40 ¬ice("Not logged in! Use /twitter_login username pass!");
44 my ( $target, $text ) = split ' ', $data, 2;
45 unless ( $target and $text ) {
46 ¬ice("Usage: /dm <nick> <message>");
50 &cmd_direct_as( "$user $data", $server, $win );
54 my ( $data, $server, $win ) = @_;
57 ¬ice("Not logged in! Use /twitter_login username pass!");
61 my ( $username, $target, $text ) = split ' ', $data, 3;
62 unless ( $username and $target and $text ) {
63 ¬ice("Usage: /dm_as <username> <nick> <message>");
67 unless ( exists $twits{$username} ) {
68 ¬ice("Unknown username $username");
72 unless ( $twits{$username}
73 ->new_direct_message( { user => $target, text => $text } ) )
75 ¬ice("DM to $target failed");
79 ¬ice("DM sent to $target");
80 $nicks{$target} = time;
84 my ( $data, $server, $win ) = @_;
87 ¬ice("Not logged in! Use /twitter_login username pass!");
91 $data =~ s/^\s+|\s+$//;
93 ¬ice("Usage: /tweet <update>");
97 &cmd_tweet_as( "$user $data", $server, $win );
101 my ( $data, $server, $win ) = @_;
104 ¬ice("Not logged in! Use /twitter_login username pass!");
108 $data =~ s/^\s+|\s+$//;
109 my ( $username, $data ) = split ' ', $data, 2;
111 unless ( $username and $data ) {
112 ¬ice("Usage: /tweet_as <username> <update>");
116 unless ( exists $twits{$username} ) {
117 ¬ice("Unknown username $username");
121 if ( Irssi::settings_get_str("short_url_provider") ) {
122 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
124 my $short = makeashorterlink($url);
125 $data =~ s/\Q$url/$short/g;
130 unless ( $twits{$username}->update($data) ) {
131 ¬ice("Update failed");
135 foreach ( $data =~ /@([-\w]+)/ ) {
140 if ( Irssi::settings_get_bool("tweet_to_away")
142 and $data !~ /^[dD] / )
145 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
147 $server->send_raw("away :$data");
150 ¬ice( "Can't find bitlbee server.",
151 "Update bitlbee_server or disalbe tweet_to_away" );
155 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
159 my ( $usage_str, $api_name, $post_ref ) = @_;
162 my ( $data, $server, $win ) = @_;
165 ¬ice("Not logged in! Use /twitter_login username pass!");
169 $data =~ s/^\s+|\s+$//;
171 ¬ice("Usage: $usage_str");
175 unless ( $twit->$api_name($data) ) {
176 ¬ice("$api_name failed");
180 &$post_ref($data) if $post_ref;
185 my ( $data, $server, $win ) = @_;
187 $data =~ s/^\s+|\s+$//g;
188 if ( exists $twits{$data} ) {
189 ¬ice("Switching to $data");
190 $twit = $twits{$data};
193 ¬ice("Unknown user $data");
198 my ( $data, $server, $win ) = @_;
200 $data =~ s/^\s+|\s+$//g;
201 if ( $data and exists $twits{$data} ) {
202 ¬ice("Logging out $data...");
203 $twits{$data}->end_session();
204 delete $twits{$data};
206 ¬ice("Unknown username '$data'");
208 ¬ice("Logging out $user...");
209 $twit->end_session();
211 delete $twits{$user};
213 &cmd_switch( ( keys %twits )[0], $server, $win );
215 Irssi::timeout_remove($poll) if $poll;
222 my ( $data, $server, $win ) = @_;
224 ( $user, $pass ) = split ' ', $data, 2;
226 %friends = %nicks = ();
228 $twit = Net::Twitter->new(
234 unless ( $twit->verify_credentials() ) {
235 ¬ice("Login as $user failed");
238 &cmd_switch( ( keys %twits )[0], $server, $win );
244 my $rate_limit = $twit->rate_limit_status();
245 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
246 ¬ice("Rate limit exceeded, try again later");
251 $twits{$user} = $twit;
252 Irssi::timeout_remove($poll) if $poll;
253 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
254 ¬ice("Logged in as $user, loading friends list...");
256 ¬ice( "loaded friends: ", scalar keys %friends );
257 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
258 Irssi::settings_set_bool( "twirssi_first_run", 0 );
259 unless ( exists $friends{twirssi} ) {
260 ¬ice("Welcome to twirssi!"
261 . " Perhaps you should add \@twirssi to your friends list,"
262 . " so you can be notified when a new version is release?"
263 . " Just type /twitter_friend twirssi." );
270 ¬ice("Login failed");
275 my ( $data, $server, $win ) = @_;
277 my $loc = Irssi::settings_get_str("twirssi_location");
280 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
285 if ( not -x "/usr/bin/md5sum" and not $data ) {
287 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
294 eval { use Digest::MD5; };
298 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
303 $md5 = get("http://irc.peeron.com/~zigdon/twirssi/md5sum");
307 ¬ice("Failed to download md5sum from peeron! Aborting.");
311 unless ( open( CUR, $loc ) ) {
313 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
318 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
321 if ( $cur_md5 eq $md5 ) {
322 ¬ice("Current twirssi seems to be up to date.");
327 my $URL = "http://irc.peeron.com/~zigdon/twirssi/twirssi.pl";
328 ¬ice("Downloading twirssi from $URL");
329 LWP::Simple::getstore( $URL, "$loc.upgrade" );
332 unless ( open( NEW, "$loc.upgrade" ) ) {
334 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
339 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
342 if ( $new_md5 ne $md5 ) {
343 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
348 rename $loc, "$loc.backup"
349 or ¬ice("Failed to back up $loc: $!. Aborting")
351 rename "$loc.upgrade", $loc
352 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
355 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
356 if ( -e "$dir/autorun/$file" ) {
357 ¬ice("Updating $dir/autorun/$file");
358 unlink "$dir/autorun/$file"
359 or ¬ice("Failed to remove old $file from autorun: $!");
360 symlink "../$file", "$dir/autorun/$file"
361 or ¬ice("Failed to create symlink in autorun directory: $!");
364 ¬ice("Download complete. Reload twirssi with /script load $file");
371 my $friends = $twit->friends( { page => $page } );
372 last unless $friends;
373 $new_friends{ $_->{screen_name} } = time foreach @$friends;
375 last if @$friends == 0 or $page == 10;
376 $friends = $twit->friends( page => $page );
379 foreach ( keys %new_friends ) {
380 next if exists $friends{$_};
384 foreach ( keys %friends ) {
385 delete $friends{$_} unless exists $new_friends{$_};
390 print scalar localtime, " - get_updates starting" if &debug;
393 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
396 ->print( "Can't find a window named '"
397 . Irssi::settings_get_str('twitter_window')
398 . "'. Create it or change the value of twitter_window" );
401 ¬ice("Not logged in! Use /twitter_login username pass!");
405 my ( $fh, $filename ) = File::Temp::tempfile();
409 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
410 } elsif ( defined $pid ) { # child
417 &do_updates( $fh, $user, $twit );
418 foreach ( keys %twits ) {
420 &do_updates( $fh, $_, $twits{$_} );
423 print $fh "__friends__\n";
425 foreach ( sort keys %friends ) {
426 print $fh "$_ $friends{$_}\n";
432 print scalar localtime, " - get_updates ends" if &debug;
436 my ( $fh, $username, $obj ) = @_;
438 print scalar localtime, " - Polling for updates for $username" if &debug;
440 $obj->friends_timeline( { since => HTTP::Date::time2str($last_poll) } )
442 foreach my $t ( reverse @$tweets ) {
443 my $text = decode_entities( $t->{text} );
445 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
447 if ( Irssi::settings_get_bool("show_reply_context")
448 and $t->{in_reply_to_screen_name} ne $username
449 and $t->{in_reply_to_screen_name}
450 and not exists $friends{ $t->{in_reply_to_screen_name} } )
452 $nicks{ $t->{in_reply_to_screen_name} } = time;
453 my $context = $obj->show_status( $t->{in_reply_to_status_id} );
455 my $ctext = decode_entities( $context->{text} );
457 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
458 printf $fh "[%s%%B\@%s%%n] %s\n",
459 ( $username ne $user ? "$username: " : "" ),
460 $context->{user}{screen_name}, $ctext;
463 print "Failed to get context from $t->{in_reply_to_screen_name}"
468 if $t->{user}{screen_name} eq $username
469 and not Irssi::settings_get_bool("show_own_tweets");
470 printf $fh "%s[%s%%B\@%s%%n] %s\n",
472 ( $username ne $user ? "$username: " : "" ),
473 $t->{user}{screen_name},
477 print scalar localtime, " - Polling for replies" if &debug;
478 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
480 foreach my $t ( reverse @$tweets ) {
482 if exists $friends{ $t->{user}{screen_name} };
484 my $text = decode_entities( $t->{text} );
486 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
487 printf $fh "[%s%%B\@%s%%n] %s\n",
488 ( $username ne $user ? "$username: " : "" ),
489 $t->{user}{screen_name},
493 print scalar localtime, " - Polling for DMs" if &debug;
495 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
497 foreach my $t ( reverse @$tweets ) {
498 my $text = decode_entities( $t->{text} );
500 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
501 printf $fh "[%s%%B\@%s%%n (%%WDM%%n)] %s\n",
502 ( $username ne $user ? "$username: " : "" ),
503 $t->{sender_screen_name},
506 print scalar localtime, " - Done" if &debug;
511 my $filename = $data->[0];
513 print scalar localtime, " - checking child log at $filename" if &debug;
514 my $old_last_poll = $last_poll;
515 if ( open FILE, $filename ) {
519 last if /^__friends__/;
520 push @lines, $_ unless /^__friends__/;
529 my ( $f, $t ) = split ' ', $_;
530 $nicks{$f} = $friends{$f} = $t;
533 if ( $last_poll != $old_last_poll ) {
534 print "new last_poll = $last_poll" if &debug;
535 foreach my $line (@lines) {
537 $window->print( $line, MSGLEVEL_PUBLIC );
538 foreach ( $line =~ /\@([-\w]+)/ ) {
545 or warn "Failed to remove $filename: $!"
552 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
556 return Irssi::settings_get_bool("twirssi_debug");
560 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
564 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
566 return unless $linestart =~ /^\/(?:tweet|dm)/;
567 return if $linestart eq '/tweet' and $word !~ s/^@//;
568 push @$complist, grep /^\Q$word/i,
569 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
570 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
573 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
574 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
575 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
576 Irssi::settings_add_str( "twirssi", "twirssi_location",
577 ".irssi/scripts/twirssi.pl" );
578 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
579 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
580 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
581 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
582 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
583 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
584 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
585 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
588 Irssi::command_bind( "dm", "cmd_direct" );
589 Irssi::command_bind( "tweet", "cmd_tweet" );
590 Irssi::command_bind( "dm_as", "cmd_direct_as" );
591 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
592 Irssi::command_bind( "twitter_login", "cmd_login" );
593 Irssi::command_bind( "twitter_logout", "cmd_logout" );
594 Irssi::command_bind( "twitter_switch", "cmd_switch" );
595 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
599 print "twits: ", Dumper \%twits;
600 print "friends: ", join ", ", sort keys %friends;
601 print "nicks: ", join ", ", sort keys %nicks;
602 print "last poll: $last_poll";
609 "Twirssi v$VERSION (r$REV); Net::Twitter v$Net::Twitter::VERSION. "
610 . "See details at http://tinyurl.com/twirssi" );
616 "/twitter_friend <username>",
618 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
624 "/twitter_unfriend <username>",
626 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
629 Irssi::command_bind( "twitter_updates", "get_updates" );
630 Irssi::signal_add_last( 'complete word' => \&sig_complete );
632 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
633 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
635 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
637 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
638 eval "use WWW::Shorten::$provider;";
642 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
647 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
648 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
650 my @user = split /\s*,\s*/, $autouser;
651 my @pass = split /\s*,\s*/, $autopass;
652 if ( @user != @pass ) {
654 "Number of usernames doesn't match the number of passwords - auto-login failed"
658 while ( @user and @pass ) {
668 ->print( "Create a window named "
669 . Irssi::settings_get_str('twitter_window')
670 . " or change the value of twitter_window. Then, reload twirssi." );