10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 449 $' =~ /(\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-02 09:49:45 -0800 (Mon, 02 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 # remove any @nick at the beginning of the reply, as we'll add it anyway
223 $data =~ s/^\s*\@?$nick\s*//;
224 $data = "\@$nick " . $data;
226 if ( Irssi::settings_get_str("short_url_provider") ) {
227 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
229 my $short = makeashorterlink($url);
230 $data =~ s/\Q$url/$short/g;
235 return if &too_long($data);
239 $twits{$username}->update(
242 in_reply_to_status_id => $id_map{ lc $nick }[$id]
247 ¬ice("Update failed");
253 ¬ice("Update caused an error. Aborted");
257 foreach ( $data =~ /@([-\w]+)/ ) {
261 my $away = &update_away($data);
263 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
267 my ( $usage_str, $api_name, $post_ref ) = @_;
270 my ( $data, $server, $win ) = @_;
272 return unless &logged_in($twit);
274 $data =~ s/^\s+|\s+$//;
276 ¬ice("Usage: $usage_str");
281 unless ( $twit->$api_name($data) )
283 ¬ice("$api_name failed");
289 ¬ice("$api_name caused an error. Aborted.");
293 &$post_ref($data) if $post_ref;
298 my ( $data, $server, $win ) = @_;
300 $data =~ s/^\s+|\s+$//g;
301 if ( exists $twits{$data} ) {
302 ¬ice("Switching to $data");
303 $twit = $twits{$data};
306 ¬ice("Unknown user $data");
311 my ( $data, $server, $win ) = @_;
313 $data =~ s/^\s+|\s+$//g;
314 $data = $user unless $data;
315 return unless &valid_username($data);
318 ¬ice("Logging out $data...");
319 $twits{$data}->end_session();
320 delete $twits{$data};
322 ¬ice("Logging out $user...");
323 $twit->end_session();
325 delete $twits{$user};
327 &cmd_switch( ( keys %twits )[0], $server, $win );
329 Irssi::timeout_remove($poll) if $poll;
336 my ( $data, $server, $win ) = @_;
339 ( $user, $pass ) = split ' ', $data, 2;
340 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
341 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
343 my @user = split /\s*,\s*/, $autouser;
344 my @pass = split /\s*,\s*/, $autopass;
345 if ( @user != @pass ) {
346 ¬ice("Number of usernames doesn't match "
347 . "the number of passwords - auto-login failed" );
350 while ( @user and @pass ) {
358 ¬ice("/twitter_login requires either a username and password "
359 . "or twitter_usernames and twitter_passwords to be set." );
363 %friends = %nicks = ();
365 $twit = Net::Twitter->new(
371 unless ( $twit->verify_credentials() ) {
372 ¬ice("Login as $user failed");
375 &cmd_switch( ( keys %twits )[0], $server, $win );
381 my $rate_limit = $twit->rate_limit_status();
382 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
384 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
390 $twits{$user} = $twit;
391 Irssi::timeout_remove($poll) if $poll;
392 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
393 ¬ice("Logged in as $user, loading friends list...");
395 ¬ice( "loaded friends: ", scalar keys %friends );
396 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
397 Irssi::settings_set_bool( "twirssi_first_run", 0 );
398 unless ( exists $friends{twirssi} ) {
399 ¬ice("Welcome to twirssi!"
400 . " Perhaps you should add \@twirssi to your friends list,"
401 . " so you can be notified when a new version is release?"
402 . " Just type /twitter_friend twirssi." );
409 ¬ice("Login failed");
414 my ( $data, $server, $win ) = @_;
416 unless ( $twit and $twit->can('search') ) {
417 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
418 . "doesn't support searches." );
422 $data =~ s/^\s+|\s+$//;
426 ¬ice("Usage: /twitter_subscribe <topic>");
430 if ( exists $id_map{__searches}{$user}{$data} ) {
431 ¬ice("Already had a subscription for '$data'");
435 $id_map{__searches}{$user}{$data} = 1;
436 ¬ice("Added subscription for '$data'");
440 my ( $data, $server, $win ) = @_;
442 unless ( $twit and $twit->can('search') ) {
443 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
444 . "doesn't support searches." );
447 $data =~ s/^\s+|\s+$//;
451 ¬ice("Usage: /twitter_unsubscribe <topic>");
455 unless ( exists $id_map{__searches}{$user}{$data} ) {
456 ¬ice("No subscription found for '$data'");
460 delete $id_map{__searches}{$user}{$data};
461 ¬ice("Removed subscription for '$data'");
464 sub cmd_list_search {
465 my ( $data, $server, $win ) = @_;
468 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
470 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
471 $topics = $topics ? "$topics, $topic" : $topic;
475 ¬ice("Search subscriptions for \@$suser: $topics");
480 ¬ice("No search subscriptions set up");
485 my ( $data, $server, $win ) = @_;
487 my $loc = Irssi::settings_get_str("twirssi_location");
490 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
495 if ( not -x "/usr/bin/md5sum" and not $data ) {
497 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
504 eval { use Digest::MD5; };
508 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
513 $md5 = get("http://twirssi.com/md5sum");
517 ¬ice("Failed to download md5sum from peeron! Aborting.");
521 unless ( open( CUR, $loc ) ) {
523 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
528 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
531 if ( $cur_md5 eq $md5 ) {
532 ¬ice("Current twirssi seems to be up to date.");
537 my $URL = "http://twirssi.com/twirssi.pl";
538 ¬ice("Downloading twirssi from $URL");
539 LWP::Simple::getstore( $URL, "$loc.upgrade" );
542 unless ( open( NEW, "$loc.upgrade" ) ) {
544 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
549 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
552 if ( $new_md5 ne $md5 ) {
553 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
558 rename $loc, "$loc.backup"
559 or ¬ice("Failed to back up $loc: $!. Aborting")
561 rename "$loc.upgrade", $loc
562 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
565 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
566 if ( -e "$dir/autorun/$file" ) {
567 ¬ice("Updating $dir/autorun/$file");
568 unlink "$dir/autorun/$file"
569 or ¬ice("Failed to remove old $file from autorun: $!");
570 symlink "../$file", "$dir/autorun/$file"
571 or ¬ice("Failed to create symlink in autorun directory: $!");
574 ¬ice("Download complete. Reload twirssi with /script load $file");
584 print $fh "type:debug Loading friends page $page...\n"
585 if ( $fh and &debug );
586 my $friends = $twit->friends( { page => $page } );
587 last unless $friends;
588 $new_friends{ $_->{screen_name} } = time foreach @$friends;
590 last if @$friends == 0 or $page == 10;
595 print $fh "type:debug Error during friends list update. Aborted.\n";
599 my ( $added, $removed ) = ( 0, 0 );
600 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
601 foreach ( keys %new_friends ) {
602 next if exists $friends{$_};
607 print $fh "type:debug Scanning for removed friends...\n"
608 if ( $fh and &debug );
609 foreach ( keys %friends ) {
610 next if exists $new_friends{$_};
615 return ( $added, $removed );
619 print scalar localtime, " - get_updates starting" if &debug;
622 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
625 ->print( "Can't find a window named '"
626 . Irssi::settings_get_str('twitter_window')
627 . "'. Create it or change the value of twitter_window" );
630 return unless &logged_in($twit);
632 my ( $fh, $filename ) = File::Temp::tempfile();
636 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
637 Irssi::pidwait_add($pid);
638 } elsif ( defined $pid ) { # child
646 $error += &do_updates( $fh, $user, $twit );
647 foreach ( keys %twits ) {
649 $error += &do_updates( $fh, $_, $twits{$_} );
652 my ( $added, $removed ) = &load_friends($fh);
653 if ( $added + $removed ) {
654 print $fh "type:debug %R***%n Friends list updated: ",
656 sprintf( "%d added", $added ),
657 sprintf( "%d removed", $removed ) ),
660 print $fh "__friends__\n";
661 foreach ( sort keys %friends ) {
662 print $fh "$_ $friends{$_}\n";
666 print $fh "type:debug Update encountered errors. Aborted\n";
667 print $fh $last_poll;
674 print scalar localtime, " - get_updates ends" if &debug;
678 my ( $fh, $username, $obj ) = @_;
680 my $rate_limit = $obj->rate_limit_status();
681 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
682 ¬ice("Rate limit exceeded for $username");
686 print scalar localtime, " - Polling for updates for $username" if &debug;
689 $tweets = $obj->friends_timeline(
690 { since => HTTP::Date::time2str($last_poll) } );
694 print $fh "type:debug Error during friends_timeline call. Aborted.\n";
698 unless ( ref $tweets ) {
699 if ( $obj->can("get_error") ) {
701 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
702 if ($@) { $error = $obj->get_error() }
703 print $fh "type:debug API Error during friends_timeline call: ",
707 "type:debug API Error during friends_timeline call. Aborted.\n";
712 foreach my $t ( reverse @$tweets ) {
713 my $text = decode_entities( $t->{text} );
714 $text = &hilight($text);
716 if ( Irssi::settings_get_bool("show_reply_context")
717 and $t->{in_reply_to_screen_name} ne $username
718 and $t->{in_reply_to_screen_name}
719 and not exists $friends{ $t->{in_reply_to_screen_name} } )
721 $nicks{ $t->{in_reply_to_screen_name} } = time;
724 $context = $obj->show_status( $t->{in_reply_to_status_id} );
728 my $ctext = decode_entities( $context->{text} );
729 $ctext = &hilight($ctext);
730 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
731 $context->{id}, $username,
732 $context->{user}{screen_name}, $ctext;
735 print $fh "type:debug request to get context failed: $@";
738 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
743 if $t->{user}{screen_name} eq $username
744 and not Irssi::settings_get_bool("show_own_tweets");
745 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
746 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
749 print scalar localtime, " - Polling for replies" if &debug;
751 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
756 print $fh "type:debug Error during replies call. Aborted.\n";
760 foreach my $t ( reverse @$tweets ) {
762 if exists $friends{ $t->{user}{screen_name} };
764 my $text = decode_entities( $t->{text} );
765 $text = &hilight($text);
766 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
767 $t->{id}, $username, $t->{user}{screen_name}, $text;
770 print scalar localtime, " - Polling for DMs" if &debug;
773 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
778 print $fh "type:debug Error during direct_messages call. Aborted.\n";
782 foreach my $t ( reverse @$tweets ) {
783 my $text = decode_entities( $t->{text} );
784 $text = &hilight($text);
785 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
786 $t->{id}, $username, $t->{sender_screen_name}, $text;
789 print scalar localtime, " - Polling for subscriptions" if &debug;
790 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
792 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
793 print $fh "type:debug searching for $topic since ",
794 "$id_map{__searches}{$username}{$topic}\n";
796 $search = $obj->search(
799 since_id => $id_map{__searches}{$username}{$topic}
806 "type:debug Error during search($topic) call. Aborted.\n";
810 unless ( $search->{max_id} ) {
812 "type:debug Invalid search results when searching for $topic.",
817 $id_map{__searches}{$username}{$topic} = $search->{max_id};
818 printf $fh "id:%d account:%s type:searchid topic:%s\n",
819 $search->{max_id}, $username, $topic;
821 foreach my $t ( reverse @{ $search->{results} } ) {
822 my $text = decode_entities( $t->{text} );
823 $text = &hilight($text);
824 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
825 $t->{id}, $username, $t->{from_user}, $topic, $text;
830 print scalar localtime, " - Done" if &debug;
837 my $filename = $data->[0];
838 my $attempt = $data->[1];
840 print scalar localtime, " - checking child log at $filename ($attempt)"
843 if ( open FILE, $filename ) {
847 last if /^__friends__/;
850 foreach my $key (qw/id account nick type topic/) {
851 if (s/^$key:(\S+)\s*//) {
856 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
857 $tweet_cache{ $meta{id} } = time;
859 if ( $meta{account} ne $user ) {
860 $account = "$meta{account}: ";
864 if ( $meta{type} ne 'dm'
865 and Irssi::settings_get_bool("twirssi_track_replies")
869 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
870 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
871 $id_map{__indexes}{ $meta{nick} } = $marker;
872 $marker = ":$marker";
876 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
877 if ( $_ =~ /\@$meta{account}\W/i ) {
878 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
879 $hilight = MSGLEVEL_HILIGHT;
882 if ( $meta{type} =~ /tweet|reply/ ) {
885 ( MSGLEVEL_PUBLIC | $hilight ),
886 $meta{type}, $account, $meta{nick}, $marker, $_
888 } elsif ( $meta{type} eq 'search' ) {
891 ( MSGLEVEL_PUBLIC | $hilight ),
892 $meta{type}, $account, $meta{topic},
893 $meta{nick}, $marker, $_
895 } elsif ( $meta{type} eq 'dm' ) {
898 ( MSGLEVEL_MSGS | $hilight ),
899 $meta{type}, $account, $meta{nick}, $_
901 } elsif ( $meta{type} eq 'searchid' ) {
902 print "Search '$meta{topic}' returned id $meta{id}";
904 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
906 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
909 print "Search '$meta{topic}' returned invalid id $meta{id}";
911 print "Search '$meta{topic}' id set to $meta{id}" if &debug;
912 } elsif ( $meta{type} eq 'error' ) {
913 push @lines, [ MSGLEVEL_MSGS, $_ ];
914 } elsif ( $meta{type} eq 'debug' ) {
915 print "$_" if &debug,;
917 print "Unknown line type $meta{type}: $_" if &debug,;
927 my ( $f, $t ) = split ' ', $_;
928 $nicks{$f} = $friends{$f} = $t;
931 if ($new_last_poll) {
932 print "new last_poll = $new_last_poll" if &debug;
933 for my $line (@lines) {
934 $window->printformat(
936 "twirssi_" . $line->[1],
937 @$line[ 2 .. $#$line ]
943 or warn "Failed to remove $filename: $!"
946 # keep enough cached tweets, to make sure we don't show duplicates.
947 foreach ( keys %tweet_cache ) {
948 next if $tweet_cache{$_} >= $last_poll;
949 delete $tweet_cache{$_};
951 $last_poll = $new_last_poll;
956 Irssi::settings_get_str("twirssi_replies_store") )
958 if ( open JSON, ">$file" ) {
959 print JSON JSON::Any->objToJson( \%id_map );
962 ¬ice("Failed to write replies to $file: $!");
971 if ( $attempt < 12 ) {
972 Irssi::timeout_add_once( 5000, 'monitor_child',
973 [ $filename, $attempt + 1 ] );
975 ¬ice("Giving up on polling $filename");
976 unlink $filename unless &debug;
981 return Irssi::settings_get_bool("twirssi_debug");
985 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
991 if ( Irssi::settings_get_bool("tweet_to_away")
993 and $data !~ /^[dD] / )
996 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
998 $server->send_raw("away :$data");
1001 ¬ice( "Can't find bitlbee server.",
1002 "Update bitlbee_server or disable tweet_to_away" );
1012 my $noalert = shift;
1014 if ( length $data > 140 ) {
1015 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1023 sub valid_username {
1024 my $username = shift;
1026 unless ( exists $twits{$username} ) {
1027 ¬ice("Unknown username $username");
1037 ¬ice("Not logged in! Use /twitter_login username pass!");
1045 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1048 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1049 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1050 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1052 { # /twitter_reply gets a nick:num
1054 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1055 sort keys %{ $id_map{__indexes} };
1058 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1060 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1061 my $prefix = $word =~ s/^@//;
1062 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1063 push @$complist, grep /^\Q$word/i,
1064 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1065 @$complist = map { "\@$_" } @$complist if $prefix;
1069 sub event_send_text {
1070 my ( $line, $server, $win ) = @_;
1071 my $awin = Irssi::active_win();
1073 # if the window where we got our text was the twitter window, and the user
1074 # wants to be lazy, tweet away!
1075 if ( ( $awin->get_active_name() eq $window->{name} )
1076 and Irssi::settings_get_bool("tweet_window_input") )
1078 &cmd_tweet( $line, $server, $win );
1083 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1084 return $poll if $poll >= 60;
1091 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
1092 $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g;
1093 $text =~ s/[\n\r]/ /g;
1098 Irssi::signal_add( "send text", "event_send_text" );
1100 Irssi::theme_register(
1102 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1103 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1104 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1105 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1106 'twirssi_error', 'ERROR: $0',
1110 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1111 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1112 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1113 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1114 Irssi::settings_add_str( "twirssi", "twirssi_location",
1115 ".irssi/scripts/twirssi.pl" );
1116 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1117 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1118 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1119 ".irssi/scripts/twirssi.json" );
1120 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1121 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1122 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1123 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1124 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1125 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1126 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1127 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1129 $last_poll = time - &get_poll_time;
1130 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1133 Irssi::Windowitem::window_create(
1134 Irssi::settings_get_str('twitter_window'), 1 );
1135 $window->set_name( Irssi::settings_get_str('twitter_window') );
1139 Irssi::command_bind( "dm", "cmd_direct" );
1140 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1141 Irssi::command_bind( "tweet", "cmd_tweet" );
1142 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1143 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1144 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1145 Irssi::command_bind( "twitter_login", "cmd_login" );
1146 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1147 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1148 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1149 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1150 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1151 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1152 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1153 Irssi::command_bind( "reply", "cmd_reply" );
1154 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1156 Irssi::command_bind(
1159 print "twits: ", join ", ",
1160 map { "u: $_->{username}" } values %twits;
1161 print "friends: ", join ", ", sort keys %friends;
1162 print "nicks: ", join ", ", sort keys %nicks;
1163 print "searches: ", Dumper \%{ $id_map{__searches} };
1164 print "last poll: $last_poll";
1167 Irssi::command_bind(
1170 ¬ice("Twirssi v$VERSION (r$REV); "
1171 . "Net::Twitter v$Net::Twitter::VERSION. "
1173 . JSON::Any::handler()
1174 . ". See details at http://twirssi.com/" );
1177 Irssi::command_bind(
1180 "/twitter_friend <username>",
1182 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1185 Irssi::command_bind(
1188 "/twitter_unfriend <username>",
1190 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1193 Irssi::command_bind( "twitter_updates", "get_updates" );
1194 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1196 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1197 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1199 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1201 my $file = Irssi::settings_get_str("twirssi_replies_store");
1202 if ( $file and -r $file ) {
1203 if ( open( JSON, $file ) ) {
1208 my $ref = JSON::Any->jsonToObj($json);
1210 my $num = keys %{ $id_map{__indexes} };
1211 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1212 $num, ( $num == 1 ? "" : "s" ) );
1215 ¬ice("Failed to load old replies from $file: $!");
1219 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1220 eval "use WWW::Shorten::$provider;";
1224 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1229 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1230 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1237 ->print( "Create a window named "
1238 . Irssi::settings_get_str('twitter_window')
1239 . " or change the value of twitter_window. Then, reload twirssi." );
1242 # vim: set sts=4 expandtab: