10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
14 $VERSION = "2.1.2beta";
15 my ($REV) = '$Rev: 519 $' =~ /(\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 13:30:43 -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;
357 if ( @user != @pass ) {
358 ¬ice("Number of usernames doesn't match "
359 . "the number of passwords - auto-login failed" );
362 while ( @user and @pass ) {
370 ¬ice("/twitter_login requires either a username and password "
371 . "or twitter_usernames and twitter_passwords to be set." );
375 %friends = %nicks = ();
377 $twit = Net::Twitter->new(
383 unless ( $twit->verify_credentials() ) {
384 ¬ice("Login as $user failed");
387 &cmd_switch( ( keys %twits )[0], $server, $win );
393 my $rate_limit = $twit->rate_limit_status();
394 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
396 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
402 $twits{$user} = $twit;
403 Irssi::timeout_remove($poll) if $poll;
404 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
405 ¬ice("Logged in as $user, loading friends list...");
407 ¬ice( "loaded friends: ", scalar keys %friends );
408 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
409 Irssi::settings_set_bool( "twirssi_first_run", 0 );
410 unless ( exists $friends{twirssi} ) {
411 ¬ice("Welcome to twirssi!"
412 . " Perhaps you should add \@twirssi to your friends list,"
413 . " so you can be notified when a new version is release?"
414 . " Just type /twitter_friend twirssi." );
421 ¬ice("Login failed");
426 my ( $data, $server, $win ) = @_;
428 unless ( $twit and $twit->can('search') ) {
429 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
430 . "doesn't support searches." );
434 $data =~ s/^\s+|\s+$//;
438 ¬ice("Usage: /twitter_subscribe <topic>");
442 if ( exists $id_map{__searches}{$user}{$data} ) {
443 ¬ice("Already had a subscription for '$data'");
447 $id_map{__searches}{$user}{$data} = 1;
448 ¬ice("Added subscription for '$data'");
452 my ( $data, $server, $win ) = @_;
454 unless ( $twit and $twit->can('search') ) {
455 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
456 . "doesn't support searches." );
459 $data =~ s/^\s+|\s+$//;
463 ¬ice("Usage: /twitter_unsubscribe <topic>");
467 unless ( exists $id_map{__searches}{$user}{$data} ) {
468 ¬ice("No subscription found for '$data'");
472 delete $id_map{__searches}{$user}{$data};
473 ¬ice("Removed subscription for '$data'");
476 sub cmd_list_search {
477 my ( $data, $server, $win ) = @_;
480 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
482 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
483 $topics = $topics ? "$topics, $topic" : $topic;
487 ¬ice("Search subscriptions for \@$suser: $topics");
492 ¬ice("No search subscriptions set up");
497 my ( $data, $server, $win ) = @_;
499 my $loc = Irssi::settings_get_str("twirssi_location");
502 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
508 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
509 eval { use Digest::MD5; };
513 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
518 $md5 = get("http://twirssi.com/md5sum");
522 ¬ice("Failed to download md5sum from peeron! Aborting.");
526 unless ( open( CUR, $loc ) ) {
528 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
533 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
536 if ( $cur_md5 eq $md5 ) {
537 ¬ice("Current twirssi seems to be up to date.");
543 Irssi::settings_get_bool("twirssi_upgrade_beta")
544 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
545 : "http://twirssi.com/twirssi.pl";
546 ¬ice("Downloading twirssi from $URL");
547 LWP::Simple::getstore( $URL, "$loc.upgrade" );
549 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
550 unless ( open( NEW, "$loc.upgrade" ) ) {
552 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
557 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
560 if ( $new_md5 ne $md5 ) {
561 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
566 rename $loc, "$loc.backup"
567 or ¬ice("Failed to back up $loc: $!. Aborting")
569 rename "$loc.upgrade", $loc
570 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
573 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
574 if ( -e "$dir/autorun/$file" ) {
575 ¬ice("Updating $dir/autorun/$file");
576 unlink "$dir/autorun/$file"
577 or ¬ice("Failed to remove old $file from autorun: $!");
578 symlink "../$file", "$dir/autorun/$file"
579 or ¬ice("Failed to create symlink in autorun directory: $!");
582 ¬ice("Download complete. Reload twirssi with /script load $file");
592 print $fh "type:debug Loading friends page $page...\n"
593 if ( $fh and &debug );
594 my $friends = $twit->friends( { page => $page } );
595 last unless $friends;
596 $new_friends{ $_->{screen_name} } = time foreach @$friends;
598 last if @$friends == 0 or $page == 10;
603 print $fh "type:debug Error during friends list update. Aborted.\n";
607 my ( $added, $removed ) = ( 0, 0 );
608 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
609 foreach ( keys %new_friends ) {
610 next if exists $friends{$_};
615 print $fh "type:debug Scanning for removed friends...\n"
616 if ( $fh and &debug );
617 foreach ( keys %friends ) {
618 next if exists $new_friends{$_};
623 return ( $added, $removed );
627 print scalar localtime, " - get_updates starting" if &debug;
630 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
633 ->print( "Can't find a window named '"
634 . Irssi::settings_get_str('twitter_window')
635 . "'. Create it or change the value of twitter_window" );
638 return unless &logged_in($twit);
640 my ( $fh, $filename ) = File::Temp::tempfile();
641 binmode( $fh, ":utf8" );
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;
697 eval { $tweets = $obj->friends_timeline(); };
701 "type:debug Error during friends_timeline call: $@. Aborted.\n";
705 unless ( ref $tweets ) {
706 if ( $obj->can("get_error") ) {
708 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
709 if ($@) { $error = $obj->get_error() }
710 print $fh "type:debug API Error during friends_timeline call: ",
714 "type:debug API Error during friends_timeline call. Aborted.\n";
719 foreach my $t ( reverse @$tweets ) {
720 my $text = decode_entities( $t->{text} );
721 $text = &hilight($text);
723 if ( Irssi::settings_get_bool("show_reply_context")
724 and $t->{in_reply_to_screen_name} ne $username
725 and $t->{in_reply_to_screen_name}
726 and not exists $friends{ $t->{in_reply_to_screen_name} } )
728 $nicks{ $t->{in_reply_to_screen_name} } = time;
731 $context = $obj->show_status( $t->{in_reply_to_status_id} );
735 my $ctext = decode_entities( $context->{text} );
736 $ctext = &hilight($ctext);
737 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
738 $context->{id}, $username,
739 $context->{user}{screen_name}, $ctext;
740 if ($context->{truncated}) {
741 printf $fh "id:%d account:%s nick:%s type:ellispis %s\n",
742 $context->{id}."-url", $username,
743 $context->{user}{screen_name},
744 "http://twitter.com/$context->{user}{screen_name}/status/$context->{id}";
748 print $fh "type:debug request to get context failed: $@";
751 "type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
756 if $t->{user}{screen_name} eq $username
757 and not Irssi::settings_get_bool("show_own_tweets");
758 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
759 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
760 if ($t->{truncated}) {
761 printf $fh "id:%d account:%s nick:%s type:ellispis %s\n",
762 $t->{id}."-url", $username,
763 $t->{user}{screen_name},
764 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
768 print scalar localtime, " - Polling for replies" if &debug;
770 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
775 print $fh "type:debug Error during replies call. Aborted.\n";
779 foreach my $t ( reverse @$tweets ) {
781 if exists $friends{ $t->{user}{screen_name} };
783 my $text = decode_entities( $t->{text} );
784 $text = &hilight($text);
785 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
786 $t->{id}, $username, $t->{user}{screen_name}, $text;
787 if ($t->{truncated}) {
788 printf $fh "id:%d account:%s nick:%s type:ellispis %s\n",
789 $t->{id}."-url", $username,
790 $t->{user}{screen_name},
791 "http://twitter.com/$t->{user}{screen_name}/status/$t->{id}";
795 print scalar localtime, " - Polling for DMs" if &debug;
798 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
803 print $fh "type:debug Error during direct_messages call. Aborted.\n";
807 foreach my $t ( reverse @$tweets ) {
808 my $text = decode_entities( $t->{text} );
809 $text = &hilight($text);
810 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
811 $t->{id}, $username, $t->{sender_screen_name}, $text;
814 print scalar localtime, " - Polling for subscriptions" if &debug;
815 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
817 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
818 print $fh "type:debug searching for $topic since ",
819 "$id_map{__searches}{$username}{$topic}\n";
821 $search = $obj->search(
824 since_id => $id_map{__searches}{$username}{$topic}
831 "type:debug Error during search($topic) call. Aborted.\n";
835 unless ( $search->{max_id} ) {
837 "type:debug Invalid search results when searching for $topic.",
842 $id_map{__searches}{$username}{$topic} = $search->{max_id};
843 printf $fh "id:%d account:%s type:searchid topic:%s\n",
844 $search->{max_id}, $username, $topic;
846 foreach my $t ( reverse @{ $search->{results} } ) {
847 my $text = decode_entities( $t->{text} );
848 $text = &hilight($text);
849 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
850 $t->{id}, $username, $t->{from_user}, $topic, $text;
855 print scalar localtime, " - Done" if &debug;
862 my $filename = $data->[0];
863 my $attempt = $data->[1];
865 print scalar localtime, " - checking child log at $filename ($attempt)"
869 # first time we run we don't want to print out *everything*, so we just
872 $suppress = 1 unless keys %tweet_cache;
874 if ( open FILE, $filename ) {
879 last if /^__friends__/;
882 foreach my $key (qw/id account nick type topic/) {
883 if (s/^$key:(\S+)\s*//) {
888 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
889 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
893 $new_cache{ $meta{id} } = time;
895 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
901 if ( $meta{account} ne $user ) {
902 $account = "$meta{account}: ";
906 if ( $meta{type} ne 'dm'
907 and Irssi::settings_get_bool("twirssi_track_replies")
911 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
912 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
913 $id_map{__indexes}{ $meta{nick} } = $marker;
914 $marker = ":$marker";
918 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
919 if ( ( $_ =~ /\@$meta{account}\W/i )
920 && Irssi::settings_get_bool("twirssi_hilights") )
922 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
923 $hilight = MSGLEVEL_HILIGHT;
926 if ( $meta{type} =~ /tweet|reply/ ) {
929 ( MSGLEVEL_PUBLIC | $hilight ),
930 $meta{type}, $account, $meta{nick}, $marker, $_
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}{$_}" } grep /^\Q$word/i,
1149 sort keys %{ $id_map{__indexes} };
1152 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1154 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1155 my $prefix = $word =~ s/^@//;
1156 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1157 push @$complist, grep /^\Q$word/i,
1158 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1159 @$complist = map { "\@$_" } @$complist if $prefix;
1163 sub event_send_text {
1164 my ( $line, $server, $win ) = @_;
1165 my $awin = Irssi::active_win();
1167 # if the window where we got our text was the twitter window, and the user
1168 # wants to be lazy, tweet away!
1169 if ( ( $awin->get_active_name() eq $window->{name} )
1170 and Irssi::settings_get_bool("tweet_window_input") )
1172 &cmd_tweet( $line, $server, $win );
1177 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1178 return $poll if $poll >= 60;
1185 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1186 my $c = Irssi::settings_get_str("twirssi_nick_color");
1187 $c = $irssi_to_mirc_colors{$c};
1188 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1190 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1191 my $c = Irssi::settings_get_str("twirssi_topic_color");
1192 $c = $irssi_to_mirc_colors{$c};
1193 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1195 $text =~ s/[\n\r]/ /g;
1200 Irssi::signal_add( "send text", "event_send_text" );
1202 Irssi::theme_register(
1204 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1205 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1206 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1207 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1208 'twirssi_error', 'ERROR: $0',
1212 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1213 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1214 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1215 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1216 Irssi::settings_add_str( "twirssi", "twirssi_location",
1217 ".irssi/scripts/twirssi.pl" );
1218 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1219 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1220 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1221 ".irssi/scripts/twirssi.json" );
1222 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1223 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1224 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1225 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1226 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1227 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1228 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1229 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1230 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1231 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1232 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1233 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1234 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1235 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1237 $last_poll = time - &get_poll_time;
1238 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1241 ->print( "Couldn't find a window named '"
1242 . Irssi::settings_get_str('twitter_window')
1243 . "', trying to create it." );
1245 Irssi::Windowitem::window_create(
1246 Irssi::settings_get_str('twitter_window'), 1 );
1247 $window->set_name( Irssi::settings_get_str('twitter_window') );
1251 Irssi::command_bind( "dm", "cmd_direct" );
1252 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1253 Irssi::command_bind( "tweet", "cmd_tweet" );
1254 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1255 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1256 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1257 Irssi::command_bind( "twitter_login", "cmd_login" );
1258 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1259 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1260 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1261 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1262 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1263 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1264 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1265 Irssi::command_bind( "reply", "cmd_reply" );
1266 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1268 Irssi::command_bind(
1271 print "twits: ", join ", ",
1272 map { "u: $_->{username}" } values %twits;
1273 print "friends: ", join ", ", sort keys %friends;
1274 print "nicks: ", join ", ", sort keys %nicks;
1275 print "searches: ", Dumper \%{ $id_map{__searches} };
1276 print "last poll: $last_poll";
1277 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1278 print DUMP Dumper \%tweet_cache;
1280 print "cache written out to /tmp/twirssi.cache.txt";
1284 Irssi::command_bind(
1287 ¬ice("Twirssi v$VERSION (r$REV); "
1288 . "Net::Twitter v$Net::Twitter::VERSION. "
1290 . JSON::Any::handler()
1291 . ". See details at http://twirssi.com/" );
1294 Irssi::command_bind(
1297 "/twitter_friend <username>",
1299 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1302 Irssi::command_bind(
1305 "/twitter_unfriend <username>",
1307 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1310 Irssi::command_bind( "twitter_updates", "get_updates" );
1311 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1313 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1314 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1316 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1318 my $file = Irssi::settings_get_str("twirssi_replies_store");
1319 if ( $file and -r $file ) {
1320 if ( open( JSON, $file ) ) {
1325 my $ref = JSON::Any->jsonToObj($json);
1327 my $num = keys %{ $id_map{__indexes} };
1328 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1329 $num, ( $num == 1 ? "" : "s" ) );
1333 ¬ice("Failed to load old replies from $file: $!");
1337 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1338 eval "use WWW::Shorten::$provider;";
1342 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1347 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1348 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1356 ->print( "Create a window named "
1357 . Irssi::settings_get_str('twitter_window')
1358 . " or change the value of twitter_window. Then, reload twirssi." );
1361 # vim: set sts=4 expandtab: