10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 476 $' =~ /(\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-16 20:50:17 -0800 (Mon, 16 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");
96 ¬ice("DM sent to $target");
97 $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 $data =~ s/\s\s+/ /g;
122 my ( $username, $data ) = split ' ', $data, 2;
124 unless ( $username and $data ) {
125 ¬ice("Usage: /tweet_as <username> <update>");
129 return unless &valid_username($username);
131 if ( &too_long( $data, 1 )
132 and Irssi::settings_get_str("short_url_provider") )
134 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
136 my $short = makeashorterlink($url);
137 $data =~ s/\Q$url/$short/g;
142 return if &too_long($data);
145 unless ( $twits{$username}->update($data) )
147 ¬ice("Update failed");
153 ¬ice("Update caused an error. Aborted.");
157 foreach ( $data =~ /@([-\w]+)/ ) {
161 my $away = &update_away($data);
163 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
167 my ( $data, $server, $win ) = @_;
169 return unless &logged_in($twit);
171 $data =~ s/^\s+|\s+$//;
173 ¬ice("Usage: /reply <nick[:num]> <update>");
177 $data =~ s/^\s+|\s+$//;
178 my ( $id, $data ) = split ' ', $data, 2;
179 unless ( $id and $data ) {
180 ¬ice("Usage: /reply_as <nick[:num]> <update>");
184 &cmd_reply_as( "$user $id $data", $server, $win );
188 my ( $data, $server, $win ) = @_;
190 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
191 ¬ice("twirssi_track_replies is required in order to reply to "
192 . "specific tweets. Either enable it, or just use /tweet "
193 . "\@username <text>." );
197 return unless &logged_in($twit);
199 $data =~ s/^\s+|\s+$//;
200 my ( $username, $id, $data ) = split ' ', $data, 3;
202 unless ( $username and $data ) {
203 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
207 return unless &valid_username($username);
210 $id =~ s/[^\w\d\-:]+//g;
211 ( $nick, $id ) = split /:/, $id;
212 unless ( exists $id_map{ lc $nick } ) {
213 ¬ice("Can't find a tweet from $nick to reply to!");
217 $id = $id_map{__indexes}{$nick} unless $id;
218 unless ( $id_map{ lc $nick }[$id] ) {
219 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
223 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
225 # remove any @nick at the beginning of the reply, as we'll add it anyway
226 $data =~ s/^\s*\@?$nick\s*//;
227 $data = "\@$nick " . $data;
230 if ( Irssi::settings_get_str("short_url_provider") ) {
231 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
233 my $short = makeashorterlink($url);
234 $data =~ s/\Q$url/$short/g;
239 return if &too_long($data);
243 $twits{$username}->update(
246 in_reply_to_status_id => $id_map{ lc $nick }[$id]
251 ¬ice("Update failed");
257 ¬ice("Update caused an error. Aborted");
261 foreach ( $data =~ /@([-\w]+)/ ) {
265 my $away = &update_away($data);
267 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
271 my ( $usage_str, $api_name, $post_ref ) = @_;
274 my ( $data, $server, $win ) = @_;
276 return unless &logged_in($twit);
278 $data =~ s/^\s+|\s+$//;
280 ¬ice("Usage: $usage_str");
285 unless ( $twit->$api_name($data) )
287 ¬ice("$api_name failed");
293 ¬ice("$api_name caused an error. Aborted.");
297 &$post_ref($data) if $post_ref;
302 my ( $data, $server, $win ) = @_;
304 $data =~ s/^\s+|\s+$//g;
305 if ( exists $twits{$data} ) {
306 ¬ice("Switching to $data");
307 $twit = $twits{$data};
310 ¬ice("Unknown user $data");
315 my ( $data, $server, $win ) = @_;
317 $data =~ s/^\s+|\s+$//g;
318 $data = $user unless $data;
319 return unless &valid_username($data);
321 ¬ice("Logging out $data...");
322 $twits{$data}->end_session();
323 delete $twits{$data};
326 &cmd_switch( ( keys %twits )[0], $server, $win );
328 Irssi::timeout_remove($poll) if $poll;
334 my ( $data, $server, $win ) = @_;
337 ( $user, $pass ) = split ' ', $data, 2;
338 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
339 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
341 my @user = split /\s*,\s*/, $autouser;
342 my @pass = split /\s*,\s*/, $autopass;
343 if ( @user != @pass ) {
344 ¬ice("Number of usernames doesn't match "
345 . "the number of passwords - auto-login failed" );
348 while ( @user and @pass ) {
356 ¬ice("/twitter_login requires either a username and password "
357 . "or twitter_usernames and twitter_passwords to be set." );
361 %friends = %nicks = ();
363 $twit = Net::Twitter->new(
369 unless ( $twit->verify_credentials() ) {
370 ¬ice("Login as $user failed");
373 &cmd_switch( ( keys %twits )[0], $server, $win );
379 my $rate_limit = $twit->rate_limit_status();
380 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
382 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
388 $twits{$user} = $twit;
389 Irssi::timeout_remove($poll) if $poll;
390 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
391 ¬ice("Logged in as $user, loading friends list...");
393 ¬ice( "loaded friends: ", scalar keys %friends );
394 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
395 Irssi::settings_set_bool( "twirssi_first_run", 0 );
396 unless ( exists $friends{twirssi} ) {
397 ¬ice("Welcome to twirssi!"
398 . " Perhaps you should add \@twirssi to your friends list,"
399 . " so you can be notified when a new version is release?"
400 . " Just type /twitter_friend twirssi." );
407 ¬ice("Login failed");
412 my ( $data, $server, $win ) = @_;
414 unless ( $twit and $twit->can('search') ) {
415 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
416 . "doesn't support searches." );
420 $data =~ s/^\s+|\s+$//;
424 ¬ice("Usage: /twitter_subscribe <topic>");
428 if ( exists $id_map{__searches}{$user}{$data} ) {
429 ¬ice("Already had a subscription for '$data'");
433 $id_map{__searches}{$user}{$data} = 1;
434 ¬ice("Added subscription for '$data'");
438 my ( $data, $server, $win ) = @_;
440 unless ( $twit and $twit->can('search') ) {
441 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
442 . "doesn't support searches." );
445 $data =~ s/^\s+|\s+$//;
449 ¬ice("Usage: /twitter_unsubscribe <topic>");
453 unless ( exists $id_map{__searches}{$user}{$data} ) {
454 ¬ice("No subscription found for '$data'");
458 delete $id_map{__searches}{$user}{$data};
459 ¬ice("Removed subscription for '$data'");
462 sub cmd_list_search {
463 my ( $data, $server, $win ) = @_;
466 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
468 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
469 $topics = $topics ? "$topics, $topic" : $topic;
473 ¬ice("Search subscriptions for \@$suser: $topics");
478 ¬ice("No search subscriptions set up");
483 my ( $data, $server, $win ) = @_;
485 my $loc = Irssi::settings_get_str("twirssi_location");
488 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
493 if ( not -x "/usr/bin/md5sum" and not $data ) {
495 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
502 eval { use Digest::MD5; };
506 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
511 $md5 = get("http://twirssi.com/md5sum");
515 ¬ice("Failed to download md5sum from peeron! Aborting.");
519 unless ( open( CUR, $loc ) ) {
521 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
526 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
529 if ( $cur_md5 eq $md5 ) {
530 ¬ice("Current twirssi seems to be up to date.");
535 my $URL = "http://twirssi.com/twirssi.pl";
536 ¬ice("Downloading twirssi from $URL");
537 LWP::Simple::getstore( $URL, "$loc.upgrade" );
540 unless ( open( NEW, "$loc.upgrade" ) ) {
542 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
547 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
550 if ( $new_md5 ne $md5 ) {
551 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
556 rename $loc, "$loc.backup"
557 or ¬ice("Failed to back up $loc: $!. Aborting")
559 rename "$loc.upgrade", $loc
560 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
563 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
564 if ( -e "$dir/autorun/$file" ) {
565 ¬ice("Updating $dir/autorun/$file");
566 unlink "$dir/autorun/$file"
567 or ¬ice("Failed to remove old $file from autorun: $!");
568 symlink "../$file", "$dir/autorun/$file"
569 or ¬ice("Failed to create symlink in autorun directory: $!");
572 ¬ice("Download complete. Reload twirssi with /script load $file");
582 print $fh "type:debug Loading friends page $page...\n"
583 if ( $fh and &debug );
584 my $friends = $twit->friends( { page => $page } );
585 last unless $friends;
586 $new_friends{ $_->{screen_name} } = time foreach @$friends;
588 last if @$friends == 0 or $page == 10;
593 print $fh "type:debug Error during friends list update. Aborted.\n";
597 my ( $added, $removed ) = ( 0, 0 );
598 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
599 foreach ( keys %new_friends ) {
600 next if exists $friends{$_};
605 print $fh "type:debug Scanning for removed friends...\n"
606 if ( $fh and &debug );
607 foreach ( keys %friends ) {
608 next if exists $new_friends{$_};
613 return ( $added, $removed );
617 print scalar localtime, " - get_updates starting" if &debug;
620 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
623 ->print( "Can't find a window named '"
624 . Irssi::settings_get_str('twitter_window')
625 . "'. Create it or change the value of twitter_window" );
628 return unless &logged_in($twit);
630 my ( $fh, $filename ) = File::Temp::tempfile();
634 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
635 Irssi::pidwait_add($pid);
636 } elsif ( defined $pid ) { # child
644 $error += &do_updates( $fh, $user, $twit );
645 foreach ( keys %twits ) {
647 $error += &do_updates( $fh, $_, $twits{$_} );
650 my ( $added, $removed ) = &load_friends($fh);
651 if ( $added + $removed ) {
652 print $fh "type:debug %R***%n Friends list updated: ",
654 sprintf( "%d added", $added ),
655 sprintf( "%d removed", $removed ) ),
658 print $fh "__friends__\n";
659 foreach ( sort keys %friends ) {
660 print $fh "$_ $friends{$_}\n";
664 print $fh "type:debug Update encountered errors. Aborted\n";
665 print $fh $last_poll;
672 print scalar localtime, " - get_updates ends" if &debug;
676 my ( $fh, $username, $obj ) = @_;
678 my $rate_limit = $obj->rate_limit_status();
679 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
680 ¬ice("Rate limit exceeded for $username");
684 print scalar localtime, " - Polling for updates for $username" if &debug;
687 $tweets = $obj->friends_timeline(
688 { since => HTTP::Date::time2str($last_poll) } );
692 print $fh "type:debug Error during friends_timeline call. Aborted.\n";
696 unless ( ref $tweets ) {
697 if ( $obj->can("get_error") ) {
699 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
700 if ($@) { $error = $obj->get_error() }
701 print $fh "type:debug API Error during friends_timeline call: ",
705 "type:debug API Error during friends_timeline call. Aborted.\n";
710 foreach my $t ( reverse @$tweets ) {
711 my $text = decode_entities( $t->{text} );
712 $text = &hilight($text);
714 if ( Irssi::settings_get_bool("show_reply_context")
715 and $t->{in_reply_to_screen_name} ne $username
716 and $t->{in_reply_to_screen_name}
717 and not exists $friends{ $t->{in_reply_to_screen_name} } )
719 $nicks{ $t->{in_reply_to_screen_name} } = time;
722 $context = $obj->show_status( $t->{in_reply_to_status_id} );
726 my $ctext = decode_entities( $context->{text} );
727 $ctext = &hilight($ctext);
728 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
729 $context->{id}, $username,
730 $context->{user}{screen_name}, $ctext;
733 print $fh "type:debug request to get context failed: $@";
736 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
741 if $t->{user}{screen_name} eq $username
742 and not Irssi::settings_get_bool("show_own_tweets");
743 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
744 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
747 print scalar localtime, " - Polling for replies" if &debug;
749 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
754 print $fh "type:debug Error during replies call. Aborted.\n";
758 foreach my $t ( reverse @$tweets ) {
760 if exists $friends{ $t->{user}{screen_name} };
762 my $text = decode_entities( $t->{text} );
763 $text = &hilight($text);
764 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
765 $t->{id}, $username, $t->{user}{screen_name}, $text;
768 print scalar localtime, " - Polling for DMs" if &debug;
771 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
776 print $fh "type:debug Error during direct_messages call. Aborted.\n";
780 foreach my $t ( reverse @$tweets ) {
781 my $text = decode_entities( $t->{text} );
782 $text = &hilight($text);
783 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
784 $t->{id}, $username, $t->{sender_screen_name}, $text;
787 print scalar localtime, " - Polling for subscriptions" if &debug;
788 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
790 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
791 print $fh "type:debug searching for $topic since ",
792 "$id_map{__searches}{$username}{$topic}\n";
794 $search = $obj->search(
797 since_id => $id_map{__searches}{$username}{$topic}
804 "type:debug Error during search($topic) call. Aborted.\n";
808 unless ( $search->{max_id} ) {
810 "type:debug Invalid search results when searching for $topic.",
815 $id_map{__searches}{$username}{$topic} = $search->{max_id};
816 printf $fh "id:%d account:%s type:searchid topic:%s\n",
817 $search->{max_id}, $username, $topic;
819 foreach my $t ( reverse @{ $search->{results} } ) {
820 my $text = decode_entities( $t->{text} );
821 $text = &hilight($text);
822 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
823 $t->{id}, $username, $t->{from_user}, $topic, $text;
828 print scalar localtime, " - Done" if &debug;
835 my $filename = $data->[0];
836 my $attempt = $data->[1];
838 print scalar localtime, " - checking child log at $filename ($attempt)"
841 if ( open FILE, $filename ) {
845 last if /^__friends__/;
848 foreach my $key (qw/id account nick type topic/) {
849 if (s/^$key:(\S+)\s*//) {
854 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
855 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
856 $tweet_cache{ $meta{id} } = time;
860 if ( $meta{account} ne $user ) {
861 $account = "$meta{account}: ";
865 if ( $meta{type} ne 'dm'
866 and Irssi::settings_get_bool("twirssi_track_replies")
870 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
871 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
872 $id_map{__indexes}{ $meta{nick} } = $marker;
873 $marker = ":$marker";
877 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
878 if ( ( $_ =~ /\@$meta{account}\W/i )
879 && Irssi::settings_get_bool("twirssi_hilights") )
881 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
882 $hilight = MSGLEVEL_HILIGHT;
885 if ( $meta{type} =~ /tweet|reply/ ) {
888 ( MSGLEVEL_PUBLIC | $hilight ),
889 $meta{type}, $account, $meta{nick}, $marker, $_
891 } elsif ( $meta{type} eq 'search' ) {
894 ( MSGLEVEL_PUBLIC | $hilight ),
895 $meta{type}, $account, $meta{topic},
896 $meta{nick}, $marker, $_
899 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
901 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
904 } elsif ( $meta{type} eq 'dm' ) {
907 ( MSGLEVEL_MSGS | $hilight ),
908 $meta{type}, $account, $meta{nick}, $_
910 } elsif ( $meta{type} eq 'searchid' ) {
911 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
913 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
915 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
918 print "Search '$meta{topic}' returned invalid id $meta{id}";
920 } elsif ( $meta{type} eq 'error' ) {
921 push @lines, [ MSGLEVEL_MSGS, $_ ];
922 } elsif ( $meta{type} eq 'debug' ) {
923 print "$_" if &debug,;
925 print "Unknown line type $meta{type}: $_" if &debug,;
935 my ( $f, $t ) = split ' ', $_;
936 $nicks{$f} = $friends{$f} = $t;
939 if ($new_last_poll) {
940 print "new last_poll = $new_last_poll" if &debug;
941 for my $line (@lines) {
942 $window->printformat(
944 "twirssi_" . $line->[1],
945 @$line[ 2 .. $#$line ]
951 or warn "Failed to remove $filename: $!"
954 # keep enough cached tweets, to make sure we don't show duplicates.
955 foreach ( keys %tweet_cache ) {
956 next if $tweet_cache{$_} >= $last_poll;
957 delete $tweet_cache{$_};
959 $last_poll = $new_last_poll;
964 Irssi::settings_get_str("twirssi_replies_store") )
966 if ( open JSON, ">$file" ) {
967 print JSON JSON::Any->objToJson( \%id_map );
970 ¬ice("Failed to write replies to $file: $!");
979 if ( $attempt < 12 ) {
980 Irssi::timeout_add_once( 5000, 'monitor_child',
981 [ $filename, $attempt + 1 ] );
983 ¬ice("Giving up on polling $filename");
984 unlink $filename unless &debug;
989 return Irssi::settings_get_bool("twirssi_debug");
993 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
999 if ( Irssi::settings_get_bool("tweet_to_away")
1001 and $data !~ /^[dD] / )
1004 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1006 $server->send_raw("away :$data");
1009 ¬ice( "Can't find bitlbee server.",
1010 "Update bitlbee_server or disable tweet_to_away" );
1020 my $noalert = shift;
1022 if ( length $data > 140 ) {
1023 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1031 sub valid_username {
1032 my $username = shift;
1034 unless ( exists $twits{$username} ) {
1035 ¬ice("Unknown username $username");
1045 ¬ice("Not logged in! Use /twitter_login username pass!");
1053 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1056 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1057 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1058 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1060 { # /twitter_reply gets a nick:num
1062 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1063 sort keys %{ $id_map{__indexes} };
1066 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1068 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1069 my $prefix = $word =~ s/^@//;
1070 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1071 push @$complist, grep /^\Q$word/i,
1072 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1073 @$complist = map { "\@$_" } @$complist if $prefix;
1077 sub event_send_text {
1078 my ( $line, $server, $win ) = @_;
1079 my $awin = Irssi::active_win();
1081 # if the window where we got our text was the twitter window, and the user
1082 # wants to be lazy, tweet away!
1083 if ( ( $awin->get_active_name() eq $window->{name} )
1084 and Irssi::settings_get_bool("tweet_window_input") )
1086 &cmd_tweet( $line, $server, $win );
1091 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1092 return $poll if $poll >= 60;
1099 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1100 my $c = Irssi::settings_get_str("twirssi_nick_color");
1101 $c = $irssi_to_mirc_colors{$c};
1102 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1104 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1105 my $c = Irssi::settings_get_str("twirssi_topic_color");
1106 $c = $irssi_to_mirc_colors{$c};
1107 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1109 $text =~ s/[\n\r]/ /g;
1114 Irssi::signal_add( "send text", "event_send_text" );
1116 Irssi::theme_register(
1118 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1119 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1120 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1121 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1122 'twirssi_error', 'ERROR: $0',
1126 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1127 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1128 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1129 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1130 Irssi::settings_add_str( "twirssi", "twirssi_location",
1131 ".irssi/scripts/twirssi.pl" );
1132 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1133 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1134 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1135 ".irssi/scripts/twirssi.json" );
1136 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1137 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1138 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1139 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1140 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1141 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1142 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1143 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1144 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1145 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1146 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1147 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1149 $last_poll = time - &get_poll_time;
1150 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1153 Irssi::Windowitem::window_create(
1154 Irssi::settings_get_str('twitter_window'), 1 );
1155 $window->set_name( Irssi::settings_get_str('twitter_window') );
1159 Irssi::command_bind( "dm", "cmd_direct" );
1160 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1161 Irssi::command_bind( "tweet", "cmd_tweet" );
1162 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1163 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1164 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1165 Irssi::command_bind( "twitter_login", "cmd_login" );
1166 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1167 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1168 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1169 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1170 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1171 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1172 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1173 Irssi::command_bind( "reply", "cmd_reply" );
1174 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1176 Irssi::command_bind(
1179 print "twits: ", join ", ",
1180 map { "u: $_->{username}" } values %twits;
1181 print "friends: ", join ", ", sort keys %friends;
1182 print "nicks: ", join ", ", sort keys %nicks;
1183 print "searches: ", Dumper \%{ $id_map{__searches} };
1184 print "last poll: $last_poll";
1187 Irssi::command_bind(
1190 ¬ice("Twirssi v$VERSION (r$REV); "
1191 . "Net::Twitter v$Net::Twitter::VERSION. "
1193 . JSON::Any::handler()
1194 . ". See details at http://twirssi.com/" );
1197 Irssi::command_bind(
1200 "/twitter_friend <username>",
1202 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1205 Irssi::command_bind(
1208 "/twitter_unfriend <username>",
1210 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1213 Irssi::command_bind( "twitter_updates", "get_updates" );
1214 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1216 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1217 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1219 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1221 my $file = Irssi::settings_get_str("twirssi_replies_store");
1222 if ( $file and -r $file ) {
1223 if ( open( JSON, $file ) ) {
1228 my $ref = JSON::Any->jsonToObj($json);
1230 my $num = keys %{ $id_map{__indexes} };
1231 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1232 $num, ( $num == 1 ? "" : "s" ) );
1235 ¬ice("Failed to load old replies from $file: $!");
1239 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1240 eval "use WWW::Shorten::$provider;";
1244 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1249 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1250 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1257 ->print( "Create a window named "
1258 . Irssi::settings_get_str('twitter_window')
1259 . " or change the value of twitter_window. Then, reload twirssi." );
1262 # vim: set sts=4 expandtab: