10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
14 $VERSION = "2.1.3beta";
15 my ($REV) = '$Rev: 547 $' =~ /(\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:38:17 -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' ) {
928 [ MSGLEVEL_PUBLIC, "tweet", $account, $meta{nick}, "", $_ ];
929 } elsif ( $meta{type} eq 'search' ) {
932 ( MSGLEVEL_PUBLIC | $hilight ),
933 $meta{type}, $account, $meta{topic},
934 $meta{nick}, $marker, $_
937 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
939 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
941 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
944 } elsif ( $meta{type} eq 'dm' ) {
947 ( MSGLEVEL_MSGS | $hilight ),
948 $meta{type}, $account, $meta{nick}, $_
950 } elsif ( $meta{type} eq 'searchid' ) {
951 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
953 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
955 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
957 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
960 print "Search '$meta{topic}' returned invalid id $meta{id}";
962 } elsif ( $meta{type} eq 'error' ) {
963 push @lines, [ MSGLEVEL_MSGS, $_ ];
964 } elsif ( $meta{type} eq 'debug' ) {
965 print "$_" if &debug,;
967 print "Unknown line type $meta{type}: $_" if &debug,;
977 my ( $f, $t ) = split ' ', $_;
978 $nicks{$f} = $friends{$f} = $t;
981 if ($new_last_poll) {
982 print "new last_poll = $new_last_poll" if &debug;
984 print "First call, not printing updates" if &debug;
986 foreach my $line (@lines) {
987 $window->printformat(
989 "twirssi_" . $line->[1],
990 @$line[ 2 .. $#$line ]
997 or warn "Failed to remove $filename: $!"
1000 # commit the pending cache lines to the actual cache, now that
1001 # we've printed our output
1002 %tweet_cache = ( %tweet_cache, %new_cache );
1004 # keep enough cached tweets, to make sure we don't show duplicates.
1005 foreach ( keys %tweet_cache ) {
1006 next if $tweet_cache{$_} >= $last_poll - 3600;
1007 delete $tweet_cache{$_};
1009 $last_poll = $new_last_poll;
1014 Irssi::settings_get_str("twirssi_replies_store") )
1016 if ( open JSON, ">$file" ) {
1017 print JSON JSON::Any->objToJson( \%id_map );
1020 &ccrap("Failed to write replies to $file: $!");
1030 if ( $attempt < 24 ) {
1031 Irssi::timeout_add_once( 5000, 'monitor_child',
1032 [ $filename, $attempt + 1 ] );
1034 print "Giving up on polling $filename" if &debug;
1035 unlink $filename unless &debug;
1037 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1040 my @time = localtime($last_poll);
1041 if ( time - $last_poll < 24 * 60 * 60 ) {
1042 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1044 $since = scalar localtime($last_poll);
1047 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1052 q{ .--./ / | _.---.| },
1063 &ccrap("Haven't been able to get updated tweets since $since");
1068 return Irssi::settings_get_bool("twirssi_debug");
1072 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1076 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1082 if ( Irssi::settings_get_bool("tweet_to_away")
1084 and $data !~ /^[dD] / )
1087 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1089 $server->send_raw("away :$data");
1092 &ccrap( "Can't find bitlbee server.",
1093 "Update bitlbee_server or disable tweet_to_away" );
1103 my $noalert = shift;
1105 if ( length $data > 140 ) {
1106 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1114 sub valid_username {
1115 my $username = shift;
1117 unless ( exists $twits{$username} ) {
1118 ¬ice("Unknown username $username");
1128 ¬ice("Not logged in! Use /twitter_login username pass!");
1136 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1139 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1140 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1141 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1143 { # /twitter_reply gets a nick:num
1145 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1146 sort { $nicks{$b} <=> $nicks{$a} }
1148 keys %{ $id_map{__indexes} };
1151 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1153 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1154 my $prefix = $word =~ s/^@//;
1155 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1156 push @$complist, grep /^\Q$word/i,
1157 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1158 @$complist = map { "\@$_" } @$complist if $prefix;
1162 sub event_send_text {
1163 my ( $line, $server, $win ) = @_;
1164 my $awin = Irssi::active_win();
1166 # if the window where we got our text was the twitter window, and the user
1167 # wants to be lazy, tweet away!
1168 if ( ( $awin->get_active_name() eq $window->{name} )
1169 and Irssi::settings_get_bool("tweet_window_input") )
1171 &cmd_tweet( $line, $server, $win );
1176 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1177 return $poll if $poll >= 60;
1184 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1185 my $c = Irssi::settings_get_str("twirssi_nick_color");
1186 $c = $irssi_to_mirc_colors{$c};
1187 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1189 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1190 my $c = Irssi::settings_get_str("twirssi_topic_color");
1191 $c = $irssi_to_mirc_colors{$c};
1192 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1194 $text =~ s/[\n\r]/ /g;
1202 my $provider = Irssi::settings_get_str("short_url_provider");
1203 if ( &too_long( $data, 1 ) and $provider ) {
1205 if ( $provider eq 'Bitly' ) {
1206 @args[ 1, 2 ] = split ',',
1207 Irssi::settings_get_str("short_url_args"), 2;
1208 unless ( @args == 3 ) {
1210 "WWW::Shorten::Bitly requires a username and API key.",
1211 "Set short_url_args to username,API_key or change your",
1212 "short_url_provider."
1218 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1221 my $short = makeashorterlink(@args);
1223 $data =~ s/\Q$url/$short/g;
1225 ¬ice("Failed to shorten $url!");
1234 Irssi::signal_add( "send text", "event_send_text" );
1236 Irssi::theme_register(
1238 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1239 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1240 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1241 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1242 'twirssi_error', 'ERROR: $0',
1246 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1247 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1248 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1249 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1250 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1251 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1252 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1253 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1254 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1255 Irssi::settings_add_str( "twirssi", "twirssi_location",
1256 ".irssi/scripts/twirssi.pl" );
1257 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1258 ".irssi/scripts/twirssi.json" );
1259 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1260 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1261 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1262 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1263 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1264 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1265 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1266 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1267 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1268 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1269 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1270 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1272 $last_poll = time - &get_poll_time;
1273 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1276 ->print( "Couldn't find a window named '"
1277 . Irssi::settings_get_str('twitter_window')
1278 . "', trying to create it." );
1280 Irssi::Windowitem::window_create(
1281 Irssi::settings_get_str('twitter_window'), 1 );
1282 $window->set_name( Irssi::settings_get_str('twitter_window') );
1286 Irssi::command_bind( "dm", "cmd_direct" );
1287 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1288 Irssi::command_bind( "tweet", "cmd_tweet" );
1289 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1290 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1291 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1292 Irssi::command_bind( "twitter_login", "cmd_login" );
1293 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1294 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1295 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1296 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1297 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1298 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1299 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1300 Irssi::command_bind( "reply", "cmd_reply" );
1301 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1303 Irssi::command_bind(
1306 print "twits: ", join ", ",
1307 map { "u: $_->{username}" } values %twits;
1308 print "friends: ", join ", ", sort keys %friends;
1309 print "nicks: ", join ", ", sort keys %nicks;
1310 print "searches: ", Dumper \%{ $id_map{__searches} };
1311 print "last poll: $last_poll";
1312 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1313 print DUMP Dumper \%tweet_cache;
1315 print "cache written out to /tmp/twirssi.cache.txt";
1319 Irssi::command_bind(
1322 ¬ice("Twirssi v$VERSION (r$REV); "
1323 . "Net::Twitter v$Net::Twitter::VERSION. "
1325 . JSON::Any::handler()
1326 . ". See details at http://twirssi.com/" );
1329 Irssi::command_bind(
1332 "/twitter_friend <username>",
1334 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1337 Irssi::command_bind(
1340 "/twitter_unfriend <username>",
1342 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1345 Irssi::command_bind( "twitter_updates", "get_updates" );
1346 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1348 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1349 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1351 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1353 my $file = Irssi::settings_get_str("twirssi_replies_store");
1354 if ( $file and -r $file ) {
1355 if ( open( JSON, $file ) ) {
1360 my $ref = JSON::Any->jsonToObj($json);
1362 my $num = keys %{ $id_map{__indexes} };
1363 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1364 $num, ( $num == 1 ? "" : "s" ) );
1368 ¬ice("Failed to load old replies from $file: $!");
1372 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1373 ¬ice("Loading WWW::Shorten::$provider...");
1374 eval "use WWW::Shorten::$provider;";
1378 "Failed to load WWW::Shorten::$provider - either clear",
1379 "short_url_provider or install the CPAN module"
1384 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1385 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1393 ->print( "Create a window named "
1394 . Irssi::settings_get_str('twitter_window')
1395 . " or change the value of twitter_window. Then, reload twirssi." );
1398 # vim: set sts=4 expandtab: