10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
14 $VERSION = "2.1.2beta";
15 my ($REV) = '$Rev: 522 $' =~ /(\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-07 14:07:25 -0800 (Sat, 07 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 if ( &too_long( $data, 1 )
138 and Irssi::settings_get_str("short_url_provider") )
140 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
142 my $short = makeashorterlink($url);
143 $data =~ s/\Q$url/$short/g;
148 return if &too_long($data);
152 unless ( $twits{$username}->update($data) )
154 ¬ice("Update failed");
158 return unless $success;
161 ¬ice("Update caused an error: $@. Aborted.");
165 foreach ( $data =~ /@([-\w]+)/ ) {
169 my $away = &update_away($data);
171 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
175 my ( $data, $server, $win ) = @_;
177 return unless &logged_in($twit);
179 $data =~ s/^\s+|\s+$//;
181 ¬ice("Usage: /reply <nick[:num]> <update>");
185 $data =~ s/^\s+|\s+$//;
186 my ( $id, $data ) = split ' ', $data, 2;
187 unless ( $id and $data ) {
188 ¬ice("Usage: /reply_as <nick[:num]> <update>");
192 &cmd_reply_as( "$user $id $data", $server, $win );
196 my ( $data, $server, $win ) = @_;
198 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
199 ¬ice("twirssi_track_replies is required in order to reply to "
200 . "specific tweets. Either enable it, or just use /tweet "
201 . "\@username <text>." );
205 return unless &logged_in($twit);
207 $data =~ s/^\s+|\s+$//;
208 my ( $username, $id, $data ) = split ' ', $data, 3;
210 unless ( $username and $data ) {
211 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
215 return unless &valid_username($username);
218 $id =~ s/[^\w\d\-:]+//g;
219 ( $nick, $id ) = split /:/, $id;
220 unless ( exists $id_map{ lc $nick } ) {
221 ¬ice("Can't find a tweet from $nick to reply to!");
225 $id = $id_map{__indexes}{$nick} unless $id;
226 unless ( $id_map{ lc $nick }[$id] ) {
227 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
231 if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
233 # remove any @nick at the beginning of the reply, as we'll add it anyway
234 $data =~ s/^\s*\@?$nick\s*//;
235 $data = "\@$nick " . $data;
238 if ( &too_long( $data, 1 ) ) {
239 if ( Irssi::settings_get_str("short_url_provider") ) {
240 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
242 my $short = makeashorterlink($url);
243 $data =~ s/\Q$url/$short/g;
249 return if &too_long($data);
254 $twits{$username}->update(
257 in_reply_to_status_id => $id_map{ lc $nick }[$id]
262 ¬ice("Update failed");
266 return unless $success;
269 ¬ice("Update caused an error: $@. Aborted");
273 foreach ( $data =~ /@([-\w]+)/ ) {
277 my $away = &update_away($data);
279 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
283 my ( $usage_str, $api_name, $post_ref ) = @_;
286 my ( $data, $server, $win ) = @_;
288 return unless &logged_in($twit);
290 $data =~ s/^\s+|\s+$//;
292 ¬ice("Usage: $usage_str");
298 unless ( $twit->$api_name($data) )
300 ¬ice("$api_name failed");
304 return unless $success;
307 ¬ice("$api_name caused an error. Aborted.");
311 &$post_ref($data) if $post_ref;
316 my ( $data, $server, $win ) = @_;
318 $data =~ s/^\s+|\s+$//g;
319 if ( exists $twits{$data} ) {
320 ¬ice("Switching to $data");
321 $twit = $twits{$data};
324 ¬ice("Unknown user $data");
329 my ( $data, $server, $win ) = @_;
331 $data =~ s/^\s+|\s+$//g;
332 $data = $user unless $data;
333 return unless &valid_username($data);
335 ¬ice("Logging out $data...");
336 $twits{$data}->end_session();
337 delete $twits{$data};
340 &cmd_switch( ( keys %twits )[0], $server, $win );
342 Irssi::timeout_remove($poll) if $poll;
348 my ( $data, $server, $win ) = @_;
351 ( $user, $pass ) = split ' ', $data, 2;
352 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
353 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
355 my @user = split /\s*,\s*/, $autouser;
356 my @pass = split /\s*,\s*/, $autopass;
358 # if a password ends with a '\', it was meant to escape the comma, and
359 # it should be concatinated with the next one
363 while ($p =~ /\\$/ and @pass) {
364 $p .= ",". shift @pass;
369 if ( @user != @unescaped ) {
370 ¬ice("Number of usernames doesn't match "
371 . "the number of passwords - auto-login failed" );
374 while ( @user and @unescaped ) {
376 $p = shift @unescaped;
382 ¬ice("/twitter_login requires either a username and password "
383 . "or twitter_usernames and twitter_passwords to be set." );
387 %friends = %nicks = ();
389 $twit = Net::Twitter->new(
395 unless ( $twit->verify_credentials() ) {
396 ¬ice("Login as $user failed");
399 &cmd_switch( ( keys %twits )[0], $server, $win );
405 my $rate_limit = $twit->rate_limit_status();
406 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
408 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
414 $twits{$user} = $twit;
415 Irssi::timeout_remove($poll) if $poll;
416 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
417 ¬ice("Logged in as $user, loading friends list...");
419 ¬ice( "loaded friends: ", scalar keys %friends );
420 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
421 Irssi::settings_set_bool( "twirssi_first_run", 0 );
422 unless ( exists $friends{twirssi} ) {
423 ¬ice("Welcome to twirssi!"
424 . " Perhaps you should add \@twirssi to your friends list,"
425 . " so you can be notified when a new version is release?"
426 . " Just type /twitter_friend twirssi." );
433 ¬ice("Login failed");
438 my ( $data, $server, $win ) = @_;
440 unless ( $twit and $twit->can('search') ) {
441 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
442 . "doesn't support searches." );
446 $data =~ s/^\s+|\s+$//;
450 ¬ice("Usage: /twitter_subscribe <topic>");
454 if ( exists $id_map{__searches}{$user}{$data} ) {
455 ¬ice("Already had a subscription for '$data'");
459 $id_map{__searches}{$user}{$data} = 1;
460 ¬ice("Added subscription for '$data'");
464 my ( $data, $server, $win ) = @_;
466 unless ( $twit and $twit->can('search') ) {
467 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
468 . "doesn't support searches." );
471 $data =~ s/^\s+|\s+$//;
475 ¬ice("Usage: /twitter_unsubscribe <topic>");
479 unless ( exists $id_map{__searches}{$user}{$data} ) {
480 ¬ice("No subscription found for '$data'");
484 delete $id_map{__searches}{$user}{$data};
485 ¬ice("Removed subscription for '$data'");
488 sub cmd_list_search {
489 my ( $data, $server, $win ) = @_;
492 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
494 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
495 $topics = $topics ? "$topics, $topic" : $topic;
499 ¬ice("Search subscriptions for \@$suser: $topics");
504 ¬ice("No search subscriptions set up");
509 my ( $data, $server, $win ) = @_;
511 my $loc = Irssi::settings_get_str("twirssi_location");
514 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
520 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
521 eval { use Digest::MD5; };
525 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
530 $md5 = get("http://twirssi.com/md5sum");
534 ¬ice("Failed to download md5sum from peeron! Aborting.");
538 unless ( open( CUR, $loc ) ) {
540 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
545 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
548 if ( $cur_md5 eq $md5 ) {
549 ¬ice("Current twirssi seems to be up to date.");
555 Irssi::settings_get_bool("twirssi_upgrade_beta")
556 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
557 : "http://twirssi.com/twirssi.pl";
558 ¬ice("Downloading twirssi from $URL");
559 LWP::Simple::getstore( $URL, "$loc.upgrade" );
561 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
562 unless ( open( NEW, "$loc.upgrade" ) ) {
564 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
569 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
572 if ( $new_md5 ne $md5 ) {
573 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
578 rename $loc, "$loc.backup"
579 or ¬ice("Failed to back up $loc: $!. Aborting")
581 rename "$loc.upgrade", $loc
582 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
585 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
586 if ( -e "$dir/autorun/$file" ) {
587 ¬ice("Updating $dir/autorun/$file");
588 unlink "$dir/autorun/$file"
589 or ¬ice("Failed to remove old $file from autorun: $!");
590 symlink "../$file", "$dir/autorun/$file"
591 or ¬ice("Failed to create symlink in autorun directory: $!");
594 ¬ice("Download complete. Reload twirssi with /script load $file");
604 print $fh "type:debug Loading friends page $page...\n"
605 if ( $fh and &debug );
606 my $friends = $twit->friends( { page => $page } );
607 last unless $friends;
608 $new_friends{ $_->{screen_name} } = time foreach @$friends;
610 last if @$friends == 0 or $page == 10;
615 print $fh "type:debug Error during friends list update. Aborted.\n";
619 my ( $added, $removed ) = ( 0, 0 );
620 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
621 foreach ( keys %new_friends ) {
622 next if exists $friends{$_};
627 print $fh "type:debug Scanning for removed friends...\n"
628 if ( $fh and &debug );
629 foreach ( keys %friends ) {
630 next if exists $new_friends{$_};
635 return ( $added, $removed );
639 print scalar localtime, " - get_updates starting" if &debug;
642 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
645 ->print( "Can't find a window named '"
646 . Irssi::settings_get_str('twitter_window')
647 . "'. Create it or change the value of twitter_window" );
650 return unless &logged_in($twit);
652 my ( $fh, $filename ) = File::Temp::tempfile();
653 binmode( $fh, ":utf8" );
657 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
658 Irssi::pidwait_add($pid);
659 } elsif ( defined $pid ) { # child
667 $error += &do_updates( $fh, $user, $twit );
668 foreach ( keys %twits ) {
670 $error += &do_updates( $fh, $_, $twits{$_} );
673 my ( $added, $removed ) = &load_friends($fh);
674 if ( $added + $removed ) {
675 print $fh "type:debug %R***%n Friends list updated: ",
677 sprintf( "%d added", $added ),
678 sprintf( "%d removed", $removed ) ),
681 print $fh "__friends__\n";
682 foreach ( sort keys %friends ) {
683 print $fh "$_ $friends{$_}\n";
687 print $fh "type:debug Update encountered errors. Aborted\n";
688 print $fh $last_poll;
695 print scalar localtime, " - get_updates ends" if &debug;
699 my ( $fh, $username, $obj ) = @_;
701 my $rate_limit = $obj->rate_limit_status();
702 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
703 ¬ice("Rate limit exceeded for $username");
707 print scalar localtime, " - Polling for updates for $username" if &debug;
709 eval { $tweets = $obj->friends_timeline(); };
713 "type:debug Error during friends_timeline call: $@. Aborted.\n";
717 unless ( ref $tweets ) {
718 if ( $obj->can("get_error") ) {
720 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
721 if ($@) { $error = $obj->get_error() }
722 print $fh "type:debug API Error during friends_timeline call: ",
726 "type:debug API Error during friends_timeline call. Aborted.\n";
731 foreach my $t ( reverse @$tweets ) {
732 my $text = decode_entities( $t->{text} );
733 $text = &hilight($text);
735 if ( Irssi::settings_get_bool("show_reply_context")
736 and $t->{in_reply_to_screen_name} ne $username
737 and $t->{in_reply_to_screen_name}
738 and not exists $friends{ $t->{in_reply_to_screen_name} } )
740 $nicks{ $t->{in_reply_to_screen_name} } = time;
743 $context = $obj->show_status( $t->{in_reply_to_status_id} );
747 my $ctext = decode_entities( $context->{text} );
748 $ctext = &hilight($ctext);
749 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
750 $context->{id}, $username,
751 $context->{user}{screen_name}, $ctext;
752 if ($context->{truncated}) {
753 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
754 $context->{id}."-url", $username,
755 $context->{user}{screen_name},
756 "http://twitter.com/$context->{user}{screen_name}/status/$context->{id}";
760 print $fh "type:debug request to get context failed: $@";
763 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
768 if $t->{user}{screen_name} eq $username
769 and not Irssi::settings_get_bool("show_own_tweets");
770 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
771 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
772 if ($t->{truncated}) {
773 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
774 $t->{id}."-url", $username,
775 $t->{user}{screen_name},
776 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
780 print scalar localtime, " - Polling for replies" if &debug;
782 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
787 print $fh "type:debug Error during replies call. Aborted.\n";
791 foreach my $t ( reverse @$tweets ) {
793 if exists $friends{ $t->{user}{screen_name} };
795 my $text = decode_entities( $t->{text} );
796 $text = &hilight($text);
797 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
798 $t->{id}, $username, $t->{user}{screen_name}, $text;
799 if ($t->{truncated}) {
800 printf $fh "id:%s account:%s nick:%s type:ellispis %s\n",
801 $t->{id}."-url", $username,
802 $t->{user}{screen_name},
803 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
807 print scalar localtime, " - Polling for DMs" if &debug;
810 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
815 print $fh "type:debug Error during direct_messages call. Aborted.\n";
819 foreach my $t ( reverse @$tweets ) {
820 my $text = decode_entities( $t->{text} );
821 $text = &hilight($text);
822 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
823 $t->{id}, $username, $t->{sender_screen_name}, $text;
826 print scalar localtime, " - Polling for subscriptions" if &debug;
827 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
829 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
830 print $fh "type:debug searching for $topic since ",
831 "$id_map{__searches}{$username}{$topic}\n";
833 $search = $obj->search(
836 since_id => $id_map{__searches}{$username}{$topic}
843 "type:debug Error during search($topic) call. Aborted.\n";
847 unless ( $search->{max_id} ) {
849 "type:debug Invalid search results when searching for $topic.",
854 $id_map{__searches}{$username}{$topic} = $search->{max_id};
855 printf $fh "id:%d account:%s type:searchid topic:%s\n",
856 $search->{max_id}, $username, $topic;
858 foreach my $t ( reverse @{ $search->{results} } ) {
859 my $text = decode_entities( $t->{text} );
860 $text = &hilight($text);
861 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
862 $t->{id}, $username, $t->{from_user}, $topic, $text;
867 print scalar localtime, " - Done" if &debug;
874 my $filename = $data->[0];
875 my $attempt = $data->[1];
877 print scalar localtime, " - checking child log at $filename ($attempt)"
881 # first time we run we don't want to print out *everything*, so we just
884 $suppress = 1 unless keys %tweet_cache;
886 if ( open FILE, $filename ) {
891 last if /^__friends__/;
894 foreach my $key (qw/id account nick type topic/) {
895 if (s/^$key:(\S+)\s*//) {
900 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
901 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
905 $new_cache{ $meta{id} } = time;
907 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
913 if ( $meta{account} ne $user ) {
914 $account = "$meta{account}: ";
918 if ( $meta{type} ne 'dm'
919 and Irssi::settings_get_bool("twirssi_track_replies")
923 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
924 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
925 $id_map{__indexes}{ $meta{nick} } = $marker;
926 $marker = ":$marker";
930 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
931 if ( ( $_ =~ /\@$meta{account}\W/i )
932 && Irssi::settings_get_bool("twirssi_hilights") )
934 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
935 $hilight = MSGLEVEL_HILIGHT;
938 if ( $meta{type} =~ /tweet|reply/ ) {
941 ( MSGLEVEL_PUBLIC | $hilight ),
942 $meta{type}, $account, $meta{nick}, $marker, $_
944 } elsif ( $meta{type} eq 'ellispis' ) {
948 "tweet", $account, $meta{nick}, "", $_
950 } elsif ( $meta{type} eq 'search' ) {
953 ( MSGLEVEL_PUBLIC | $hilight ),
954 $meta{type}, $account, $meta{topic},
955 $meta{nick}, $marker, $_
958 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
960 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
962 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
965 } elsif ( $meta{type} eq 'dm' ) {
968 ( MSGLEVEL_MSGS | $hilight ),
969 $meta{type}, $account, $meta{nick}, $_
971 } elsif ( $meta{type} eq 'searchid' ) {
972 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
974 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
976 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
978 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
981 print "Search '$meta{topic}' returned invalid id $meta{id}";
983 } elsif ( $meta{type} eq 'error' ) {
984 push @lines, [ MSGLEVEL_MSGS, $_ ];
985 } elsif ( $meta{type} eq 'debug' ) {
986 print "$_" if &debug,;
988 print "Unknown line type $meta{type}: $_" if &debug,;
998 my ( $f, $t ) = split ' ', $_;
999 $nicks{$f} = $friends{$f} = $t;
1002 if ($new_last_poll) {
1003 print "new last_poll = $new_last_poll" if &debug;
1005 print "First call, not printing updates" if &debug;
1007 foreach my $line (@lines) {
1008 $window->printformat(
1010 "twirssi_" . $line->[1],
1011 @$line[ 2 .. $#$line ]
1018 or warn "Failed to remove $filename: $!"
1021 # commit the pending cache lines to the actual cache, now that
1022 # we've printed our output
1023 %tweet_cache = ( %tweet_cache, %new_cache );
1025 # keep enough cached tweets, to make sure we don't show duplicates.
1026 foreach ( keys %tweet_cache ) {
1027 next if $tweet_cache{$_} >= $last_poll - 3600;
1028 delete $tweet_cache{$_};
1030 $last_poll = $new_last_poll;
1035 Irssi::settings_get_str("twirssi_replies_store") )
1037 if ( open JSON, ">$file" ) {
1038 print JSON JSON::Any->objToJson( \%id_map );
1041 &ccrap("Failed to write replies to $file: $!");
1051 if ( $attempt < 24 ) {
1052 Irssi::timeout_add_once( 5000, 'monitor_child',
1053 [ $filename, $attempt + 1 ] );
1055 print "Giving up on polling $filename" if &debug;
1056 unlink $filename unless &debug;
1058 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1061 my @time = localtime($last_poll);
1062 if ( time - $last_poll < 24 * 60 * 60 ) {
1063 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1065 $since = scalar localtime($last_poll);
1068 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1073 q{ .--./ / | _.---.| },
1084 &ccrap("Haven't been able to get updated tweets since $since");
1089 return Irssi::settings_get_bool("twirssi_debug");
1093 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1097 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1103 if ( Irssi::settings_get_bool("tweet_to_away")
1105 and $data !~ /^[dD] / )
1108 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1110 $server->send_raw("away :$data");
1113 &ccrap( "Can't find bitlbee server.",
1114 "Update bitlbee_server or disable tweet_to_away" );
1124 my $noalert = shift;
1126 if ( length $data > 140 ) {
1127 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1135 sub valid_username {
1136 my $username = shift;
1138 unless ( exists $twits{$username} ) {
1139 ¬ice("Unknown username $username");
1149 ¬ice("Not logged in! Use /twitter_login username pass!");
1157 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1160 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1161 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1162 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1164 { # /twitter_reply gets a nick:num
1166 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1167 sort {$nicks{$b} <=> $nicks{$a}}
1169 keys %{ $id_map{__indexes} };
1172 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1174 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1175 my $prefix = $word =~ s/^@//;
1176 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1177 push @$complist, grep /^\Q$word/i,
1178 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1179 @$complist = map { "\@$_" } @$complist if $prefix;
1183 sub event_send_text {
1184 my ( $line, $server, $win ) = @_;
1185 my $awin = Irssi::active_win();
1187 # if the window where we got our text was the twitter window, and the user
1188 # wants to be lazy, tweet away!
1189 if ( ( $awin->get_active_name() eq $window->{name} )
1190 and Irssi::settings_get_bool("tweet_window_input") )
1192 &cmd_tweet( $line, $server, $win );
1197 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1198 return $poll if $poll >= 60;
1205 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1206 my $c = Irssi::settings_get_str("twirssi_nick_color");
1207 $c = $irssi_to_mirc_colors{$c};
1208 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1210 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1211 my $c = Irssi::settings_get_str("twirssi_topic_color");
1212 $c = $irssi_to_mirc_colors{$c};
1213 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1215 $text =~ s/[\n\r]/ /g;
1220 Irssi::signal_add( "send text", "event_send_text" );
1222 Irssi::theme_register(
1224 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1225 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1226 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1227 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1228 'twirssi_error', 'ERROR: $0',
1232 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1233 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1234 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1235 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1236 Irssi::settings_add_str( "twirssi", "twirssi_location",
1237 ".irssi/scripts/twirssi.pl" );
1238 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1239 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1240 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1241 ".irssi/scripts/twirssi.json" );
1242 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1243 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1244 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1245 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1246 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1247 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1248 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1249 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1250 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1251 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1252 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1253 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1254 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1255 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1257 $last_poll = time - &get_poll_time;
1258 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1261 ->print( "Couldn't find a window named '"
1262 . Irssi::settings_get_str('twitter_window')
1263 . "', trying to create it." );
1265 Irssi::Windowitem::window_create(
1266 Irssi::settings_get_str('twitter_window'), 1 );
1267 $window->set_name( Irssi::settings_get_str('twitter_window') );
1271 Irssi::command_bind( "dm", "cmd_direct" );
1272 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1273 Irssi::command_bind( "tweet", "cmd_tweet" );
1274 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1275 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1276 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1277 Irssi::command_bind( "twitter_login", "cmd_login" );
1278 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1279 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1280 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1281 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1282 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1283 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1284 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1285 Irssi::command_bind( "reply", "cmd_reply" );
1286 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1288 Irssi::command_bind(
1291 print "twits: ", join ", ",
1292 map { "u: $_->{username}" } values %twits;
1293 print "friends: ", join ", ", sort keys %friends;
1294 print "nicks: ", join ", ", sort keys %nicks;
1295 print "searches: ", Dumper \%{ $id_map{__searches} };
1296 print "last poll: $last_poll";
1297 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1298 print DUMP Dumper \%tweet_cache;
1300 print "cache written out to /tmp/twirssi.cache.txt";
1304 Irssi::command_bind(
1307 ¬ice("Twirssi v$VERSION (r$REV); "
1308 . "Net::Twitter v$Net::Twitter::VERSION. "
1310 . JSON::Any::handler()
1311 . ". See details at http://twirssi.com/" );
1314 Irssi::command_bind(
1317 "/twitter_friend <username>",
1319 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1322 Irssi::command_bind(
1325 "/twitter_unfriend <username>",
1327 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1330 Irssi::command_bind( "twitter_updates", "get_updates" );
1331 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1333 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1334 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1336 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1338 my $file = Irssi::settings_get_str("twirssi_replies_store");
1339 if ( $file and -r $file ) {
1340 if ( open( JSON, $file ) ) {
1345 my $ref = JSON::Any->jsonToObj($json);
1347 my $num = keys %{ $id_map{__indexes} };
1348 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1349 $num, ( $num == 1 ? "" : "s" ) );
1353 ¬ice("Failed to load old replies from $file: $!");
1357 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1358 eval "use WWW::Shorten::$provider;";
1362 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1367 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1368 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1376 ->print( "Create a window named "
1377 . Irssi::settings_get_str('twitter_window')
1378 . " or change the value of twitter_window. Then, reload twirssi." );
1381 # vim: set sts=4 expandtab: