9 $Data::Dumper::Indent = 1;
11 use vars qw($VERSION %IRSSI);
13 $VERSION = "2.1.3beta";
14 my ($REV) = '$Rev: 550 $' =~ /(\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-03-14 14:10:35 -0700 (Sat, 14 Mar 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: /tweet <update>");
118 &cmd_tweet_as( "$user\@$defservice $data", $server, $win );
122 my ( $data, $server, $win ) = @_;
124 return unless &logged_in($twit);
126 $data =~ s/^\s+|\s+$//;
127 $data =~ s/\s\s+/ /g;
128 my ( $username, $data ) = split ' ', $data, 2;
130 unless ( $username and $data ) {
131 ¬ice("Usage: /tweet_as <username> <update>");
135 return unless $username = &valid_username($username);
137 $data = &shorten($data);
139 return if &too_long($data);
143 unless ( $twits{$username}->update($data) )
145 ¬ice("Update failed");
149 return unless $success;
152 ¬ice("Update caused an error: $@. Aborted.");
156 foreach ( $data =~ /@([-\w]+)/ ) {
160 my $away = &update_away($data);
162 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
166 my ( $data, $server, $win ) = @_;
168 return unless &logged_in($twit);
170 $data =~ s/^\s+|\s+$//;
172 ¬ice("Usage: /reply <nick[:num]> <update>");
176 $data =~ s/^\s+|\s+$//;
177 my ( $id, $data ) = split ' ', $data, 2;
178 unless ( $id and $data ) {
179 ¬ice("Usage: /reply_as <nick[:num]> <update>");
183 &cmd_reply_as( "$user $id $data", $server, $win );
187 my ( $data, $server, $win ) = @_;
189 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
190 ¬ice("twirssi_track_replies is required in order to reply to "
191 . "specific tweets. Either enable it, or just use /tweet "
192 . "\@username <text>." );
196 return unless &logged_in($twit);
198 $data =~ s/^\s+|\s+$//;
199 my ( $username, $id, $data ) = split ' ', $data, 3;
201 unless ( $username and $data ) {
202 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
206 return unless $username = &valid_username($username);
209 $id =~ s/[^\w\d\-:]+//g;
210 ( $nick, $id ) = split /:/, $id;
211 unless ( exists $id_map{ lc $nick } ) {
212 ¬ice("Can't find a tweet from $nick to reply to!");
216 $id = $id_map{__indexes}{$nick} unless $id;
217 unless ( $id_map{ lc $nick }[$id] ) {
218 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
222 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
224 # remove any @nick at the beginning of the reply, as we'll add it anyway
225 $data =~ s/^\s*\@?$nick\s*//;
226 $data = "\@$nick " . $data;
229 $data = &shorten($data);
231 return if &too_long($data);
236 $twits{$username}->update(
239 in_reply_to_status_id => $id_map{ lc $nick }[$id]
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 ( $usage_str, $api_name, $post_ref ) = @_;
268 my ( $data, $server, $win ) = @_;
270 return unless &logged_in($twit);
272 $data =~ s/^\s+|\s+$//;
274 ¬ice("Usage: $usage_str");
280 unless ( $twit->$api_name($data) )
282 ¬ice("$api_name failed");
286 return unless $success;
289 ¬ice("$api_name caused an error. Aborted.");
293 &$post_ref($data) if $post_ref;
298 my ( $data, $server, $win ) = @_;
300 $data =~ s/^\s+|\s+$//g;
301 $data = &normalize_username($data);
302 if ( exists $twits{$data} ) {
303 ¬ice("Switching to $data");
304 $twit = $twits{$data};
305 if ( $data =~ /(.*)\@(.*)/ ) {
309 ¬ice("Couldn't figure out what service '$data' is on");
312 ¬ice("Unknown user $data");
317 my ( $data, $server, $win ) = @_;
319 $data =~ s/^\s+|\s+$//g;
320 $data = $user unless $data;
321 return unless $data = &valid_username($data);
323 ¬ice("Logging out $data...");
324 $twits{$data}->end_session();
325 delete $twits{$data};
328 &cmd_switch( ( keys %twits )[0], $server, $win );
330 Irssi::timeout_remove($poll) if $poll;
336 my ( $data, $server, $win ) = @_;
339 ( $user, $pass ) = split ' ', $data, 2;
340 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
341 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
343 my @user = split /\s*,\s*/, $autouser;
344 my @pass = split /\s*,\s*/, $autopass;
346 # if a password ends with a '\', it was meant to escape the comma, and
347 # it should be concatinated with the next one
351 while ( $p =~ /\\$/ and @pass ) {
352 $p .= "," . shift @pass;
357 if ( @user != @unescaped ) {
358 ¬ice("Number of usernames doesn't match "
359 . "the number of passwords - auto-login failed" );
362 while ( @user and @unescaped ) {
364 $p = shift @unescaped;
370 ¬ice("/twitter_login requires either a username and password "
371 . "or twitter_usernames and twitter_passwords to be set." );
375 %friends = %nicks = ();
378 if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
379 ( $user, $service ) = ( $1, $2 );
381 $service = Irssi::settings_get_str("twirssi_default_service");
383 $defservice = $service = ucfirst lc $service;
385 eval "use Net::$service";
388 "Failed to load Net::$service when trying to log in as $user: $@");
392 $twit = "Net::$service"->new(
398 unless ( $twit->verify_credentials() ) {
399 ¬ice("Login as $user\@$service failed");
402 &cmd_switch( ( keys %twits )[0], $server, $win );
408 my $rate_limit = $twit->rate_limit_status();
409 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
411 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
417 $twits{"$user\@$service"} = $twit;
418 Irssi::timeout_remove($poll) if $poll;
419 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
420 ¬ice("Logged in as $user\@$service, loading friends list...");
422 ¬ice( "loaded friends: ", scalar keys %friends );
423 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
424 Irssi::settings_set_bool( "twirssi_first_run", 0 );
425 unless ( exists $friends{twirssi} ) {
426 ¬ice("Welcome to twirssi!"
427 . " Perhaps you should add \@twirssi to your friends list,"
428 . " so you can be notified when a new version is release?"
429 . " Just type /twitter_friend twirssi." );
436 ¬ice("Login failed");
441 my ( $data, $server, $win ) = @_;
443 unless ( $twit and $twit->can('search') ) {
444 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
445 . "doesn't support searches." );
449 $data =~ s/^\s+|\s+$//;
453 ¬ice("Usage: /twitter_subscribe <topic>");
457 if ( exists $id_map{__searches}{$user}{$data} ) {
458 ¬ice("Already had a subscription for '$data'");
462 $id_map{__searches}{$user}{$data} = 1;
463 ¬ice("Added subscription for '$data'");
467 my ( $data, $server, $win ) = @_;
469 unless ( $twit and $twit->can('search') ) {
470 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
471 . "doesn't support searches." );
474 $data =~ s/^\s+|\s+$//;
478 ¬ice("Usage: /twitter_unsubscribe <topic>");
482 unless ( exists $id_map{__searches}{$user}{$data} ) {
483 ¬ice("No subscription found for '$data'");
487 delete $id_map{__searches}{$user}{$data};
488 ¬ice("Removed subscription for '$data'");
491 sub cmd_list_search {
492 my ( $data, $server, $win ) = @_;
495 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
497 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
498 $topics = $topics ? "$topics, $topic" : $topic;
502 ¬ice("Search subscriptions for \@$suser: $topics");
507 ¬ice("No search subscriptions set up");
512 my ( $data, $server, $win ) = @_;
514 my $loc = Irssi::settings_get_str("twirssi_location");
517 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
523 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
524 eval { use Digest::MD5; };
528 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
533 $md5 = get("http://twirssi.com/md5sum");
537 ¬ice("Failed to download md5sum from peeron! Aborting.");
541 unless ( open( CUR, $loc ) ) {
543 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
548 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
551 if ( $cur_md5 eq $md5 ) {
552 ¬ice("Current twirssi seems to be up to date.");
558 Irssi::settings_get_bool("twirssi_upgrade_beta")
559 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
560 : "http://twirssi.com/twirssi.pl";
561 ¬ice("Downloading twirssi from $URL");
562 LWP::Simple::getstore( $URL, "$loc.upgrade" );
564 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
565 unless ( open( NEW, "$loc.upgrade" ) ) {
567 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
572 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
575 if ( $new_md5 ne $md5 ) {
576 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
581 rename $loc, "$loc.backup"
582 or ¬ice("Failed to back up $loc: $!. Aborting")
584 rename "$loc.upgrade", $loc
585 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
588 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
589 if ( -e "$dir/autorun/$file" ) {
590 ¬ice("Updating $dir/autorun/$file");
591 unlink "$dir/autorun/$file"
592 or ¬ice("Failed to remove old $file from autorun: $!");
593 symlink "../$file", "$dir/autorun/$file"
594 or ¬ice("Failed to create symlink in autorun directory: $!");
597 ¬ice("Download complete. Reload twirssi with /script load $file");
607 print $fh "type:debug Loading friends page $page...\n"
608 if ( $fh and &debug );
609 my $friends = $twit->friends( { page => $page } );
610 last unless $friends;
611 $new_friends{ $_->{screen_name} } = time foreach @$friends;
613 last if @$friends == 0 or $page == 10;
618 print $fh "type:debug Error during friends list update. Aborted.\n";
622 my ( $added, $removed ) = ( 0, 0 );
623 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
624 foreach ( keys %new_friends ) {
625 next if exists $friends{$_};
630 print $fh "type:debug Scanning for removed friends...\n"
631 if ( $fh and &debug );
632 foreach ( keys %friends ) {
633 next if exists $new_friends{$_};
638 return ( $added, $removed );
642 print scalar localtime, " - get_updates starting" if &debug;
645 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
648 ->print( "Can't find a window named '"
649 . Irssi::settings_get_str('twitter_window')
650 . "'. Create it or change the value of twitter_window" );
653 return unless &logged_in($twit);
655 my ( $fh, $filename ) = File::Temp::tempfile();
656 binmode( $fh, ":utf8" );
660 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
661 Irssi::pidwait_add($pid);
662 } elsif ( defined $pid ) { # child
670 foreach ( keys %twits ) {
671 $error += &do_updates( $fh, $_, $twits{$_} );
674 my ( $added, $removed ) = &load_friends($fh);
675 if ( $added + $removed ) {
676 print $fh "type:debug %R***%n Friends list updated: ",
678 sprintf( "%d added", $added ),
679 sprintf( "%d removed", $removed ) ),
682 print $fh "__friends__\n";
683 foreach ( sort keys %friends ) {
684 print $fh "$_ $friends{$_}\n";
688 print $fh "type:debug Update encountered errors. Aborted\n";
689 print $fh $last_poll;
696 print scalar localtime, " - get_updates ends" if &debug;
700 my ( $fh, $username, $obj ) = @_;
702 my $rate_limit = $obj->rate_limit_status();
703 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
704 ¬ice("Rate limit exceeded for $username");
708 print scalar localtime, " - Polling for updates for $username" if &debug;
710 eval { $tweets = $obj->friends_timeline(); };
714 "type:debug Error during friends_timeline call: $@. Aborted.\n";
718 unless ( ref $tweets ) {
719 if ( $obj->can("get_error") ) {
721 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
722 if ($@) { $error = $obj->get_error() }
723 print $fh "type:debug API Error during friends_timeline call: ",
727 "type:debug API Error during friends_timeline call. Aborted.\n";
732 foreach my $t ( reverse @$tweets ) {
733 my $text = decode_entities( $t->{text} );
734 $text = &hilight($text);
736 if ( Irssi::settings_get_bool("show_reply_context")
737 and $t->{in_reply_to_screen_name} ne $username
738 and $t->{in_reply_to_screen_name}
739 and not exists $friends{ $t->{in_reply_to_screen_name} } )
741 $nicks{ $t->{in_reply_to_screen_name} } = time;
744 $context = $obj->show_status( $t->{in_reply_to_status_id} );
748 my $ctext = decode_entities( $context->{text} );
749 $ctext = &hilight($ctext);
750 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
751 $context->{id}, $username,
752 $context->{user}{screen_name}, $ctext;
753 if ( $context->{truncated} and ref($obj) ne 'Net::Identica' ) {
754 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
755 $context->{id} . "-url", $username,
756 $context->{user}{screen_name},
757 "http://twitter.com/$context->{user}{screen_name}/status/$context->{id}";
761 print $fh "type:debug request to get context failed: $@";
764 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
769 if $t->{user}{screen_name} eq $username
770 and not Irssi::settings_get_bool("show_own_tweets");
771 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
772 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
773 if ( $t->{truncated} and ref($obj) ne 'Net::Identica' ) {
774 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
775 $t->{id} . "-url", $username,
776 $t->{user}{screen_name},
777 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
781 print scalar localtime, " - Polling for replies" if &debug;
783 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
788 print $fh "type:debug Error during replies call. Aborted.\n";
792 foreach my $t ( reverse @$tweets ) {
794 if exists $friends{ $t->{user}{screen_name} };
796 my $text = decode_entities( $t->{text} );
797 $text = &hilight($text);
798 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
799 $t->{id}, $username, $t->{user}{screen_name}, $text;
800 if ( $t->{truncated} ) {
801 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
802 $t->{id} . "-url", $username,
803 $t->{user}{screen_name},
804 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
808 print scalar localtime, " - Polling for DMs" if &debug;
811 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
816 print $fh "type:debug Error during direct_messages call. Aborted.\n";
820 foreach my $t ( reverse @$tweets ) {
821 my $text = decode_entities( $t->{text} );
822 $text = &hilight($text);
823 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
824 $t->{id}, $username, $t->{sender_screen_name}, $text;
827 print scalar localtime, " - Polling for subscriptions" if &debug;
828 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
830 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
831 print $fh "type:debug searching for $topic since ",
832 "$id_map{__searches}{$username}{$topic}\n";
834 $search = $obj->search(
837 since_id => $id_map{__searches}{$username}{$topic}
844 "type:debug Error during search($topic) call. Aborted.\n";
848 unless ( $search->{max_id} ) {
850 "type:debug Invalid search results when searching for $topic.",
855 $id_map{__searches}{$username}{$topic} = $search->{max_id};
856 printf $fh "id:%d account:%s type:searchid topic:%s\n",
857 $search->{max_id}, $username, $topic;
859 foreach my $t ( reverse @{ $search->{results} } ) {
860 my $text = decode_entities( $t->{text} );
861 $text = &hilight($text);
862 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
863 $t->{id}, $username, $t->{from_user}, $topic, $text;
868 print scalar localtime, " - Done" if &debug;
875 my $filename = $data->[0];
876 my $attempt = $data->[1];
878 print scalar localtime, " - checking child log at $filename ($attempt)"
882 # first time we run we don't want to print out *everything*, so we just
885 $suppress = 1 unless keys %tweet_cache;
887 if ( open FILE, $filename ) {
892 last if /^__friends__/;
895 foreach my $key (qw/id account nick type topic/) {
896 if (s/^$key:(\S+)\s*//) {
901 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
902 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
906 $new_cache{ $meta{id} } = time;
908 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
914 if ( lc $meta{account} ne lc "$user\@$defservice" ) {
915 $meta{account} =~ s/\@(\w+)$//;
919 lc Irssi::settings_get_str("twirssi_default_service") )
921 $account = "$meta{account}: ";
923 $account = "$meta{account}\@$service: ";
928 if ( $meta{type} ne 'dm'
929 and Irssi::settings_get_bool("twirssi_track_replies")
933 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
934 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
935 $id_map{__indexes}{ $meta{nick} } = $marker;
936 $marker = ":$marker";
940 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
941 if ( ( $_ =~ /\@$meta{account}\W/i )
942 && Irssi::settings_get_bool("twirssi_hilights") )
944 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
945 $hilight = MSGLEVEL_HILIGHT;
948 if ( $meta{type} =~ /tweet|reply/ ) {
951 ( MSGLEVEL_PUBLIC | $hilight ),
952 $meta{type}, $account, $meta{nick}, $marker, $_
954 } elsif ( $meta{type} eq 'ellispis' ) {
956 [ MSGLEVEL_PUBLIC, "tweet", $account, $meta{nick}, "", $_ ];
957 } elsif ( $meta{type} eq 'search' ) {
960 ( MSGLEVEL_PUBLIC | $hilight ),
961 $meta{type}, $account, $meta{topic},
962 $meta{nick}, $marker, $_
965 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
967 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
969 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
972 } elsif ( $meta{type} eq 'dm' ) {
975 ( MSGLEVEL_MSGS | $hilight ),
976 $meta{type}, $account, $meta{nick}, $_
978 } elsif ( $meta{type} eq 'searchid' ) {
979 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
981 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
983 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
985 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
988 print "Search '$meta{topic}' returned invalid id $meta{id}";
990 } elsif ( $meta{type} eq 'error' ) {
991 push @lines, [ MSGLEVEL_MSGS, $_ ];
992 } elsif ( $meta{type} eq 'debug' ) {
993 print "$_" if &debug,;
995 print "Unknown line type $meta{type}: $_" if &debug,;
1002 $new_last_poll = $_;
1005 my ( $f, $t ) = split ' ', $_;
1006 $nicks{$f} = $friends{$f} = $t;
1009 if ($new_last_poll) {
1010 print "new last_poll = $new_last_poll" if &debug;
1012 print "First call, not printing updates" if &debug;
1014 foreach my $line (@lines) {
1015 $window->printformat(
1017 "twirssi_" . $line->[1],
1018 @$line[ 2 .. $#$line ]
1025 or warn "Failed to remove $filename: $!"
1028 # commit the pending cache lines to the actual cache, now that
1029 # we've printed our output
1030 %tweet_cache = ( %tweet_cache, %new_cache );
1032 # keep enough cached tweets, to make sure we don't show duplicates.
1033 foreach ( keys %tweet_cache ) {
1034 next if $tweet_cache{$_} >= $last_poll - 3600;
1035 delete $tweet_cache{$_};
1037 $last_poll = $new_last_poll;
1042 Irssi::settings_get_str("twirssi_replies_store") )
1044 if ( open JSON, ">$file" ) {
1045 print JSON JSON::Any->objToJson( \%id_map );
1048 &ccrap("Failed to write replies to $file: $!");
1058 if ( $attempt < 24 ) {
1059 Irssi::timeout_add_once( 5000, 'monitor_child',
1060 [ $filename, $attempt + 1 ] );
1062 print "Giving up on polling $filename" if &debug;
1063 unlink $filename unless &debug;
1065 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1068 my @time = localtime($last_poll);
1069 if ( time - $last_poll < 24 * 60 * 60 ) {
1070 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1072 $since = scalar localtime($last_poll);
1075 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1080 q{ .--./ / | _.---.| },
1091 &ccrap("Haven't been able to get updated tweets since $since");
1096 return Irssi::settings_get_bool("twirssi_debug");
1100 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1104 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1110 if ( Irssi::settings_get_bool("tweet_to_away")
1112 and $data !~ /^[dD] / )
1115 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1117 $server->send_raw("away :$data");
1120 &ccrap( "Can't find bitlbee server.",
1121 "Update bitlbee_server or disable tweet_to_away" );
1131 my $noalert = shift;
1133 if ( length $data > 140 ) {
1134 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1142 sub valid_username {
1143 my $username = shift;
1145 $username = &normalize_username($username);
1147 unless ( exists $twits{$username} ) {
1148 ¬ice("Unknown username $username");
1158 ¬ice("Not logged in! Use /twitter_login username pass!");
1166 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1169 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1170 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1171 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1173 { # /twitter_reply gets a nick:num
1175 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1176 sort { $nicks{$b} <=> $nicks{$a} }
1178 keys %{ $id_map{__indexes} };
1181 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1183 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1184 my $prefix = $word =~ s/^@//;
1185 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1186 push @$complist, grep /^\Q$word/i,
1187 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1188 @$complist = map { "\@$_" } @$complist if $prefix;
1192 sub event_send_text {
1193 my ( $line, $server, $win ) = @_;
1194 my $awin = Irssi::active_win();
1196 # if the window where we got our text was the twitter window, and the user
1197 # wants to be lazy, tweet away!
1198 if ( ( $awin->get_active_name() eq $window->{name} )
1199 and Irssi::settings_get_bool("tweet_window_input") )
1201 &cmd_tweet( $line, $server, $win );
1206 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1207 return $poll if $poll >= 60;
1214 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1215 my $c = Irssi::settings_get_str("twirssi_nick_color");
1216 $c = $irssi_to_mirc_colors{$c};
1217 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1219 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1220 my $c = Irssi::settings_get_str("twirssi_topic_color");
1221 $c = $irssi_to_mirc_colors{$c};
1222 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1224 $text =~ s/[\n\r]/ /g;
1232 my $provider = Irssi::settings_get_str("short_url_provider");
1235 Irssi::settings_get_bool("twirssi_always_shorten")
1236 or &too_long( $data, 1 )
1242 if ( $provider eq 'Bitly' ) {
1243 @args[ 1, 2 ] = split ',',
1244 Irssi::settings_get_str("short_url_args"), 2;
1245 unless ( @args == 3 ) {
1247 "WWW::Shorten::Bitly requires a username and API key.",
1248 "Set short_url_args to username,API_key or change your",
1249 "short_url_provider."
1255 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1258 my $short = makeashorterlink(@args);
1260 $data =~ s/\Q$url/$short/g;
1262 ¬ice("Failed to shorten $url!");
1271 sub normalize_username {
1274 my ( $username, $service ) = split /\@/, $user, 2;
1276 $service = ucfirst lc $service;
1279 ucfirst lc Irssi::settings_get_str("twirssi_default_service");
1280 unless ( exists $twits{"$username\@$service"} ) {
1282 foreach my $t ( sort keys %twits ) {
1283 next unless $t =~ /^\Q$username\E\@(Twitter|Identica)/;
1289 ¬ice("Can't find a logged in user '$user'");
1294 return "$username\@$service";
1297 Irssi::signal_add( "send text", "event_send_text" );
1299 Irssi::theme_register(
1301 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1302 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1303 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1304 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1305 'twirssi_error', 'ERROR: $0',
1309 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1310 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1311 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1312 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1313 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1314 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1315 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1316 Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
1317 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1318 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1319 Irssi::settings_add_str( "twirssi", "twirssi_location",
1320 ".irssi/scripts/twirssi.pl" );
1321 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1322 ".irssi/scripts/twirssi.json" );
1323 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1324 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1325 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1326 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1327 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1328 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1329 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1330 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1331 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1332 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1333 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1334 Irssi::settings_add_bool( "twirssi", "twirssi_always_shorten", 0 );
1335 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1337 $last_poll = time - &get_poll_time;
1338 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1341 ->print( "Couldn't find a window named '"
1342 . Irssi::settings_get_str('twitter_window')
1343 . "', trying to create it." );
1345 Irssi::Windowitem::window_create(
1346 Irssi::settings_get_str('twitter_window'), 1 );
1347 $window->set_name( Irssi::settings_get_str('twitter_window') );
1351 Irssi::command_bind( "dm", "cmd_direct" );
1352 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1353 Irssi::command_bind( "tweet", "cmd_tweet" );
1354 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1355 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1356 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1357 Irssi::command_bind( "twitter_login", "cmd_login" );
1358 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1359 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1360 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1361 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1362 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1363 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1364 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1365 Irssi::command_bind( "reply", "cmd_reply" );
1366 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1368 Irssi::command_bind(
1371 print "twits: ", join ", ",
1372 map { "u: $_->{username}\@" . ref($_) } values %twits;
1373 print "selected: $user\@$defservice";
1374 print "friends: ", join ", ", sort keys %friends;
1375 print "nicks: ", join ", ", sort keys %nicks;
1376 print "searches: ", Dumper \%{ $id_map{__searches} };
1377 print "last poll: $last_poll";
1378 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1379 print DUMP Dumper \%tweet_cache;
1381 print "cache written out to /tmp/twirssi.cache.txt";
1385 Irssi::command_bind(
1388 ¬ice("Twirssi v$VERSION (r$REV); "
1389 . "Net::Twitter v$Net::Twitter::VERSION. "
1391 . JSON::Any::handler()
1392 . ". See details at http://twirssi.com/" );
1395 Irssi::command_bind(
1398 "/twitter_friend <username>",
1400 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1403 Irssi::command_bind(
1406 "/twitter_unfriend <username>",
1408 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1411 Irssi::command_bind( "twitter_updates", "get_updates" );
1412 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1414 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1415 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1417 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1419 my $file = Irssi::settings_get_str("twirssi_replies_store");
1420 if ( $file and -r $file ) {
1421 if ( open( JSON, $file ) ) {
1426 my $ref = JSON::Any->jsonToObj($json);
1428 my $num = keys %{ $id_map{__indexes} };
1429 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1430 $num, ( $num == 1 ? "" : "s" ) );
1434 ¬ice("Failed to load old replies from $file: $!");
1438 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1439 ¬ice("Loading WWW::Shorten::$provider...");
1440 eval "use WWW::Shorten::$provider;";
1444 "Failed to load WWW::Shorten::$provider - either clear",
1445 "short_url_provider or install the CPAN module"
1450 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1451 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1459 ->print( "Create a window named "
1460 . Irssi::settings_get_str('twitter_window')
1461 . " or change the value of twitter_window. Then, reload twirssi." );
1464 # vim: set sts=4 expandtab: