9 $Data::Dumper::Indent = 1;
11 use vars qw($VERSION %IRSSI);
13 $VERSION = "2.2.1beta";
14 my ($REV) = '$Rev: 607 $' =~ /(\d+)/;
16 authors => 'Dan Boger',
17 contact => 'zigdon@gmail.com',
19 description => 'Send twitter updates using /tweet. '
20 . 'Can optionally set your bitlbee /away message to same',
21 license => 'GNU GPL v2',
22 url => 'http://twirssi.com',
23 changed => '$Date: 2009-04-09 21:22:55 -0700 (Thu, 09 Apr 2009) $',
38 my %irssi_to_mirc_colors = (
58 my ( $data, $server, $win ) = @_;
60 return unless &logged_in($twit);
62 my ( $target, $text ) = split ' ', $data, 2;
63 unless ( $target and $text ) {
64 ¬ice("Usage: /dm <nick> <message>");
68 &cmd_direct_as( "$user $data", $server, $win );
72 my ( $data, $server, $win ) = @_;
74 return unless &logged_in($twit);
76 my ( $username, $target, $text ) = split ' ', $data, 3;
77 unless ( $username and $target and $text ) {
78 ¬ice("Usage: /dm_as <username> <nick> <message>");
82 return unless $username = &valid_username($username);
85 if ( $twits{$username}
86 ->new_direct_message( { user => $target, text => $text } ) )
88 ¬ice("DM sent to $target");
89 $nicks{$target} = time;
93 $error = JSON::Any->jsonToObj( $twits{$username}->get_error() );
94 $error = $error->{error};
97 ¬ice("DM to $target failed");
102 ¬ice("DM caused an error: $@");
108 my ( $data, $server, $win ) = @_;
110 return unless &logged_in($twit);
112 $data =~ s/^\s+|\s+$//;
114 ¬ice("Usage: /retweet <nick[:num]> [comment]");
118 my ( $id, $data ) = split ' ', $data, 2;
120 &cmd_retweet_as( "$user $id $data", $server, $win );
124 my ( $data, $server, $win ) = @_;
126 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
127 ¬ice("twirssi_track_replies is required in order to reteet.");
131 return unless &logged_in($twit);
133 $data =~ s/^\s+|\s+$//;
134 my ( $username, $id, $data ) = split ' ', $data, 3;
137 ¬ice("Usage: /retweet_as <username> <nick[:num]> [comment]");
141 return unless $username = &valid_username($username);
144 $id =~ s/[^\w\d\-:]+//g;
145 ( $nick, $id ) = split /:/, $id;
146 unless ( exists $id_map{ lc $nick } ) {
147 ¬ice("Can't find a tweet from $nick to retweet!");
151 $id = $id_map{__indexes}{$nick} unless $id;
152 unless ( $id_map{ lc $nick }[$id] ) {
153 ¬ice("Can't find a tweet numbered $id from $nick to retweet!");
157 unless ( $id_map{__tweets}{ lc $nick }[$id] ) {
158 ¬ice("The text of this tweet isn't saved, sorry!");
162 # Irssi::settings_add_str( "twirssi", "twirssi_retweet_format", 'RT $n: $t ${-- $c$}' );
163 my $text = Irssi::settings_get_str("twirssi_retweet_format");
164 $text =~ s/\$n/\@$nick/g;
166 $text =~ s/\${|\$}//g;
167 $text =~ s/\$c/$data/;
169 $text =~ s/\${.*?\$}//;
171 $text =~ s/\$t/$id_map{__tweets}{ lc $nick }[$id]/;
173 $data = &shorten($text);
175 return if &too_long($data);
180 $twits{$username}->update(
183 in_reply_to_status_id => $id_map{ lc $nick }[$id]
188 ¬ice("Update failed");
192 return unless $success;
195 ¬ice("Update caused an error: $@. Aborted");
199 foreach ( $data =~ /@([-\w]+)/ ) {
203 ¬ice("Retweet sent");
207 my ( $data, $server, $win ) = @_;
209 return unless &logged_in($twit);
211 $data =~ s/^\s+|\s+$//;
213 ¬ice("Usage: /tweet <update>");
217 &cmd_tweet_as( "$user\@$defservice $data", $server, $win );
221 my ( $data, $server, $win ) = @_;
223 return unless &logged_in($twit);
225 $data =~ s/^\s+|\s+$//;
226 $data =~ s/\s\s+/ /g;
227 my ( $username, $data ) = split ' ', $data, 2;
229 unless ( $username and $data ) {
230 ¬ice("Usage: /tweet_as <username> <update>");
234 return unless $username = &valid_username($username);
236 $data = &shorten($data);
238 return if &too_long($data);
242 unless ( $twits{$username}->update($data) )
244 ¬ice("Update failed");
248 return unless $success;
251 ¬ice("Update caused an error: $@. Aborted.");
255 foreach ( $data =~ /@([-\w]+)/ ) {
259 my $away = &update_away($data);
261 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
265 my ( $data, $server, $win ) = @_;
267 return unless &logged_in($twit);
269 $data =~ s/^\s+|\s+$//;
271 ¬ice("Usage: /reply <nick[:num]> <update>");
275 my ( $id, $data ) = split ' ', $data, 2;
276 unless ( $id and $data ) {
277 ¬ice("Usage: /reply <nick[:num]> <update>");
281 &cmd_reply_as( "$user $id $data", $server, $win );
285 my ( $data, $server, $win ) = @_;
287 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
288 ¬ice("twirssi_track_replies is required in order to reply to "
289 . "specific tweets. Either enable it, or just use /tweet "
290 . "\@username <text>." );
294 return unless &logged_in($twit);
296 $data =~ s/^\s+|\s+$//;
297 my ( $username, $id, $data ) = split ' ', $data, 3;
299 unless ( $username and $data ) {
300 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
304 return unless $username = &valid_username($username);
307 $id =~ s/[^\w\d\-:]+//g;
308 ( $nick, $id ) = split /:/, $id;
309 unless ( exists $id_map{ lc $nick } ) {
310 ¬ice("Can't find a tweet from $nick to reply to!");
314 $id = $id_map{__indexes}{$nick} unless $id;
315 unless ( $id_map{ lc $nick }[$id] ) {
316 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
320 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
322 # remove any @nick at the beginning of the reply, as we'll add it anyway
323 $data =~ s/^\s*\@?$nick\s*//;
324 $data = "\@$nick " . $data;
327 $data = &shorten($data);
329 return if &too_long($data);
334 $twits{$username}->update(
337 in_reply_to_status_id => $id_map{ lc $nick }[$id]
342 ¬ice("Update failed");
346 return unless $success;
349 ¬ice("Update caused an error: $@. Aborted");
353 foreach ( $data =~ /@([-\w]+)/ ) {
357 my $away = &update_away($data);
359 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
363 my ( $usage_str, $api_name, $post_ref ) = @_;
366 my ( $data, $server, $win ) = @_;
368 return unless &logged_in($twit);
370 $data =~ s/^\s+|\s+$//;
372 ¬ice("Usage: $usage_str");
378 unless ( $twit->$api_name($data) )
380 ¬ice("$api_name failed");
384 return unless $success;
387 ¬ice("$api_name caused an error. Aborted.");
391 &$post_ref($data) if $post_ref;
396 my ( $data, $server, $win ) = @_;
398 $data =~ s/^\s+|\s+$//g;
399 $data = &normalize_username($data);
400 if ( exists $twits{$data} ) {
401 ¬ice("Switching to $data");
402 $twit = $twits{$data};
403 if ( $data =~ /(.*)\@(.*)/ ) {
407 ¬ice("Couldn't figure out what service '$data' is on");
410 ¬ice("Unknown user $data");
415 my ( $data, $server, $win ) = @_;
417 $data =~ s/^\s+|\s+$//g;
418 $data = $user unless $data;
419 return unless $data = &valid_username($data);
421 ¬ice("Logging out $data...");
422 $twits{$data}->end_session();
423 delete $twits{$data};
426 &cmd_switch( ( keys %twits )[0], $server, $win );
428 Irssi::timeout_remove($poll) if $poll;
434 my ( $data, $server, $win ) = @_;
437 ( $user, $pass ) = split ' ', $data, 2;
438 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
439 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
441 my @user = split /\s*,\s*/, $autouser;
442 my @pass = split /\s*,\s*/, $autopass;
444 # if a password ends with a '\', it was meant to escape the comma, and
445 # it should be concatinated with the next one
449 while ( $p =~ /\\$/ and @pass ) {
450 $p .= "," . shift @pass;
455 if ( @user != @unescaped ) {
456 ¬ice("Number of usernames doesn't match "
457 . "the number of passwords - auto-login failed" );
460 while ( @user and @unescaped ) {
462 $p = shift @unescaped;
468 ¬ice("/twitter_login requires either a username and password "
469 . "or twitter_usernames and twitter_passwords to be set." );
473 %friends = %nicks = ();
476 if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
477 ( $user, $service ) = ( $1, $2 );
479 $service = Irssi::settings_get_str("twirssi_default_service");
481 $defservice = $service = ucfirst lc $service;
483 eval "use Net::$service";
486 "Failed to load Net::$service when trying to log in as $user: $@");
490 $twit = "Net::$service"->new(
496 unless ( $twit->verify_credentials() ) {
497 ¬ice("Login as $user\@$service failed");
500 &cmd_switch( ( keys %twits )[0], $server, $win );
506 my $rate_limit = $twit->rate_limit_status();
507 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
509 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
515 $twits{"$user\@$service"} = $twit;
516 Irssi::timeout_remove($poll) if $poll;
517 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
518 ¬ice("Logged in as $user\@$service, loading friends list...");
520 ¬ice( "loaded friends: ", scalar keys %friends );
521 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
522 Irssi::settings_set_bool( "twirssi_first_run", 0 );
523 unless ( exists $friends{twirssi} ) {
524 ¬ice("Welcome to twirssi!"
525 . " Perhaps you should add \@twirssi to your friends list,"
526 . " so you can be notified when a new version is release?"
527 . " Just type /twitter_friend twirssi." );
534 ¬ice("Login failed");
539 my ( $data, $server, $win ) = @_;
541 unless ( $twit and $twit->can('search') ) {
542 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
543 . "doesn't support searches." );
547 $data =~ s/^\s+|\s+$//;
551 ¬ice("Usage: /twitter_subscribe <topic>");
555 if ( exists $id_map{__searches}{$user}{$data} ) {
556 ¬ice("Already had a subscription for '$data'");
560 $id_map{__searches}{$user}{$data} = 1;
561 ¬ice("Added subscription for '$data'");
565 my ( $data, $server, $win ) = @_;
567 unless ( $twit and $twit->can('search') ) {
568 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
569 . "doesn't support searches." );
572 $data =~ s/^\s+|\s+$//;
576 ¬ice("Usage: /twitter_unsubscribe <topic>");
580 unless ( exists $id_map{__searches}{$user}{$data} ) {
581 ¬ice("No subscription found for '$data'");
585 delete $id_map{__searches}{$user}{$data};
586 ¬ice("Removed subscription for '$data'");
589 sub cmd_list_search {
590 my ( $data, $server, $win ) = @_;
593 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
595 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
596 $topics = $topics ? "$topics, $topic" : $topic;
600 ¬ice("Search subscriptions for \@$suser: $topics");
605 ¬ice("No search subscriptions set up");
610 my ( $data, $server, $win ) = @_;
612 my $loc = Irssi::settings_get_str("twirssi_location");
615 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
621 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
622 eval { use Digest::MD5; };
626 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
631 $md5 = get("http://twirssi.com/md5sum");
635 ¬ice("Failed to download md5sum from peeron! Aborting.");
639 unless ( open( CUR, $loc ) ) {
641 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
646 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
649 if ( $cur_md5 eq $md5 ) {
650 ¬ice("Current twirssi seems to be up to date.");
656 Irssi::settings_get_bool("twirssi_upgrade_beta")
657 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
658 : "http://twirssi.com/twirssi.pl";
659 ¬ice("Downloading twirssi from $URL");
660 LWP::Simple::getstore( $URL, "$loc.upgrade" );
662 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
663 unless ( open( NEW, "$loc.upgrade" ) ) {
665 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
670 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
673 if ( $new_md5 ne $md5 ) {
674 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
679 rename $loc, "$loc.backup"
680 or ¬ice("Failed to back up $loc: $!. Aborting")
682 rename "$loc.upgrade", $loc
683 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
686 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
687 if ( -e "$dir/autorun/$file" ) {
688 ¬ice("Updating $dir/autorun/$file");
689 unlink "$dir/autorun/$file"
690 or ¬ice("Failed to remove old $file from autorun: $!");
691 symlink "../$file", "$dir/autorun/$file"
692 or ¬ice("Failed to create symlink in autorun directory: $!");
695 ¬ice("Download complete. Reload twirssi with /script load $file");
705 print $fh "type:debug Loading friends page $page...\n"
706 if ( $fh and &debug );
707 my $friends = $twit->friends( { page => $page } );
708 last unless $friends;
709 $new_friends{ $_->{screen_name} } = time foreach @$friends;
711 last if @$friends == 0 or $page == 10;
716 print $fh "type:debug Error during friends list update. Aborted.\n";
720 my ( $added, $removed ) = ( 0, 0 );
721 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
722 foreach ( keys %new_friends ) {
723 next if exists $friends{$_};
728 print $fh "type:debug Scanning for removed friends...\n"
729 if ( $fh and &debug );
730 foreach ( keys %friends ) {
731 next if exists $new_friends{$_};
736 return ( $added, $removed );
740 print scalar localtime, " - get_updates starting" if &debug;
743 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
746 ->print( "Can't find a window named '"
747 . Irssi::settings_get_str('twitter_window')
748 . "'. Create it or change the value of twitter_window" );
751 return unless &logged_in($twit);
753 my ( $fh, $filename ) = File::Temp::tempfile();
754 binmode( $fh, ":utf8" );
758 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
759 Irssi::pidwait_add($pid);
760 } elsif ( defined $pid ) { # child
769 foreach ( keys %twits ) {
770 $error++ unless &do_updates( $fh, $_, $twits{$_}, \%context_cache );
773 my ( $added, $removed ) = &load_friends($fh);
774 if ( $added + $removed ) {
775 print $fh "type:debug %R***%n Friends list updated: ",
777 sprintf( "%d added", $added ),
778 sprintf( "%d removed", $removed ) ),
781 print $fh "__friends__\n";
782 foreach ( sort keys %friends ) {
783 print $fh "$_ $friends{$_}\n";
787 print $fh "type:debug Update encountered errors. Aborted\n";
788 print $fh "-- $last_poll";
790 print $fh "-- $new_poll";
795 print scalar localtime, " - get_updates ends" if &debug;
799 my ( $fh, $username, $obj, $cache ) = @_;
801 my $rate_limit = $obj->rate_limit_status();
802 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
803 ¬ice("Rate limit exceeded for $username");
807 print scalar localtime, " - Polling for updates for $username" if &debug;
811 if ( $id_map{__last_id}{$username}{timeline} )
813 $tweets = $obj->friends_timeline(
814 { since_id => $id_map{__last_id}{$username}{timeline} } );
816 $tweets = $obj->friends_timeline();
821 print $fh "type:debug Error during friends_timeline call: Aborted.\n";
822 print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
826 unless ( ref $tweets ) {
827 if ( $obj->can("get_error") ) {
828 my $error = "Unknown error";
829 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
830 unless ($@) { $error = $obj->get_error() }
832 "type:debug API Error during friends_timeline call: Aborted\n";
833 print $fh "type:debug : $_\n" foreach split /\n/, Dumper($error);
837 "type:debug API Error during friends_timeline call. Aborted.\n";
842 foreach my $t ( reverse @$tweets ) {
843 my $text = decode_entities( $t->{text} );
844 $text = &hilight($text);
846 if ( Irssi::settings_get_bool("show_reply_context")
847 and $t->{in_reply_to_screen_name} ne $username
848 and $t->{in_reply_to_screen_name}
849 and not exists $friends{ $t->{in_reply_to_screen_name} } )
851 $nicks{ $t->{in_reply_to_screen_name} } = time;
853 unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
855 $cache->{ $t->{in_reply_to_status_id} } =
856 $obj->show_status( $t->{in_reply_to_status_id} );
860 $context = $cache->{ $t->{in_reply_to_status_id} };
863 my $ctext = decode_entities( $context->{text} );
864 $ctext = &hilight($ctext);
865 if ( $context->{truncated} and ref($obj) ne 'Net::Identica' ) {
867 " -- http://twitter.com/$context->{user}{screen_name}"
868 . "/status/$context->{id}";
870 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
871 $context->{id}, $username,
872 $context->{user}{screen_name}, $ctext;
877 if $t->{user}{screen_name} eq $username
878 and not Irssi::settings_get_bool("show_own_tweets");
879 if ( $t->{truncated} and ref($obj) ne 'Net::Identica' ) {
880 $text .= " -- http://twitter.com/$t->{user}{screen_name}"
881 . "/status/$t->{id}";
883 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
884 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
885 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
887 printf $fh "id:%d account:%s type:last_id timeline\n",
888 $new_poll_id, $username;
890 print scalar localtime, " - Polling for replies" if &debug;
893 if ( $id_map{__last_id}{$username}{reply} )
895 $tweets = $obj->replies(
896 { since_id => $id_map{__last_id}{$username}{reply} } )
899 $tweets = $obj->replies() || [];
904 print $fh "type:debug Error during replies call. Aborted.\n";
908 foreach my $t ( reverse @$tweets ) {
910 if exists $friends{ $t->{user}{screen_name} };
912 my $text = decode_entities( $t->{text} );
913 $text = &hilight($text);
914 if ( $t->{truncated} ) {
915 $text .= " -- http://twitter.com/$t->{user}{screen_name}"
916 . "/status/$t->{id}";
918 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
919 $t->{id}, $username, $t->{user}{screen_name}, $text;
920 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
922 printf $fh "id:%d account:%s type:last_id reply\n", $new_poll_id, $username;
924 print scalar localtime, " - Polling for DMs" if &debug;
927 if ( $id_map{__last_id}{$username}{dm} )
929 $tweets = $obj->direct_messages(
930 { since_id => $id_map{__last_id}{$username}{dm} } )
933 $tweets = $obj->direct_messages() || [];
938 print $fh "type:debug Error during direct_messages call. Aborted.\n";
942 foreach my $t ( reverse @$tweets ) {
943 my $text = decode_entities( $t->{text} );
944 $text = &hilight($text);
945 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
946 $t->{id}, $username, $t->{sender_screen_name}, $text;
947 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
949 printf $fh "id:%d account:%s type:last_id dm\n", $new_poll_id, $username;
951 print scalar localtime, " - Polling for subscriptions" if &debug;
952 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
954 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
955 print $fh "type:debug searching for $topic since ",
956 "$id_map{__searches}{$username}{$topic}\n";
958 $search = $obj->search(
961 since_id => $id_map{__searches}{$username}{$topic}
968 "type:debug Error during search($topic) call. Aborted.\n";
972 unless ( $search->{max_id} ) {
973 print $fh "type:debug Invalid search results when searching",
974 " for $topic. Aborted.\n";
978 $id_map{__searches}{$username}{$topic} = $search->{max_id};
979 printf $fh "id:%d account:%s type:searchid topic:%s\n",
980 $search->{max_id}, $username, $topic;
982 foreach my $t ( reverse @{ $search->{results} } ) {
983 my $text = decode_entities( $t->{text} );
984 $text = &hilight($text);
985 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
986 $t->{id}, $username, $t->{from_user}, $topic, $text;
987 $new_poll_id = $t->{id}
989 or $t->{id} < $new_poll_id;
994 print scalar localtime, " - Done" if &debug;
1001 my $filename = $data->[0];
1002 my $attempt = $data->[1];
1004 print scalar localtime, " - checking child log at $filename ($attempt)"
1006 my ($new_last_poll);
1008 # first time we run we don't want to print out *everything*, so we just
1011 $suppress = 1 unless keys %tweet_cache;
1013 if ( open FILE, $filename ) {
1018 last if /^__friends__/;
1022 foreach my $key (qw/id account nick type topic/) {
1023 if (s/^$key:(\S+)\s*//) {
1028 if ( not $meta{type} or $meta{type} !~ /searchid|last_id/ ) {
1029 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
1033 $new_cache{ $meta{id} } = time;
1035 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
1041 $meta{account} =~ s/\@(\w+)$//;
1042 $meta{service} = $1;
1044 lc $meta{service} eq
1045 lc Irssi::settings_get_str("twirssi_default_service") )
1047 $account = "$meta{account}: "
1048 if lc "$meta{account}\@$meta{service}" ne lc
1049 "$user\@$defservice";
1051 $account = "$meta{account}\@$meta{service}: ";
1055 if ( $meta{type} ne 'dm'
1056 and Irssi::settings_get_bool("twirssi_track_replies")
1060 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
1061 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
1062 $id_map{__indexes}{ $meta{nick} } = $marker;
1063 $id_map{__tweets}{ lc $meta{nick} }[$marker] = $_;
1064 $marker = ":$marker";
1068 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
1070 '@' . substr( $meta{account}, 0, index( $meta{account}, "@" ) );
1071 if ( $_ =~ /\Q$nick\E(?:\W|$)/i
1072 and Irssi::settings_get_bool("twirssi_hilights") )
1074 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
1075 $hilight = MSGLEVEL_HILIGHT;
1078 if ( $meta{type} =~ /tweet|reply/ ) {
1081 ( MSGLEVEL_PUBLIC | $hilight ),
1082 $meta{type}, $account, $meta{nick}, $marker, $_
1084 } elsif ( $meta{type} eq 'search' ) {
1087 ( MSGLEVEL_PUBLIC | $hilight ),
1088 $meta{type}, $account, $meta{topic},
1089 $meta{nick}, $marker, $_
1092 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
1094 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1096 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1099 } elsif ( $meta{type} eq 'dm' ) {
1102 ( MSGLEVEL_MSGS | $hilight ),
1103 $meta{type}, $account, $meta{nick}, $_
1105 } elsif ( $meta{type} eq 'searchid' ) {
1106 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
1108 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
1110 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1112 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1115 print "Search '$meta{topic}' returned invalid id $meta{id}";
1117 } elsif ( $meta{type} eq 'last_id' ) {
1118 $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} =
1120 if $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} <
1122 } elsif ( $meta{type} eq 'error' ) {
1123 push @lines, [ MSGLEVEL_MSGS, $_ ];
1124 } elsif ( $meta{type} eq 'debug' ) {
1125 print "$_" if &debug,;
1127 print "Unknown line type $meta{type}: $_" if &debug,;
1134 ($new_last_poll) = ($1);
1137 my ( $f, $t ) = split ' ', $_;
1138 $nicks{$f} = $friends{$f} = $t;
1141 if ($new_last_poll) {
1142 print "new last_poll = $new_last_poll" if &debug;
1143 print "new last_poll_id = ", Dumper( $id_map{__last_id} ) if &debug;
1145 print "First call, not printing updates" if &debug;
1147 foreach my $line (@lines) {
1148 $window->printformat(
1150 "twirssi_" . $line->[1],
1151 @$line[ 2 .. $#$line ]
1158 or warn "Failed to remove $filename: $!"
1161 # commit the pending cache lines to the actual cache, now that
1162 # we've printed our output
1163 %tweet_cache = ( %tweet_cache, %new_cache );
1165 # keep enough cached tweets, to make sure we don't show duplicates.
1166 foreach ( keys %tweet_cache ) {
1167 next if $tweet_cache{$_} >= $last_poll - 3600;
1168 delete $tweet_cache{$_};
1170 $last_poll = $new_last_poll;
1175 Irssi::settings_get_str("twirssi_replies_store") )
1177 if ( open JSON, ">$file" ) {
1178 print JSON JSON::Any->objToJson( \%id_map );
1181 &ccrap("Failed to write replies to $file: $!");
1191 if ( $attempt < 24 ) {
1192 Irssi::timeout_add_once( 5000, 'monitor_child',
1193 [ $filename, $attempt + 1 ] );
1195 print "Giving up on polling $filename" if &debug;
1196 unlink $filename unless &debug;
1198 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1201 my @time = localtime($last_poll);
1202 if ( time - $last_poll < 24 * 60 * 60 ) {
1203 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1205 $since = scalar localtime($last_poll);
1208 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1213 q{ .--./ / | _.---.| },
1224 &ccrap("Haven't been able to get updated tweets since $since");
1229 return Irssi::settings_get_bool("twirssi_debug");
1233 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1237 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1243 if ( Irssi::settings_get_bool("tweet_to_away")
1245 and $data !~ /^[dD] / )
1248 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1250 $server->send_raw("away :$data");
1253 &ccrap( "Can't find bitlbee server.",
1254 "Update bitlbee_server or disable tweet_to_away" );
1264 my $noalert = shift;
1266 if ( length $data > 140 ) {
1267 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1275 sub valid_username {
1276 my $username = shift;
1278 $username = &normalize_username($username);
1280 unless ( exists $twits{$username} ) {
1281 ¬ice("Unknown username $username");
1291 ¬ice("Not logged in! Use /twitter_login username pass!");
1299 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1302 $linestart =~ /^\/(?:retweet|twitter_reply)(?:_as)?\s*$/
1303 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1304 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1306 { # /twitter_reply gets a nick:num
1308 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1309 sort { $nicks{$b} <=> $nicks{$a} }
1311 keys %{ $id_map{__indexes} };
1314 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1316 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1317 my $prefix = $word =~ s/^@//;
1318 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1319 push @$complist, grep /^\Q$word/i,
1320 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1321 @$complist = map { "\@$_" } @$complist if $prefix;
1325 sub event_send_text {
1326 my ( $line, $server, $win ) = @_;
1327 my $awin = Irssi::active_win();
1329 # if the window where we got our text was the twitter window, and the user
1330 # wants to be lazy, tweet away!
1331 if ( ( $awin->get_active_name() eq $window->{name} )
1332 and Irssi::settings_get_bool("tweet_window_input") )
1334 &cmd_tweet( $line, $server, $win );
1339 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1340 return $poll if $poll >= 60;
1347 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1348 my $c = Irssi::settings_get_str("twirssi_nick_color");
1349 $c = $irssi_to_mirc_colors{$c};
1350 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1352 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1353 my $c = Irssi::settings_get_str("twirssi_topic_color");
1354 $c = $irssi_to_mirc_colors{$c};
1355 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1357 $text =~ s/[\n\r]/ /g;
1365 my $provider = Irssi::settings_get_str("short_url_provider");
1368 Irssi::settings_get_bool("twirssi_always_shorten")
1369 or &too_long( $data, 1 )
1375 if ( $provider eq 'Bitly' ) {
1376 @args[ 1, 2 ] = split ',',
1377 Irssi::settings_get_str("short_url_args"), 2;
1378 unless ( @args == 3 ) {
1380 "WWW::Shorten::Bitly requires a username and API key.",
1381 "Set short_url_args to username,API_key or change your",
1382 "short_url_provider."
1388 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1391 my $short = makeashorterlink(@args);
1393 $data =~ s/\Q$url/$short/g;
1395 ¬ice("Failed to shorten $url!");
1404 sub normalize_username {
1407 my ( $username, $service ) = split /\@/, $user, 2;
1409 $service = ucfirst lc $service;
1412 ucfirst lc Irssi::settings_get_str("twirssi_default_service");
1413 unless ( exists $twits{"$username\@$service"} ) {
1415 foreach my $t ( sort keys %twits ) {
1416 next unless $t =~ /^\Q$username\E\@(Twitter|Identica)/;
1422 ¬ice("Can't find a logged in user '$user'");
1427 return "$username\@$service";
1430 Irssi::signal_add( "send text", "event_send_text" );
1432 Irssi::theme_register(
1434 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1435 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1436 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1437 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1438 'twirssi_error', 'ERROR: $0',
1442 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1443 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1444 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1445 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1446 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1447 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1448 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1449 Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
1450 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1451 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1452 Irssi::settings_add_str( "twirssi", "twirssi_retweet_format",
1453 'RT $n: "$t" ${-- $c$}' );
1454 Irssi::settings_add_str( "twirssi", "twirssi_location",
1455 ".irssi/scripts/twirssi.pl" );
1456 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1457 ".irssi/scripts/twirssi.json" );
1458 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1459 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1460 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1461 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1462 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1463 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1464 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1465 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1466 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1467 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1468 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1469 Irssi::settings_add_bool( "twirssi", "twirssi_always_shorten", 0 );
1470 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1472 $last_poll = time - &get_poll_time;
1473 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1476 ->print( "Couldn't find a window named '"
1477 . Irssi::settings_get_str('twitter_window')
1478 . "', trying to create it." );
1480 Irssi::Windowitem::window_create(
1481 Irssi::settings_get_str('twitter_window'), 1 );
1482 $window->set_name( Irssi::settings_get_str('twitter_window') );
1486 Irssi::command_bind( "dm", "cmd_direct" );
1487 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1488 Irssi::command_bind( "tweet", "cmd_tweet" );
1489 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1490 Irssi::command_bind( "retweet", "cmd_retweet" );
1491 Irssi::command_bind( "retweet_as", "cmd_retweet_as" );
1492 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1493 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1494 Irssi::command_bind( "twitter_login", "cmd_login" );
1495 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1496 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1497 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1498 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1499 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1500 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1501 Irssi::command_bind( "twitter_updates", "get_updates" );
1502 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1503 Irssi::command_bind( "reply", "cmd_reply" );
1504 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1506 Irssi::command_bind(
1509 print "twits: ", join ", ",
1510 map { "u: $_->{username}\@" . ref($_) } values %twits;
1511 print "selected: $user\@$defservice";
1512 print "friends: ", join ", ", sort keys %friends;
1513 print "nicks: ", join ", ", sort keys %nicks;
1514 print "searches: ", Dumper \%{ $id_map{__searches} };
1515 print "last poll: $last_poll";
1516 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1517 print DUMP Dumper \%tweet_cache;
1519 print "cache written out to /tmp/twirssi.cache.txt";
1523 Irssi::command_bind(
1526 ¬ice("Twirssi v$VERSION (r$REV); "
1527 . "Net::Twitter v$Net::Twitter::VERSION. "
1529 . JSON::Any::handler()
1530 . ". See details at http://twirssi.com/" );
1533 Irssi::command_bind(
1536 "/twitter_friend <username>",
1538 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1541 Irssi::command_bind(
1544 "/twitter_unfriend <username>",
1546 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1549 Irssi::command_bind(
1550 "twitter_device_updates",
1552 "/twitter_device_updates none|im|sms",
1553 "update_delivery_device",
1554 sub { ¬ice("Device updated to $_[0]"); }
1557 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1559 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1560 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1562 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1564 my $file = Irssi::settings_get_str("twirssi_replies_store");
1565 if ( $file and -r $file ) {
1566 if ( open( JSON, $file ) ) {
1571 my $ref = JSON::Any->jsonToObj($json);
1573 my $num = keys %{ $id_map{__indexes} };
1574 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1575 $num, ( $num == 1 ? "" : "s" ) );
1579 ¬ice("Failed to load old replies from $file: $!");
1583 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1584 ¬ice("Loading WWW::Shorten::$provider...");
1585 eval "use WWW::Shorten::$provider;";
1589 "Failed to load WWW::Shorten::$provider - either clear",
1590 "short_url_provider or install the CPAN module"
1595 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1596 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1604 ->print( "Create a window named "
1605 . Irssi::settings_get_str('twitter_window')
1606 . " or change the value of twitter_window. Then, reload twirssi." );
1609 # vim: set sts=4 expandtab: