11 $Data::Dumper::Indent = 1;
13 use vars qw($VERSION %IRSSI);
16 my ($REV) = '$Rev: 484 $' =~ /(\d+)/;
18 authors => 'Dan Boger',
19 contact => 'zigdon@gmail.com',
21 description => 'Send twitter/identica updates using /tweet or /dent. '
22 . 'Can optionally set your bitlbee /away message to same',
23 license => 'GNU GPL v2',
24 url => 'http://twirssi.com',
25 changed => '$Date: 2009-02-21 13:53:25 -0800 (Sat, 21 Feb 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 &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 $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 &valid_username($username);
137 if ( &too_long( $data, 1 )
138 and Irssi::settings_get_str("short_url_provider") )
140 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
142 my $short = makeashorterlink($url);
143 $data =~ s/\Q$url/$short/g;
148 return if &too_long($data);
152 unless ( $twits{$username}->update($data) )
154 ¬ice("Update failed");
158 return unless $success;
161 ¬ice("Update caused an error: $@. Aborted.");
165 foreach ( $data =~ /@([-\w]+)/ ) {
169 my $away = &update_away($data);
171 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
175 my ( $data, $server, $win ) = @_;
177 return unless &logged_in($twit);
179 $data =~ s/^\s+|\s+$//;
181 ¬ice("Usage: /reply <nick[:num]> <update>");
185 $data =~ s/^\s+|\s+$//;
186 my ( $id, $data ) = split ' ', $data, 2;
187 unless ( $id and $data ) {
188 ¬ice("Usage: /reply_as <nick[:num]> <update>");
192 &cmd_reply_as( "$user $id $data", $server, $win );
196 my ( $data, $server, $win ) = @_;
198 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
199 ¬ice("twirssi_track_replies is required in order to reply to "
200 . "specific tweets. Either enable it, or just use /tweet "
201 . "\@username <text>." );
205 return unless &logged_in($twit);
207 $data =~ s/^\s+|\s+$//;
208 my ( $username, $id, $data ) = split ' ', $data, 3;
210 unless ( $username and $data ) {
211 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
215 return unless &valid_username($username);
218 $id =~ s/[^\w\d\-:]+//g;
219 ( $nick, $id ) = split /:/, $id;
220 unless ( exists $id_map{ lc $nick } ) {
221 ¬ice("Can't find a tweet from $nick to reply to!");
225 $id = $id_map{__indexes}{$nick} unless $id;
226 unless ( $id_map{ lc $nick }[$id] ) {
227 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
231 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
233 # remove any @nick at the beginning of the reply, as we'll add it anyway
234 $data =~ s/^\s*\@?$nick\s*//;
235 $data = "\@$nick " . $data;
238 if ( &too_long( $data, 1 ) ) {
239 if ( Irssi::settings_get_str("short_url_provider") ) {
240 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
242 my $short = makeashorterlink($url);
243 $data =~ s/\Q$url/$short/g;
249 return if &too_long($data);
254 $twits{$username}->update(
257 in_reply_to_status_id => $id_map{ lc $nick }[$id]
262 ¬ice("Update failed");
266 return unless $success;
269 ¬ice("Update caused an error: $@. Aborted");
273 foreach ( $data =~ /@([-\w]+)/ ) {
277 my $away = &update_away($data);
279 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
283 my ( $usage_str, $api_name, $post_ref ) = @_;
286 my ( $data, $server, $win ) = @_;
288 return unless &logged_in($twit);
290 $data =~ s/^\s+|\s+$//;
292 ¬ice("Usage: $usage_str");
298 unless ( $twit->$api_name($data) )
300 ¬ice("$api_name failed");
304 return unless $success;
307 ¬ice("$api_name caused an error. Aborted.");
311 &$post_ref($data) if $post_ref;
316 my ( $data, $server, $win ) = @_;
318 $data =~ s/^\s+|\s+$//g;
319 if ( exists $twits{$data} ) {
320 ¬ice("Switching to $data");
321 $twit = $twits{$data};
324 ¬ice("Unknown user $data");
328 sub cmd_logout_twitter {
329 my ( $data, $server, $win ) = @_;
331 $data =~ s/^\s+|\s+$//g;
332 $data = $user unless $data;
333 return unless &valid_username($data);
335 ¬ice("Logging out $data...");
336 $twits{$data}->end_session();
337 delete $twits{$data};
340 &cmd_switch( ( keys %twits )[0], $server, $win );
342 Irssi::timeout_remove($poll) if $poll;
347 sub cmd_logout_identica {
348 my ( $data, $server, $win ) = @_;
350 $data =~ s/^\s+|\s+$//g;
351 $data = $user unless $data;
352 return unless &valid_username($data);
354 ¬ice("Logging out $data...");
355 $twits{$data}->end_session();
356 delete $twits{$data};
359 &cmd_switch( ( keys %twits )[0], $server, $win );
361 Irssi::timeout_remove($poll) if $poll;
366 sub cmd_login_twitter {
367 my ( $data, $server, $win ) = @_;
370 ( $user, $pass ) = split ' ', $data, 2;
371 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames_twitter")
372 and my $autopass = Irssi::settings_get_str("twitter_passwords_twitter") )
374 my @user = split /\s*,\s*/, $autouser;
375 my @pass = split /\s*,\s*/, $autopass;
376 if ( @user != @pass ) {
377 ¬ice("Number of usernames doesn't match "
378 . "the number of passwords - auto-login failed" );
381 while ( @user and @pass ) {
384 &cmd_login_twitter("$u $p");
389 ¬ice("/twitter_login requires either a username and password "
390 . "or twitter_usernames_twitter and twitter_passwords_twitter to be set." );
394 %friends = %nicks = ();
396 $twit = Net::Twitter->new(
402 unless ( $twit->verify_credentials() ) {
403 ¬ice("Login as $user failed");
406 &cmd_switch( ( keys %twits )[0], $server, $win );
412 my $rate_limit = $twit->rate_limit_status();
413 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
415 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
421 $twits{$user} = $twit;
422 Irssi::timeout_remove($poll) if $poll;
423 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
424 ¬ice("Logged in as $user, loading friends list...");
426 ¬ice( "loaded friends: ", scalar keys %friends );
427 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
428 Irssi::settings_set_bool( "twirssi_first_run", 0 );
429 unless ( exists $friends{twirssi} ) {
430 ¬ice("Welcome to twirssi!"
431 . " Perhaps you should add \@twirssi to your friends list,"
432 . " so you can be notified when a new version is release?"
433 . " Just type /twitter_friend twirssi." );
440 ¬ice("Login failed");
444 sub cmd_login_identica {
445 my ( $data, $server, $win ) = @_;
448 ( $user, $pass ) = split ' ', $data, 2;
449 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames_identica")
450 and my $autopass = Irssi::settings_get_str("twitter_passwords_identica") )
452 my @user = split /\s*,\s*/, $autouser;
453 my @pass = split /\s*,\s*/, $autopass;
454 if ( @user != @pass ) {
455 ¬ice("Number of usernames doesn't match "
456 . "the number of passwords - auto-login failed" );
459 while ( @user and @pass ) {
462 &cmd_login_identica("$u $p");
467 ¬ice("/identica_login requires either a username and password "
468 . "or twitter_usernames_identica and twitter_passwords_identica to be set." );
472 %friends = %nicks = ();
474 $twit = Net::Identica->new(
480 unless ( $twit->verify_credentials() ) {
481 ¬ice("Login as $user failed");
484 &cmd_switch( ( keys %twits )[0], $server, $win );
490 my $rate_limit = $twit->rate_limit_status();
491 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
493 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
499 $twits{$user} = $twit;
500 Irssi::timeout_remove($poll) if $poll;
501 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
502 ¬ice("Logged in as $user, loading friends list...");
504 ¬ice( "loaded friends: ", scalar keys %friends );
505 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
506 Irssi::settings_set_bool( "twirssi_first_run", 0 );
507 unless ( exists $friends{twirssi} ) {
508 ¬ice("Welcome to twirssi!"
509 . " Perhaps you should add \@twirssi to your friends list,"
510 . " so you can be notified when a new version is release?"
511 . " Just type /twitter_friend twirssi." );
518 ¬ice("Login failed");
522 sub cmd_add_search_twitter {
523 my ( $data, $server, $win ) = @_;
525 unless ( $twit and $twit->can('search') ) {
526 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
527 . "doesn't support searches." );
531 $data =~ s/^\s+|\s+$//;
535 ¬ice("Usage: /twitter_subscribe <topic>");
539 if ( exists $id_map{__searches}{$user}{$data} ) {
540 ¬ice("Already had a subscription for '$data'");
544 $id_map{__searches}{$user}{$data} = 1;
545 ¬ice("Added subscription for '$data'");
548 sub cmd_add_search_identica {
549 my ( $data, $server, $win ) = @_;
551 unless ( $twit and $twit->can('search') ) {
552 ¬ice("ERROR: Your version of Net::Identica ($Net::Identica::VERSION) "
553 . "doesn't support searches." );
557 $data =~ s/^\s+|\s+$//;
561 ¬ice("Usage: /twitter_subscribe <topic>");
565 if ( exists $id_map{__searches}{$user}{$data} ) {
566 ¬ice("Already had a subscription for '$data'");
570 $id_map{__searches}{$user}{$data} = 1;
571 ¬ice("Added subscription for '$data'");
574 sub cmd_del_search_twitter {
575 my ( $data, $server, $win ) = @_;
577 unless ( $twit and $twit->can('search') ) {
578 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
579 . "doesn't support searches." );
582 $data =~ s/^\s+|\s+$//;
586 ¬ice("Usage: /twitter_unsubscribe <topic>");
590 unless ( exists $id_map{__searches}{$user}{$data} ) {
591 ¬ice("No subscription found for '$data'");
595 delete $id_map{__searches}{$user}{$data};
596 ¬ice("Removed subscription for '$data'");
599 sub cmd_del_search_identica {
600 my ( $data, $server, $win ) = @_;
602 unless ( $twit and $twit->can('search') ) {
603 ¬ice("ERROR: Your version of Net::Identica ($Net::Identica::VERSION) "
604 . "doesn't support searches." );
607 $data =~ s/^\s+|\s+$//;
611 ¬ice("Usage: /identica_unsubscribe <topic>");
615 unless ( exists $id_map{__searches}{$user}{$data} ) {
616 ¬ice("No subscription found for '$data'");
620 delete $id_map{__searches}{$user}{$data};
621 ¬ice("Removed subscription for '$data'");
624 sub cmd_list_search {
625 my ( $data, $server, $win ) = @_;
628 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
630 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
631 $topics = $topics ? "$topics, $topic" : $topic;
635 ¬ice("Search subscriptions for \@$suser: $topics");
640 ¬ice("No search subscriptions set up");
645 my ( $data, $server, $win ) = @_;
647 my $loc = Irssi::settings_get_str("twirssi_location");
650 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
657 eval { use Digest::MD5; };
661 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
666 $md5 = get("http://twirssi.com/md5sum");
670 ¬ice("Failed to download md5sum from peeron! Aborting.");
674 unless ( open( CUR, $loc ) ) {
676 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
681 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
684 if ( $cur_md5 eq $md5 ) {
685 ¬ice("Current twirssi seems to be up to date.");
690 my $URL = "http://twirssi.com/twirssi.pl";
691 ¬ice("Downloading twirssi from $URL");
692 LWP::Simple::getstore( $URL, "$loc.upgrade" );
695 unless ( open( NEW, "$loc.upgrade" ) ) {
697 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
702 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
705 if ( $new_md5 ne $md5 ) {
706 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
711 rename $loc, "$loc.backup"
712 or ¬ice("Failed to back up $loc: $!. Aborting")
714 rename "$loc.upgrade", $loc
715 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
718 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
719 if ( -e "$dir/autorun/$file" ) {
720 ¬ice("Updating $dir/autorun/$file");
721 unlink "$dir/autorun/$file"
722 or ¬ice("Failed to remove old $file from autorun: $!");
723 symlink "../$file", "$dir/autorun/$file"
724 or ¬ice("Failed to create symlink in autorun directory: $!");
727 ¬ice("Download complete. Reload twirssi with /script load $file");
737 print $fh "type:debug Loading friends page $page...\n"
738 if ( $fh and &debug );
739 my $friends = $twit->friends( { page => $page } );
740 last unless $friends;
741 $new_friends{ $_->{screen_name} } = time foreach @$friends;
743 last if @$friends == 0 or $page == 10;
748 print $fh "type:debug Error during friends list update. Aborted.\n";
752 my ( $added, $removed ) = ( 0, 0 );
753 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
754 foreach ( keys %new_friends ) {
755 next if exists $friends{$_};
760 print $fh "type:debug Scanning for removed friends...\n"
761 if ( $fh and &debug );
762 foreach ( keys %friends ) {
763 next if exists $new_friends{$_};
768 return ( $added, $removed );
772 print scalar localtime, " - get_updates starting" if &debug;
775 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
778 ->print( "Can't find a window named '"
779 . Irssi::settings_get_str('twitter_window')
780 . "'. Create it or change the value of twitter_window" );
783 return unless &logged_in($twit);
785 my ( $fh, $filename ) = File::Temp::tempfile();
786 binmode($fh, ":utf8");
790 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
791 Irssi::pidwait_add($pid);
792 } elsif ( defined $pid ) { # child
800 $error += &do_updates( $fh, $user, $twit );
801 foreach ( keys %twits ) {
803 $error += &do_updates( $fh, $_, $twits{$_} );
806 my ( $added, $removed ) = &load_friends($fh);
807 if ( $added + $removed ) {
808 print $fh "type:debug %R***%n Friends list updated: ",
810 sprintf( "%d added", $added ),
811 sprintf( "%d removed", $removed ) ),
814 print $fh "__friends__\n";
815 foreach ( sort keys %friends ) {
816 print $fh "$_ $friends{$_}\n";
820 print $fh "type:debug Update encountered errors. Aborted\n";
821 print $fh $last_poll;
828 print scalar localtime, " - get_updates ends" if &debug;
832 my ( $fh, $username, $obj ) = @_;
834 my $rate_limit = $obj->rate_limit_status();
835 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
836 ¬ice("Rate limit exceeded for $username");
840 print scalar localtime, " - Polling for updates for $username" if &debug;
843 $tweets = $obj->friends_timeline(
844 { since => HTTP::Date::time2str($last_poll) } );
849 "type:debug Error during friends_timeline call: $@. Aborted.\n";
853 unless ( ref $tweets ) {
854 if ( $obj->can("get_error") ) {
856 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
857 if ($@) { $error = $obj->get_error() }
858 print $fh "type:debug API Error during friends_timeline call: ",
862 "type:debug API Error during friends_timeline call. Aborted.\n";
867 foreach my $t ( reverse @$tweets ) {
868 my $text = decode_entities( $t->{text} );
869 $text = &hilight($text);
871 if ( Irssi::settings_get_bool("show_reply_context")
872 and $t->{in_reply_to_screen_name} ne $username
873 and $t->{in_reply_to_screen_name}
874 and not exists $friends{ $t->{in_reply_to_screen_name} } )
876 $nicks{ $t->{in_reply_to_screen_name} } = time;
879 $context = $obj->show_status( $t->{in_reply_to_status_id} );
883 my $ctext = decode_entities( $context->{text} );
884 $ctext = &hilight($ctext);
885 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
886 $context->{id}, $username,
887 $context->{user}{screen_name}, $ctext;
890 print $fh "type:debug request to get context failed: $@";
893 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
898 if $t->{user}{screen_name} eq $username
899 and not Irssi::settings_get_bool("show_own_tweets");
900 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
901 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
904 print scalar localtime, " - Polling for replies" if &debug;
906 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
911 print $fh "type:debug Error during replies call. Aborted.\n";
915 foreach my $t ( reverse @$tweets ) {
917 if exists $friends{ $t->{user}{screen_name} };
919 my $text = decode_entities( $t->{text} );
920 $text = &hilight($text);
921 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
922 $t->{id}, $username, $t->{user}{screen_name}, $text;
925 print scalar localtime, " - Polling for DMs" if &debug;
928 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
933 print $fh "type:debug Error during direct_messages call. Aborted.\n";
937 foreach my $t ( reverse @$tweets ) {
938 my $text = decode_entities( $t->{text} );
939 $text = &hilight($text);
940 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
941 $t->{id}, $username, $t->{sender_screen_name}, $text;
944 print scalar localtime, " - Polling for subscriptions" if &debug;
945 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
947 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
948 print $fh "type:debug searching for $topic since ",
949 "$id_map{__searches}{$username}{$topic}\n";
951 $search = $obj->search(
954 since_id => $id_map{__searches}{$username}{$topic}
961 "type:debug Error during search($topic) call. Aborted.\n";
965 unless ( $search->{max_id} ) {
967 "type:debug Invalid search results when searching for $topic.",
972 $id_map{__searches}{$username}{$topic} = $search->{max_id};
973 printf $fh "id:%d account:%s type:searchid topic:%s\n",
974 $search->{max_id}, $username, $topic;
976 foreach my $t ( reverse @{ $search->{results} } ) {
977 my $text = decode_entities( $t->{text} );
978 $text = &hilight($text);
979 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
980 $t->{id}, $username, $t->{from_user}, $topic, $text;
985 print scalar localtime, " - Done" if &debug;
992 my $filename = $data->[0];
993 my $attempt = $data->[1];
995 print scalar localtime, " - checking child log at $filename ($attempt)"
998 if ( open FILE, $filename ) {
1002 last if /^__friends__/;
1005 foreach my $key (qw/id account nick type topic/) {
1006 if (s/^$key:(\S+)\s*//) {
1011 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
1012 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
1013 $tweet_cache{ $meta{id} } = time;
1017 if ( $meta{account} ne $user ) {
1018 $account = "$meta{account}: ";
1022 if ( $meta{type} ne 'dm'
1023 and Irssi::settings_get_bool("twirssi_track_replies")
1027 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
1028 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
1029 $id_map{__indexes}{ $meta{nick} } = $marker;
1030 $marker = ":$marker";
1034 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
1035 if ( ( $_ =~ /\@$meta{account}\W/i )
1036 && Irssi::settings_get_bool("twirssi_hilights") )
1038 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
1039 $hilight = MSGLEVEL_HILIGHT;
1042 if ( $meta{type} =~ /tweet|reply/ ) {
1045 ( MSGLEVEL_PUBLIC | $hilight ),
1046 $meta{type}, $account, $meta{nick}, $marker, $_
1048 } elsif ( $meta{type} eq 'search' ) {
1051 ( MSGLEVEL_PUBLIC | $hilight ),
1052 $meta{type}, $account, $meta{topic},
1053 $meta{nick}, $marker, $_
1056 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1058 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1061 } elsif ( $meta{type} eq 'dm' ) {
1064 ( MSGLEVEL_MSGS | $hilight ),
1065 $meta{type}, $account, $meta{nick}, $_
1067 } elsif ( $meta{type} eq 'searchid' ) {
1068 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
1070 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
1072 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
1075 print "Search '$meta{topic}' returned invalid id $meta{id}";
1077 } elsif ( $meta{type} eq 'error' ) {
1078 push @lines, [ MSGLEVEL_MSGS, $_ ];
1079 } elsif ( $meta{type} eq 'debug' ) {
1080 print "$_" if &debug,;
1082 print "Unknown line type $meta{type}: $_" if &debug,;
1089 $new_last_poll = $_;
1092 my ( $f, $t ) = split ' ', $_;
1093 $nicks{$f} = $friends{$f} = $t;
1096 if ($new_last_poll) {
1097 print "new last_poll = $new_last_poll" if &debug;
1098 for my $line (@lines) {
1099 $window->printformat(
1101 "twirssi_" . $line->[1],
1102 @$line[ 2 .. $#$line ]
1108 or warn "Failed to remove $filename: $!"
1111 # keep enough cached tweets, to make sure we don't show duplicates.
1112 foreach ( keys %tweet_cache ) {
1113 next if $tweet_cache{$_} >= $last_poll;
1114 delete $tweet_cache{$_};
1116 $last_poll = $new_last_poll;
1121 Irssi::settings_get_str("twirssi_replies_store") )
1123 if ( open JSON, ">$file" ) {
1124 print JSON JSON::Any->objToJson( \%id_map );
1127 ¬ice("Failed to write replies to $file: $!");
1136 if ( $attempt < 24 ) {
1137 Irssi::timeout_add_once( 5000, 'monitor_child',
1138 [ $filename, $attempt + 1 ] );
1140 print "Giving up on polling $filename" if &debug;
1141 unlink $filename unless &debug;
1143 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1146 my @time = localtime($last_poll);
1147 if ( time - $last_poll < 24 * 60 * 60 ) {
1148 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1150 $since = scalar localtime($last_poll);
1152 ¬ice("Haven't been able to get updated tweets since $since");
1157 return Irssi::settings_get_bool("twirssi_debug");
1161 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1167 if ( Irssi::settings_get_bool("tweet_to_away")
1169 and $data !~ /^[dD] / )
1172 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1174 $server->send_raw("away :$data");
1177 ¬ice( "Can't find bitlbee server.",
1178 "Update bitlbee_server or disable tweet_to_away" );
1188 my $noalert = shift;
1190 if ( length $data > 140 ) {
1191 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1199 sub valid_username {
1200 my $username = shift;
1202 unless ( exists $twits{$username} ) {
1203 ¬ice("Unknown username $username");
1213 ¬ice("Not logged in! Use /twitter_login username pass!");
1221 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1224 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1225 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1226 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1228 { # /twitter_reply gets a nick:num
1230 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1231 sort keys %{ $id_map{__indexes} };
1234 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1236 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1237 my $prefix = $word =~ s/^@//;
1238 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1239 push @$complist, grep /^\Q$word/i,
1240 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1241 @$complist = map { "\@$_" } @$complist if $prefix;
1245 sub event_send_text {
1246 my ( $line, $server, $win ) = @_;
1247 my $awin = Irssi::active_win();
1249 # if the window where we got our text was the twitter window, and the user
1250 # wants to be lazy, tweet away!
1251 if ( ( $awin->get_active_name() eq $window->{name} )
1252 and Irssi::settings_get_bool("tweet_window_input") )
1254 &cmd_tweet( $line, $server, $win );
1259 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1260 return $poll if $poll >= 60;
1267 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1268 my $c = Irssi::settings_get_str("twirssi_nick_color");
1269 $c = $irssi_to_mirc_colors{$c};
1270 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1272 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1273 my $c = Irssi::settings_get_str("twirssi_topic_color");
1274 $c = $irssi_to_mirc_colors{$c};
1275 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1277 $text =~ s/[\n\r]/ /g;
1282 Irssi::signal_add( "send text", "event_send_text" );
1284 Irssi::theme_register(
1286 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1287 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1288 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1289 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1290 'twirssi_error', 'ERROR: $0',
1294 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1295 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1296 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1297 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1298 Irssi::settings_add_str( "twirssi", "twirssi_location",
1299 ".irssi/scripts/twirssi.pl" );
1300 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1301 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1302 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1303 ".irssi/scripts/twirssi.json" );
1304 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1305 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1306 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1307 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1308 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1309 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1310 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1311 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1312 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1313 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1314 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1315 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1316 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1318 $last_poll = time - &get_poll_time;
1319 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1322 Irssi::Windowitem::window_create(
1323 Irssi::settings_get_str('twitter_window'), 1 );
1324 $window->set_name( Irssi::settings_get_str('twitter_window') );
1328 Irssi::command_bind( "dm", "cmd_direct" );
1329 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1330 Irssi::command_bind( "tweet", "cmd_tweet" );
1331 Irssi::command_bind( "dent", "cmd_tweet" );
1332 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1333 Irssi::command_bind( "dent_as", "cmd_tweet_as" );
1334 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1335 Irssi::command_bind( "identica_reply", "cmd_reply" );
1336 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1337 Irssi::command_bind( "identica_reply_as", "cmd_reply_as" );
1338 Irssi::command_bind( "twitter_login", "cmd_login_twitter" );
1339 Irssi::command_bind( "twitter_logout", "cmd_logout_twitter" );
1340 Irssi::command_bind( "identica_login", "cmd_login_identica" );
1341 Irssi::command_bind( "identica_logout", "cmd_logout_identica" );
1342 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1343 Irssi::command_bind( "twitter_subscribe", "cmd_add_search_twitter" );
1344 Irssi::command_bind( "identica_subscribe", "cmd_add_search_identica" );
1345 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search_twitter" );
1346 Irssi::command_bind( "identica_unsubscribe", "cmd_del_search_identica" );
1347 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1348 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1349 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1350 Irssi::command_bind( "reply", "cmd_reply" );
1351 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1353 Irssi::command_bind(
1356 print "twits: ", join ", ",
1357 map { "u: $_->{username}" } values %twits;
1358 print "friends: ", join ", ", sort keys %friends;
1359 print "nicks: ", join ", ", sort keys %nicks;
1360 print "searches: ", Dumper \%{ $id_map{__searches} };
1361 print "last poll: $last_poll";
1364 Irssi::command_bind(
1367 ¬ice("Twirssi v$VERSION (r$REV); "
1368 . "Net::Identica v$Net::Identica::VERSION. "
1369 . "Net::Twitter v$Net::Twitter::VERSION."
1371 . JSON::Any::handler()
1372 . ". See details at http://twirssi.com/" );
1375 Irssi::command_bind(
1378 "/twitter_friend <username>",
1380 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1383 Irssi::command_bind(
1386 "/twitter_unfriend <username>",
1388 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1391 Irssi::command_bind( "twitter_updates", "get_updates" );
1392 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1394 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1395 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1397 " %Y||%C `%N Log in with /twitter_login or /identica_login, send updates with /tweet or /dent");
1399 my $file = Irssi::settings_get_str("twirssi_replies_store");
1400 if ( $file and -r $file ) {
1401 if ( open( JSON, $file ) ) {
1406 my $ref = JSON::Any->jsonToObj($json);
1408 my $num = keys %{ $id_map{__indexes} };
1409 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1410 $num, ( $num == 1 ? "" : "s" ) );
1413 ¬ice("Failed to load old replies from $file: $!");
1417 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1418 eval "use WWW::Shorten::$provider;";
1422 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1427 if ( my $autouser = Irssi::settings_get_str("twitter_usernames_twitter")
1428 and my $autopass = Irssi::settings_get_str("twitter_passwords_twitter") )
1430 &cmd_login_twitter();
1433 if ( my $autouser = Irssi::settings_get_str("twitter_usernames_identica")
1434 and my $autopass = Irssi::settings_get_str("twitter_passwords_identica") )
1436 &cmd_login_identica();
1441 ->print( "Create a window named "
1442 . Irssi::settings_get_str('twitter_window')
1443 . " or change the value of twitter_window. Then, reload twirssi." );