10 use POSIX qw/:sys_wait_h/;
11 use Net::Twitter qw/3.05/;
12 $Data::Dumper::Indent = 1;
14 use vars qw($VERSION %IRSSI);
16 $VERSION = "2.4.2beta";
18 authors => 'Dan Boger',
19 contact => 'zigdon@gmail.com',
21 description => 'Send twitter updates using /tweet. '
22 . 'Can optionally set your bitlbee /away message to same',
23 license => 'GNU GPL v2',
24 url => 'http://twirssi.com',
25 changed => 'Fri Jan 22 14:40:48 PST 2010',
36 my $last_friends_poll = 0;
44 my %fix_replies_index;
46 my %irssi_to_mirc_colors = (
66 my ( $data, $server, $win ) = @_;
68 return unless &logged_in($twit);
70 my ( $target, $text ) = split ' ', $data, 2;
71 unless ( $target and $text ) {
72 ¬ice("Usage: /dm <nick> <message>");
76 &cmd_direct_as( "$user $data", $server, $win );
80 my ( $data, $server, $win ) = @_;
82 return unless &logged_in($twit);
84 my ( $username, $target, $text ) = split ' ', $data, 3;
85 unless ( $username and $target and $text ) {
86 ¬ice("Usage: /dm_as <username> <nick> <message>");
90 return unless $username = &valid_username($username);
93 if ( $twits{$username}
94 ->new_direct_message( { user => $target, text => $text } ) )
96 ¬ice("DM sent to $target: $text");
97 $nicks{$target} = time;
101 $error = JSON::Any->jsonToObj( $twits{$username}->get_error() );
102 $error = $error->{error};
104 die $error if $error;
105 ¬ice("DM to $target failed");
110 ¬ice("DM caused an error: $@");
116 my ( $data, $server, $win ) = @_;
118 return unless &logged_in($twit);
120 $data =~ s/^\s+|\s+$//;
122 ¬ice("Usage: /retweet <nick[:num]> [comment]");
126 my ( $id, $data ) = split ' ', $data, 2;
128 &cmd_retweet_as( "$user $id $data", $server, $win );
132 my ( $data, $server, $win ) = @_;
134 return unless &logged_in($twit);
136 $data =~ s/^\s+|\s+$//;
137 my ( $username, $id, $data ) = split ' ', $data, 3;
140 ¬ice("Usage: /retweet_as <username> <nick[:num]> [comment]");
144 return unless $username = &valid_username($username);
147 $id =~ s/[^\w\d\-:]+//g;
148 ( $nick, $id ) = split /:/, $id;
149 unless ( exists $id_map{ lc $nick } ) {
150 ¬ice("Can't find a tweet from $nick to retweet!");
154 $id = $id_map{__indexes}{$nick} unless $id;
155 unless ( $id_map{ lc $nick }[$id] ) {
156 ¬ice("Can't find a tweet numbered $id from $nick to retweet!");
160 unless ( $id_map{__tweets}{ lc $nick }[$id] ) {
161 ¬ice("The text of this tweet isn't saved, sorry!");
165 # Irssi::settings_add_str( "twirssi", "twirssi_retweet_format", 'RT $n: $t ${-- $c$}' );
166 my $text = Irssi::settings_get_str("twirssi_retweet_format");
167 $text =~ s/\$n/\@$nick/g;
169 $text =~ s/\${|\$}//g;
170 $text =~ s/\$c/$data/;
172 $text =~ s/\${.*?\$}//;
174 $text =~ s/\$t/$id_map{__tweets}{ lc $nick }[$id]/;
176 my $modified = $data;
177 $data = &shorten($text);
179 return if $modified and &too_long($data);
185 $success = $twits{$username}->update(
189 # in_reply_to_status_id => $id_map{ lc $nick }[$id]
194 $twits{$username}->retweet( { id => $id_map{ lc $nick }[$id] } );
195 $success = $success->{id} if ref $success;
197 ¬ice("Update failed") unless $success;
199 return unless $success;
202 ¬ice("Update caused an error: $@. Aborted");
206 foreach ( $data =~ /@([-\w]+)/ ) {
210 ¬ice("Retweet sent");
214 my ( $data, $server, $win ) = @_;
216 return unless &logged_in($twit);
218 $data =~ s/^\s+|\s+$//;
220 ¬ice("Usage: /tweet <update>");
224 &cmd_tweet_as( "$user\@$defservice $data", $server, $win );
228 my ( $data, $server, $win ) = @_;
230 return unless &logged_in($twit);
232 $data =~ s/^\s+|\s+$//;
233 $data =~ s/\s\s+/ /g;
234 my ( $username, $data ) = split ' ', $data, 2;
236 unless ( $username and $data ) {
237 ¬ice("Usage: /tweet_as <username> <update>");
241 return unless $username = &valid_username($username);
243 $data = &shorten($data);
245 return if &too_long($data);
249 unless ( $twits{$username}->update($data) )
251 ¬ice("Update failed");
255 return unless $success;
258 ¬ice("Update caused an error: $@. Aborted.");
262 foreach ( $data =~ /@([-\w]+)/ ) {
266 my $away = &update_away($data);
268 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
272 my ( $data, $server, $win ) = @_;
274 return unless &logged_in($twit);
276 $data =~ s/^\s+|\s+$//;
278 ¬ice("Usage: /reply <nick[:num]> <update>");
282 my ( $id, $data ) = split ' ', $data, 2;
283 unless ( $id and $data ) {
284 ¬ice("Usage: /reply <nick[:num]> <update>");
288 &cmd_reply_as( "$user $id $data", $server, $win );
292 my ( $data, $server, $win ) = @_;
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 eval { $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 ) = @_;
436 print "logging in: $data" if &debug;
438 print "manual data login" if &debug;
439 ( $user, $pass ) = split ' ', $data, 2;
440 unless ( Irssi::settings_get_bool("twirssi_use_oauth") or $pass ) {
441 ¬ice("usage: /twitter_login <username>[\@<service>] <password>");
444 } elsif ( Irssi::settings_get_bool("twirssi_use_oauth")
445 and my $autouser = Irssi::settings_get_str("twitter_usernames") )
447 print "oauth autouser login" if &debug;
448 foreach my $user ( split /,/, $autouser ) {
452 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
453 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
455 print "autouser login" if &debug;
456 my @user = split /\s*,\s*/, $autouser;
457 my @pass = split /\s*,\s*/, $autopass;
459 # if a password ends with a '\', it was meant to escape the comma, and
460 # it should be concatinated with the next one
464 while ( $p =~ /\\$/ and @pass ) {
465 $p .= "," . shift @pass;
470 if ( @user != @unescaped ) {
471 ¬ice("Number of usernames doesn't match "
472 . "the number of passwords - auto-login failed" );
475 while ( @user and @unescaped ) {
477 $p = shift @unescaped;
483 ¬ice("/twitter_login requires either a username/password "
484 . "or twitter_usernames and twitter_passwords to be set. "
485 . "Note that if twirssi_use_oauth is true, passwords are "
490 %friends = %nicks = ();
493 if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
494 ( $user, $service ) = ( $1, $2 );
496 $service = Irssi::settings_get_str("twirssi_default_service");
498 $defservice = $service = ucfirst lc $service;
500 if ( $service eq 'Twitter'
501 and Irssi::settings_get_bool("twirssi_use_oauth") )
503 print "Attempting OAuth for $user\@$service" if &debug;
505 if ( $service eq 'Identica' )
507 $twit = Net::Twitter->new(
509 traits => [ 'API::REST', 'API::Search' ],
511 ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
514 $twit = Net::Twitter->new(
515 traits => [ 'API::REST', 'OAuth', 'API::Search' ],
516 consumer_key => 'BZVAvBma4GxdiRwXIvbnw',
518 '0T5kahwLyb34vciGZsgkA9lsjtGCQ05vxVE2APXM',
520 ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
526 if ( open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) )
530 next unless m/$user\@$service (\S+) (\S+)/i;
531 print "Trying cached oauth creds for $user\@$service"
533 $twit->access_token($1);
534 $twit->access_token_secret($2);
540 unless ( $twit->authorized ) {
542 eval { $url = $twit->get_authorization_url; };
545 ¬ice("ERROR: Failed to get OAuth authorization_url. "
546 . "Try again later." );
550 "Twirssi not autorized to access $service for $user.",
551 "Please authorize at the following url, then enter the pin",
552 "supplied with /twirssi_oauth $user\@$service <pin>",
556 $oauth{pending}{"$user\@$service"} = $twit;
561 $twit = Net::Twitter->new(
562 $service eq 'Identica' ? ( identica => 1 ) : (),
566 ssl => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1,
571 ¬ice("Failed to create object! Aborting.");
575 return &verify_twitter_object( $server, $win, $user, $service, $twit );
579 my ( $data, $server, $win ) = @_;
580 my ( $key, $pin ) = split ' ', $data;
581 my ( $user, $service );
582 $key = &normalize_username($key);
583 if ( $key =~ /^(.*)@(Twitter|Identica)$/ ) {
584 ( $user, $service ) = ( $1, $2 );
587 print "Applying pin to $key" if &debug;
589 unless ( exists $oauth{pending}{$key} ) {
590 ¬ice("There isn't a pending oauth request for $key. "
591 . "Try /twitter_login first" );
595 my $twit = $oauth{pending}{$key};
596 my ( $access_token, $access_token_secret );
598 ( $access_token, $access_token_secret ) =
599 $twit->request_access_token( verifier => $pin );
603 ¬ice("Invalid pin, try again.");
607 delete $oauth{pending}{$key};
609 my $store_file = Irssi::settings_get_str("twirssi_oauth_store");
612 if ( open( OAUTH, $store_file ) ) {
622 push @store, "$key $access_token $access_token_secret";
624 if ( open( OAUTH, ">$store_file.new" ) ) {
625 print OAUTH "$_\n" foreach @store;
627 rename "$store_file.new", $store_file
628 or ¬ice("Failed to rename $store_file.new: $!");
630 ¬ice("Failed to write $store_file.new: $!");
633 ¬ice("No persistant storage set for OAuth. "
634 . "Please /set twirssi_oauth_store to a writable filename." );
637 return &verify_twitter_object( $server, $win, $user, $service, $twit );
640 sub verify_twitter_object {
641 my ( $server, $win, $user, $service, $twit ) = @_;
643 if ( my $timeout = Irssi::settings_get_int("twitter_timeout")
644 and $twit->can('ua') )
646 $twit->ua->timeout($timeout);
647 ¬ice("Twitter timeout set to $timeout");
650 unless ( $twit->verify_credentials() ) {
651 ¬ice("Login as $user\@$service failed");
653 if ( not Irssi::settings_get_bool("twirssi_avoid_ssl") ) {
655 "It's possible you're missing one of the modules required for "
656 . "SSL logins. Try setting twirssi_avoid_ssl to on. See "
657 . "http://cpansearch.perl.org/src/GAAS/libwww-perl-5.831/README.SSL "
658 . "for the detailed requirements." );
663 &cmd_switch( ( keys %twits )[0], $server, $win );
668 my $rate_limit = $twit->rate_limit_status();
669 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
671 "Rate limit exceeded, try again after $rate_limit->{reset_time}");
676 print "saving object for $user\@$service" if &debug;
677 $twits{"$user\@$service"} = $twit;
678 Irssi::timeout_remove($poll) if $poll;
679 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
680 ¬ice("Logged in as $user\@$service, loading friends list...");
682 ¬ice( "loaded friends: " . scalar keys %friends );
683 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
684 Irssi::settings_set_bool( "twirssi_first_run", 0 );
692 my ( $data, $server, $win ) = @_;
695 ¬ice("Usage: /twitter_add_follow_extra <username>");
699 $data =~ s/^\s+|\s+$//;
703 if ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
704 ¬ice("Already following all replies by \@$data");
708 $id_map{__fixreplies}{"$user\@$defservice"}{$data} = 1;
709 ¬ice("Will now follow all replies by \@$data");
713 my ( $data, $server, $win ) = @_;
716 ¬ice("Usage: /twitter_del_follow_extra <username>");
720 $data =~ s/^\s+|\s+$//;
724 unless ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
725 ¬ice("Wasn't following all replies by \@$data");
729 delete $id_map{__fixreplies}{"$user\@$defservice"}{$data};
730 ¬ice("Will no longer follow all replies by \@$data");
733 sub cmd_list_follow {
734 my ( $data, $server, $win ) = @_;
737 foreach my $suser ( sort keys %{ $id_map{__fixreplies} } ) {
739 foreach my $fruser ( sort keys %{ $id_map{__fixreplies}{$suser} } ) {
740 $frusers = $frusers ? "$frusers, $fruser" : $fruser;
744 ¬ice("Following all replies as \@$suser: $frusers");
749 ¬ice("Not following all replies by anyone");
754 my ( $data, $server, $win ) = @_;
756 unless ( $twit and $twit->can('search') ) {
757 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
758 . "doesn't support searches." );
762 $data =~ s/^\s+|\s+$//;
766 ¬ice("Usage: /twitter_subscribe <topic>");
770 if ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
771 ¬ice("Already had a subscription for '$data'");
775 $id_map{__searches}{"$user\@$defservice"}{$data} = 1;
776 ¬ice("Added subscription for '$data'");
780 my ( $data, $server, $win ) = @_;
782 unless ( $twit and $twit->can('search') ) {
783 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
784 . "doesn't support searches." );
787 $data =~ s/^\s+|\s+$//;
791 ¬ice("Usage: /twitter_unsubscribe <topic>");
795 unless ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
796 ¬ice("No subscription found for '$data'");
800 delete $id_map{__searches}{"$user\@$defservice"}{$data};
801 ¬ice("Removed subscription for '$data'");
804 sub cmd_list_search {
805 my ( $data, $server, $win ) = @_;
808 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
810 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
811 $topics = $topics ? "$topics, $topic" : $topic;
815 ¬ice("Search subscriptions for \@$suser: $topics");
820 ¬ice("No search subscriptions set up");
825 my ( $data, $server, $win ) = @_;
827 my $loc = Irssi::settings_get_str("twirssi_location");
829 ¬ice("$loc isn't writable, can't upgrade."
830 . " Perhaps you need to /set twirssi_location?" );
835 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
836 eval { use Digest::MD5; };
839 ¬ice("Failed to load Digest::MD5."
840 . " Try '/twirssi_upgrade nomd5' to skip MD5 verification" );
844 $md5 = get("http://twirssi.com/md5sum");
848 ¬ice("Failed to download md5sum from peeron! Aborting.");
852 unless ( open( CUR, $loc ) ) {
853 ¬ice("Failed to read $loc."
854 . " Check that /set twirssi_location is set to the correct location."
859 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
862 if ( $cur_md5 eq $md5 ) {
863 ¬ice("Current twirssi seems to be up to date.");
869 Irssi::settings_get_bool("twirssi_upgrade_beta")
870 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
871 : "http://twirssi.com/twirssi.pl";
872 ¬ice("Downloading twirssi from $URL");
873 LWP::Simple::getstore( $URL, "$loc.upgrade" );
875 unless ( -s "$loc.upgrade" ) {
876 ¬ice("Failed to save $loc.upgrade."
877 . " Check that /set twirssi_location is set to the correct location."
882 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
883 unless ( open( NEW, "$loc.upgrade" ) ) {
884 ¬ice("Failed to read $loc.upgrade."
885 . " Check that /set twirssi_location is set to the correct location."
890 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
893 if ( $new_md5 ne $md5 ) {
894 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
899 rename $loc, "$loc.backup"
900 or ¬ice("Failed to back up $loc: $!. Aborting")
902 rename "$loc.upgrade", $loc
903 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
906 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
907 if ( -e "$dir/autorun/$file" ) {
908 ¬ice("Updating $dir/autorun/$file");
909 unlink "$dir/autorun/$file"
910 or ¬ice("Failed to remove old $file from autorun: $!");
911 symlink "../$file", "$dir/autorun/$file"
912 or ¬ice("Failed to create symlink in autorun directory: $!");
915 ¬ice("Download complete. Reload twirssi with /script load $file");
924 while ( $page < 11 and $cursor ne "0" )
926 print $fh "type:debug Loading friends page $page...\n"
927 if ( $fh and &debug );
929 if ( ref $twit =~ /^Net::Twitter/ ) {
930 $friends = $twit->friends( { cursor => $cursor } );
931 last unless $friends;
932 $cursor = $friends->{next_cursor};
933 $friends = $friends->{users};
935 $friends = $twit->friends( { page => $page } );
936 last unless $friends;
938 $new_friends{ $_->{screen_name} } = time foreach @$friends;
944 print $fh "type:debug Error during friends list update. Aborted.\n"
949 my ( $added, $removed ) = ( 0, 0 );
950 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
951 foreach ( keys %new_friends ) {
952 next if exists $friends{$_};
957 print $fh "type:debug Scanning for removed friends...\n"
958 if ( $fh and &debug );
959 foreach ( keys %friends ) {
960 next if exists $new_friends{$_};
965 return ( $added, $removed );
969 print scalar localtime, " - get_updates starting" if &debug;
972 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
975 ->print( "Can't find a window named '"
976 . Irssi::settings_get_str('twitter_window')
977 . "'. Create it or change the value of twitter_window" );
980 return unless &logged_in($twit);
982 my ( $fh, $filename ) = File::Temp::tempfile();
983 binmode( $fh, ":" . &get_charset );
986 if ($child_pid) { # parent
987 Irssi::timeout_add_once( 5000, 'monitor_child',
988 [ "$filename.done", 0 ] );
989 Irssi::pidwait_add($child_pid);
990 } elsif ( defined $child_pid ) { # child
999 foreach ( keys %twits ) {
1000 $error++ unless &do_updates( $fh, $_, $twits{$_}, \%context_cache );
1002 if ( $id_map{__fixreplies}{$_} ) {
1003 my @frusers = sort keys %{ $id_map{__fixreplies}{$_} };
1006 unless &get_timeline( $fh, $frusers[ $fix_replies_index{$_} ],
1007 $_, $twits{$_}, \%context_cache );
1009 $fix_replies_index{$_}++;
1010 $fix_replies_index{$_} = 0
1011 if $fix_replies_index{$_} >= @frusers;
1012 print $fh "id:$fix_replies_index{$_} ",
1013 "account:$_ type:fix_replies_index\n";
1017 print $fh "__friends__\n";
1019 time - $last_friends_poll >
1020 Irssi::settings_get_int('twitter_friends_poll') )
1022 print $fh "__updated ", time, "\n";
1023 my ( $added, $removed ) = &load_friends($fh);
1024 if ( $added + $removed ) {
1025 print $fh "type:debug %R***%n Friends list updated: ",
1027 sprintf( "%d added", $added ),
1028 sprintf( "%d removed", $removed ) ),
1033 foreach ( sort keys %friends ) {
1034 print $fh "$_ $friends{$_}\n";
1038 print $fh "type:debug Update encountered errors. Aborted\n";
1039 print $fh "-- $last_poll";
1041 print $fh "-- $new_poll";
1044 rename $filename, "$filename.done";
1047 &ccrap("Failed to fork for updating: $!");
1049 print scalar localtime, " - get_updates ends" if &debug;
1053 my ( $fh, $username, $obj, $cache ) = @_;
1056 my $rate_limit = $obj->rate_limit_status();
1057 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
1058 ¬ice("Rate limit exceeded for $username");
1063 print scalar localtime, " - Polling for updates for $username" if &debug;
1065 my $new_poll_id = 0;
1067 if ( $id_map{__last_id}{$username}{timeline} )
1069 $tweets = $obj->home_timeline( { count => 100 } );
1071 $tweets = $obj->home_timeline();
1076 print $fh "type:debug Error during home_timeline call: Aborted.\n";
1077 print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
1081 unless ( ref $tweets ) {
1082 if ( $obj->can("get_error") ) {
1083 my $error = "Unknown error";
1084 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
1085 unless ($@) { $error = $obj->get_error() }
1087 "type:debug API Error during home_timeline call: Aborted\n";
1088 print $fh "type:debug : $_\n" foreach split /\n/, Dumper($error);
1092 "type:debug API Error during home_timeline call. Aborted.\n";
1098 Irssi::settings_get_str("twirssi_ignored_tags")
1099 ? split ' ', Irssi::settings_get_str("twirssi_ignored_tags")
1102 Irssi::settings_get_str("twirssi_stripped_tags")
1103 ? split ' ', Irssi::settings_get_str("twirssi_stripped_tags")
1105 foreach my $t ( reverse @$tweets ) {
1106 my $text = &get_text( $t, $obj );
1107 my $reply = "tweet";
1110 foreach my $tag (@ignore_tags) {
1111 next unless $text =~ /\b\Q$tag\E\b/i;
1113 $text = "(ignored: $tag) $text" if &debug;
1116 next if not &debug and $match;
1118 foreach my $tag (@strip_tags) {
1119 $text =~ s/\b\Q$tag\E\b//gi;
1122 if ( Irssi::settings_get_bool("show_reply_context")
1123 and $t->{in_reply_to_screen_name} ne $username
1124 and $t->{in_reply_to_screen_name}
1125 and not exists $friends{ $t->{in_reply_to_screen_name} } )
1127 $nicks{ $t->{in_reply_to_screen_name} } = time;
1129 unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
1131 $cache->{ $t->{in_reply_to_status_id} } =
1132 $obj->show_status( $t->{in_reply_to_status_id} );
1136 $context = $cache->{ $t->{in_reply_to_status_id} };
1139 my $ctext = &get_text( $context, $obj );
1140 printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
1141 $context->{id}, $username,
1142 $context->{user}{screen_name}, $ctext;
1147 if $t->{user}{screen_name} eq $username
1148 and not Irssi::settings_get_bool("show_own_tweets");
1149 printf $fh "id:%s account:%s nick:%s type:%s %s\n",
1150 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
1151 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
1153 printf $fh "id:%s account:%s type:last_id timeline\n",
1154 $new_poll_id, $username;
1156 print scalar localtime, " - Polling for replies since ",
1157 $id_map{__last_id}{$username}{reply}
1161 if ( $id_map{__last_id}{$username}{reply} )
1163 $tweets = $obj->replies(
1164 { since_id => $id_map{__last_id}{$username}{reply} } )
1167 $tweets = $obj->replies() || [];
1172 print $fh "type:debug Error during replies call. Aborted.\n";
1176 foreach my $t ( reverse @$tweets ) {
1178 if exists $friends{ $t->{user}{screen_name} };
1180 my $text = &get_text( $t, $obj );
1181 printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
1182 $t->{id}, $username, $t->{user}{screen_name}, $text;
1183 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
1185 printf $fh "id:%s account:%s type:last_id reply\n", $new_poll_id, $username;
1187 print scalar localtime, " - Polling for DMs" if &debug;
1190 if ( $id_map{__last_id}{$username}{dm} )
1192 $tweets = $obj->direct_messages(
1193 { since_id => $id_map{__last_id}{$username}{dm} } )
1196 $tweets = $obj->direct_messages() || [];
1201 print $fh "type:debug Error during direct_messages call. Aborted.\n";
1205 foreach my $t ( reverse @$tweets ) {
1206 my $text = decode_entities( $t->{text} );
1207 $text =~ s/[\n\r]/ /g;
1208 printf $fh "id:%s account:%s nick:%s type:dm %s\n",
1209 $t->{id}, $username, $t->{sender_screen_name}, $text;
1210 $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
1212 printf $fh "id:%s account:%s type:last_id dm\n", $new_poll_id, $username;
1214 print scalar localtime, " - Polling for subscriptions" if &debug;
1215 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
1217 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
1218 print $fh "type:debug searching for $topic since ",
1219 "$id_map{__searches}{$username}{$topic}\n";
1221 $search = $obj->search(
1224 since_id => $id_map{__searches}{$username}{$topic}
1231 "type:debug Error during search($topic) call. Aborted.\n";
1235 unless ( $search->{max_id} ) {
1236 print $fh "type:debug Invalid search results when searching",
1237 " for $topic. Aborted.\n";
1241 $id_map{__searches}{$username}{$topic} = $search->{max_id};
1242 $topic =~ s/ /%20/g;
1243 printf $fh "id:%s account:%s type:searchid topic:%s\n",
1244 $search->{max_id}, $username, $topic;
1246 foreach my $t ( reverse @{ $search->{results} } ) {
1247 my $text = &get_text( $t, $obj );
1248 printf $fh "id:%s account:%s nick:%s type:search topic:%s %s\n",
1249 $t->{id}, $username, $t->{from_user}, $topic, $text;
1250 $new_poll_id = $t->{id}
1252 or $t->{id} < $new_poll_id;
1257 print scalar localtime, " - Done" if &debug;
1263 my ( $fh, $target, $username, $obj, $cache ) = @_;
1265 my $last_id = $id_map{__last_id}{$username}{$target};
1267 print $fh "type:debug get_timeline("
1268 . "$fix_replies_index{$username}=$target > $last_id) started."
1269 . " username = $username\n";
1271 $tweets = $obj->user_timeline(
1274 ( $last_id ? ( since_id => $last_id ) : () ),
1281 "type:debug Error during user_timeline($target) call: Aborted.\n";
1282 print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
1288 "type:debug user_timeline($target) call returned undef! Aborted\n";
1292 foreach my $t ( reverse @$tweets ) {
1293 my $text = &get_text( $t, $obj );
1294 my $reply = "tweet";
1295 if ( Irssi::settings_get_bool("show_reply_context")
1296 and $t->{in_reply_to_screen_name} ne $username
1297 and $t->{in_reply_to_screen_name}
1298 and not exists $friends{ $t->{in_reply_to_screen_name} } )
1300 $nicks{ $t->{in_reply_to_screen_name} } = time;
1302 unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
1304 $cache->{ $t->{in_reply_to_status_id} } =
1305 $obj->show_status( $t->{in_reply_to_status_id} );
1309 $context = $cache->{ $t->{in_reply_to_status_id} };
1312 my $ctext = &get_text( $context, $obj );
1313 printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
1314 $context->{id}, $username,
1315 $context->{user}{screen_name}, $ctext;
1319 printf $fh "id:%s account:%s nick:%s type:%s %s\n",
1320 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
1321 $last_id = $t->{id} if $last_id < $t->{id};
1323 printf $fh "id:%s account:%s type:last_id_fixreplies %s\n",
1324 $last_id, $username, $target;
1331 my $filename = $data->[0];
1332 my $attempt = $data->[1];
1334 print scalar localtime, " - checking child log at $filename ($attempt)"
1336 my ($new_last_poll);
1338 # reap any random leftover processes - work around a bug in irssi on gentoo
1339 waitpid( -1, WNOHANG );
1341 # first time we run we don't want to print out *everything*, so we just
1344 if ( open FILE, $filename ) {
1345 binmode FILE, ":" . &get_charset;
1349 last if /^__friends__/;
1350 unless (/\n$/) { # skip partial lines
1351 # print "Skipping partial line: $_" if &debug;
1358 foreach my $key (qw/id account nick type topic/) {
1359 if (s/^$key:((?:\S|\\ )+)\s*//) {
1361 $meta{$key} =~ s/%20/ /g;
1365 if ( $meta{type} and $meta{type} eq 'fix_replies_index' ) {
1366 $fix_replies_index{ $meta{account} } = $meta{id};
1367 print "fix_replies_index for $meta{account} set to $meta{id}"
1372 if ( not $meta{type} or $meta{type} !~ /searchid|last_id/ ) {
1373 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
1377 $new_cache{ $meta{id} } = time;
1379 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
1385 $meta{account} =~ s/\@(\w+)$//;
1386 $meta{service} = $1;
1388 lc $meta{service} eq
1389 lc Irssi::settings_get_str("twirssi_default_service") )
1391 $account = "$meta{account}: "
1392 if lc "$meta{account}\@$meta{service}" ne lc
1393 "$user\@$defservice";
1395 $account = "$meta{account}\@$meta{service}: ";
1399 if ( $meta{type} ne 'dm' and $meta{nick} and $meta{id} ) {
1400 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
1401 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
1402 $id_map{__indexes}{ $meta{nick} } = $marker;
1403 $id_map{__tweets}{ lc $meta{nick} }[$marker] = $_;
1404 $marker = ":$marker";
1408 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
1409 my $nick = "\@$meta{account}";
1410 if ( $_ =~ /\Q$nick\E(?:\W|$)/i
1411 and Irssi::settings_get_bool("twirssi_hilights") )
1413 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
1414 $hilight = MSGLEVEL_HILIGHT;
1417 if ( $meta{type} =~ /tweet|reply/ ) {
1420 ( MSGLEVEL_PUBLIC | $hilight ),
1421 $meta{type}, $account, $meta{nick}, $marker, $_
1423 } elsif ( $meta{type} eq 'search' ) {
1426 ( MSGLEVEL_PUBLIC | $hilight ),
1427 $meta{type}, $account, $meta{topic},
1428 $meta{nick}, $marker, $_
1431 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
1433 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1435 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1438 } elsif ( $meta{type} eq 'dm' ) {
1441 ( MSGLEVEL_MSGS | $hilight ),
1442 $meta{type}, $account, $meta{nick}, $_
1444 } elsif ( $meta{type} eq 'searchid' ) {
1445 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
1448 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
1450 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1452 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1455 print "Search '$meta{topic}' returned invalid id $meta{id}";
1457 } elsif ( $meta{type} eq 'last_id' ) {
1458 $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} =
1460 if $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} <
1462 } elsif ( $meta{type} eq 'last_id_fixreplies' ) {
1463 $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} =
1465 if $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} <
1467 } elsif ( $meta{type} eq 'error' ) {
1468 push @lines, [ MSGLEVEL_MSGS, $_ ];
1469 } elsif ( $meta{type} eq 'debug' ) {
1470 print "$_" if &debug,;
1472 print "Unknown line type $meta{type}: $_" if &debug,;
1478 if (/^__updated (\d+)$/) {
1479 $last_friends_poll = $1;
1480 print "Friend list updated" if &debug;
1485 $new_last_poll = $1;
1486 if ( $new_last_poll >= $last_poll ) {
1489 print "Impossible! ",
1490 "new_last_poll=$new_last_poll < last_poll=$last_poll!"
1492 undef $new_last_poll;
1496 my ( $f, $t ) = split ' ', $_;
1497 $nicks{$f} = $friends{$f} = $t;
1500 if ($new_last_poll) {
1501 print "new last_poll = $new_last_poll" if &debug;
1502 print "new last_poll_id = ", Dumper( $id_map{__last_id} ) if &debug;
1504 print "First call, not printing updates" if &debug;
1506 foreach my $line (@lines) {
1507 $window->printformat(
1509 "twirssi_" . $line->[1],
1510 @$line[ 2 .. $#$line - 1 ],
1511 &hilight( $line->[-1] )
1518 or warn "Failed to remove $filename: $!"
1521 # commit the pending cache lines to the actual cache, now that
1522 # we've printed our output
1523 %tweet_cache = ( %tweet_cache, %new_cache );
1525 # keep enough cached tweets, to make sure we don't show duplicates.
1526 foreach ( keys %tweet_cache ) {
1527 next if $tweet_cache{$_} >= $last_poll - 3600;
1528 delete $tweet_cache{$_};
1530 $last_poll = $new_last_poll;
1532 # make sure the pid is removed from the waitpid list
1533 Irssi::pidwait_remove($child_pid);
1535 # and that we don't leave any zombies behind, somehow
1536 waitpid( -1, WNOHANG );
1541 Irssi::settings_get_str("twirssi_replies_store") )
1543 if ( open JSON, ">$file" ) {
1544 print JSON JSON::Any->objToJson( \%id_map );
1547 &ccrap("Failed to write replies to $file: $!");
1558 if ( $attempt < 24 ) {
1559 Irssi::timeout_add_once( 5000, 'monitor_child',
1560 [ $filename, $attempt + 1 ] );
1562 print "Giving up on polling $filename" if &debug;
1563 Irssi::pidwait_remove($child_pid);
1564 waitpid( -1, WNOHANG );
1565 unlink $filename unless &debug;
1567 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1570 my @time = localtime($last_poll);
1571 if ( time - $last_poll < 24 * 60 * 60 ) {
1572 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1574 $since = scalar localtime($last_poll);
1577 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1582 q{ .--./ / | _.---.| },
1591 if ( time - $last_poll < 600 ) {
1592 &ccrap("Haven't been able to get updated tweets since $since");
1598 return Irssi::settings_get_bool("twirssi_debug");
1602 foreach my $msg (@_) {
1603 $window->print( "%R***%n $msg", MSGLEVEL_PUBLIC );
1608 foreach my $msg (@_) {
1609 $window->print( "%R***%n $msg", MSGLEVEL_CLIENTCRAP );
1616 if ( Irssi::settings_get_bool("tweet_to_away")
1618 and $data !~ /^[dD] / )
1621 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1623 $server->send_raw("away :$data");
1626 &ccrap( "Can't find bitlbee server.",
1627 "Update bitlbee_server or disable tweet_to_away" );
1637 my $noalert = shift;
1639 if ( length $data > 140 ) {
1640 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1648 sub valid_username {
1649 my $username = shift;
1651 $username = &normalize_username($username);
1653 unless ( exists $twits{$username} ) {
1654 ¬ice("Unknown username $username");
1664 ¬ice("Not logged in! Use /twitter_login username pass!");
1672 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1675 $linestart =~ /^\/(?:retweet|twitter_reply)(?:_as)?\s*$/
1676 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1677 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1679 { # /twitter_reply gets a nick:num
1681 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1682 sort { $nicks{$b} <=> $nicks{$a} }
1684 keys %{ $id_map{__indexes} };
1688 /^\/(twitter_unfriend|twitter_add_follow_extra|twitter_del_follow_extra)\s*$/
1690 { # /twitter_unfriend gets a nick
1692 push @$complist, grep /^\Q$word/i,
1693 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1696 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1698 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1699 my $prefix = $word =~ s/^@//;
1700 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1701 push @$complist, grep /^\Q$word/i,
1702 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1703 @$complist = map { "\@$_" } @$complist if $prefix;
1707 sub event_send_text {
1708 my ( $line, $server, $win ) = @_;
1709 my $awin = Irssi::active_win();
1711 # if the window where we got our text was the twitter window, and the user
1712 # wants to be lazy, tweet away!
1713 if ( ( $awin->get_active_name() eq $window->{name} )
1714 and Irssi::settings_get_bool("tweet_window_input") )
1716 &cmd_tweet( $line, $server, $win );
1721 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1722 return $poll if $poll >= 60;
1727 my $charset = Irssi::settings_get_str("twirssi_charset");
1728 return "utf8" if $charset =~ /^\s*$/;
1735 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1736 my $c = Irssi::settings_get_str("twirssi_nick_color");
1737 $c = $irssi_to_mirc_colors{$c};
1738 $text =~ s/(^|\W)\@(\w+)/$1\cC$c\@$2\cO/g if $c;
1740 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1741 my $c = Irssi::settings_get_str("twirssi_topic_color");
1742 $c = $irssi_to_mirc_colors{$c};
1743 $text =~ s/(^|\W)(\#|\!)([-\w]+)/$1\cC$c$2$3\cO/g if $c;
1745 $text =~ s/[\n\r]/ /g;
1753 my $provider = Irssi::settings_get_str("short_url_provider");
1756 Irssi::settings_get_bool("twirssi_always_shorten")
1757 or &too_long( $data, 1 )
1763 if ( $provider eq 'Bitly' ) {
1764 @args[ 1, 2 ] = split ',',
1765 Irssi::settings_get_str("short_url_args"), 2;
1766 unless ( @args == 3 ) {
1768 "WWW::Shorten::Bitly requires a username and API key.",
1769 "Set short_url_args to username,API_key or change your",
1770 "short_url_provider."
1772 return decode &get_charset, $data;
1776 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1779 my $short = makeashorterlink(@args);
1781 $data =~ s/\Q$url/$short/g;
1783 ¬ice("Failed to shorten $url!");
1789 return decode &get_charset, $data;
1792 sub normalize_username {
1795 my ( $username, $service ) = split /\@/, $user, 2;
1797 $service = ucfirst lc $service;
1800 ucfirst lc Irssi::settings_get_str("twirssi_default_service");
1801 unless ( exists $twits{"$username\@$service"} ) {
1803 foreach my $t ( sort keys %twits ) {
1804 next unless $t =~ /^\Q$username\E\@(Twitter|Identica)/;
1810 ¬ice("Can't find a logged in user '$user'");
1815 return "$username\@$service";
1821 my $text = decode_entities( $tweet->{text} );
1822 if ( $tweet->{truncated} ) {
1823 if ( exists $tweet->{retweeted_status} ) {
1824 $text = "RT \@$tweet->{retweeted_status}{user}{screen_name}: "
1825 . "$tweet->{retweeted_status}{text}";
1826 } elsif ( $object->isa('Net::Twitter') ) {
1827 $text .= " -- http://twitter.com/$tweet->{user}{screen_name}"
1828 . "/status/$tweet->{id}";
1832 $text =~ s/[\n\r]/ /g;
1837 Irssi::signal_add( "send text", "event_send_text" );
1839 Irssi::theme_register(
1841 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1842 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1843 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1844 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1845 'twirssi_error', 'ERROR: $0',
1849 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1850 Irssi::settings_add_str( "twirssi", "twirssi_charset", "utf8" );
1851 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1852 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1853 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1854 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1855 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1856 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1857 Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
1858 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1859 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1860 Irssi::settings_add_str( "twirssi", "twirssi_ignored_tags", "" );
1861 Irssi::settings_add_str( "twirssi", "twirssi_stripped_tags", "" );
1862 Irssi::settings_add_str( "twirssi", "twirssi_retweet_format",
1863 'RT $n: "$t" ${-- $c$}' );
1864 Irssi::settings_add_str( "twirssi", "twirssi_location",
1865 Irssi::get_irssi_dir . "/scripts/twirssi.pl" );
1866 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1867 Irssi::get_irssi_dir . "/scripts/twirssi.json" );
1868 Irssi::settings_add_str( "twirssi", "twirssi_oauth_store",
1869 Irssi::get_irssi_dir . "/scripts/twirssi.oauth" );
1871 Irssi::settings_add_int( "twirssi", "twitter_friends_poll", 600 );
1872 Irssi::settings_add_int( "twirssi", "twitter_timeout", 30 );
1874 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1875 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1876 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1877 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1878 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1879 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1880 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1881 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1882 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1883 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1884 Irssi::settings_add_bool( "twirssi", "twirssi_always_shorten", 0 );
1885 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1886 Irssi::settings_add_bool( "twirssi", "twirssi_avoid_ssl", 0 );
1887 Irssi::settings_add_bool( "twirssi", "twirssi_use_oauth", 1 );
1889 $last_poll = time - &get_poll_time;
1890 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1893 ->print( "Couldn't find a window named '"
1894 . Irssi::settings_get_str('twitter_window')
1895 . "', trying to create it." );
1897 Irssi::Windowitem::window_create(
1898 Irssi::settings_get_str('twitter_window'), 1 );
1899 $window->set_name( Irssi::settings_get_str('twitter_window') );
1903 Irssi::command_bind( "dm", "cmd_direct" );
1904 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1905 Irssi::command_bind( "tweet", "cmd_tweet" );
1906 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1907 Irssi::command_bind( "retweet", "cmd_retweet" );
1908 Irssi::command_bind( "retweet_as", "cmd_retweet_as" );
1909 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1910 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1911 Irssi::command_bind( "twitter_login", "cmd_login" );
1912 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1913 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1914 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1915 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1916 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1917 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1918 Irssi::command_bind( "twirssi_oauth", "cmd_oauth" );
1919 Irssi::command_bind( "twitter_updates", "get_updates" );
1920 Irssi::command_bind( "twitter_add_follow_extra", "cmd_add_follow" );
1921 Irssi::command_bind( "twitter_del_follow_extra", "cmd_del_follow" );
1922 Irssi::command_bind( "twitter_list_follow_extra", "cmd_list_follow" );
1923 Irssi::command_bind( "bitlbee_away", "update_away" );
1924 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1925 Irssi::command_bind( "reply", "cmd_reply" );
1926 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1928 Irssi::command_bind(
1931 print "twits: ", join ", ",
1932 map { "u: $_->{username}\@" . ref($_) } values %twits;
1933 print "selected: $user\@$defservice";
1934 print "friends: ", join ", ", sort keys %friends;
1935 print "nicks: ", join ", ", sort keys %nicks;
1936 print "searches: ", Dumper \%{ $id_map{__searches} };
1937 print "last poll: $last_poll";
1938 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1939 print DUMP Dumper \%tweet_cache;
1941 print "cache written out to /tmp/twirssi.cache.txt";
1945 Irssi::command_bind(
1949 "Twirssi v$VERSION; "
1951 $Net::Twitter::VERSION
1952 ? "Net::Twitter v$Net::Twitter::VERSION. "
1956 $Net::Identica::VERSION
1957 ? "Net::Identica v$Net::Identica::VERSION. "
1961 . JSON::Any::handler()
1962 . ". See details at http://twirssi.com/"
1966 Irssi::command_bind(
1969 "/twitter_follow <username>",
1971 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1974 Irssi::command_bind(
1977 "/twitter_unfriend <username>",
1979 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1982 Irssi::command_bind(
1983 "twitter_device_updates",
1985 "/twitter_device_updates none|im|sms",
1986 "update_delivery_device",
1987 sub { ¬ice("Device updated to $_[0]"); }
1990 Irssi::command_bind(
1993 "/twitter_block <username>",
1995 sub { ¬ice("Blocked $_[0]"); }
1998 Irssi::command_bind(
2001 "/twitter_unblock <username>",
2003 sub { ¬ice("Unblock $_[0]"); }
2006 Irssi::signal_add_last( 'complete word' => \&sig_complete );
2009 " %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N",
2010 " %C(_(\\%N http://twirssi.com/ for full docs",
2011 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet"
2014 my $file = Irssi::settings_get_str("twirssi_replies_store");
2015 if ( $file and -r $file ) {
2016 if ( open( JSON, $file ) ) {
2021 my $ref = JSON::Any->jsonToObj($json);
2023 my $num = keys %{ $id_map{__indexes} };
2024 ¬ice( sprintf "Loaded old replies from %d contact%s.",
2025 $num, ( $num == 1 ? "" : "s" ) );
2030 ¬ice("Failed to load old replies from $file: $!");
2034 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
2035 ¬ice("Loading WWW::Shorten::$provider...");
2036 eval "use WWW::Shorten::$provider;";
2040 "Failed to load WWW::Shorten::$provider - either clear",
2041 "short_url_provider or install the CPAN module"
2046 if ( my $autouser = Irssi::settings_get_str("twitter_usernames") ) {
2053 ->print( "Create a window named "
2054 . Irssi::settings_get_str('twitter_window')
2055 . " or change the value of twitter_window. Then, reload twirssi." );
2058 # vim: set sts=4 expandtab: