9 $Data::Dumper::Indent = 1;
11 use vars qw($VERSION %IRSSI);
13 $VERSION = "2.2.1beta";
14 my ($REV) = '$Rev: 569 $' =~ /(\d+)/;
16 authors => 'Dan Boger',
17 contact => 'zigdon@gmail.com',
19 description => 'Send twitter updates using /tweet. '
20 . 'Can optionally set your bitlbee /away message to same',
21 license => 'GNU GPL v2',
22 url => 'http://twirssi.com',
23 changed => '$Date: 2009-03-19 11:44:03 -0700 (Thu, 19 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 $username = &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\@$defservice $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 $username = &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 $username = &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 $data = &normalize_username($data);
302 if ( exists $twits{$data} ) {
303 ¬ice("Switching to $data");
304 $twit = $twits{$data};
305 if ( $data =~ /(.*)\@(.*)/ ) {
309 ¬ice("Couldn't figure out what service '$data' is on");
312 ¬ice("Unknown user $data");
317 my ( $data, $server, $win ) = @_;
319 $data =~ s/^\s+|\s+$//g;
320 $data = $user unless $data;
321 return unless $data = &valid_username($data);
323 ¬ice("Logging out $data...");
324 $twits{$data}->end_session();
325 delete $twits{$data};
328 &cmd_switch( ( keys %twits )[0], $server, $win );
330 Irssi::timeout_remove($poll) if $poll;
336 my ( $data, $server, $win ) = @_;
339 ( $user, $pass ) = split ' ', $data, 2;
340 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
341 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
343 my @user = split /\s*,\s*/, $autouser;
344 my @pass = split /\s*,\s*/, $autopass;
346 # if a password ends with a '\', it was meant to escape the comma, and
347 # it should be concatinated with the next one
351 while ( $p =~ /\\$/ and @pass ) {
352 $p .= "," . shift @pass;
357 if ( @user != @unescaped ) {
358 ¬ice("Number of usernames doesn't match "
359 . "the number of passwords - auto-login failed" );
362 while ( @user and @unescaped ) {
364 $p = shift @unescaped;
370 ¬ice("/twitter_login requires either a username and password "
371 . "or twitter_usernames and twitter_passwords to be set." );
375 %friends = %nicks = ();
378 if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
379 ( $user, $service ) = ( $1, $2 );
381 $service = Irssi::settings_get_str("twirssi_default_service");
383 $defservice = $service = ucfirst lc $service;
385 eval "use Net::$service";
388 "Failed to load Net::$service when trying to log in as $user: $@");
392 $twit = "Net::$service"->new(
398 unless ( $twit->verify_credentials() ) {
399 ¬ice("Login as $user\@$service failed");
402 &cmd_switch( ( keys %twits )[0], $server, $win );
408 my $rate_limit = $twit->rate_limit_status();
409 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
411 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
417 $twits{"$user\@$service"} = $twit;
418 Irssi::timeout_remove($poll) if $poll;
419 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
420 ¬ice("Logged in as $user\@$service, loading friends list...");
422 ¬ice( "loaded friends: ", scalar keys %friends );
423 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
424 Irssi::settings_set_bool( "twirssi_first_run", 0 );
425 unless ( exists $friends{twirssi} ) {
426 ¬ice("Welcome to twirssi!"
427 . " Perhaps you should add \@twirssi to your friends list,"
428 . " so you can be notified when a new version is release?"
429 . " Just type /twitter_friend twirssi." );
436 ¬ice("Login failed");
441 my ( $data, $server, $win ) = @_;
443 unless ( $twit and $twit->can('search') ) {
444 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
445 . "doesn't support searches." );
449 $data =~ s/^\s+|\s+$//;
453 ¬ice("Usage: /twitter_subscribe <topic>");
457 if ( exists $id_map{__searches}{$user}{$data} ) {
458 ¬ice("Already had a subscription for '$data'");
462 $id_map{__searches}{$user}{$data} = 1;
463 ¬ice("Added subscription for '$data'");
467 my ( $data, $server, $win ) = @_;
469 unless ( $twit and $twit->can('search') ) {
470 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
471 . "doesn't support searches." );
474 $data =~ s/^\s+|\s+$//;
478 ¬ice("Usage: /twitter_unsubscribe <topic>");
482 unless ( exists $id_map{__searches}{$user}{$data} ) {
483 ¬ice("No subscription found for '$data'");
487 delete $id_map{__searches}{$user}{$data};
488 ¬ice("Removed subscription for '$data'");
491 sub cmd_list_search {
492 my ( $data, $server, $win ) = @_;
495 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
497 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
498 $topics = $topics ? "$topics, $topic" : $topic;
502 ¬ice("Search subscriptions for \@$suser: $topics");
507 ¬ice("No search subscriptions set up");
512 my ( $data, $server, $win ) = @_;
514 my $loc = Irssi::settings_get_str("twirssi_location");
517 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
523 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
524 eval { use Digest::MD5; };
528 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
533 $md5 = get("http://twirssi.com/md5sum");
537 ¬ice("Failed to download md5sum from peeron! Aborting.");
541 unless ( open( CUR, $loc ) ) {
543 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
548 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
551 if ( $cur_md5 eq $md5 ) {
552 ¬ice("Current twirssi seems to be up to date.");
558 Irssi::settings_get_bool("twirssi_upgrade_beta")
559 ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
560 : "http://twirssi.com/twirssi.pl";
561 ¬ice("Downloading twirssi from $URL");
562 LWP::Simple::getstore( $URL, "$loc.upgrade" );
564 unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
565 unless ( open( NEW, "$loc.upgrade" ) ) {
567 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
572 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
575 if ( $new_md5 ne $md5 ) {
576 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
581 rename $loc, "$loc.backup"
582 or ¬ice("Failed to back up $loc: $!. Aborting")
584 rename "$loc.upgrade", $loc
585 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
588 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
589 if ( -e "$dir/autorun/$file" ) {
590 ¬ice("Updating $dir/autorun/$file");
591 unlink "$dir/autorun/$file"
592 or ¬ice("Failed to remove old $file from autorun: $!");
593 symlink "../$file", "$dir/autorun/$file"
594 or ¬ice("Failed to create symlink in autorun directory: $!");
597 ¬ice("Download complete. Reload twirssi with /script load $file");
607 print $fh "type:debug Loading friends page $page...\n"
608 if ( $fh and &debug );
609 my $friends = $twit->friends( { page => $page } );
610 last unless $friends;
611 $new_friends{ $_->{screen_name} } = time foreach @$friends;
613 last if @$friends == 0 or $page == 10;
618 print $fh "type:debug Error during friends list update. Aborted.\n";
622 my ( $added, $removed ) = ( 0, 0 );
623 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
624 foreach ( keys %new_friends ) {
625 next if exists $friends{$_};
630 print $fh "type:debug Scanning for removed friends...\n"
631 if ( $fh and &debug );
632 foreach ( keys %friends ) {
633 next if exists $new_friends{$_};
638 return ( $added, $removed );
642 print scalar localtime, " - get_updates starting" if &debug;
645 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
648 ->print( "Can't find a window named '"
649 . Irssi::settings_get_str('twitter_window')
650 . "'. Create it or change the value of twitter_window" );
653 return unless &logged_in($twit);
655 my ( $fh, $filename ) = File::Temp::tempfile();
656 binmode( $fh, ":utf8" );
660 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
661 Irssi::pidwait_add($pid);
662 } elsif ( defined $pid ) { # child
670 foreach ( keys %twits ) {
671 $error += &do_updates( $fh, $_, $twits{$_} );
674 my ( $added, $removed ) = &load_friends($fh);
675 if ( $added + $removed ) {
676 print $fh "type:debug %R***%n Friends list updated: ",
678 sprintf( "%d added", $added ),
679 sprintf( "%d removed", $removed ) ),
682 print $fh "__friends__\n";
683 foreach ( sort keys %friends ) {
684 print $fh "$_ $friends{$_}\n";
688 print $fh "type:debug Update encountered errors. Aborted\n";
689 print $fh $last_poll;
696 print scalar localtime, " - get_updates ends" if &debug;
700 my ( $fh, $username, $obj ) = @_;
702 my $rate_limit = $obj->rate_limit_status();
703 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
704 ¬ice("Rate limit exceeded for $username");
708 print scalar localtime, " - Polling for updates for $username" if &debug;
710 eval { $tweets = $obj->friends_timeline(); };
714 "type:debug Error during friends_timeline call: $@. Aborted.\n";
718 unless ( ref $tweets ) {
719 if ( $obj->can("get_error") ) {
721 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
722 if ($@) { $error = $obj->get_error() }
723 print $fh "type:debug API Error during friends_timeline call: ",
727 "type:debug API Error during friends_timeline call. Aborted.\n";
732 foreach my $t ( reverse @$tweets ) {
733 my $text = decode_entities( $t->{text} );
734 $text = &hilight($text);
736 if ( Irssi::settings_get_bool("show_reply_context")
737 and $t->{in_reply_to_screen_name} ne $username
738 and $t->{in_reply_to_screen_name}
739 and not exists $friends{ $t->{in_reply_to_screen_name} } )
741 $nicks{ $t->{in_reply_to_screen_name} } = time;
744 $context = $obj->show_status( $t->{in_reply_to_status_id} );
748 my $ctext = decode_entities( $context->{text} );
749 $ctext = &hilight($ctext);
750 if ( $context->{truncated} and ref($obj) ne 'Net::Identica' ) {
752 " -- http://twitter.com/$context->{user}{screen_name}"
753 . "/status/$context->{id}";
755 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
756 $context->{id}, $username,
757 $context->{user}{screen_name}, $ctext;
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 if ( $t->{truncated} and ref($obj) ne 'Net::Identica' ) {
771 $text .= " -- http://twitter.com/$t->{user}{screen_name}"
772 . "/status/$t->{id}";
774 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
775 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
778 print scalar localtime, " - Polling for replies" if &debug;
780 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
785 print $fh "type:debug Error during replies call. Aborted.\n";
789 foreach my $t ( reverse @$tweets ) {
791 if exists $friends{ $t->{user}{screen_name} };
793 my $text = decode_entities( $t->{text} );
794 $text = &hilight($text);
795 if ( $t->{truncated} ) {
796 $text .= " -- http://twitter.com/$t->{user}{screen_name}"
797 . "/status/$t->{id}";
799 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
800 $t->{id}, $username, $t->{user}{screen_name}, $text;
803 print scalar localtime, " - Polling for DMs" if &debug;
806 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
811 print $fh "type:debug Error during direct_messages call. Aborted.\n";
815 foreach my $t ( reverse @$tweets ) {
816 my $text = decode_entities( $t->{text} );
817 $text = &hilight($text);
818 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
819 $t->{id}, $username, $t->{sender_screen_name}, $text;
822 print scalar localtime, " - Polling for subscriptions" if &debug;
823 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
825 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
826 print $fh "type:debug searching for $topic since ",
827 "$id_map{__searches}{$username}{$topic}\n";
829 $search = $obj->search(
832 since_id => $id_map{__searches}{$username}{$topic}
839 "type:debug Error during search($topic) call. Aborted.\n";
843 unless ( $search->{max_id} ) {
845 "type:debug Invalid search results when searching for $topic.",
850 $id_map{__searches}{$username}{$topic} = $search->{max_id};
851 printf $fh "id:%d account:%s type:searchid topic:%s\n",
852 $search->{max_id}, $username, $topic;
854 foreach my $t ( reverse @{ $search->{results} } ) {
855 my $text = decode_entities( $t->{text} );
856 $text = &hilight($text);
857 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
858 $t->{id}, $username, $t->{from_user}, $topic, $text;
863 print scalar localtime, " - Done" if &debug;
870 my $filename = $data->[0];
871 my $attempt = $data->[1];
873 print scalar localtime, " - checking child log at $filename ($attempt)"
877 # first time we run we don't want to print out *everything*, so we just
880 $suppress = 1 unless keys %tweet_cache;
882 if ( open FILE, $filename ) {
887 last if /^__friends__/;
890 foreach my $key (qw/id account nick type topic/) {
891 if (s/^$key:(\S+)\s*//) {
896 if ( not $meta{type} or $meta{type} ne 'searchid' ) {
897 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
901 $new_cache{ $meta{id} } = time;
903 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
909 if ( lc $meta{account} ne lc "$user\@$defservice" ) {
910 $meta{account} =~ s/\@(\w+)$//;
914 lc Irssi::settings_get_str("twirssi_default_service") )
916 $account = "$meta{account}: ";
918 $account = "$meta{account}\@$service: ";
923 if ( $meta{type} ne 'dm'
924 and Irssi::settings_get_bool("twirssi_track_replies")
928 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
929 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
930 $id_map{__indexes}{ $meta{nick} } = $marker;
931 $marker = ":$marker";
935 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
937 '@' . substr( $meta{account}, 0, index( $meta{account}, "@" ) );
938 if ( $_ =~ /\Q$nick\E(?:\W|$)/i
939 and Irssi::settings_get_bool("twirssi_hilights") )
941 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
942 $hilight = MSGLEVEL_HILIGHT;
945 if ( $meta{type} =~ /tweet|reply/ ) {
948 ( MSGLEVEL_PUBLIC | $hilight ),
949 $meta{type}, $account, $meta{nick}, $marker, $_
951 } elsif ( $meta{type} eq 'search' ) {
954 ( MSGLEVEL_PUBLIC | $hilight ),
955 $meta{type}, $account, $meta{topic},
956 $meta{nick}, $marker, $_
959 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
961 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
963 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
966 } elsif ( $meta{type} eq 'dm' ) {
969 ( MSGLEVEL_MSGS | $hilight ),
970 $meta{type}, $account, $meta{nick}, $_
972 } elsif ( $meta{type} eq 'searchid' ) {
973 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
975 exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
977 $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
979 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
982 print "Search '$meta{topic}' returned invalid id $meta{id}";
984 } elsif ( $meta{type} eq 'error' ) {
985 push @lines, [ MSGLEVEL_MSGS, $_ ];
986 } elsif ( $meta{type} eq 'debug' ) {
987 print "$_" if &debug,;
989 print "Unknown line type $meta{type}: $_" if &debug,;
999 my ( $f, $t ) = split ' ', $_;
1000 $nicks{$f} = $friends{$f} = $t;
1003 if ($new_last_poll) {
1004 print "new last_poll = $new_last_poll" if &debug;
1006 print "First call, not printing updates" if &debug;
1008 foreach my $line (@lines) {
1009 $window->printformat(
1011 "twirssi_" . $line->[1],
1012 @$line[ 2 .. $#$line ]
1019 or warn "Failed to remove $filename: $!"
1022 # commit the pending cache lines to the actual cache, now that
1023 # we've printed our output
1024 %tweet_cache = ( %tweet_cache, %new_cache );
1026 # keep enough cached tweets, to make sure we don't show duplicates.
1027 foreach ( keys %tweet_cache ) {
1028 next if $tweet_cache{$_} >= $last_poll - 3600;
1029 delete $tweet_cache{$_};
1031 $last_poll = $new_last_poll;
1036 Irssi::settings_get_str("twirssi_replies_store") )
1038 if ( open JSON, ">$file" ) {
1039 print JSON JSON::Any->objToJson( \%id_map );
1042 &ccrap("Failed to write replies to $file: $!");
1052 if ( $attempt < 24 ) {
1053 Irssi::timeout_add_once( 5000, 'monitor_child',
1054 [ $filename, $attempt + 1 ] );
1056 print "Giving up on polling $filename" if &debug;
1057 unlink $filename unless &debug;
1059 return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
1062 my @time = localtime($last_poll);
1063 if ( time - $last_poll < 24 * 60 * 60 ) {
1064 $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
1066 $since = scalar localtime($last_poll);
1069 if ( not $failwhale and time - $last_poll > 60 * 60 ) {
1074 q{ .--./ / | _.---.| },
1085 &ccrap("Haven't been able to get updated tweets since $since");
1090 return Irssi::settings_get_bool("twirssi_debug");
1094 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
1098 $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP );
1104 if ( Irssi::settings_get_bool("tweet_to_away")
1106 and $data !~ /^[dD] / )
1109 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
1111 $server->send_raw("away :$data");
1114 &ccrap( "Can't find bitlbee server.",
1115 "Update bitlbee_server or disable tweet_to_away" );
1125 my $noalert = shift;
1127 if ( length $data > 140 ) {
1128 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1136 sub valid_username {
1137 my $username = shift;
1139 $username = &normalize_username($username);
1141 unless ( exists $twits{$username} ) {
1142 ¬ice("Unknown username $username");
1152 ¬ice("Not logged in! Use /twitter_login username pass!");
1160 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1163 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1164 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1165 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1167 { # /twitter_reply gets a nick:num
1169 @$complist = map { "$_:$id_map{__indexes}{$_}" }
1170 sort { $nicks{$b} <=> $nicks{$a} }
1172 keys %{ $id_map{__indexes} };
1175 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1177 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1178 my $prefix = $word =~ s/^@//;
1179 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1180 push @$complist, grep /^\Q$word/i,
1181 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1182 @$complist = map { "\@$_" } @$complist if $prefix;
1186 sub event_send_text {
1187 my ( $line, $server, $win ) = @_;
1188 my $awin = Irssi::active_win();
1190 # if the window where we got our text was the twitter window, and the user
1191 # wants to be lazy, tweet away!
1192 if ( ( $awin->get_active_name() eq $window->{name} )
1193 and Irssi::settings_get_bool("tweet_window_input") )
1195 &cmd_tweet( $line, $server, $win );
1200 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1201 return $poll if $poll >= 60;
1208 if ( Irssi::settings_get_str("twirssi_nick_color") ) {
1209 my $c = Irssi::settings_get_str("twirssi_nick_color");
1210 $c = $irssi_to_mirc_colors{$c};
1211 $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
1213 if ( Irssi::settings_get_str("twirssi_topic_color") ) {
1214 my $c = Irssi::settings_get_str("twirssi_topic_color");
1215 $c = $irssi_to_mirc_colors{$c};
1216 $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
1218 $text =~ s/[\n\r]/ /g;
1226 my $provider = Irssi::settings_get_str("short_url_provider");
1229 Irssi::settings_get_bool("twirssi_always_shorten")
1230 or &too_long( $data, 1 )
1236 if ( $provider eq 'Bitly' ) {
1237 @args[ 1, 2 ] = split ',',
1238 Irssi::settings_get_str("short_url_args"), 2;
1239 unless ( @args == 3 ) {
1241 "WWW::Shorten::Bitly requires a username and API key.",
1242 "Set short_url_args to username,API_key or change your",
1243 "short_url_provider."
1249 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
1252 my $short = makeashorterlink(@args);
1254 $data =~ s/\Q$url/$short/g;
1256 ¬ice("Failed to shorten $url!");
1265 sub normalize_username {
1268 my ( $username, $service ) = split /\@/, $user, 2;
1270 $service = ucfirst lc $service;
1273 ucfirst lc Irssi::settings_get_str("twirssi_default_service");
1274 unless ( exists $twits{"$username\@$service"} ) {
1276 foreach my $t ( sort keys %twits ) {
1277 next unless $t =~ /^\Q$username\E\@(Twitter|Identica)/;
1283 ¬ice("Can't find a logged in user '$user'");
1288 return "$username\@$service";
1291 Irssi::signal_add( "send text", "event_send_text" );
1293 Irssi::theme_register(
1295 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1296 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1297 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1298 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1299 'twirssi_error', 'ERROR: $0',
1303 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1304 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1305 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1306 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1307 Irssi::settings_add_str( "twirssi", "short_url_args", undef );
1308 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1309 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1310 Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
1311 Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
1312 Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
1313 Irssi::settings_add_str( "twirssi", "twirssi_location",
1314 ".irssi/scripts/twirssi.pl" );
1315 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1316 ".irssi/scripts/twirssi.json" );
1317 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta", 0 );
1318 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1319 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1320 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1321 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1322 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1323 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1324 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick", 1 );
1325 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1326 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts", 1 );
1327 Irssi::settings_add_bool( "twirssi", "twirssi_hilights", 1 );
1328 Irssi::settings_add_bool( "twirssi", "twirssi_always_shorten", 0 );
1329 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1331 $last_poll = time - &get_poll_time;
1332 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1335 ->print( "Couldn't find a window named '"
1336 . Irssi::settings_get_str('twitter_window')
1337 . "', trying to create it." );
1339 Irssi::Windowitem::window_create(
1340 Irssi::settings_get_str('twitter_window'), 1 );
1341 $window->set_name( Irssi::settings_get_str('twitter_window') );
1345 Irssi::command_bind( "dm", "cmd_direct" );
1346 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1347 Irssi::command_bind( "tweet", "cmd_tweet" );
1348 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1349 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1350 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1351 Irssi::command_bind( "twitter_login", "cmd_login" );
1352 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1353 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1354 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1355 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1356 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1357 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1358 Irssi::command_bind( "twitter_updates", "get_updates" );
1359 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1360 Irssi::command_bind( "reply", "cmd_reply" );
1361 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1363 Irssi::command_bind(
1366 print "twits: ", join ", ",
1367 map { "u: $_->{username}\@" . ref($_) } values %twits;
1368 print "selected: $user\@$defservice";
1369 print "friends: ", join ", ", sort keys %friends;
1370 print "nicks: ", join ", ", sort keys %nicks;
1371 print "searches: ", Dumper \%{ $id_map{__searches} };
1372 print "last poll: $last_poll";
1373 if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
1374 print DUMP Dumper \%tweet_cache;
1376 print "cache written out to /tmp/twirssi.cache.txt";
1380 Irssi::command_bind(
1383 ¬ice("Twirssi v$VERSION (r$REV); "
1384 . "Net::Twitter v$Net::Twitter::VERSION. "
1386 . JSON::Any::handler()
1387 . ". See details at http://twirssi.com/" );
1390 Irssi::command_bind(
1393 "/twitter_friend <username>",
1395 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1398 Irssi::command_bind(
1401 "/twitter_unfriend <username>",
1403 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1406 Irssi::command_bind(
1407 "twitter_device_updates",
1409 "/twitter_device_updates none|im|sms",
1410 "update_delivery_device",
1411 sub { ¬ice("Device updated to $_[0]"); }
1414 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1416 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1417 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1419 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1421 my $file = Irssi::settings_get_str("twirssi_replies_store");
1422 if ( $file and -r $file ) {
1423 if ( open( JSON, $file ) ) {
1428 my $ref = JSON::Any->jsonToObj($json);
1430 my $num = keys %{ $id_map{__indexes} };
1431 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1432 $num, ( $num == 1 ? "" : "s" ) );
1436 ¬ice("Failed to load old replies from $file: $!");
1440 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1441 ¬ice("Loading WWW::Shorten::$provider...");
1442 eval "use WWW::Shorten::$provider;";
1446 "Failed to load WWW::Shorten::$provider - either clear",
1447 "short_url_provider or install the CPAN module"
1452 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1453 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1461 ->print( "Create a window named "
1462 . Irssi::settings_get_str('twitter_window')
1463 . " or change the value of twitter_window. Then, reload twirssi." );
1466 # vim: set sts=4 expandtab: