10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 481 $' =~ /(\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-19 14:22:45 -0800 (Thu, 19 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 ( Irssi::settings_get_str("short_url_provider") ) {
238 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
240 my $short = makeashorterlink($url);
241 $data =~ s/\Q$url/$short/g;
246 return if &too_long($data);
251 $twits{$username}->update(
254 in_reply_to_status_id => $id_map{ lc $nick }[$id]
259 ¬ice("Update failed");
263 return unless $success;
266 ¬ice("Update caused an error: $@. Aborted");
270 foreach ( $data =~ /@([-\w]+)/ ) {
274 my $away = &update_away($data);
276 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
280 my ( $usage_str, $api_name, $post_ref ) = @_;
283 my ( $data, $server, $win ) = @_;
285 return unless &logged_in($twit);
287 $data =~ s/^\s+|\s+$//;
289 ¬ice("Usage: $usage_str");
295 unless ( $twit->$api_name($data) )
297 ¬ice("$api_name failed");
301 return unless $success;
304 ¬ice("$api_name caused an error. Aborted.");
308 &$post_ref($data) if $post_ref;
313 my ( $data, $server, $win ) = @_;
315 $data =~ s/^\s+|\s+$//g;
316 if ( exists $twits{$data} ) {
317 ¬ice("Switching to $data");
318 $twit = $twits{$data};
321 ¬ice("Unknown user $data");
326 my ( $data, $server, $win ) = @_;
328 $data =~ s/^\s+|\s+$//g;
329 $data = $user unless $data;
330 return unless &valid_username($data);
332 ¬ice("Logging out $data...");
333 $twits{$data}->end_session();
334 delete $twits{$data};
337 &cmd_switch( ( keys %twits )[0], $server, $win );
339 Irssi::timeout_remove($poll) if $poll;
345 my ( $data, $server, $win ) = @_;
348 ( $user, $pass ) = split ' ', $data, 2;
349 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
350 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
352 my @user = split /\s*,\s*/, $autouser;
353 my @pass = split /\s*,\s*/, $autopass;
354 if ( @user != @pass ) {
355 ¬ice("Number of usernames doesn't match "
356 . "the number of passwords - auto-login failed" );
359 while ( @user and @pass ) {
367 ¬ice("/twitter_login requires either a username and password "
368 . "or twitter_usernames and twitter_passwords to be set." );
372 %friends = %nicks = ();
374 $twit = Net::Twitter->new(
380 unless ( $twit->verify_credentials() ) {
381 ¬ice("Login as $user failed");
384 &cmd_switch( ( keys %twits )[0], $server, $win );
390 my $rate_limit = $twit->rate_limit_status();
391 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
393 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
399 $twits{$user} = $twit;
400 Irssi::timeout_remove($poll) if $poll;
401 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
402 ¬ice("Logged in as $user, loading friends list...");
404 ¬ice( "loaded friends: ", scalar keys %friends );
405 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
406 Irssi::settings_set_bool( "twirssi_first_run", 0 );
407 unless ( exists $friends{twirssi} ) {
408 ¬ice("Welcome to twirssi!"
409 . " Perhaps you should add \@twirssi to your friends list,"
410 . " so you can be notified when a new version is release?"
411 . " Just type /twitter_friend twirssi." );
418 ¬ice("Login failed");
423 my ( $data, $server, $win ) = @_;
425 unless ( $twit and $twit->can('search') ) {
426 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
427 . "doesn't support searches." );
431 $data =~ s/^\s+|\s+$//;
435 ¬ice("Usage: /twitter_subscribe <topic>");
439 if ( exists $id_map{__searches}{$user}{$data} ) {
440 ¬ice("Already had a subscription for '$data'");
444 $id_map{__searches}{$user}{$data} = 1;
445 ¬ice("Added subscription for '$data'");
449 my ( $data, $server, $win ) = @_;
451 unless ( $twit and $twit->can('search') ) {
452 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
453 . "doesn't support searches." );
456 $data =~ s/^\s+|\s+$//;
460 ¬ice("Usage: /twitter_unsubscribe <topic>");
464 unless ( exists $id_map{__searches}{$user}{$data} ) {
465 ¬ice("No subscription found for '$data'");
469 delete $id_map{__searches}{$user}{$data};
470 ¬ice("Removed subscription for '$data'");
473 sub cmd_list_search {
474 my ( $data, $server, $win ) = @_;
477 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
479 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
480 $topics = $topics ? "$topics, $topic" : $topic;
484 ¬ice("Search subscriptions for \@$suser: $topics");
489 ¬ice("No search subscriptions set up");
494 my ( $data, $server, $win ) = @_;
496 my $loc = Irssi::settings_get_str("twirssi_location");
499 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
504 if ( not -x "/usr/bin/md5sum" and not $data ) {
506 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
513 eval { use Digest::MD5; };
517 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
522 $md5 = get("http://twirssi.com/md5sum");
526 ¬ice("Failed to download md5sum from peeron! Aborting.");
530 unless ( open( CUR, $loc ) ) {
532 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
537 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
540 if ( $cur_md5 eq $md5 ) {
541 ¬ice("Current twirssi seems to be up to date.");
546 my $URL = "http://twirssi.com/twirssi.pl";
547 ¬ice("Downloading twirssi from $URL");
548 LWP::Simple::getstore( $URL, "$loc.upgrade" );
551 unless ( open( NEW, "$loc.upgrade" ) ) {
553 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
558 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
561 if ( $new_md5 ne $md5 ) {
562 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
567 rename $loc, "$loc.backup"
568 or ¬ice("Failed to back up $loc: $!. Aborting")
570 rename "$loc.upgrade", $loc
571 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
574 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
575 if ( -e "$dir/autorun/$file" ) {
576 ¬ice("Updating $dir/autorun/$file");
577 unlink "$dir/autorun/$file"
578 or ¬ice("Failed to remove old $file from autorun: $!");
579 symlink "../$file", "$dir/autorun/$file"
580 or ¬ice("Failed to create symlink in autorun directory: $!");
583 ¬ice("Download complete. Reload twirssi with /script load $file");
593 print $fh "type:debug Loading friends page $page...\n"
594 if ( $fh and &debug );
595 my $friends = $twit->friends( { page => $page } );
596 last unless $friends;
597 $new_friends{ $_->{screen_name} } = time foreach @$friends;
599 last if @$friends == 0 or $page == 10;
604 print $fh "type:debug Error during friends list update. Aborted.\n";
608 my ( $added, $removed ) = ( 0, 0 );
609 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
610 foreach ( keys %new_friends ) {
611 next if exists $friends{$_};
616 print $fh "type:debug Scanning for removed friends...\n"
617 if ( $fh and &debug );
618 foreach ( keys %friends ) {
619 next if exists $new_friends{$_};
624 return ( $added, $removed );
628 print scalar localtime, " - get_updates starting" if &debug;
631 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
634 ->print( "Can't find a window named '"
635 . Irssi::settings_get_str('twitter_window')
636 . "'. Create it or change the value of twitter_window" );
639 return unless &logged_in($twit);
641 my ( $fh, $filename ) = File::Temp::tempfile();
645 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
646 Irssi::pidwait_add($pid);
647 } elsif ( defined $pid ) { # child
655 $error += &do_updates( $fh, $user, $twit );
656 foreach ( keys %twits ) {
658 $error += &do_updates( $fh, $_, $twits{$_} );
661 my ( $added, $removed ) = &load_friends($fh);
662 if ( $added + $removed ) {
663 print $fh "type:debug %R***%n Friends list updated: ",
665 sprintf( "%d added", $added ),
666 sprintf( "%d removed", $removed ) ),
669 print $fh "__friends__\n";
670 foreach ( sort keys %friends ) {
671 print $fh "$_ $friends{$_}\n";
675 print $fh "type:debug Update encountered errors. Aborted\n";
676 print $fh $last_poll;
683 print scalar localtime, " - get_updates ends" if &debug;
687 my ( $fh, $username, $obj ) = @_;
689 my $rate_limit = $obj->rate_limit_status();
690 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
691 ¬ice("Rate limit exceeded for $username");
695 print scalar localtime, " - Polling for updates for $username" if &debug;
698 $tweets = $obj->friends_timeline(
699 { since => HTTP::Date::time2str($last_poll) } );
704 "type:debug Error during friends_timeline call: $@. Aborted.\n";
708 unless ( ref $tweets ) {
709 if ( $obj->can("get_error") ) {
711 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
712 if ($@) { $error = $obj->get_error() }
713 print $fh "type:debug API Error during friends_timeline call: ",
717 "type:debug API Error during friends_timeline call. Aborted.\n";
722 foreach my $t ( reverse @$tweets ) {
723 my $text = decode_entities( $t->{text} );
724 $text = &hilight($text);
726 if ( Irssi::settings_get_bool("show_reply_context")
727 and $t->{in_reply_to_screen_name} ne $username
728 and $t->{in_reply_to_screen_name}
729 and not exists $friends{ $t->{in_reply_to_screen_name} } )
731 $nicks{ $t->{in_reply_to_screen_name} } = time;
734 $context = $obj->show_status( $t->{in_reply_to_status_id} );
738 my $ctext = decode_entities( $context->{text} );
739 $ctext = &hilight($ctext);
740 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
741 $context->{id}, $username,
742 $context->{user}{screen_name}, $ctext;
745 print $fh "type:debug request to get context failed: $@";
748 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
753 if $t->{user}{screen_name} eq $username
754 and not Irssi::settings_get_bool("show_own_tweets");
755 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
756 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
759 print scalar localtime, " - Polling for replies" if &debug;
761 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
766 print $fh "type:debug Error during replies call. Aborted.\n";
770 foreach my $t ( reverse @$tweets ) {
772 if exists $friends{ $t->{user}{screen_name} };
774 my $text = decode_entities( $t->{text} );
775 $text = &hilight($text);
776 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
777 $t->{id}, $username, $t->{user}{screen_name}, $text;
780 print scalar localtime, " - Polling for DMs" if &debug;
783 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
788 print $fh "type:debug Error during direct_messages call. Aborted.\n";
792 foreach my $t ( reverse @$tweets ) {
793 my $text = decode_entities( $t->{text} );
794 $text = &hilight($text);
795 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
796 $t->{id}, $username, $t->{sender_screen_name}, $text;
799 print scalar localtime, " - Polling for subscriptions" if &debug;
800 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
802 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
803 print $fh "type:debug searching for $topic since ",
804 "$id_map{__searches}{$username}{$topic}\n";
806 $search = $obj->search(
809 since_id => $id_map{__searches}{$username}{$topic}
816 "type:debug Error during search($topic) call. Aborted.\n";
820 unless ( $search->{max_id} ) {
822 "type:debug Invalid search results when searching for $topic.",
827 $id_map{__searches}{$username}{$topic} = $search->{max_id};
828 printf $fh "id:%d account:%s type:searchid topic:%s\n",
829 $search->{max_id}, $username, $topic;
831 foreach my $t ( reverse @{ $search->{results} } ) {
832 my $text = decode_entities( $t->{text} );
833 $text = &hilight($text);
834 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
835 $t->{id}, $username, $t->{from_user}, $topic, $text;
840 print scalar localtime, " - Done" if &debug;
847 my $filename = $data->[0];
848 my $attempt = $data->[1];
850 print scalar localtime, " - checking child log at $filename ($attempt)"
853 if ( open FILE, $filename ) {
857 last if /^__friends__/;
860 foreach my $key (qw/id account nick type topic/) {
861 if (s/^$key:(\S+)\s*//) {
866 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
867 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
868 $tweet_cache{ $meta{id} } = time;
872 if ( $meta{account} ne $user ) {
873 $account = "$meta{account}: ";
877 if ( $meta{type} ne 'dm'
878 and Irssi::settings_get_bool("twirssi_track_replies")
882 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
883 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
884 $id_map{__indexes}{ $meta{nick} } = $marker;
885 $marker = ":$marker";
889 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
890 if ( ( $_ =~ /\@$meta{account}\W/i )
891 && Irssi::settings_get_bool("twirssi_hilights") )
893 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
894 $hilight = MSGLEVEL_HILIGHT;
897 if ( $meta{type} =~ /tweet|reply/ ) {
900 ( MSGLEVEL_PUBLIC | $hilight ),
901 $meta{type}, $account, $meta{nick}, $marker, $_
903 } elsif ( $meta{type} eq 'search' ) {
906 ( MSGLEVEL_PUBLIC | $hilight ),
907 $meta{type}, $account, $meta{topic},
908 $meta{nick}, $marker, $_
911 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
913 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
916 } elsif ( $meta{type} eq 'dm' ) {
919 ( MSGLEVEL_MSGS | $hilight ),
920 $meta{type}, $account, $meta{nick}, $_
922 } elsif ( $meta{type} eq 'searchid' ) {
923 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
925 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
927 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
930 print "Search '$meta{topic}' returned invalid id $meta{id}";
932 } elsif ( $meta{type} eq 'error' ) {
933 push @lines, [ MSGLEVEL_MSGS, $_ ];
934 } elsif ( $meta{type} eq 'debug' ) {
935 print "$_" if &debug,;
937 print "Unknown line type $meta{type}: $_" if &debug,;
947 my ( $f, $t ) = split ' ', $_;
948 $nicks{$f} = $friends{$f} = $t;
951 if ($new_last_poll) {
952 print "new last_poll = $new_last_poll" if &debug;
953 for my $line (@lines) {
954 $window->printformat(
956 "twirssi_" . $line->[1],
957 @$line[ 2 .. $#$line ]
963 or warn "Failed to remove $filename: $!"
966 # keep enough cached tweets, to make sure we don't show duplicates.
967 foreach ( keys %tweet_cache ) {
968 next if $tweet_cache{$_} >= $last_poll;
969 delete $tweet_cache{$_};
971 $last_poll = $new_last_poll;
976 Irssi::settings_get_str("twirssi_replies_store") )
978 if ( open JSON, ">$file" ) {
979 print JSON JSON::Any->objToJson( \%id_map );
982 ¬ice("Failed to write replies to $file: $!");
991 if ( $attempt < 24 ) {
992 Irssi::timeout_add_once( 5000, 'monitor_child',
993 [ $filename, $attempt + 1 ] );
995 print "Giving up on polling $filename" if &debug;
996 unlink $filename unless &debug;
998 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1001 my @time = localtime($last_poll);
1002 if ( time - $last_poll < 24 * 60 * 60 ) {
1003 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1005 $since = scalar localtime($last_poll);
1007 ¬ice("Haven't been able to get updated tweets since $since");
1012 return Irssi::settings_get_bool("twirssi_debug");
1016 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1022 if ( Irssi::settings_get_bool("tweet_to_away")
1024 and $data !~ /^[dD] / )
1027 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1029 $server->send_raw("away :$data");
1032 ¬ice( "Can't find bitlbee server.",
1033 "Update bitlbee_server or disable tweet_to_away" );
1043 my $noalert = shift;
1045 if ( length $data > 140 ) {
1046 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1054 sub valid_username {
1055 my $username = shift;
1057 unless ( exists $twits{$username} ) {
1058 ¬ice("Unknown username $username");
1068 ¬ice("Not logged in! Use /twitter_login username pass!");
1076 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1079 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1080 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1081 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1083 { # /twitter_reply gets a nick:num
1085 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1086 sort keys %{ $id_map{__indexes} };
1089 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1091 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1092 my $prefix = $word =~ s/^@//;
1093 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1094 push @$complist, grep /^\Q$word/i,
1095 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1096 @$complist = map { "\@$_" } @$complist if $prefix;
1100 sub event_send_text {
1101 my ( $line, $server, $win ) = @_;
1102 my $awin = Irssi::active_win();
1104 # if the window where we got our text was the twitter window, and the user
1105 # wants to be lazy, tweet away!
1106 if ( ( $awin->get_active_name() eq $window->{name} )
1107 and Irssi::settings_get_bool("tweet_window_input") )
1109 &cmd_tweet( $line, $server, $win );
1114 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1115 return $poll if $poll >= 60;
1122 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1123 my $c = Irssi::settings_get_str("twirssi_nick_color");
1124 $c = $irssi_to_mirc_colors{$c};
1125 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1127 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1128 my $c = Irssi::settings_get_str("twirssi_topic_color");
1129 $c = $irssi_to_mirc_colors{$c};
1130 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1132 $text =~ s/[\n\r]/ /g;
1137 Irssi::signal_add( "send text", "event_send_text" );
1139 Irssi::theme_register(
1141 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1142 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1143 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1144 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1145 'twirssi_error', 'ERROR: $0',
1149 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1150 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1151 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1152 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1153 Irssi::settings_add_str( "twirssi", "twirssi_location",
1154 ".irssi/scripts/twirssi.pl" );
1155 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1156 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1157 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1158 ".irssi/scripts/twirssi.json" );
1159 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1160 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1161 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1162 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1163 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1164 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1165 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1166 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1167 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1168 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1169 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1170 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1171 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1173 $last_poll = time - &get_poll_time;
1174 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1177 Irssi::Windowitem::window_create(
1178 Irssi::settings_get_str('twitter_window'), 1 );
1179 $window->set_name( Irssi::settings_get_str('twitter_window') );
1183 Irssi::command_bind( "dm", "cmd_direct" );
1184 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1185 Irssi::command_bind( "tweet", "cmd_tweet" );
1186 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1187 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1188 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1189 Irssi::command_bind( "twitter_login", "cmd_login" );
1190 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1191 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1192 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1193 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1194 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1195 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1196 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1197 Irssi::command_bind( "reply", "cmd_reply" );
1198 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1200 Irssi::command_bind(
1203 print "twits: ", join ", ",
1204 map { "u: $_->{username}" } values %twits;
1205 print "friends: ", join ", ", sort keys %friends;
1206 print "nicks: ", join ", ", sort keys %nicks;
1207 print "searches: ", Dumper \%{ $id_map{__searches} };
1208 print "last poll: $last_poll";
1211 Irssi::command_bind(
1214 ¬ice("Twirssi v$VERSION (r$REV); "
1215 . "Net::Twitter v$Net::Twitter::VERSION. "
1217 . JSON::Any::handler()
1218 . ". See details at http://twirssi.com/" );
1221 Irssi::command_bind(
1224 "/twitter_friend <username>",
1226 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1229 Irssi::command_bind(
1232 "/twitter_unfriend <username>",
1234 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1237 Irssi::command_bind( "twitter_updates", "get_updates" );
1238 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1240 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1241 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1243 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1245 my $file = Irssi::settings_get_str("twirssi_replies_store");
1246 if ( $file and -r $file ) {
1247 if ( open( JSON, $file ) ) {
1252 my $ref = JSON::Any->jsonToObj($json);
1254 my $num = keys %{ $id_map{__indexes} };
1255 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1256 $num, ( $num == 1 ? "" : "s" ) );
1259 ¬ice("Failed to load old replies from $file: $!");
1263 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1264 eval "use WWW::Shorten::$provider;";
1268 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1273 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1274 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1281 ->print( "Create a window named "
1282 . Irssi::settings_get_str('twitter_window')
1283 . " or change the value of twitter_window. Then, reload twirssi." );
1286 # vim: set sts=4 expandtab: