10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 483 $' =~ /(\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-20 14:37:28 -0800 (Fri, 20 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 if ( $twits{$username}
85 ->new_direct_message( { user => $target, text => $text } ) )
87 ¬ice("DM sent to $target");
88 $nicks{$target} = time;
92 $error = JSON::Any->jsonToObj( $twits{$username}->get_error() );
93 $error = $error->{error};
96 ¬ice("DM to $target failed");
101 ¬ice("DM caused an error: $@");
107 my ( $data, $server, $win ) = @_;
109 return unless &logged_in($twit);
111 $data =~ s/^\s+|\s+$//;
113 ¬ice("Usage: /tweet <update>");
117 &cmd_tweet_as( "$user $data", $server, $win );
121 my ( $data, $server, $win ) = @_;
123 return unless &logged_in($twit);
125 $data =~ s/^\s+|\s+$//;
126 $data =~ s/\s\s+/ /g;
127 my ( $username, $data ) = split ' ', $data, 2;
129 unless ( $username and $data ) {
130 ¬ice("Usage: /tweet_as <username> <update>");
134 return unless &valid_username($username);
136 if ( &too_long( $data, 1 )
137 and Irssi::settings_get_str("short_url_provider") )
139 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
141 my $short = makeashorterlink($url);
142 $data =~ s/\Q$url/$short/g;
147 return if &too_long($data);
151 unless ( $twits{$username}->update($data) )
153 ¬ice("Update failed");
157 return unless $success;
160 ¬ice("Update caused an error: $@. Aborted.");
164 foreach ( $data =~ /@([-\w]+)/ ) {
168 my $away = &update_away($data);
170 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
174 my ( $data, $server, $win ) = @_;
176 return unless &logged_in($twit);
178 $data =~ s/^\s+|\s+$//;
180 ¬ice("Usage: /reply <nick[:num]> <update>");
184 $data =~ s/^\s+|\s+$//;
185 my ( $id, $data ) = split ' ', $data, 2;
186 unless ( $id and $data ) {
187 ¬ice("Usage: /reply_as <nick[:num]> <update>");
191 &cmd_reply_as( "$user $id $data", $server, $win );
195 my ( $data, $server, $win ) = @_;
197 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
198 ¬ice("twirssi_track_replies is required in order to reply to "
199 . "specific tweets. Either enable it, or just use /tweet "
200 . "\@username <text>." );
204 return unless &logged_in($twit);
206 $data =~ s/^\s+|\s+$//;
207 my ( $username, $id, $data ) = split ' ', $data, 3;
209 unless ( $username and $data ) {
210 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
214 return unless &valid_username($username);
217 $id =~ s/[^\w\d\-:]+//g;
218 ( $nick, $id ) = split /:/, $id;
219 unless ( exists $id_map{ lc $nick } ) {
220 ¬ice("Can't find a tweet from $nick to reply to!");
224 $id = $id_map{__indexes}{$nick} unless $id;
225 unless ( $id_map{ lc $nick }[$id] ) {
226 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
230 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
232 # remove any @nick at the beginning of the reply, as we'll add it anyway
233 $data =~ s/^\s*\@?$nick\s*//;
234 $data = "\@$nick " . $data;
237 if ( &too_long( $data, 1 ) ) {
238 if ( Irssi::settings_get_str("short_url_provider") ) {
239 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
241 my $short = makeashorterlink($url);
242 $data =~ s/\Q$url/$short/g;
248 return if &too_long($data);
253 $twits{$username}->update(
256 in_reply_to_status_id => $id_map{ lc $nick }[$id]
261 ¬ice("Update failed");
265 return unless $success;
268 ¬ice("Update caused an error: $@. Aborted");
272 foreach ( $data =~ /@([-\w]+)/ ) {
276 my $away = &update_away($data);
278 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
282 my ( $usage_str, $api_name, $post_ref ) = @_;
285 my ( $data, $server, $win ) = @_;
287 return unless &logged_in($twit);
289 $data =~ s/^\s+|\s+$//;
291 ¬ice("Usage: $usage_str");
297 unless ( $twit->$api_name($data) )
299 ¬ice("$api_name failed");
303 return unless $success;
306 ¬ice("$api_name caused an error. Aborted.");
310 &$post_ref($data) if $post_ref;
315 my ( $data, $server, $win ) = @_;
317 $data =~ s/^\s+|\s+$//g;
318 if ( exists $twits{$data} ) {
319 ¬ice("Switching to $data");
320 $twit = $twits{$data};
323 ¬ice("Unknown user $data");
328 my ( $data, $server, $win ) = @_;
330 $data =~ s/^\s+|\s+$//g;
331 $data = $user unless $data;
332 return unless &valid_username($data);
334 ¬ice("Logging out $data...");
335 $twits{$data}->end_session();
336 delete $twits{$data};
339 &cmd_switch( ( keys %twits )[0], $server, $win );
341 Irssi::timeout_remove($poll) if $poll;
347 my ( $data, $server, $win ) = @_;
350 ( $user, $pass ) = split ' ', $data, 2;
351 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
352 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
354 my @user = split /\s*,\s*/, $autouser;
355 my @pass = split /\s*,\s*/, $autopass;
356 if ( @user != @pass ) {
357 ¬ice("Number of usernames doesn't match "
358 . "the number of passwords - auto-login failed" );
361 while ( @user and @pass ) {
369 ¬ice("/twitter_login requires either a username and password "
370 . "or twitter_usernames and twitter_passwords to be set." );
374 %friends = %nicks = ();
376 $twit = Net::Twitter->new(
382 unless ( $twit->verify_credentials() ) {
383 ¬ice("Login as $user failed");
386 &cmd_switch( ( keys %twits )[0], $server, $win );
392 my $rate_limit = $twit->rate_limit_status();
393 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
395 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
401 $twits{$user} = $twit;
402 Irssi::timeout_remove($poll) if $poll;
403 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
404 ¬ice("Logged in as $user, loading friends list...");
406 ¬ice( "loaded friends: ", scalar keys %friends );
407 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
408 Irssi::settings_set_bool( "twirssi_first_run", 0 );
409 unless ( exists $friends{twirssi} ) {
410 ¬ice("Welcome to twirssi!"
411 . " Perhaps you should add \@twirssi to your friends list,"
412 . " so you can be notified when a new version is release?"
413 . " Just type /twitter_friend twirssi." );
420 ¬ice("Login failed");
425 my ( $data, $server, $win ) = @_;
427 unless ( $twit and $twit->can('search') ) {
428 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
429 . "doesn't support searches." );
433 $data =~ s/^\s+|\s+$//;
437 ¬ice("Usage: /twitter_subscribe <topic>");
441 if ( exists $id_map{__searches}{$user}{$data} ) {
442 ¬ice("Already had a subscription for '$data'");
446 $id_map{__searches}{$user}{$data} = 1;
447 ¬ice("Added subscription for '$data'");
451 my ( $data, $server, $win ) = @_;
453 unless ( $twit and $twit->can('search') ) {
454 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
455 . "doesn't support searches." );
458 $data =~ s/^\s+|\s+$//;
462 ¬ice("Usage: /twitter_unsubscribe <topic>");
466 unless ( exists $id_map{__searches}{$user}{$data} ) {
467 ¬ice("No subscription found for '$data'");
471 delete $id_map{__searches}{$user}{$data};
472 ¬ice("Removed subscription for '$data'");
475 sub cmd_list_search {
476 my ( $data, $server, $win ) = @_;
479 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
481 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
482 $topics = $topics ? "$topics, $topic" : $topic;
486 ¬ice("Search subscriptions for \@$suser: $topics");
491 ¬ice("No search subscriptions set up");
496 my ( $data, $server, $win ) = @_;
498 my $loc = Irssi::settings_get_str("twirssi_location");
501 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
508 eval { use Digest::MD5; };
512 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
517 $md5 = get("http://twirssi.com/md5sum");
521 ¬ice("Failed to download md5sum from peeron! Aborting.");
525 unless ( open( CUR, $loc ) ) {
527 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
532 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
535 if ( $cur_md5 eq $md5 ) {
536 ¬ice("Current twirssi seems to be up to date.");
541 my $URL = "http://twirssi.com/twirssi.pl";
542 ¬ice("Downloading twirssi from $URL");
543 LWP::Simple::getstore( $URL, "$loc.upgrade" );
546 unless ( open( NEW, "$loc.upgrade" ) ) {
548 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
553 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
556 if ( $new_md5 ne $md5 ) {
557 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
562 rename $loc, "$loc.backup"
563 or ¬ice("Failed to back up $loc: $!. Aborting")
565 rename "$loc.upgrade", $loc
566 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
569 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
570 if ( -e "$dir/autorun/$file" ) {
571 ¬ice("Updating $dir/autorun/$file");
572 unlink "$dir/autorun/$file"
573 or ¬ice("Failed to remove old $file from autorun: $!");
574 symlink "../$file", "$dir/autorun/$file"
575 or ¬ice("Failed to create symlink in autorun directory: $!");
578 ¬ice("Download complete. Reload twirssi with /script load $file");
588 print $fh "type:debug Loading friends page $page...\n"
589 if ( $fh and &debug );
590 my $friends = $twit->friends( { page => $page } );
591 last unless $friends;
592 $new_friends{ $_->{screen_name} } = time foreach @$friends;
594 last if @$friends == 0 or $page == 10;
599 print $fh "type:debug Error during friends list update. Aborted.\n";
603 my ( $added, $removed ) = ( 0, 0 );
604 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
605 foreach ( keys %new_friends ) {
606 next if exists $friends{$_};
611 print $fh "type:debug Scanning for removed friends...\n"
612 if ( $fh and &debug );
613 foreach ( keys %friends ) {
614 next if exists $new_friends{$_};
619 return ( $added, $removed );
623 print scalar localtime, " - get_updates starting" if &debug;
626 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
629 ->print( "Can't find a window named '"
630 . Irssi::settings_get_str('twitter_window')
631 . "'. Create it or change the value of twitter_window" );
634 return unless &logged_in($twit);
636 my ( $fh, $filename ) = File::Temp::tempfile();
640 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
641 Irssi::pidwait_add($pid);
642 } elsif ( defined $pid ) { # child
650 $error += &do_updates( $fh, $user, $twit );
651 foreach ( keys %twits ) {
653 $error += &do_updates( $fh, $_, $twits{$_} );
656 my ( $added, $removed ) = &load_friends($fh);
657 if ( $added + $removed ) {
658 print $fh "type:debug %R***%n Friends list updated: ",
660 sprintf( "%d added", $added ),
661 sprintf( "%d removed", $removed ) ),
664 print $fh "__friends__\n";
665 foreach ( sort keys %friends ) {
666 print $fh "$_ $friends{$_}\n";
670 print $fh "type:debug Update encountered errors. Aborted\n";
671 print $fh $last_poll;
678 print scalar localtime, " - get_updates ends" if &debug;
682 my ( $fh, $username, $obj ) = @_;
684 my $rate_limit = $obj->rate_limit_status();
685 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
686 ¬ice("Rate limit exceeded for $username");
690 print scalar localtime, " - Polling for updates for $username" if &debug;
693 $tweets = $obj->friends_timeline(
694 { since => HTTP::Date::time2str($last_poll) } );
699 "type:debug Error during friends_timeline call: $@. Aborted.\n";
703 unless ( ref $tweets ) {
704 if ( $obj->can("get_error") ) {
706 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
707 if ($@) { $error = $obj->get_error() }
708 print $fh "type:debug API Error during friends_timeline call: ",
712 "type:debug API Error during friends_timeline call. Aborted.\n";
717 foreach my $t ( reverse @$tweets ) {
718 my $text = decode_entities( $t->{text} );
719 $text = &hilight($text);
721 if ( Irssi::settings_get_bool("show_reply_context")
722 and $t->{in_reply_to_screen_name} ne $username
723 and $t->{in_reply_to_screen_name}
724 and not exists $friends{ $t->{in_reply_to_screen_name} } )
726 $nicks{ $t->{in_reply_to_screen_name} } = time;
729 $context = $obj->show_status( $t->{in_reply_to_status_id} );
733 my $ctext = decode_entities( $context->{text} );
734 $ctext = &hilight($ctext);
735 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
736 $context->{id}, $username,
737 $context->{user}{screen_name}, $ctext;
740 print $fh "type:debug request to get context failed: $@";
743 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
748 if $t->{user}{screen_name} eq $username
749 and not Irssi::settings_get_bool("show_own_tweets");
750 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
751 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
754 print scalar localtime, " - Polling for replies" if &debug;
756 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
761 print $fh "type:debug Error during replies call. Aborted.\n";
765 foreach my $t ( reverse @$tweets ) {
767 if exists $friends{ $t->{user}{screen_name} };
769 my $text = decode_entities( $t->{text} );
770 $text = &hilight($text);
771 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
772 $t->{id}, $username, $t->{user}{screen_name}, $text;
775 print scalar localtime, " - Polling for DMs" if &debug;
778 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
783 print $fh "type:debug Error during direct_messages call. Aborted.\n";
787 foreach my $t ( reverse @$tweets ) {
788 my $text = decode_entities( $t->{text} );
789 $text = &hilight($text);
790 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
791 $t->{id}, $username, $t->{sender_screen_name}, $text;
794 print scalar localtime, " - Polling for subscriptions" if &debug;
795 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
797 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
798 print $fh "type:debug searching for $topic since ",
799 "$id_map{__searches}{$username}{$topic}\n";
801 $search = $obj->search(
804 since_id => $id_map{__searches}{$username}{$topic}
811 "type:debug Error during search($topic) call. Aborted.\n";
815 unless ( $search->{max_id} ) {
817 "type:debug Invalid search results when searching for $topic.",
822 $id_map{__searches}{$username}{$topic} = $search->{max_id};
823 printf $fh "id:%d account:%s type:searchid topic:%s\n",
824 $search->{max_id}, $username, $topic;
826 foreach my $t ( reverse @{ $search->{results} } ) {
827 my $text = decode_entities( $t->{text} );
828 $text = &hilight($text);
829 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
830 $t->{id}, $username, $t->{from_user}, $topic, $text;
835 print scalar localtime, " - Done" if &debug;
842 my $filename = $data->[0];
843 my $attempt = $data->[1];
845 print scalar localtime, " - checking child log at $filename ($attempt)"
848 if ( open FILE, $filename ) {
852 last if /^__friends__/;
855 foreach my $key (qw/id account nick type topic/) {
856 if (s/^$key:(\S+)\s*//) {
861 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
862 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
863 $tweet_cache{ $meta{id} } = time;
867 if ( $meta{account} ne $user ) {
868 $account = "$meta{account}: ";
872 if ( $meta{type} ne 'dm'
873 and Irssi::settings_get_bool("twirssi_track_replies")
877 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
878 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
879 $id_map{__indexes}{ $meta{nick} } = $marker;
880 $marker = ":$marker";
884 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
885 if ( ( $_ =~ /\@$meta{account}\W/i )
886 && Irssi::settings_get_bool("twirssi_hilights") )
888 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
889 $hilight = MSGLEVEL_HILIGHT;
892 if ( $meta{type} =~ /tweet|reply/ ) {
895 ( MSGLEVEL_PUBLIC | $hilight ),
896 $meta{type}, $account, $meta{nick}, $marker, $_
898 } elsif ( $meta{type} eq 'search' ) {
901 ( MSGLEVEL_PUBLIC | $hilight ),
902 $meta{type}, $account, $meta{topic},
903 $meta{nick}, $marker, $_
906 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
908 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
911 } elsif ( $meta{type} eq 'dm' ) {
914 ( MSGLEVEL_MSGS | $hilight ),
915 $meta{type}, $account, $meta{nick}, $_
917 } elsif ( $meta{type} eq 'searchid' ) {
918 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
920 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
922 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
925 print "Search '$meta{topic}' returned invalid id $meta{id}";
927 } elsif ( $meta{type} eq 'error' ) {
928 push @lines, [ MSGLEVEL_MSGS, $_ ];
929 } elsif ( $meta{type} eq 'debug' ) {
930 print "$_" if &debug,;
932 print "Unknown line type $meta{type}: $_" if &debug,;
942 my ( $f, $t ) = split ' ', $_;
943 $nicks{$f} = $friends{$f} = $t;
946 if ($new_last_poll) {
947 print "new last_poll = $new_last_poll" if &debug;
948 for my $line (@lines) {
949 $window->printformat(
951 "twirssi_" . $line->[1],
952 @$line[ 2 .. $#$line ]
958 or warn "Failed to remove $filename: $!"
961 # keep enough cached tweets, to make sure we don't show duplicates.
962 foreach ( keys %tweet_cache ) {
963 next if $tweet_cache{$_} >= $last_poll;
964 delete $tweet_cache{$_};
966 $last_poll = $new_last_poll;
971 Irssi::settings_get_str("twirssi_replies_store") )
973 if ( open JSON, ">$file" ) {
974 print JSON JSON::Any->objToJson( \%id_map );
977 ¬ice("Failed to write replies to $file: $!");
986 if ( $attempt < 24 ) {
987 Irssi::timeout_add_once( 5000, 'monitor_child',
988 [ $filename, $attempt + 1 ] );
990 print "Giving up on polling $filename" if &debug;
991 unlink $filename unless &debug;
993 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
996 my @time = localtime($last_poll);
997 if ( time - $last_poll < 24 * 60 * 60 ) {
998 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1000 $since = scalar localtime($last_poll);
1002 ¬ice("Haven't been able to get updated tweets since $since");
1007 return Irssi::settings_get_bool("twirssi_debug");
1011 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1017 if ( Irssi::settings_get_bool("tweet_to_away")
1019 and $data !~ /^[dD] / )
1022 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1024 $server->send_raw("away :$data");
1027 ¬ice( "Can't find bitlbee server.",
1028 "Update bitlbee_server or disable tweet_to_away" );
1038 my $noalert = shift;
1040 if ( length $data > 140 ) {
1041 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1049 sub valid_username {
1050 my $username = shift;
1052 unless ( exists $twits{$username} ) {
1053 ¬ice("Unknown username $username");
1063 ¬ice("Not logged in! Use /twitter_login username pass!");
1071 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1074 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1075 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1076 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1078 { # /twitter_reply gets a nick:num
1080 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1081 sort keys %{ $id_map{__indexes} };
1084 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1086 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1087 my $prefix = $word =~ s/^@//;
1088 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1089 push @$complist, grep /^\Q$word/i,
1090 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1091 @$complist = map { "\@$_" } @$complist if $prefix;
1095 sub event_send_text {
1096 my ( $line, $server, $win ) = @_;
1097 my $awin = Irssi::active_win();
1099 # if the window where we got our text was the twitter window, and the user
1100 # wants to be lazy, tweet away!
1101 if ( ( $awin->get_active_name() eq $window->{name} )
1102 and Irssi::settings_get_bool("tweet_window_input") )
1104 &cmd_tweet( $line, $server, $win );
1109 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1110 return $poll if $poll >= 60;
1117 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1118 my $c = Irssi::settings_get_str("twirssi_nick_color");
1119 $c = $irssi_to_mirc_colors{$c};
1120 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1122 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1123 my $c = Irssi::settings_get_str("twirssi_topic_color");
1124 $c = $irssi_to_mirc_colors{$c};
1125 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1127 $text =~ s/[\n\r]/ /g;
1132 Irssi::signal_add( "send text", "event_send_text" );
1134 Irssi::theme_register(
1136 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1137 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1138 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1139 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1140 'twirssi_error', 'ERROR: $0',
1144 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1145 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1146 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1147 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1148 Irssi::settings_add_str( "twirssi", "twirssi_location",
1149 ".irssi/scripts/twirssi.pl" );
1150 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1151 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1152 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1153 ".irssi/scripts/twirssi.json" );
1154 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1155 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1156 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1157 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1158 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1159 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1160 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1161 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1162 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1163 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1164 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1165 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1166 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1168 $last_poll = time - &get_poll_time;
1169 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1172 Irssi::Windowitem::window_create(
1173 Irssi::settings_get_str('twitter_window'), 1 );
1174 $window->set_name( Irssi::settings_get_str('twitter_window') );
1178 Irssi::command_bind( "dm", "cmd_direct" );
1179 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1180 Irssi::command_bind( "tweet", "cmd_tweet" );
1181 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1182 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1183 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1184 Irssi::command_bind( "twitter_login", "cmd_login" );
1185 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1186 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1187 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1188 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1189 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1190 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1191 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1192 Irssi::command_bind( "reply", "cmd_reply" );
1193 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1195 Irssi::command_bind(
1198 print "twits: ", join ", ",
1199 map { "u: $_->{username}" } values %twits;
1200 print "friends: ", join ", ", sort keys %friends;
1201 print "nicks: ", join ", ", sort keys %nicks;
1202 print "searches: ", Dumper \%{ $id_map{__searches} };
1203 print "last poll: $last_poll";
1206 Irssi::command_bind(
1209 ¬ice("Twirssi v$VERSION (r$REV); "
1210 . "Net::Twitter v$Net::Twitter::VERSION. "
1212 . JSON::Any::handler()
1213 . ". See details at http://twirssi.com/" );
1216 Irssi::command_bind(
1219 "/twitter_friend <username>",
1221 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1224 Irssi::command_bind(
1227 "/twitter_unfriend <username>",
1229 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1232 Irssi::command_bind( "twitter_updates", "get_updates" );
1233 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1235 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1236 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1238 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1240 my $file = Irssi::settings_get_str("twirssi_replies_store");
1241 if ( $file and -r $file ) {
1242 if ( open( JSON, $file ) ) {
1247 my $ref = JSON::Any->jsonToObj($json);
1249 my $num = keys %{ $id_map{__indexes} };
1250 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1251 $num, ( $num == 1 ? "" : "s" ) );
1254 ¬ice("Failed to load old replies from $file: $!");
1258 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1259 eval "use WWW::Shorten::$provider;";
1263 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1268 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1269 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1276 ->print( "Create a window named "
1277 . Irssi::settings_get_str('twitter_window')
1278 . " or change the value of twitter_window. Then, reload twirssi." );
1281 # vim: set sts=4 expandtab: