10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 490 $' =~ /(\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-25 10:21:27 -0800 (Wed, 25 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");
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;
348 my ( $data, $server, $win ) = @_;
351 ( $user, $pass ) = split ' ', $data, 2;
352 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
353 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
355 my @user = split /\s*,\s*/, $autouser;
356 my @pass = split /\s*,\s*/, $autopass;
357 if ( @user != @pass ) {
358 ¬ice("Number of usernames doesn't match "
359 . "the number of passwords - auto-login failed" );
362 while ( @user and @pass ) {
370 ¬ice("/twitter_login requires either a username and password "
371 . "or twitter_usernames and twitter_passwords to be set." );
375 %friends = %nicks = ();
377 $twit = Net::Twitter->new(
383 unless ( $twit->verify_credentials() ) {
384 ¬ice("Login as $user failed");
387 &cmd_switch( ( keys %twits )[0], $server, $win );
393 my $rate_limit = $twit->rate_limit_status();
394 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
396 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
402 $twits{$user} = $twit;
403 Irssi::timeout_remove($poll) if $poll;
404 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
405 ¬ice("Logged in as $user, loading friends list...");
407 ¬ice( "loaded friends: ", scalar keys %friends );
408 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
409 Irssi::settings_set_bool( "twirssi_first_run", 0 );
410 unless ( exists $friends{twirssi} ) {
411 ¬ice("Welcome to twirssi!"
412 . " Perhaps you should add \@twirssi to your friends list,"
413 . " so you can be notified when a new version is release?"
414 . " Just type /twitter_friend twirssi." );
421 ¬ice("Login failed");
426 my ( $data, $server, $win ) = @_;
428 unless ( $twit and $twit->can('search') ) {
429 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
430 . "doesn't support searches." );
434 $data =~ s/^\s+|\s+$//;
438 ¬ice("Usage: /twitter_subscribe <topic>");
442 if ( exists $id_map{__searches}{$user}{$data} ) {
443 ¬ice("Already had a subscription for '$data'");
447 $id_map{__searches}{$user}{$data} = 1;
448 ¬ice("Added subscription for '$data'");
452 my ( $data, $server, $win ) = @_;
454 unless ( $twit and $twit->can('search') ) {
455 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
456 . "doesn't support searches." );
459 $data =~ s/^\s+|\s+$//;
463 ¬ice("Usage: /twitter_unsubscribe <topic>");
467 unless ( exists $id_map{__searches}{$user}{$data} ) {
468 ¬ice("No subscription found for '$data'");
472 delete $id_map{__searches}{$user}{$data};
473 ¬ice("Removed subscription for '$data'");
476 sub cmd_list_search {
477 my ( $data, $server, $win ) = @_;
480 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
482 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
483 $topics = $topics ? "$topics, $topic" : $topic;
487 ¬ice("Search subscriptions for \@$suser: $topics");
492 ¬ice("No search subscriptions set up");
497 my ( $data, $server, $win ) = @_;
499 my $loc = Irssi::settings_get_str("twirssi_location");
502 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
509 eval { use Digest::MD5; };
513 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
518 $md5 = get("http://twirssi.com/md5sum");
522 ¬ice("Failed to download md5sum from peeron! Aborting.");
526 unless ( open( CUR, $loc ) ) {
528 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
533 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
536 if ( $cur_md5 eq $md5 ) {
537 ¬ice("Current twirssi seems to be up to date.");
542 my $URL = "http://twirssi.com/twirssi.pl";
543 ¬ice("Downloading twirssi from $URL");
544 LWP::Simple::getstore( $URL, "$loc.upgrade" );
547 unless ( open( NEW, "$loc.upgrade" ) ) {
549 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
554 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
557 if ( $new_md5 ne $md5 ) {
558 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
563 rename $loc, "$loc.backup"
564 or ¬ice("Failed to back up $loc: $!. Aborting")
566 rename "$loc.upgrade", $loc
567 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
570 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
571 if ( -e "$dir/autorun/$file" ) {
572 ¬ice("Updating $dir/autorun/$file");
573 unlink "$dir/autorun/$file"
574 or ¬ice("Failed to remove old $file from autorun: $!");
575 symlink "../$file", "$dir/autorun/$file"
576 or ¬ice("Failed to create symlink in autorun directory: $!");
579 ¬ice("Download complete. Reload twirssi with /script load $file");
589 print $fh "type:debug Loading friends page $page...\n"
590 if ( $fh and &debug );
591 my $friends = $twit->friends( { page => $page } );
592 last unless $friends;
593 $new_friends{ $_->{screen_name} } = time foreach @$friends;
595 last if @$friends == 0 or $page == 10;
600 print $fh "type:debug Error during friends list update. Aborted.\n";
604 my ( $added, $removed ) = ( 0, 0 );
605 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
606 foreach ( keys %new_friends ) {
607 next if exists $friends{$_};
612 print $fh "type:debug Scanning for removed friends...\n"
613 if ( $fh and &debug );
614 foreach ( keys %friends ) {
615 next if exists $new_friends{$_};
620 return ( $added, $removed );
624 print scalar localtime, " - get_updates starting" if &debug;
627 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
630 ->print( "Can't find a window named '"
631 . Irssi::settings_get_str('twitter_window')
632 . "'. Create it or change the value of twitter_window" );
635 return unless &logged_in($twit);
637 my ( $fh, $filename ) = File::Temp::tempfile();
638 binmode( $fh, ":utf8" );
642 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
643 Irssi::pidwait_add($pid);
644 } elsif ( defined $pid ) { # child
652 $error += &do_updates( $fh, $user, $twit );
653 foreach ( keys %twits ) {
655 $error += &do_updates( $fh, $_, $twits{$_} );
658 my ( $added, $removed ) = &load_friends($fh);
659 if ( $added + $removed ) {
660 print $fh "type:debug %R***%n Friends list updated: ",
662 sprintf( "%d added", $added ),
663 sprintf( "%d removed", $removed ) ),
666 print $fh "__friends__\n";
667 foreach ( sort keys %friends ) {
668 print $fh "$_ $friends{$_}\n";
672 print $fh "type:debug Update encountered errors. Aborted\n";
673 print $fh $last_poll;
680 print scalar localtime, " - get_updates ends" if &debug;
684 my ( $fh, $username, $obj ) = @_;
686 my $rate_limit = $obj->rate_limit_status();
687 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
688 ¬ice("Rate limit exceeded for $username");
692 print scalar localtime, " - Polling for updates for $username" if &debug;
694 eval { $tweets = $obj->friends_timeline(); };
698 "type:debug Error during friends_timeline call: $@. Aborted.\n";
702 unless ( ref $tweets ) {
703 if ( $obj->can("get_error") ) {
705 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
706 if ($@) { $error = $obj->get_error() }
707 print $fh "type:debug API Error during friends_timeline call: ",
711 "type:debug API Error during friends_timeline call. Aborted.\n";
716 foreach my $t ( reverse @$tweets ) {
717 my $text = decode_entities( $t->{text} );
718 $text = &hilight($text);
720 if ( Irssi::settings_get_bool("show_reply_context")
721 and $t->{in_reply_to_screen_name} ne $username
722 and $t->{in_reply_to_screen_name}
723 and not exists $friends{ $t->{in_reply_to_screen_name} } )
725 $nicks{ $t->{in_reply_to_screen_name} } = time;
728 $context = $obj->show_status( $t->{in_reply_to_status_id} );
732 my $ctext = decode_entities( $context->{text} );
733 $ctext = &hilight($ctext);
734 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
735 $context->{id}, $username,
736 $context->{user}{screen_name}, $ctext;
739 print $fh "type:debug request to get context failed: $@";
742 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
747 if $t->{user}{screen_name} eq $username
748 and not Irssi::settings_get_bool("show_own_tweets");
749 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
750 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
753 print scalar localtime, " - Polling for replies" if &debug;
755 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
760 print $fh "type:debug Error during replies call. Aborted.\n";
764 foreach my $t ( reverse @$tweets ) {
766 if exists $friends{ $t->{user}{screen_name} };
768 my $text = decode_entities( $t->{text} );
769 $text = &hilight($text);
770 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
771 $t->{id}, $username, $t->{user}{screen_name}, $text;
774 print scalar localtime, " - Polling for DMs" if &debug;
777 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
782 print $fh "type:debug Error during direct_messages call. Aborted.\n";
786 foreach my $t ( reverse @$tweets ) {
787 my $text = decode_entities( $t->{text} );
788 $text = &hilight($text);
789 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
790 $t->{id}, $username, $t->{sender_screen_name}, $text;
793 print scalar localtime, " - Polling for subscriptions" if &debug;
794 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
796 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
797 print $fh "type:debug searching for $topic since ",
798 "$id_map{__searches}{$username}{$topic}\n";
800 $search = $obj->search(
803 since_id => $id_map{__searches}{$username}{$topic}
810 "type:debug Error during search($topic) call. Aborted.\n";
814 unless ( $search->{max_id} ) {
816 "type:debug Invalid search results when searching for $topic.",
821 $id_map{__searches}{$username}{$topic} = $search->{max_id};
822 printf $fh "id:%d account:%s type:searchid topic:%s\n",
823 $search->{max_id}, $username, $topic;
825 foreach my $t ( reverse @{ $search->{results} } ) {
826 my $text = decode_entities( $t->{text} );
827 $text = &hilight($text);
828 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
829 $t->{id}, $username, $t->{from_user}, $topic, $text;
834 print scalar localtime, " - Done" if &debug;
841 my $filename = $data->[0];
842 my $attempt = $data->[1];
844 print scalar localtime, " - checking child log at $filename ($attempt)"
848 # first time we run we don't want to print out *everything*, so we just
851 $suppress = 1 unless keys %tweet_cache;
853 if ( open FILE, $filename ) {
858 last if /^__friends__/;
861 foreach my $key (qw/id account nick type topic/) {
862 if (s/^$key:(\S+)\s*//) {
867 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
868 $new_cache{ $meta{id} } = time;
870 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
876 if ( $meta{account} ne $user ) {
877 $account = "$meta{account}: ";
881 if ( $meta{type} ne 'dm'
882 and Irssi::settings_get_bool("twirssi_track_replies")
886 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
887 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
888 $id_map{__indexes}{ $meta{nick} } = $marker;
889 $marker = ":$marker";
893 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
894 if ( ( $_ =~ /\@$meta{account}\W/i )
895 && Irssi::settings_get_bool("twirssi_hilights") )
897 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
898 $hilight = MSGLEVEL_HILIGHT;
901 if ( $meta{type} =~ /tweet|reply/ ) {
904 ( MSGLEVEL_PUBLIC | $hilight ),
905 $meta{type}, $account, $meta{nick}, $marker, $_
907 } elsif ( $meta{type} eq 'search' ) {
910 ( MSGLEVEL_PUBLIC | $hilight ),
911 $meta{type}, $account, $meta{topic},
912 $meta{nick}, $marker, $_
915 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
917 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
919 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
922 } elsif ( $meta{type} eq 'dm' ) {
925 ( MSGLEVEL_MSGS | $hilight ),
926 $meta{type}, $account, $meta{nick}, $_
928 } elsif ( $meta{type} eq 'searchid' ) {
929 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
931 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
933 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
935 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
938 print "Search '$meta{topic}' returned invalid id $meta{id}";
940 } elsif ( $meta{type} eq 'error' ) {
941 push @lines, [ MSGLEVEL_MSGS, $_ ];
942 } elsif ( $meta{type} eq 'debug' ) {
943 print "$_" if &debug,;
945 print "Unknown line type $meta{type}: $_" if &debug,;
955 my ( $f, $t ) = split ' ', $_;
956 $nicks{$f} = $friends{$f} = $t;
959 if ($new_last_poll) {
960 print "new last_poll = $new_last_poll" if &debug;
962 print "First call, not printing updates" if &debug;
964 foreach my $line (@lines) {
965 $window->printformat(
967 "twirssi_" . $line->[1],
968 @$line[ 2 .. $#$line ]
975 or warn "Failed to remove $filename: $!"
978 # commit the pending cache lines to the actual cache, now that
979 # we've printed our output
980 %tweet_cache = ( %tweet_cache, %new_cache );
982 # keep enough cached tweets, to make sure we don't show duplicates.
983 foreach ( keys %tweet_cache ) {
984 next if $tweet_cache{$_} >= $last_poll - 3600;
985 delete $tweet_cache{$_};
987 $last_poll = $new_last_poll;
992 Irssi::settings_get_str("twirssi_replies_store") )
994 if ( open JSON, ">$file" ) {
995 print JSON JSON::Any->objToJson( \%id_map );
998 ¬ice("Failed to write replies to $file: $!");
1008 if ( $attempt < 24 ) {
1009 Irssi::timeout_add_once( 5000, 'monitor_child',
1010 [ $filename, $attempt + 1 ] );
1012 print "Giving up on polling $filename" if &debug;
1013 unlink $filename unless &debug;
1015 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1018 my @time = localtime($last_poll);
1019 if ( time - $last_poll < 24 * 60 * 60 ) {
1020 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1022 $since = scalar localtime($last_poll);
1025 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1030 q{ .--./ / | _.---.| },
1041 ¬ice("Haven't been able to get updated tweets since $since");
1046 return Irssi::settings_get_bool("twirssi_debug");
1050 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1056 if ( Irssi::settings_get_bool("tweet_to_away")
1058 and $data !~ /^[dD] / )
1061 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1063 $server->send_raw("away :$data");
1066 ¬ice( "Can't find bitlbee server.",
1067 "Update bitlbee_server or disable tweet_to_away" );
1077 my $noalert = shift;
1079 if ( length $data > 140 ) {
1080 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1088 sub valid_username {
1089 my $username = shift;
1091 unless ( exists $twits{$username} ) {
1092 ¬ice("Unknown username $username");
1102 ¬ice("Not logged in! Use /twitter_login username pass!");
1110 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1113 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1114 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1115 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1117 { # /twitter_reply gets a nick:num
1119 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1120 sort keys %{ $id_map{__indexes} };
1123 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1125 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1126 my $prefix = $word =~ s/^@//;
1127 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1128 push @$complist, grep /^\Q$word/i,
1129 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1130 @$complist = map { "\@$_" } @$complist if $prefix;
1134 sub event_send_text {
1135 my ( $line, $server, $win ) = @_;
1136 my $awin = Irssi::active_win();
1138 # if the window where we got our text was the twitter window, and the user
1139 # wants to be lazy, tweet away!
1140 if ( ( $awin->get_active_name() eq $window->{name} )
1141 and Irssi::settings_get_bool("tweet_window_input") )
1143 &cmd_tweet( $line, $server, $win );
1148 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1149 return $poll if $poll >= 60;
1156 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1157 my $c = Irssi::settings_get_str("twirssi_nick_color");
1158 $c = $irssi_to_mirc_colors{$c};
1159 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1161 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1162 my $c = Irssi::settings_get_str("twirssi_topic_color");
1163 $c = $irssi_to_mirc_colors{$c};
1164 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1166 $text =~ s/[\n\r]/ /g;
1171 Irssi::signal_add( "send text", "event_send_text" );
1173 Irssi::theme_register(
1175 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1176 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1177 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1178 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1179 'twirssi_error', 'ERROR: $0',
1183 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1184 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1185 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1186 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1187 Irssi::settings_add_str( "twirssi", "twirssi_location",
1188 ".irssi/scripts/twirssi.pl" );
1189 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1190 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1191 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1192 ".irssi/scripts/twirssi.json" );
1193 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1194 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1195 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1196 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1197 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1198 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1199 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1200 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1201 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1202 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1203 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1204 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1205 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1207 $last_poll = time - &get_poll_time;
1208 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1211 Irssi::Windowitem::window_create(
1212 Irssi::settings_get_str('twitter_window'), 1 );
1213 $window->set_name( Irssi::settings_get_str('twitter_window') );
1217 Irssi::command_bind( "dm", "cmd_direct" );
1218 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1219 Irssi::command_bind( "tweet", "cmd_tweet" );
1220 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1221 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1222 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1223 Irssi::command_bind( "twitter_login", "cmd_login" );
1224 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1225 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1226 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1227 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1228 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1229 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1230 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1231 Irssi::command_bind( "reply", "cmd_reply" );
1232 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1234 Irssi::command_bind(
1237 print "twits: ", join ", ",
1238 map { "u: $_->{username}" } values %twits;
1239 print "friends: ", join ", ", sort keys %friends;
1240 print "nicks: ", join ", ", sort keys %nicks;
1241 print "searches: ", Dumper \%{ $id_map{__searches} };
1242 print "last poll: $last_poll";
1243 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1244 print DUMP Dumper \%tweet_cache;
1246 print "cache written out to /tmp/twirssi.cache.txt";
1250 Irssi::command_bind(
1253 ¬ice("Twirssi v$VERSION (r$REV); "
1254 . "Net::Twitter v$Net::Twitter::VERSION. "
1256 . JSON::Any::handler()
1257 . ". See details at http://twirssi.com/" );
1260 Irssi::command_bind(
1263 "/twitter_friend <username>",
1265 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1268 Irssi::command_bind(
1271 "/twitter_unfriend <username>",
1273 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1276 Irssi::command_bind( "twitter_updates", "get_updates" );
1277 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1279 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1280 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1282 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1284 my $file = Irssi::settings_get_str("twirssi_replies_store");
1285 if ( $file and -r $file ) {
1286 if ( open( JSON, $file ) ) {
1291 my $ref = JSON::Any->jsonToObj($json);
1293 my $num = keys %{ $id_map{__indexes} };
1294 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1295 $num, ( $num == 1 ? "" : "s" ) );
1299 ¬ice("Failed to load old replies from $file: $!");
1303 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1304 eval "use WWW::Shorten::$provider;";
1308 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1313 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1314 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1322 ->print( "Create a window named "
1323 . Irssi::settings_get_str('twitter_window')
1324 . " or change the value of twitter_window. Then, reload twirssi." );
1327 # vim: set sts=4 expandtab: