10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 453 $' =~ /(\d+)/;
17 authors => 'Dan Boger',
18 contact => 'zigdon@gmail.com',
20 description => 'Send twitter updates using /tweet. '
21 . 'Can optionally set your bitlbee /away message to same',
22 license => 'GNU GPL v2',
23 url => 'http://twirssi.com',
24 changed => '$Date: 2009-02-03 23:48:19 -0800 (Tue, 03 Feb 2009) $',
37 my %irssi_to_mirc_colors = (
57 my ( $data, $server, $win ) = @_;
59 return unless &logged_in($twit);
61 my ( $target, $text ) = split ' ', $data, 2;
62 unless ( $target and $text ) {
63 ¬ice("Usage: /dm <nick> <message>");
67 &cmd_direct_as( "$user $data", $server, $win );
71 my ( $data, $server, $win ) = @_;
73 return unless &logged_in($twit);
75 my ( $username, $target, $text ) = split ' ', $data, 3;
76 unless ( $username and $target and $text ) {
77 ¬ice("Usage: /dm_as <username> <nick> <message>");
81 return unless &valid_username($username);
84 unless ( $twits{$username}
85 ->new_direct_message( { user => $target, text => $text } ) )
87 ¬ice("DM to $target failed");
93 ¬ice("DM caused an error. Aborted");
97 ¬ice("DM sent to $target");
98 $nicks{$target} = time;
102 my ( $data, $server, $win ) = @_;
104 return unless &logged_in($twit);
106 $data =~ s/^\s+|\s+$//;
108 ¬ice("Usage: /tweet <update>");
112 &cmd_tweet_as( "$user $data", $server, $win );
116 my ( $data, $server, $win ) = @_;
118 return unless &logged_in($twit);
120 $data =~ s/^\s+|\s+$//;
121 my ( $username, $data ) = split ' ', $data, 2;
123 unless ( $username and $data ) {
124 ¬ice("Usage: /tweet_as <username> <update>");
128 return unless &valid_username($username);
130 if ( &too_long( $data, 1 )
131 and Irssi::settings_get_str("short_url_provider") )
133 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
135 my $short = makeashorterlink($url);
136 $data =~ s/\Q$url/$short/g;
141 return if &too_long($data);
144 unless ( $twits{$username}->update($data) )
146 ¬ice("Update failed");
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 &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 if ( Irssi::settings_get_str("short_url_provider") ) {
230 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
232 my $short = makeashorterlink($url);
233 $data =~ s/\Q$url/$short/g;
238 return if &too_long($data);
242 $twits{$username}->update(
245 in_reply_to_status_id => $id_map{ lc $nick }[$id]
250 ¬ice("Update failed");
256 ¬ice("Update caused an error. Aborted");
260 foreach ( $data =~ /@([-\w]+)/ ) {
264 my $away = &update_away($data);
266 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
270 my ( $usage_str, $api_name, $post_ref ) = @_;
273 my ( $data, $server, $win ) = @_;
275 return unless &logged_in($twit);
277 $data =~ s/^\s+|\s+$//;
279 ¬ice("Usage: $usage_str");
284 unless ( $twit->$api_name($data) )
286 ¬ice("$api_name failed");
292 ¬ice("$api_name caused an error. Aborted.");
296 &$post_ref($data) if $post_ref;
301 my ( $data, $server, $win ) = @_;
303 $data =~ s/^\s+|\s+$//g;
304 if ( exists $twits{$data} ) {
305 ¬ice("Switching to $data");
306 $twit = $twits{$data};
309 ¬ice("Unknown user $data");
314 my ( $data, $server, $win ) = @_;
316 $data =~ s/^\s+|\s+$//g;
317 $data = $user unless $data;
318 return unless &valid_username($data);
320 ¬ice("Logging out $data...");
321 $twits{$data}->end_session();
322 delete $twits{$data};
325 &cmd_switch( ( keys %twits )[0], $server, $win );
327 Irssi::timeout_remove($poll) if $poll;
333 my ( $data, $server, $win ) = @_;
336 ( $user, $pass ) = split ' ', $data, 2;
337 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
338 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
340 my @user = split /\s*,\s*/, $autouser;
341 my @pass = split /\s*,\s*/, $autopass;
342 if ( @user != @pass ) {
343 ¬ice("Number of usernames doesn't match "
344 . "the number of passwords - auto-login failed" );
347 while ( @user and @pass ) {
355 ¬ice("/twitter_login requires either a username and password "
356 . "or twitter_usernames and twitter_passwords to be set." );
360 %friends = %nicks = ();
362 $twit = Net::Twitter->new(
368 unless ( $twit->verify_credentials() ) {
369 ¬ice("Login as $user failed");
372 &cmd_switch( ( keys %twits )[0], $server, $win );
378 my $rate_limit = $twit->rate_limit_status();
379 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
381 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
387 $twits{$user} = $twit;
388 Irssi::timeout_remove($poll) if $poll;
389 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
390 ¬ice("Logged in as $user, loading friends list...");
392 ¬ice( "loaded friends: ", scalar keys %friends );
393 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
394 Irssi::settings_set_bool( "twirssi_first_run", 0 );
395 unless ( exists $friends{twirssi} ) {
396 ¬ice("Welcome to twirssi!"
397 . " Perhaps you should add \@twirssi to your friends list,"
398 . " so you can be notified when a new version is release?"
399 . " Just type /twitter_friend twirssi." );
406 ¬ice("Login failed");
411 my ( $data, $server, $win ) = @_;
413 unless ( $twit and $twit->can('search') ) {
414 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
415 . "doesn't support searches." );
419 $data =~ s/^\s+|\s+$//;
423 ¬ice("Usage: /twitter_subscribe <topic>");
427 if ( exists $id_map{__searches}{$user}{$data} ) {
428 ¬ice("Already had a subscription for '$data'");
432 $id_map{__searches}{$user}{$data} = 1;
433 ¬ice("Added subscription for '$data'");
437 my ( $data, $server, $win ) = @_;
439 unless ( $twit and $twit->can('search') ) {
440 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
441 . "doesn't support searches." );
444 $data =~ s/^\s+|\s+$//;
448 ¬ice("Usage: /twitter_unsubscribe <topic>");
452 unless ( exists $id_map{__searches}{$user}{$data} ) {
453 ¬ice("No subscription found for '$data'");
457 delete $id_map{__searches}{$user}{$data};
458 ¬ice("Removed subscription for '$data'");
461 sub cmd_list_search {
462 my ( $data, $server, $win ) = @_;
465 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
467 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
468 $topics = $topics ? "$topics, $topic" : $topic;
472 ¬ice("Search subscriptions for \@$suser: $topics");
477 ¬ice("No search subscriptions set up");
482 my ( $data, $server, $win ) = @_;
484 my $loc = Irssi::settings_get_str("twirssi_location");
487 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
492 if ( not -x "/usr/bin/md5sum" and not $data ) {
494 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
501 eval { use Digest::MD5; };
505 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
510 $md5 = get("http://twirssi.com/md5sum");
514 ¬ice("Failed to download md5sum from peeron! Aborting.");
518 unless ( open( CUR, $loc ) ) {
520 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
525 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
528 if ( $cur_md5 eq $md5 ) {
529 ¬ice("Current twirssi seems to be up to date.");
534 my $URL = "http://twirssi.com/twirssi.pl";
535 ¬ice("Downloading twirssi from $URL");
536 LWP::Simple::getstore( $URL, "$loc.upgrade" );
539 unless ( open( NEW, "$loc.upgrade" ) ) {
541 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
546 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
549 if ( $new_md5 ne $md5 ) {
550 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
555 rename $loc, "$loc.backup"
556 or ¬ice("Failed to back up $loc: $!. Aborting")
558 rename "$loc.upgrade", $loc
559 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
562 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
563 if ( -e "$dir/autorun/$file" ) {
564 ¬ice("Updating $dir/autorun/$file");
565 unlink "$dir/autorun/$file"
566 or ¬ice("Failed to remove old $file from autorun: $!");
567 symlink "../$file", "$dir/autorun/$file"
568 or ¬ice("Failed to create symlink in autorun directory: $!");
571 ¬ice("Download complete. Reload twirssi with /script load $file");
581 print $fh "type:debug Loading friends page $page...\n"
582 if ( $fh and &debug );
583 my $friends = $twit->friends( { page => $page } );
584 last unless $friends;
585 $new_friends{ $_->{screen_name} } = time foreach @$friends;
587 last if @$friends == 0 or $page == 10;
592 print $fh "type:debug Error during friends list update. Aborted.\n";
596 my ( $added, $removed ) = ( 0, 0 );
597 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
598 foreach ( keys %new_friends ) {
599 next if exists $friends{$_};
604 print $fh "type:debug Scanning for removed friends...\n"
605 if ( $fh and &debug );
606 foreach ( keys %friends ) {
607 next if exists $new_friends{$_};
612 return ( $added, $removed );
616 print scalar localtime, " - get_updates starting" if &debug;
619 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
622 ->print( "Can't find a window named '"
623 . Irssi::settings_get_str('twitter_window')
624 . "'. Create it or change the value of twitter_window" );
627 return unless &logged_in($twit);
629 my ( $fh, $filename ) = File::Temp::tempfile();
633 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
634 Irssi::pidwait_add($pid);
635 } elsif ( defined $pid ) { # child
643 $error += &do_updates( $fh, $user, $twit );
644 foreach ( keys %twits ) {
646 $error += &do_updates( $fh, $_, $twits{$_} );
649 my ( $added, $removed ) = &load_friends($fh);
650 if ( $added + $removed ) {
651 print $fh "type:debug %R***%n Friends list updated: ",
653 sprintf( "%d added", $added ),
654 sprintf( "%d removed", $removed ) ),
657 print $fh "__friends__\n";
658 foreach ( sort keys %friends ) {
659 print $fh "$_ $friends{$_}\n";
663 print $fh "type:debug Update encountered errors. Aborted\n";
664 print $fh $last_poll;
671 print scalar localtime, " - get_updates ends" if &debug;
675 my ( $fh, $username, $obj ) = @_;
677 my $rate_limit = $obj->rate_limit_status();
678 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
679 ¬ice("Rate limit exceeded for $username");
683 print scalar localtime, " - Polling for updates for $username" if &debug;
686 $tweets = $obj->friends_timeline(
687 { since => HTTP::Date::time2str($last_poll) } );
691 print $fh "type:debug Error during friends_timeline call. Aborted.\n";
695 unless ( ref $tweets ) {
696 if ( $obj->can("get_error") ) {
698 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
699 if ($@) { $error = $obj->get_error() }
700 print $fh "type:debug API Error during friends_timeline call: ",
704 "type:debug API Error during friends_timeline call. Aborted.\n";
709 foreach my $t ( reverse @$tweets ) {
710 my $text = decode_entities( $t->{text} );
711 $text = &hilight($text);
713 if ( Irssi::settings_get_bool("show_reply_context")
714 and $t->{in_reply_to_screen_name} ne $username
715 and $t->{in_reply_to_screen_name}
716 and not exists $friends{ $t->{in_reply_to_screen_name} } )
718 $nicks{ $t->{in_reply_to_screen_name} } = time;
721 $context = $obj->show_status( $t->{in_reply_to_status_id} );
725 my $ctext = decode_entities( $context->{text} );
726 $ctext = &hilight($ctext);
727 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
728 $context->{id}, $username,
729 $context->{user}{screen_name}, $ctext;
732 print $fh "type:debug request to get context failed: $@";
735 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
740 if $t->{user}{screen_name} eq $username
741 and not Irssi::settings_get_bool("show_own_tweets");
742 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
743 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
746 print scalar localtime, " - Polling for replies" if &debug;
748 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
753 print $fh "type:debug Error during replies call. Aborted.\n";
757 foreach my $t ( reverse @$tweets ) {
759 if exists $friends{ $t->{user}{screen_name} };
761 my $text = decode_entities( $t->{text} );
762 $text = &hilight($text);
763 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
764 $t->{id}, $username, $t->{user}{screen_name}, $text;
767 print scalar localtime, " - Polling for DMs" if &debug;
770 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
775 print $fh "type:debug Error during direct_messages call. Aborted.\n";
779 foreach my $t ( reverse @$tweets ) {
780 my $text = decode_entities( $t->{text} );
781 $text = &hilight($text);
782 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
783 $t->{id}, $username, $t->{sender_screen_name}, $text;
786 print scalar localtime, " - Polling for subscriptions" if &debug;
787 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
789 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
790 print $fh "type:debug searching for $topic since ",
791 "$id_map{__searches}{$username}{$topic}\n";
793 $search = $obj->search(
796 since_id => $id_map{__searches}{$username}{$topic}
803 "type:debug Error during search($topic) call. Aborted.\n";
807 unless ( $search->{max_id} ) {
809 "type:debug Invalid search results when searching for $topic.",
814 $id_map{__searches}{$username}{$topic} = $search->{max_id};
815 printf $fh "id:%d account:%s type:searchid topic:%s\n",
816 $search->{max_id}, $username, $topic;
818 foreach my $t ( reverse @{ $search->{results} } ) {
819 my $text = decode_entities( $t->{text} );
820 $text = &hilight($text);
821 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
822 $t->{id}, $username, $t->{from_user}, $topic, $text;
827 print scalar localtime, " - Done" if &debug;
834 my $filename = $data->[0];
835 my $attempt = $data->[1];
837 print scalar localtime, " - checking child log at $filename ($attempt)"
840 if ( open FILE, $filename ) {
844 last if /^__friends__/;
847 foreach my $key (qw/id account nick type topic/) {
848 if (s/^$key:(\S+)\s*//) {
853 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
854 $tweet_cache{ $meta{id} } = time;
856 if ( $meta{account} ne $user ) {
857 $account = "$meta{account}: ";
861 if ( $meta{type} ne 'dm'
862 and Irssi::settings_get_bool("twirssi_track_replies")
866 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
867 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
868 $id_map{__indexes}{ $meta{nick} } = $marker;
869 $marker = ":$marker";
873 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
874 if ( $_ =~ /\@$meta{account}\W/i ) {
875 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
876 $hilight = MSGLEVEL_HILIGHT;
879 if ( $meta{type} =~ /tweet|reply/ ) {
882 ( MSGLEVEL_PUBLIC | $hilight ),
883 $meta{type}, $account, $meta{nick}, $marker, $_
885 } elsif ( $meta{type} eq 'search' ) {
888 ( MSGLEVEL_PUBLIC | $hilight ),
889 $meta{type}, $account, $meta{topic},
890 $meta{nick}, $marker, $_
893 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
895 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
898 } elsif ( $meta{type} eq 'dm' ) {
901 ( MSGLEVEL_MSGS | $hilight ),
902 $meta{type}, $account, $meta{nick}, $_
904 } elsif ( $meta{type} eq 'searchid' ) {
905 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
907 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
909 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
912 print "Search '$meta{topic}' returned invalid id $meta{id}";
914 print "Search '$meta{topic}' id set to $meta{id}" if &debug;
915 } elsif ( $meta{type} eq 'error' ) {
916 push @lines, [ MSGLEVEL_MSGS, $_ ];
917 } elsif ( $meta{type} eq 'debug' ) {
918 print "$_" if &debug,;
920 print "Unknown line type $meta{type}: $_" if &debug,;
930 my ( $f, $t ) = split ' ', $_;
931 $nicks{$f} = $friends{$f} = $t;
934 if ($new_last_poll) {
935 print "new last_poll = $new_last_poll" if &debug;
936 for my $line (@lines) {
937 $window->printformat(
939 "twirssi_" . $line->[1],
940 @$line[ 2 .. $#$line ]
946 or warn "Failed to remove $filename: $!"
949 # keep enough cached tweets, to make sure we don't show duplicates.
950 foreach ( keys %tweet_cache ) {
951 next if $tweet_cache{$_} >= $last_poll;
952 delete $tweet_cache{$_};
954 $last_poll = $new_last_poll;
959 Irssi::settings_get_str("twirssi_replies_store") )
961 if ( open JSON, ">$file" ) {
962 print JSON JSON::Any->objToJson( \%id_map );
965 ¬ice("Failed to write replies to $file: $!");
974 if ( $attempt < 12 ) {
975 Irssi::timeout_add_once( 5000, 'monitor_child',
976 [ $filename, $attempt + 1 ] );
978 ¬ice("Giving up on polling $filename");
979 unlink $filename unless &debug;
984 return Irssi::settings_get_bool("twirssi_debug");
988 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
994 if ( Irssi::settings_get_bool("tweet_to_away")
996 and $data !~ /^[dD] / )
999 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1001 $server->send_raw("away :$data");
1004 ¬ice( "Can't find bitlbee server.",
1005 "Update bitlbee_server or disable tweet_to_away" );
1015 my $noalert = shift;
1017 if ( length $data > 140 ) {
1018 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1026 sub valid_username {
1027 my $username = shift;
1029 unless ( exists $twits{$username} ) {
1030 ¬ice("Unknown username $username");
1040 ¬ice("Not logged in! Use /twitter_login username pass!");
1048 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1051 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1052 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1053 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1055 { # /twitter_reply gets a nick:num
1057 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1058 sort keys %{ $id_map{__indexes} };
1061 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1063 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1064 my $prefix = $word =~ s/^@//;
1065 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1066 push @$complist, grep /^\Q$word/i,
1067 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1068 @$complist = map { "\@$_" } @$complist if $prefix;
1072 sub event_send_text {
1073 my ( $line, $server, $win ) = @_;
1074 my $awin = Irssi::active_win();
1076 # if the window where we got our text was the twitter window, and the user
1077 # wants to be lazy, tweet away!
1078 if ( ( $awin->get_active_name() eq $window->{name} )
1079 and Irssi::settings_get_bool("tweet_window_input") )
1081 &cmd_tweet( $line, $server, $win );
1086 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1087 return $poll if $poll >= 60;
1094 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
1095 $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g;
1096 $text =~ s/[\n\r]/ /g;
1101 Irssi::signal_add( "send text", "event_send_text" );
1103 Irssi::theme_register(
1105 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1106 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1107 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1108 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1109 'twirssi_error', 'ERROR: $0',
1113 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1114 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1115 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1116 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1117 Irssi::settings_add_str( "twirssi", "twirssi_location",
1118 ".irssi/scripts/twirssi.pl" );
1119 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1120 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1121 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1122 ".irssi/scripts/twirssi.json" );
1123 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1124 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1125 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1126 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1127 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1128 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1129 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1130 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1131 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1133 $last_poll = time - &get_poll_time;
1134 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1137 Irssi::Windowitem::window_create(
1138 Irssi::settings_get_str('twitter_window'), 1 );
1139 $window->set_name( Irssi::settings_get_str('twitter_window') );
1143 Irssi::command_bind( "dm", "cmd_direct" );
1144 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1145 Irssi::command_bind( "tweet", "cmd_tweet" );
1146 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1147 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1148 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1149 Irssi::command_bind( "twitter_login", "cmd_login" );
1150 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1151 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1152 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1153 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1154 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1155 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1156 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1157 Irssi::command_bind( "reply", "cmd_reply" );
1158 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1160 Irssi::command_bind(
1163 print "twits: ", join ", ",
1164 map { "u: $_->{username}" } values %twits;
1165 print "friends: ", join ", ", sort keys %friends;
1166 print "nicks: ", join ", ", sort keys %nicks;
1167 print "searches: ", Dumper \%{ $id_map{__searches} };
1168 print "last poll: $last_poll";
1171 Irssi::command_bind(
1174 ¬ice("Twirssi v$VERSION (r$REV); "
1175 . "Net::Twitter v$Net::Twitter::VERSION. "
1177 . JSON::Any::handler()
1178 . ". See details at http://twirssi.com/" );
1181 Irssi::command_bind(
1184 "/twitter_friend <username>",
1186 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1189 Irssi::command_bind(
1192 "/twitter_unfriend <username>",
1194 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1197 Irssi::command_bind( "twitter_updates", "get_updates" );
1198 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1200 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1201 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1203 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1205 my $file = Irssi::settings_get_str("twirssi_replies_store");
1206 if ( $file and -r $file ) {
1207 if ( open( JSON, $file ) ) {
1212 my $ref = JSON::Any->jsonToObj($json);
1214 my $num = keys %{ $id_map{__indexes} };
1215 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1216 $num, ( $num == 1 ? "" : "s" ) );
1219 ¬ice("Failed to load old replies from $file: $!");
1223 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1224 eval "use WWW::Shorten::$provider;";
1228 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1233 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1234 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1241 ->print( "Create a window named "
1242 . Irssi::settings_get_str('twitter_window')
1243 . " or change the value of twitter_window. Then, reload twirssi." );
1246 # vim: set sts=4 expandtab: