10 $Data::Dumper::Indent = 1;
12 use vars qw($VERSION %IRSSI);
15 my ($REV) = '$Rev: 443 $' =~ /(\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-01-29 18:25:38 -0800 (Thu, 29 Jan 2009) $',
37 my %irssi_to_mirc_colors = (
57 my ( $data, $server, $win ) = @_;
59 return unless &logged_in($twit);
61 my ( $target, $text ) = split ' ', $data, 2;
62 unless ( $target and $text ) {
63 ¬ice("Usage: /dm <nick> <message>");
67 &cmd_direct_as( "$user $data", $server, $win );
71 my ( $data, $server, $win ) = @_;
73 return unless &logged_in($twit);
75 my ( $username, $target, $text ) = split ' ', $data, 3;
76 unless ( $username and $target and $text ) {
77 ¬ice("Usage: /dm_as <username> <nick> <message>");
81 return unless &valid_username($username);
84 unless ( $twits{$username}
85 ->new_direct_message( { user => $target, text => $text } ) )
87 ¬ice("DM to $target failed");
93 ¬ice("DM caused an error. Aborted");
97 ¬ice("DM sent to $target");
98 $nicks{$target} = time;
102 my ( $data, $server, $win ) = @_;
104 return unless &logged_in($twit);
106 $data =~ s/^\s+|\s+$//;
108 ¬ice("Usage: /tweet <update>");
112 &cmd_tweet_as( "$user $data", $server, $win );
116 my ( $data, $server, $win ) = @_;
118 return unless &logged_in($twit);
120 $data =~ s/^\s+|\s+$//;
121 my ( $username, $data ) = split ' ', $data, 2;
123 unless ( $username and $data ) {
124 ¬ice("Usage: /tweet_as <username> <update>");
128 return unless &valid_username($username);
130 if ( &too_long($data) and Irssi::settings_get_str("short_url_provider") ) {
131 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
133 my $short = makeashorterlink($url);
134 $data =~ s/\Q$url/$short/g;
139 return if &too_long($data);
142 unless ( $twits{$username}->update($data) )
144 ¬ice("Update failed");
150 ¬ice("Update caused an error. Aborted.");
154 foreach ( $data =~ /@([-\w]+)/ ) {
158 my $away = &update_away($data);
160 ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
164 my ( $data, $server, $win ) = @_;
166 return unless &logged_in($twit);
168 $data =~ s/^\s+|\s+$//;
170 ¬ice("Usage: /reply <nick[:num]> <update>");
174 $data =~ s/^\s+|\s+$//;
175 my ( $id, $data ) = split ' ', $data, 2;
176 unless ( $id and $data ) {
177 ¬ice("Usage: /reply_as <nick[:num]> <update>");
181 &cmd_reply_as( "$user $id $data", $server, $win );
185 my ( $data, $server, $win ) = @_;
187 unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
188 ¬ice("twirssi_track_replies is required in order to reply to "
189 . "specific tweets. Either enable it, or just use /tweet "
190 . "\@username <text>." );
194 return unless &logged_in($twit);
196 $data =~ s/^\s+|\s+$//;
197 my ( $username, $id, $data ) = split ' ', $data, 3;
199 unless ( $username and $data ) {
200 ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
204 return unless &valid_username($username);
207 $id =~ s/[^\w\d\-:]+//g;
208 ( $nick, $id ) = split /:/, $id;
209 unless ( exists $id_map{ lc $nick } ) {
210 ¬ice("Can't find a tweet from $nick to reply to!");
214 $id = $id_map{__indexes}{$nick} unless $id;
215 unless ( $id_map{ lc $nick }[$id] ) {
216 ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
220 # remove any @nick at the beginning of the reply, as we'll add it anyway
221 $data =~ s/^\s*\@?$nick\s*//;
222 $data = "\@$nick " . $data;
224 if ( Irssi::settings_get_str("short_url_provider") ) {
225 foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
227 my $short = makeashorterlink($url);
228 $data =~ s/\Q$url/$short/g;
233 return if &too_long($data);
237 $twits{$username}->update(
240 in_reply_to_status_id => $id_map{ lc $nick }[$id]
245 ¬ice("Update failed");
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");
279 unless ( $twit->$api_name($data) )
281 ¬ice("$api_name failed");
287 ¬ice("$api_name caused an error. Aborted.");
291 &$post_ref($data) if $post_ref;
296 my ( $data, $server, $win ) = @_;
298 $data =~ s/^\s+|\s+$//g;
299 if ( exists $twits{$data} ) {
300 ¬ice("Switching to $data");
301 $twit = $twits{$data};
304 ¬ice("Unknown user $data");
309 my ( $data, $server, $win ) = @_;
311 $data =~ s/^\s+|\s+$//g;
312 return unless &valid_username($data);
315 ¬ice("Logging out $data...");
316 $twits{$data}->end_session();
317 delete $twits{$data};
319 ¬ice("Logging out $user...");
320 $twit->end_session();
322 delete $twits{$user};
324 &cmd_switch( ( keys %twits )[0], $server, $win );
326 Irssi::timeout_remove($poll) if $poll;
333 my ( $data, $server, $win ) = @_;
336 ( $user, $pass ) = split ' ', $data, 2;
337 } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
338 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
340 my @user = split /\s*,\s*/, $autouser;
341 my @pass = split /\s*,\s*/, $autopass;
342 if ( @user != @pass ) {
343 ¬ice("Number of usernames doesn't match "
344 . "the number of passwords - auto-login failed" );
347 while ( @user and @pass ) {
355 ¬ice("/twitter_login requires either a username and password "
356 . "or twitter_usernames and twitter_passwords to be set." );
360 %friends = %nicks = ();
362 $twit = Net::Twitter->new(
368 unless ( $twit->verify_credentials() ) {
369 ¬ice("Login as $user failed");
372 &cmd_switch( ( keys %twits )[0], $server, $win );
378 my $rate_limit = $twit->rate_limit_status();
379 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
381 "Rate limit exceeded, try again after $rate_limit->{reset_time}"
387 $twits{$user} = $twit;
388 Irssi::timeout_remove($poll) if $poll;
389 $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
390 ¬ice("Logged in as $user, loading friends list...");
392 ¬ice( "loaded friends: ", scalar keys %friends );
393 if ( Irssi::settings_get_bool("twirssi_first_run") ) {
394 Irssi::settings_set_bool( "twirssi_first_run", 0 );
395 unless ( exists $friends{twirssi} ) {
396 ¬ice("Welcome to twirssi!"
397 . " Perhaps you should add \@twirssi to your friends list,"
398 . " so you can be notified when a new version is release?"
399 . " Just type /twitter_friend twirssi." );
406 ¬ice("Login failed");
411 my ( $data, $server, $win ) = @_;
413 unless ( $twit and $twit->can('search') ) {
414 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
415 . "doesn't support searches." );
419 $data =~ s/^\s+|\s+$//;
421 if ( exists $id_map{__searches}{$user}{$data} ) {
422 ¬ice("Already had a subscription for '$data'");
426 $id_map{__searches}{$user}{$data} = 1;
427 ¬ice("Added subscription for '$data'");
431 my ( $data, $server, $win ) = @_;
433 unless ( $twit and $twit->can('search') ) {
434 ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
435 . "doesn't support searches." );
438 $data =~ s/^\s+|\s+$//;
440 unless ( exists $id_map{__searches}{$user}{$data} ) {
441 ¬ice("No subscription found for '$data'");
445 delete $id_map{__searches}{$user}{$data};
446 ¬ice("Removed subscription for '$data'");
449 sub cmd_list_search {
450 my ( $data, $server, $win ) = @_;
453 foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
455 foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
456 $topics = $topics ? "$topics, $topic" : $topic;
460 ¬ice("Search subscriptions for \@$suser: $topics");
465 ¬ice("No search subscriptions set up");
470 my ( $data, $server, $win ) = @_;
472 my $loc = Irssi::settings_get_str("twirssi_location");
475 "$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
480 if ( not -x "/usr/bin/md5sum" and not $data ) {
482 "/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
489 eval { use Digest::MD5; };
493 "Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
498 $md5 = get("http://twirssi.com/md5sum");
502 ¬ice("Failed to download md5sum from peeron! Aborting.");
506 unless ( open( CUR, $loc ) ) {
508 "Failed to read $loc. Check that /set twirssi_location is set to the correct location."
513 my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
516 if ( $cur_md5 eq $md5 ) {
517 ¬ice("Current twirssi seems to be up to date.");
522 my $URL = "http://twirssi.com/twirssi.pl";
523 ¬ice("Downloading twirssi from $URL");
524 LWP::Simple::getstore( $URL, "$loc.upgrade" );
527 unless ( open( NEW, "$loc.upgrade" ) ) {
529 "Failed to read $loc.upgrade. Check that /set twirssi_location is set to the correct location."
534 my $new_md5 = Digest::MD5::md5_hex(<NEW>);
537 if ( $new_md5 ne $md5 ) {
538 ¬ice("MD5 verification failed. expected $md5, got $new_md5");
543 rename $loc, "$loc.backup"
544 or ¬ice("Failed to back up $loc: $!. Aborting")
546 rename "$loc.upgrade", $loc
547 or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
550 my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
551 if ( -e "$dir/autorun/$file" ) {
552 ¬ice("Updating $dir/autorun/$file");
553 unlink "$dir/autorun/$file"
554 or ¬ice("Failed to remove old $file from autorun: $!");
555 symlink "../$file", "$dir/autorun/$file"
556 or ¬ice("Failed to create symlink in autorun directory: $!");
559 ¬ice("Download complete. Reload twirssi with /script load $file");
569 print $fh "type:debug Loading friends page $page...\n"
570 if ( $fh and &debug );
571 my $friends = $twit->friends( { page => $page } );
572 last unless $friends;
573 $new_friends{ $_->{screen_name} } = time foreach @$friends;
575 last if @$friends == 0 or $page == 10;
580 print $fh "type:debug Error during friends list update. Aborted.\n";
584 my ( $added, $removed ) = ( 0, 0 );
585 print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
586 foreach ( keys %new_friends ) {
587 next if exists $friends{$_};
592 print $fh "type:debug Scanning for removed friends...\n"
593 if ( $fh and &debug );
594 foreach ( keys %friends ) {
595 next if exists $new_friends{$_};
600 return ( $added, $removed );
604 print scalar localtime, " - get_updates starting" if &debug;
607 Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
610 ->print( "Can't find a window named '"
611 . Irssi::settings_get_str('twitter_window')
612 . "'. Create it or change the value of twitter_window" );
615 return unless &logged_in($twit);
617 my ( $fh, $filename ) = File::Temp::tempfile();
621 Irssi::timeout_add_once( 5000, 'monitor_child', [ $filename, 0 ] );
622 Irssi::pidwait_add($pid);
623 } elsif ( defined $pid ) { # child
631 $error += &do_updates( $fh, $user, $twit );
632 foreach ( keys %twits ) {
634 $error += &do_updates( $fh, $_, $twits{$_} );
637 my ( $added, $removed ) = &load_friends($fh);
638 if ( $added + $removed ) {
639 print $fh "type:debug %R***%n Friends list updated: ",
641 sprintf( "%d added", $added ),
642 sprintf( "%d removed", $removed ) ),
645 print $fh "__friends__\n";
646 foreach ( sort keys %friends ) {
647 print $fh "$_ $friends{$_}\n";
651 print $fh "type:debug Update encountered errors. Aborted\n";
652 print $fh $last_poll;
659 print scalar localtime, " - get_updates ends" if &debug;
663 my ( $fh, $username, $obj ) = @_;
665 my $rate_limit = $obj->rate_limit_status();
666 if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
667 ¬ice("Rate limit exceeded for $username");
671 print scalar localtime, " - Polling for updates for $username" if &debug;
674 $tweets = $obj->friends_timeline(
675 { since => HTTP::Date::time2str($last_poll) } );
679 print $fh "type:debug Error during friends_timeline call. Aborted.\n";
683 unless ( ref $tweets ) {
684 if ( $obj->can("get_error") ) {
686 eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
687 if ($@) { $error = $obj->get_error() }
688 print $fh "type:debug API Error during friends_timeline call: ",
692 "type:debug API Error during friends_timeline call. Aborted.\n";
697 foreach my $t ( reverse @$tweets ) {
698 my $text = decode_entities( $t->{text} );
699 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
700 $text =~ s/[\n\r]/ /g;
702 if ( Irssi::settings_get_bool("show_reply_context")
703 and $t->{in_reply_to_screen_name} ne $username
704 and $t->{in_reply_to_screen_name}
705 and not exists $friends{ $t->{in_reply_to_screen_name} } )
707 $nicks{ $t->{in_reply_to_screen_name} } = time;
710 $context = $obj->show_status( $t->{in_reply_to_status_id} );
714 my $ctext = decode_entities( $context->{text} );
715 $ctext =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
716 $ctext =~ s/[\n\r]/ /g;
717 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
718 $context->{id}, $username,
719 $context->{user}{screen_name}, $ctext;
722 print $fh "type:debug request to get context failed: $@";
725 "type:debug Failed to get context from $t->{in_reply_to_screen_name}"
730 if $t->{user}{screen_name} eq $username
731 and not Irssi::settings_get_bool("show_own_tweets");
732 printf $fh "id:%d account:%s nick:%s type:%s %s\n",
733 $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
736 print scalar localtime, " - Polling for replies" if &debug;
738 $tweets = $obj->replies( { since => HTTP::Date::time2str($last_poll) } )
743 print $fh "type:debug Error during replies call. Aborted.\n";
747 foreach my $t ( reverse @$tweets ) {
749 if exists $friends{ $t->{user}{screen_name} };
751 my $text = decode_entities( $t->{text} );
752 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
753 $text =~ s/[\n\r]/ /g;
754 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
755 $t->{id}, $username, $t->{user}{screen_name}, $text;
758 print scalar localtime, " - Polling for DMs" if &debug;
761 $obj->direct_messages( { since => HTTP::Date::time2str($last_poll) } )
766 print $fh "type:debug Error during direct_messages call. Aborted.\n";
770 foreach my $t ( reverse @$tweets ) {
771 my $text = decode_entities( $t->{text} );
772 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
773 $text =~ s/[\n\r]/ /g;
774 printf $fh "id:%d account:%s nick:%s type:dm %s\n",
775 $t->{id}, $username, $t->{sender_screen_name}, $text;
778 print scalar localtime, " - Polling for subscriptions" if &debug;
779 if ( $obj->can('search') and $id_map{__searches}{$username} ) {
781 foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
783 $search = $obj->search(
786 since_id => $id_map{__searches}{$username}{$topic}
793 "type:debug Error during search($topic) call. Aborted.\n";
797 unless ( $search->{max_id} ) {
799 "type:debug Invalid search results when searching for $topic. Aborted.\n";
803 $id_map{__searches}{$username}{$topic} = $search->{max_id};
804 printf $fh "id:%d account:%s type:searchid topic:%s\n",
805 $search->{max_id}, $username, $topic;
807 foreach my $t ( reverse @{ $search->{results} } ) {
808 my $text = decode_entities( $t->{text} );
809 $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
810 $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g;
811 $text =~ s/[\n\r]/ /g;
812 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
813 $t->{id}, $username, $t->{from_user}, $topic, $text;
818 print scalar localtime, " - Done" if &debug;
825 my $filename = $data->[0];
826 my $attempt = $data->[1];
828 print scalar localtime, " - checking child log at $filename ($attempt)"
831 if ( open FILE, $filename ) {
835 last if /^__friends__/;
838 foreach my $key (qw/id account nick type topic/) {
839 if (s/^$key:(\S+)\s*//) {
844 next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
845 $tweet_cache{ $meta{id} } = time;
847 if ( $meta{account} ne $user ) {
848 $account = "$meta{account}: ";
852 if ( $meta{type} ne 'dm'
853 and Irssi::settings_get_bool("twirssi_track_replies")
857 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
858 $id_map{ lc $meta{nick} }[$marker] = $meta{id};
859 $id_map{__indexes}{ $meta{nick} } = $marker;
860 $marker = ":$marker";
864 $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
865 if ( $_ =~ /\@($meta{account})\W/ ) {
866 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
867 $hilight = MSGLEVEL_HILIGHT;
870 if ( $meta{type} =~ /tweet|reply/ ) {
873 ( MSGLEVEL_PUBLIC | $hilight ),
874 $meta{type}, $account, $meta{nick}, $marker, $_
876 } elsif ( $meta{type} eq 'search' ) {
879 ( MSGLEVEL_PUBLIC | $hilight ),
880 $meta{type}, $account, $meta{topic},
881 $meta{nick}, $marker, $_
883 } elsif ( $meta{type} eq 'dm' ) {
886 ( MSGLEVEL_MSGS | $hilight ),
887 $meta{type}, $account, $meta{nick}, $_
889 } elsif ( $meta{type} eq 'searchid' ) {
890 $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
892 print "Search '$meta{topic}' id set to $meta{id}" if &debug;
893 } elsif ( $meta{type} eq 'error' ) {
894 push @lines, [ MSGLEVEL_MSGS, $_ ];
895 } elsif ( $meta{type} eq 'debug' ) {
896 print "$_" if &debug,;
898 print "Unknown line type $meta{type}: $_" if &debug,;
908 my ( $f, $t ) = split ' ', $_;
909 $nicks{$f} = $friends{$f} = $t;
912 if ($new_last_poll) {
913 print "new last_poll = $new_last_poll" if &debug;
914 for my $line (@lines) {
915 $window->printformat(
917 "twirssi_" . $line->[1],
918 @$line[ 2 .. $#$line ]
924 or warn "Failed to remove $filename: $!"
927 # keep enough cached tweets, to make sure we don't show duplicates.
928 foreach ( keys %tweet_cache ) {
929 next if $tweet_cache{$_} >= $last_poll;
930 delete $tweet_cache{$_};
932 $last_poll = $new_last_poll;
937 Irssi::settings_get_str("twirssi_replies_store") )
939 if ( open JSON, ">$file" ) {
940 print JSON JSON::Any->objToJson( \%id_map );
943 ¬ice("Failed to write replies to $file: $!");
952 if ( $attempt < 12 ) {
953 Irssi::timeout_add_once( 5000, 'monitor_child',
954 [ $filename, $attempt + 1 ] );
956 ¬ice("Giving up on polling $filename");
957 unlink $filename unless &debug;
962 return Irssi::settings_get_bool("twirssi_debug");
966 $window->print( "%R***%n @_", MSGLEVEL_PUBLIC );
972 if ( Irssi::settings_get_bool("tweet_to_away")
974 and $data !~ /^[dD] / )
977 Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
979 $server->send_raw("away :$data");
982 ¬ice( "Can't find bitlbee server.",
983 "Update bitlbee_server or disable tweet_to_away" );
995 if ( length $data > 140 ) {
996 ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
1004 sub valid_username {
1005 my $username = shift;
1007 unless ( exists $twits{$username} ) {
1008 ¬ice("Unknown username $username");
1018 ¬ice("Not logged in! Use /twitter_login username pass!");
1026 my ( $complist, $window, $word, $linestart, $want_space ) = @_;
1029 $linestart =~ /^\/twitter_reply(?:_as)?\s*$/
1030 or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
1031 and $linestart =~ /^\/reply(?:_as)?\s*$/ )
1033 { # /twitter_reply gets a nick:num
1035 @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i,
1036 sort keys %{ $id_map{__indexes} };
1039 # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
1041 if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
1042 my $prefix = $word =~ s/^@//;
1043 $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
1044 push @$complist, grep /^\Q$word/i,
1045 sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
1046 @$complist = map { "\@$_" } @$complist if $prefix;
1050 sub event_send_text {
1051 my ( $line, $server, $win ) = @_;
1052 my $awin = Irssi::active_win();
1054 # if the window where we got our text was the twitter window, and the user
1055 # wants to be lazy, tweet away!
1056 if ( ( $awin->get_active_name() eq $window->{name} )
1057 and Irssi::settings_get_bool("tweet_window_input") )
1059 &cmd_tweet( $line, $server, $win );
1064 my $poll = Irssi::settings_get_int("twitter_poll_interval");
1065 return $poll if $poll >= 60;
1069 Irssi::signal_add( "send text", "event_send_text" );
1071 Irssi::theme_register(
1073 'twirssi_tweet', '[$0%B@$1%n$2] $3',
1074 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
1075 'twirssi_reply', '[$0\--> %B@$1%n$2] $3',
1076 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2',
1077 'twirssi_error', 'ERROR: $0',
1081 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
1082 Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" );
1083 Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" );
1084 Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" );
1085 Irssi::settings_add_str( "twirssi", "twirssi_location",
1086 ".irssi/scripts/twirssi.pl" );
1087 Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
1088 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
1089 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
1090 ".irssi/scripts/twirssi.json" );
1091 Irssi::settings_add_bool( "twirssi", "tweet_to_away", 0 );
1092 Irssi::settings_add_bool( "twirssi", "show_reply_context", 0 );
1093 Irssi::settings_add_bool( "twirssi", "show_own_tweets", 1 );
1094 Irssi::settings_add_bool( "twirssi", "twirssi_debug", 0 );
1095 Irssi::settings_add_bool( "twirssi", "twirssi_first_run", 1 );
1096 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies", 1 );
1097 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
1098 Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
1100 $last_poll = time - &get_poll_time;
1101 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
1104 Irssi::Windowitem::window_create(
1105 Irssi::settings_get_str('twitter_window'), 1 );
1106 $window->set_name( Irssi::settings_get_str('twitter_window') );
1110 Irssi::command_bind( "dm", "cmd_direct" );
1111 Irssi::command_bind( "dm_as", "cmd_direct_as" );
1112 Irssi::command_bind( "tweet", "cmd_tweet" );
1113 Irssi::command_bind( "tweet_as", "cmd_tweet_as" );
1114 Irssi::command_bind( "twitter_reply", "cmd_reply" );
1115 Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" );
1116 Irssi::command_bind( "twitter_login", "cmd_login" );
1117 Irssi::command_bind( "twitter_logout", "cmd_logout" );
1118 Irssi::command_bind( "twitter_switch", "cmd_switch" );
1119 Irssi::command_bind( "twitter_subscribe", "cmd_add_search" );
1120 Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" );
1121 Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
1122 Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" );
1123 if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
1124 Irssi::command_bind( "reply", "cmd_reply" );
1125 Irssi::command_bind( "reply_as", "cmd_reply_as" );
1127 Irssi::command_bind(
1130 print "twits: ", join ", ",
1131 map { "u: $_->{username}" } values %twits;
1132 print "friends: ", join ", ", sort keys %friends;
1133 print "nicks: ", join ", ", sort keys %nicks;
1134 print "searches: ", Dumper \%{ $id_map{__searches} };
1135 print "last poll: $last_poll";
1138 Irssi::command_bind(
1141 ¬ice("Twirssi v$VERSION (r$REV); "
1142 . "Net::Twitter v$Net::Twitter::VERSION. "
1144 . JSON::Any::handler()
1145 . ". See details at http://twirssi.com/" );
1148 Irssi::command_bind(
1151 "/twitter_friend <username>",
1153 sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
1156 Irssi::command_bind(
1159 "/twitter_unfriend <username>",
1161 sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
1164 Irssi::command_bind( "twitter_updates", "get_updates" );
1165 Irssi::signal_add_last( 'complete word' => \&sig_complete );
1167 ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N (r$REV)");
1168 ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs");
1170 " %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
1172 my $file = Irssi::settings_get_str("twirssi_replies_store");
1173 if ( $file and -r $file ) {
1174 if ( open( JSON, $file ) ) {
1179 my $ref = JSON::Any->jsonToObj($json);
1181 my $num = keys %{ $id_map{__indexes} };
1182 ¬ice( sprintf "Loaded old replies from %d contact%s.",
1183 $num, ( $num == 1 ? "" : "s" ) );
1186 ¬ice("Failed to load old replies from $file: $!");
1190 if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
1191 eval "use WWW::Shorten::$provider;";
1195 "Failed to load WWW::Shorten::$provider - either clear short_url_provider or install the CPAN module"
1200 if ( my $autouser = Irssi::settings_get_str("twitter_usernames")
1201 and my $autopass = Irssi::settings_get_str("twitter_passwords") )
1208 ->print( "Create a window named "
1209 . Irssi::settings_get_str('twitter_window')
1210 . " or change the value of twitter_window. Then, reload twirssi." );
1213 # vim: set sts=4 expandtab: