10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 312 $' =~ /(\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 failed");
241 my $rate_limit = $twit->rate_limit_status();
242 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
243 ¬ice("Rate limit exceeded, try again later");
248 $twits{$user} = $twit;
249 Irssi::timeout_remove($poll) if $poll;
250 $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" );
251 ¬ice("Logged in as $user, loading friends list...");
253 ¬ice( "loaded friends: ", scalar keys %friends );
258 ¬ice("Login failed");
263 my ( $data, $server, $win ) = @_;
265 my $loc = Irssi::settings_get_str("twirssi_location");
268 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
273 if ( not -x "/usr/bin/md5sum" and not $data ) {
275 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
282 eval { use Digest::MD5; };
286 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
291 $md5 = get("http://irc.peeron.com/~zigdon/twirssi/md5sum");
295 ¬ice("Failed to download md5sum from peeron! Aborting.");
299 unless ( open( CUR, $loc ) ) {
301 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
306 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
309 if ( $cur_md5 eq $md5 ) {
310 ¬ice("Current twirssi seems to be up to date.");
315 my $URL = "http://irc.peeron.com/~zigdon/twirssi/twirssi.pl";
316 ¬ice("Downloading twirssi from $URL");
317 LWP::Simple::getstore( $URL, "$loc.upgrade" );
320 unless ( open( NEW, "$loc.upgrade" ) ) {
322 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
327 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
330 if ( $new_md5 ne $md5 ) {
331 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
336 rename $loc, "$loc.backup"
337 or ¬ice("Failed to back up $loc: $!. Aborting")
339 rename "$loc.upgrade", $loc
340 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
343 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
344 if ( -e "$dir/autorun/$file" ) {
345 ¬ice("Updating $dir/autorun/$file");
346 unlink "$dir/autorun/$file"
347 or ¬ice("Failed to remove old $file from autorun: $!");
348 symlink "../$file", "$dir/autorun/$file"
349 or ¬ice("Failed to create symlink in autorun directory: $!");
352 ¬ice("Download complete. Reload twirssi with /script load $file");
359 my $friends = $twit->friends( { page => $page } );
360 last unless $friends;
361 $new_friends{ $_->{screen_name} } = time foreach @$friends;
363 last if @$friends == 0 or $page == 10;
364 $friends = $twit->friends( page => $page );
367 foreach ( keys %new_friends ) {
368 next if exists $friends{$_};
372 foreach ( keys %friends ) {
373 delete $friends{$_} unless exists $new_friends{$_};
378 print scalar localtime, " - get_updates starting" if &debug;
381 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
384 ->print( "Can't find a window named '"
385 . Irssi::settings_get_str('twitter_window')
386 . "'. Create it or change the value of twitter_window" );
389 ¬ice("Not logged in! Use /twitter_login username pass!");
393 my ( $fh, $filename ) = File::Temp::tempfile();
397 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
398 } elsif ( defined $pid ) { # child
405 &do_updates( $fh, $user, $twit );
406 foreach ( keys %twits ) {
408 &do_updates( $fh, $_, $twits{$_} );
411 print $fh "__friends__\n";
413 foreach ( sort keys %friends ) {
414 print $fh "$_ $friends{$_}\n";
420 print scalar localtime, " - get_updates ends" if &debug;
424 my ( $fh, $username, $obj ) = @_;
426 print scalar localtime, " - Polling for updates for $username" if &debug;
428 $obj->friends_timeline( { since => HTTP::Date::time2str($last_poll) } )
430 foreach my $t ( reverse @$tweets ) {
431 my $text = decode_entities( $t->{text} );
433 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
435 if ( Irssi::settings_get_bool("show_reply_context")
436 and $t->{in_reply_to_screen_name} ne $username
437 and $t->{in_reply_to_screen_name}
438 and not exists $friends{ $t->{in_reply_to_screen_name} } )
440 $nicks{ $t->{in_reply_to_screen_name} } = time;
441 my $context = $obj->show_status( $t->{in_reply_to_status_id} );
443 my $ctext = decode_entities( $context->{text} );
445 $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
446 printf $fh "[%s%%B\@%s%%n] %s\n",
447 ( $username ne $user ? "$username: " : "" ),
448 $context->{user}{screen_name}, $ctext;
451 print "Failed to get context from $t->{in_reply_to_screen_name}"
456 if $t->{user}{screen_name} eq $username
457 and not Irssi::settings_get_bool("show_own_tweets");
458 printf $fh "%s[%s%%B\@%s%%n] %s\n",
460 ( $username ne $user ? "$username: " : "" ),
461 $t->{user}{screen_name},
465 print scalar localtime, " - Polling for replies" if &debug;
466 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
468 foreach my $t ( reverse @$tweets ) {
470 if exists $friends{ $t->{user}{screen_name} };
472 my $text = decode_entities( $t->{text} );
474 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
475 printf $fh "[%s%%B\@%s%%n] %s\n",
476 ( $username ne $user ? "$username: " : "" ),
477 $t->{user}{screen_name},
481 print scalar localtime, " - Polling for DMs" if &debug;
483 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
485 foreach my $t ( reverse @$tweets ) {
486 my $text = decode_entities( $t->{text} );
488 $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/g;
489 printf $fh "[%s%%B\@%s%%n (%%WDM%%n)] %s\n",
490 ( $username ne $user ? "$username: " : "" ),
491 $t->{sender_screen_name},
494 print scalar localtime, " - Done" if &debug;
499 my $filename = $data->[0];
501 print scalar localtime, " - checking child log at $filename" if &debug;
502 my $old_last_poll = $last_poll;
503 if ( open FILE, $filename ) {
507 last if /^__friends__/;
508 push @lines, $_ unless /^__friends__/;
517 my ( $f, $t ) = split ' ', $_;
518 $nicks{$f} = $friends{$f} = $t;
521 if ( $last_poll != $old_last_poll ) {
522 print "new last_poll = $last_poll" if &debug;
523 foreach my $line (@lines) {
525 $window->print( $line, MSGLEVEL_PUBLIC );
526 foreach ( $line =~ /\@([-\w]+)/ ) {
533 or warn "Failed to remove $filename: $!"
540 Irssi::timeout_add_once( 5000, 'monitor_child', [$filename] );
544 return Irssi::settings_get_bool("twirssi_debug");
548 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
552 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
554 return unless $linestart =~ /^\/(?:tweet|dm)/;
555 return if $linestart eq '/tweet' and $word !~ s/^@//;
556 push @$complist, grep /^\Q$word/i,
557 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
558 @$complist = map { "\@$_" } @$complist if $linestart eq '/tweet';
561 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
562 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
563 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
564 Irssi::settings_add_str( "twirssi", "twirssi_location",
565 ".irssi/scripts/twirssi.pl" );
566 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
567 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
568 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
569 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
570 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
573 Irssi::command_bind( "dm", "cmd_direct" );
574 Irssi::command_bind( "tweet", "cmd_tweet" );
575 Irssi::command_bind( "dm_as", "cmd_direct_as" );
576 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
577 Irssi::command_bind( "twitter_login", "cmd_login" );
578 Irssi::command_bind( "twitter_logout", "cmd_logout" );
579 Irssi::command_bind( "twitter_switch", "cmd_switch" );
580 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
584 print "twits: ", Dumper \%twits;
585 print "friends: ", join ", ", sort keys %friends;
586 print "nicks: ", join ", ", sort keys %nicks;
587 print "last poll: $last_poll";
594 "Twirssi v$VERSION (r$REV); Net::Twitter v$Net::Twitter::VERSION. "
595 . "See details at http://tinyurl.com/twirssi" );
601 "/twitter_friend <username>",
603 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
609 "/twitter_unfriend <username>",
611 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
614 Irssi::command_bind( "twitter_updates", "get_updates" );
615 Irssi::signal_add_last( 'complete word' => \&sig_complete );
617 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
618 ¬ice(" %C(_(\\%N http://tinyurl.com/twirssi for full docs");
620 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
622 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
623 eval "use WWW::Shorten::$provider;";
627 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
633 ->print( "Create a window named "
634 . Irssi::settings_get_str('twitter_window')
635 . " or change the value of twitter_window. Then, reload twirssi." );