10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
14 $VERSION = "2.1.3beta";
15 my ($REV) = '$Rev: 546 $' =~ /(\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-03-13 10:37:30 -0700 (Fri, 13 Mar 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 $data = &shorten($data);
139 return if &too_long($data);
143 unless ( $twits{$username}->update($data) )
145 ¬ice("Update failed");
149 return unless $success;
152 ¬ice("Update caused an error: $@. Aborted.");
156 foreach ( $data =~ /@([-\w]+)/ ) {
160 my $away = &update_away($data);
162 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
166 my ( $data, $server, $win ) = @_;
168 return unless &logged_in($twit);
170 $data =~ s/^\s+|\s+$//;
172 ¬ice("Usage: /reply <nick[:num]> <update>");
176 $data =~ s/^\s+|\s+$//;
177 my ( $id, $data ) = split ' ', $data, 2;
178 unless ( $id and $data ) {
179 ¬ice("Usage: /reply_as <nick[:num]> <update>");
183 &cmd_reply_as( "$user $id $data", $server, $win );
187 my ( $data, $server, $win ) = @_;
189 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
190 ¬ice("twirssi_track_replies is required in order to reply to "
191 . "specific tweets. Either enable it, or just use /tweet "
192 . "\@username <text>." );
196 return unless &logged_in($twit);
198 $data =~ s/^\s+|\s+$//;
199 my ( $username, $id, $data ) = split ' ', $data, 3;
201 unless ( $username and $data ) {
202 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
206 return unless &valid_username($username);
209 $id =~ s/[^\w\d\-:]+//g;
210 ( $nick, $id ) = split /:/, $id;
211 unless ( exists $id_map{ lc $nick } ) {
212 ¬ice("Can't find a tweet from $nick to reply to!");
216 $id = $id_map{__indexes}{$nick} unless $id;
217 unless ( $id_map{ lc $nick }[$id] ) {
218 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
222 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
224 # remove any @nick at the beginning of the reply, as we'll add it anyway
225 $data =~ s/^\s*\@?$nick\s*//;
226 $data = "\@$nick " . $data;
229 $data = &shorten($data);
231 return if &too_long($data);
236 $twits{$username}->update(
239 in_reply_to_status_id => $id_map{ lc $nick }[$id]
244 ¬ice("Update failed");
248 return unless $success;
251 ¬ice("Update caused an error: $@. Aborted");
255 foreach ( $data =~ /@([-\w]+)/ ) {
259 my $away = &update_away($data);
261 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
265 my ( $usage_str, $api_name, $post_ref ) = @_;
268 my ( $data, $server, $win ) = @_;
270 return unless &logged_in($twit);
272 $data =~ s/^\s+|\s+$//;
274 ¬ice("Usage: $usage_str");
280 unless ( $twit->$api_name($data) )
282 ¬ice("$api_name failed");
286 return unless $success;
289 ¬ice("$api_name caused an error. Aborted.");
293 &$post_ref($data) if $post_ref;
298 my ( $data, $server, $win ) = @_;
300 $data =~ s/^\s+|\s+$//g;
301 if ( exists $twits{$data} ) {
302 ¬ice("Switching to $data");
303 $twit = $twits{$data};
306 ¬ice("Unknown user $data");
311 my ( $data, $server, $win ) = @_;
313 $data =~ s/^\s+|\s+$//g;
314 $data = $user unless $data;
315 return unless &valid_username($data);
317 ¬ice("Logging out $data...");
318 $twits{$data}->end_session();
319 delete $twits{$data};
322 &cmd_switch( ( keys %twits )[0], $server, $win );
324 Irssi::timeout_remove($poll) if $poll;
330 my ( $data, $server, $win ) = @_;
333 ( $user, $pass ) = split ' ', $data, 2;
334 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
335 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
337 my @user = split /\s*,\s*/, $autouser;
338 my @pass = split /\s*,\s*/, $autopass;
340 # if a password ends with a '\', it was meant to escape the comma, and
341 # it should be concatinated with the next one
345 while ($p =~ /\\$/ and @pass) {
346 $p .= ",". shift @pass;
351 if ( @user != @unescaped ) {
352 ¬ice("Number of usernames doesn't match "
353 . "the number of passwords - auto-login failed" );
356 while ( @user and @unescaped ) {
358 $p = shift @unescaped;
364 ¬ice("/twitter_login requires either a username and password "
365 . "or twitter_usernames and twitter_passwords to be set." );
369 %friends = %nicks = ();
371 $twit = Net::Twitter->new(
377 unless ( $twit->verify_credentials() ) {
378 ¬ice("Login as $user failed");
381 &cmd_switch( ( keys %twits )[0], $server, $win );
387 my $rate_limit = $twit->rate_limit_status();
388 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
390 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
396 $twits{$user} = $twit;
397 Irssi::timeout_remove($poll) if $poll;
398 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
399 ¬ice("Logged in as $user, loading friends list...");
401 ¬ice( "loaded friends: ", scalar keys %friends );
402 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
403 Irssi::settings_set_bool( "twirssi_first_run", 0 );
404 unless ( exists $friends{twirssi} ) {
405 ¬ice("Welcome to twirssi!"
406 . " Perhaps you should add \@twirssi to your friends list,"
407 . " so you can be notified when a new version is release?"
408 . " Just type /twitter_friend twirssi." );
415 ¬ice("Login failed");
420 my ( $data, $server, $win ) = @_;
422 unless ( $twit and $twit->can('search') ) {
423 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
424 . "doesn't support searches." );
428 $data =~ s/^\s+|\s+$//;
432 ¬ice("Usage: /twitter_subscribe <topic>");
436 if ( exists $id_map{__searches}{$user}{$data} ) {
437 ¬ice("Already had a subscription for '$data'");
441 $id_map{__searches}{$user}{$data} = 1;
442 ¬ice("Added subscription for '$data'");
446 my ( $data, $server, $win ) = @_;
448 unless ( $twit and $twit->can('search') ) {
449 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
450 . "doesn't support searches." );
453 $data =~ s/^\s+|\s+$//;
457 ¬ice("Usage: /twitter_unsubscribe <topic>");
461 unless ( exists $id_map{__searches}{$user}{$data} ) {
462 ¬ice("No subscription found for '$data'");
466 delete $id_map{__searches}{$user}{$data};
467 ¬ice("Removed subscription for '$data'");
470 sub cmd_list_search {
471 my ( $data, $server, $win ) = @_;
474 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
476 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
477 $topics = $topics ? "$topics, $topic" : $topic;
481 ¬ice("Search subscriptions for \@$suser: $topics");
486 ¬ice("No search subscriptions set up");
491 my ( $data, $server, $win ) = @_;
493 my $loc = Irssi::settings_get_str("twirssi_location");
496 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
502 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
503 eval { use Digest::MD5; };
507 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
512 $md5 = get("http://twirssi.com/md5sum");
516 ¬ice("Failed to download md5sum from peeron! Aborting.");
520 unless ( open( CUR, $loc ) ) {
522 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
527 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
530 if ( $cur_md5 eq $md5 ) {
531 ¬ice("Current twirssi seems to be up to date.");
537 Irssi::settings_get_bool("twirssi_upgrade_beta")
538 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
539 : "http://twirssi.com/twirssi.pl";
540 ¬ice("Downloading twirssi from $URL");
541 LWP::Simple::getstore( $URL, "$loc.upgrade" );
543 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
544 unless ( open( NEW, "$loc.upgrade" ) ) {
546 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
551 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
554 if ( $new_md5 ne $md5 ) {
555 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
560 rename $loc, "$loc.backup"
561 or ¬ice("Failed to back up $loc: $!. Aborting")
563 rename "$loc.upgrade", $loc
564 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
567 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
568 if ( -e "$dir/autorun/$file" ) {
569 ¬ice("Updating $dir/autorun/$file");
570 unlink "$dir/autorun/$file"
571 or ¬ice("Failed to remove old $file from autorun: $!");
572 symlink "../$file", "$dir/autorun/$file"
573 or ¬ice("Failed to create symlink in autorun directory: $!");
576 ¬ice("Download complete. Reload twirssi with /script load $file");
586 print $fh "type:debug Loading friends page $page...\n"
587 if ( $fh and &debug );
588 my $friends = $twit->friends( { page => $page } );
589 last unless $friends;
590 $new_friends{ $_->{screen_name} } = time foreach @$friends;
592 last if @$friends == 0 or $page == 10;
597 print $fh "type:debug Error during friends list update. Aborted.\n";
601 my ( $added, $removed ) = ( 0, 0 );
602 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
603 foreach ( keys %new_friends ) {
604 next if exists $friends{$_};
609 print $fh "type:debug Scanning for removed friends...\n"
610 if ( $fh and &debug );
611 foreach ( keys %friends ) {
612 next if exists $new_friends{$_};
617 return ( $added, $removed );
621 print scalar localtime, " - get_updates starting" if &debug;
624 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
627 ->print( "Can't find a window named '"
628 . Irssi::settings_get_str('twitter_window')
629 . "'. Create it or change the value of twitter_window" );
632 return unless &logged_in($twit);
634 my ( $fh, $filename ) = File::Temp::tempfile();
635 binmode( $fh, ":utf8" );
639 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
640 Irssi::pidwait_add($pid);
641 } elsif ( defined $pid ) { # child
649 $error += &do_updates( $fh, $user, $twit );
650 foreach ( keys %twits ) {
652 $error += &do_updates( $fh, $_, $twits{$_} );
655 my ( $added, $removed ) = &load_friends($fh);
656 if ( $added + $removed ) {
657 print $fh "type:debug %R***%n Friends list updated: ",
659 sprintf( "%d added", $added ),
660 sprintf( "%d removed", $removed ) ),
663 print $fh "__friends__\n";
664 foreach ( sort keys %friends ) {
665 print $fh "$_ $friends{$_}\n";
669 print $fh "type:debug Update encountered errors. Aborted\n";
670 print $fh $last_poll;
677 print scalar localtime, " - get_updates ends" if &debug;
681 my ( $fh, $username, $obj ) = @_;
683 my $rate_limit = $obj->rate_limit_status();
684 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
685 ¬ice("Rate limit exceeded for $username");
689 print scalar localtime, " - Polling for updates for $username" if &debug;
691 eval { $tweets = $obj->friends_timeline(); };
695 "type:debug Error during friends_timeline call: $@. Aborted.\n";
699 unless ( ref $tweets ) {
700 if ( $obj->can("get_error") ) {
702 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
703 if ($@) { $error = $obj->get_error() }
704 print $fh "type:debug API Error during friends_timeline call: ",
708 "type:debug API Error during friends_timeline call. Aborted.\n";
713 foreach my $t ( reverse @$tweets ) {
714 my $text = decode_entities( $t->{text} );
715 $text = &hilight($text);
717 if ( Irssi::settings_get_bool("show_reply_context")
718 and $t->{in_reply_to_screen_name} ne $username
719 and $t->{in_reply_to_screen_name}
720 and not exists $friends{ $t->{in_reply_to_screen_name} } )
722 $nicks{ $t->{in_reply_to_screen_name} } = time;
725 $context = $obj->show_status( $t->{in_reply_to_status_id} );
729 my $ctext = decode_entities( $context->{text} );
730 $ctext = &hilight($ctext);
731 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
732 $context->{id}, $username,
733 $context->{user}{screen_name}, $ctext;
734 if ($context->{truncated}) {
735 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
736 $context->{id}."-url", $username,
737 $context->{user}{screen_name},
738 "http://twitter.com/$context->{user}{screen_name}/status/$context->{id}";
742 print $fh "type:debug request to get context failed: $@";
745 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
750 if $t->{user}{screen_name} eq $username
751 and not Irssi::settings_get_bool("show_own_tweets");
752 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
753 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
754 if ($t->{truncated}) {
755 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
756 $t->{id}."-url", $username,
757 $t->{user}{screen_name},
758 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
762 print scalar localtime, " - Polling for replies" if &debug;
764 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
769 print $fh "type:debug Error during replies call. Aborted.\n";
773 foreach my $t ( reverse @$tweets ) {
775 if exists $friends{ $t->{user}{screen_name} };
777 my $text = decode_entities( $t->{text} );
778 $text = &hilight($text);
779 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
780 $t->{id}, $username, $t->{user}{screen_name}, $text;
781 if ($t->{truncated}) {
782 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
783 $t->{id}."-url", $username,
784 $t->{user}{screen_name},
785 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
789 print scalar localtime, " - Polling for DMs" if &debug;
792 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
797 print $fh "type:debug Error during direct_messages call. Aborted.\n";
801 foreach my $t ( reverse @$tweets ) {
802 my $text = decode_entities( $t->{text} );
803 $text = &hilight($text);
804 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
805 $t->{id}, $username, $t->{sender_screen_name}, $text;
808 print scalar localtime, " - Polling for subscriptions" if &debug;
809 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
811 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
812 print $fh "type:debug searching for $topic since ",
813 "$id_map{__searches}{$username}{$topic}\n";
815 $search = $obj->search(
818 since_id => $id_map{__searches}{$username}{$topic}
825 "type:debug Error during search($topic) call. Aborted.\n";
829 unless ( $search->{max_id} ) {
831 "type:debug Invalid search results when searching for $topic.",
836 $id_map{__searches}{$username}{$topic} = $search->{max_id};
837 printf $fh "id:%d account:%s type:searchid topic:%s\n",
838 $search->{max_id}, $username, $topic;
840 foreach my $t ( reverse @{ $search->{results} } ) {
841 my $text = decode_entities( $t->{text} );
842 $text = &hilight($text);
843 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
844 $t->{id}, $username, $t->{from_user}, $topic, $text;
849 print scalar localtime, " - Done" if &debug;
856 my $filename = $data->[0];
857 my $attempt = $data->[1];
859 print scalar localtime, " - checking child log at $filename ($attempt)"
863 # first time we run we don't want to print out *everything*, so we just
866 $suppress = 1 unless keys %tweet_cache;
868 if ( open FILE, $filename ) {
873 last if /^__friends__/;
876 foreach my $key (qw/id account nick type topic/) {
877 if (s/^$key:(\S+)\s*//) {
882 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
883 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
887 $new_cache{ $meta{id} } = time;
889 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
895 if ( $meta{account} ne $user ) {
896 $account = "$meta{account}: ";
900 if ( $meta{type} ne 'dm'
901 and Irssi::settings_get_bool("twirssi_track_replies")
905 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
906 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
907 $id_map{__indexes}{ $meta{nick} } = $marker;
908 $marker = ":$marker";
912 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
913 if ( ( $_ =~ /\@$meta{account}\W/i )
914 && Irssi::settings_get_bool("twirssi_hilights") )
916 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
917 $hilight = MSGLEVEL_HILIGHT;
920 if ( $meta{type} =~ /tweet|reply/ ) {
923 ( MSGLEVEL_PUBLIC | $hilight ),
924 $meta{type}, $account, $meta{nick}, $marker, $_
926 } elsif ( $meta{type} eq 'ellispis' ) {
930 "tweet", $account, $meta{nick}, "", $_
932 } elsif ( $meta{type} eq 'search' ) {
935 ( MSGLEVEL_PUBLIC | $hilight ),
936 $meta{type}, $account, $meta{topic},
937 $meta{nick}, $marker, $_
940 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
942 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
944 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
947 } elsif ( $meta{type} eq 'dm' ) {
950 ( MSGLEVEL_MSGS | $hilight ),
951 $meta{type}, $account, $meta{nick}, $_
953 } elsif ( $meta{type} eq 'searchid' ) {
954 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
956 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
958 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
960 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
963 print "Search '$meta{topic}' returned invalid id $meta{id}";
965 } elsif ( $meta{type} eq 'error' ) {
966 push @lines, [ MSGLEVEL_MSGS, $_ ];
967 } elsif ( $meta{type} eq 'debug' ) {
968 print "$_" if &debug,;
970 print "Unknown line type $meta{type}: $_" if &debug,;
980 my ( $f, $t ) = split ' ', $_;
981 $nicks{$f} = $friends{$f} = $t;
984 if ($new_last_poll) {
985 print "new last_poll = $new_last_poll" if &debug;
987 print "First call, not printing updates" if &debug;
989 foreach my $line (@lines) {
990 $window->printformat(
992 "twirssi_" . $line->[1],
993 @$line[ 2 .. $#$line ]
1000 or warn "Failed to remove $filename: $!"
1003 # commit the pending cache lines to the actual cache, now that
1004 # we've printed our output
1005 %tweet_cache = ( %tweet_cache, %new_cache );
1007 # keep enough cached tweets, to make sure we don't show duplicates.
1008 foreach ( keys %tweet_cache ) {
1009 next if $tweet_cache{$_} >= $last_poll - 3600;
1010 delete $tweet_cache{$_};
1012 $last_poll = $new_last_poll;
1017 Irssi::settings_get_str("twirssi_replies_store") )
1019 if ( open JSON, ">$file" ) {
1020 print JSON JSON::Any->objToJson( \%id_map );
1023 &ccrap("Failed to write replies to $file: $!");
1033 if ( $attempt < 24 ) {
1034 Irssi::timeout_add_once( 5000, 'monitor_child',
1035 [ $filename, $attempt + 1 ] );
1037 print "Giving up on polling $filename" if &debug;
1038 unlink $filename unless &debug;
1040 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1043 my @time = localtime($last_poll);
1044 if ( time - $last_poll < 24 * 60 * 60 ) {
1045 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1047 $since = scalar localtime($last_poll);
1050 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1055 q{ .--./ / | _.---.| },
1066 &ccrap("Haven't been able to get updated tweets since $since");
1071 return Irssi::settings_get_bool("twirssi_debug");
1075 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1079 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1085 if ( Irssi::settings_get_bool("tweet_to_away")
1087 and $data !~ /^[dD] / )
1090 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1092 $server->send_raw("away :$data");
1095 &ccrap( "Can't find bitlbee server.",
1096 "Update bitlbee_server or disable tweet_to_away" );
1106 my $noalert = shift;
1108 if ( length $data > 140 ) {
1109 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1117 sub valid_username {
1118 my $username = shift;
1120 unless ( exists $twits{$username} ) {
1121 ¬ice("Unknown username $username");
1131 ¬ice("Not logged in! Use /twitter_login username pass!");
1139 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1142 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1143 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1144 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1146 { # /twitter_reply gets a nick:num
1148 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1149 sort {$nicks{$b} <=> $nicks{$a}}
1151 keys %{ $id_map{__indexes} };
1154 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1156 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1157 my $prefix = $word =~ s/^@//;
1158 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1159 push @$complist, grep /^\Q$word/i,
1160 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1161 @$complist = map { "\@$_" } @$complist if $prefix;
1165 sub event_send_text {
1166 my ( $line, $server, $win ) = @_;
1167 my $awin = Irssi::active_win();
1169 # if the window where we got our text was the twitter window, and the user
1170 # wants to be lazy, tweet away!
1171 if ( ( $awin->get_active_name() eq $window->{name} )
1172 and Irssi::settings_get_bool("tweet_window_input") )
1174 &cmd_tweet( $line, $server, $win );
1179 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1180 return $poll if $poll >= 60;
1187 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1188 my $c = Irssi::settings_get_str("twirssi_nick_color");
1189 $c = $irssi_to_mirc_colors{$c};
1190 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1192 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1193 my $c = Irssi::settings_get_str("twirssi_topic_color");
1194 $c = $irssi_to_mirc_colors{$c};
1195 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1197 $text =~ s/[\n\r]/ /g;
1205 my $provider = Irssi::settings_get_str("short_url_provider");
1206 if ( &too_long( $data, 1 ) and $provider )
1209 if ($provider eq 'Bitly') {
1210 @args[1,2] = split ',', Irssi::settings_get_str("short_url_args"), 2;
1211 unless (@args == 3) {
1212 &ccrap("WWW::Shorten::Bitly requires a username and API key.",
1213 "Set short_url_args to username,API_key or change your",
1214 "short_url_provider.");
1219 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1222 my $short = makeashorterlink(@args);
1224 $data =~ s/\Q$url/$short/g;
1226 ¬ice("Failed to shorten $url!");
1235 Irssi::signal_add( "send text", "event_send_text" );
1237 Irssi::theme_register(
1239 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1240 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1241 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1242 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1243 'twirssi_error', 'ERROR: $0',
1247 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1248 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1249 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1250 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1251 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1252 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1253 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1254 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1255 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1256 Irssi::settings_add_str( "twirssi", "twirssi_location",
1257 ".irssi/scripts/twirssi.pl" );
1258 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1259 ".irssi/scripts/twirssi.json" );
1260 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1261 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1262 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1263 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1264 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1265 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1266 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1267 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1268 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1269 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1270 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1271 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1273 $last_poll = time - &get_poll_time;
1274 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1277 ->print( "Couldn't find a window named '"
1278 . Irssi::settings_get_str('twitter_window')
1279 . "', trying to create it." );
1281 Irssi::Windowitem::window_create(
1282 Irssi::settings_get_str('twitter_window'), 1 );
1283 $window->set_name( Irssi::settings_get_str('twitter_window') );
1287 Irssi::command_bind( "dm", "cmd_direct" );
1288 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1289 Irssi::command_bind( "tweet", "cmd_tweet" );
1290 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1291 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1292 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1293 Irssi::command_bind( "twitter_login", "cmd_login" );
1294 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1295 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1296 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1297 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1298 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1299 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1300 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1301 Irssi::command_bind( "reply", "cmd_reply" );
1302 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1304 Irssi::command_bind(
1307 print "twits: ", join ", ",
1308 map { "u: $_->{username}" } values %twits;
1309 print "friends: ", join ", ", sort keys %friends;
1310 print "nicks: ", join ", ", sort keys %nicks;
1311 print "searches: ", Dumper \%{ $id_map{__searches} };
1312 print "last poll: $last_poll";
1313 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1314 print DUMP Dumper \%tweet_cache;
1316 print "cache written out to /tmp/twirssi.cache.txt";
1320 Irssi::command_bind(
1323 ¬ice("Twirssi v$VERSION (r$REV); "
1324 . "Net::Twitter v$Net::Twitter::VERSION. "
1326 . JSON::Any::handler()
1327 . ". See details at http://twirssi.com/" );
1330 Irssi::command_bind(
1333 "/twitter_friend <username>",
1335 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1338 Irssi::command_bind(
1341 "/twitter_unfriend <username>",
1343 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1346 Irssi::command_bind( "twitter_updates", "get_updates" );
1347 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1349 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1350 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1352 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1354 my $file = Irssi::settings_get_str("twirssi_replies_store");
1355 if ( $file and -r $file ) {
1356 if ( open( JSON, $file ) ) {
1361 my $ref = JSON::Any->jsonToObj($json);
1363 my $num = keys %{ $id_map{__indexes} };
1364 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1365 $num, ( $num == 1 ? "" : "s" ) );
1369 ¬ice("Failed to load old replies from $file: $!");
1373 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1374 ¬ice("Loading WWW::Shorten::$provider...");
1375 eval "use WWW::Shorten::$provider;";
1378 ¬ice( "Failed to load WWW::Shorten::$provider - either clear",
1379 "short_url_provider or install the CPAN module");
1383 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1384 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1392 ->print( "Create a window named "
1393 . Irssi::settings_get_str('twitter_window')
1394 . " or change the value of twitter_window. Then, reload twirssi." );
1397 # vim: set sts=4 expandtab: