10 use POSIX qw/:sys_wait_h/;
 
  11 use Net::Twitter qw/3.05/;
 
  12 $Data::Dumper::Indent = 1;
 
  14 use vars qw($VERSION %IRSSI);
 
  16 $VERSION = "2.4.2beta";
 
  18     authors     => 'Dan Boger',
 
  19     contact     => 'zigdon@gmail.com',
 
  21     description => 'Send twitter updates using /tweet.  '
 
  22       . 'Can optionally set your bitlbee /away message to same',
 
  23     license => 'GNU GPL v2',
 
  24     url     => 'http://twirssi.com',
 
  25     changed => 'Fri Jan 22 14:40:48 PST 2010',
 
  36 my $last_friends_poll = 0;
 
  44 my %fix_replies_index;
 
  46 my %irssi_to_mirc_colors = (
 
  66     my ( $data, $server, $win ) = @_;
 
  68     return unless &logged_in($twit);
 
  70     my ( $target, $text ) = split ' ', $data, 2;
 
  71     unless ( $target and $text ) {
 
  72         ¬ice("Usage: /dm <nick> <message>");
 
  76     &cmd_direct_as( "$user $data", $server, $win );
 
  80     my ( $data, $server, $win ) = @_;
 
  82     return unless &logged_in($twit);
 
  84     my ( $username, $target, $text ) = split ' ', $data, 3;
 
  85     unless ( $username and $target and $text ) {
 
  86         ¬ice("Usage: /dm_as <username> <nick> <message>");
 
  90     return unless $username = &valid_username($username);
 
  93         if ( $twits{$username}
 
  94             ->new_direct_message( { user => $target, text => $text } ) )
 
  96             ¬ice("DM sent to $target: $text");
 
  97             $nicks{$target} = time;
 
 101                 $error = JSON::Any->jsonToObj( $twits{$username}->get_error() );
 
 102                 $error = $error->{error};
 
 104             die $error if $error;
 
 105             ¬ice("DM to $target failed");
 
 110         ¬ice("DM caused an error: $@");
 
 116     my ( $data, $server, $win ) = @_;
 
 118     return unless &logged_in($twit);
 
 120     $data =~ s/^\s+|\s+$//;
 
 122         ¬ice("Usage: /retweet <nick[:num]> [comment]");
 
 126     my ( $id, $data ) = split ' ', $data, 2;
 
 128     &cmd_retweet_as( "$user $id $data", $server, $win );
 
 132     my ( $data, $server, $win ) = @_;
 
 134     return unless &logged_in($twit);
 
 136     $data =~ s/^\s+|\s+$//;
 
 137     my ( $username, $id, $data ) = split ' ', $data, 3;
 
 140         ¬ice("Usage: /retweet_as <username> <nick[:num]> [comment]");
 
 144     return unless $username = &valid_username($username);
 
 147     $id =~ s/[^\w\d\-:]+//g;
 
 148     ( $nick, $id ) = split /:/, $id;
 
 149     unless ( exists $id_map{ lc $nick } ) {
 
 150         ¬ice("Can't find a tweet from $nick to retweet!");
 
 154     $id = $id_map{__indexes}{$nick} unless $id;
 
 155     unless ( $id_map{ lc $nick }[$id] ) {
 
 156         ¬ice("Can't find a tweet numbered $id from $nick to retweet!");
 
 160     unless ( $id_map{__tweets}{ lc $nick }[$id] ) {
 
 161         ¬ice("The text of this tweet isn't saved, sorry!");
 
 165 # Irssi::settings_add_str( "twirssi", "twirssi_retweet_format", 'RT $n: $t ${-- $c$}' );
 
 166     my $text = Irssi::settings_get_str("twirssi_retweet_format");
 
 167     $text =~ s/\$n/\@$nick/g;
 
 169         $text =~ s/\${|\$}//g;
 
 170         $text =~ s/\$c/$data/;
 
 172         $text =~ s/\${.*?\$}//;
 
 174     $text =~ s/\$t/$id_map{__tweets}{ lc $nick }[$id]/;
 
 176     my $modified = $data;
 
 177     $data = &shorten($text);
 
 179     return if $modified and &too_long($data);
 
 185             $success = $twits{$username}->update(
 
 189                     # in_reply_to_status_id => $id_map{ lc $nick }[$id]
 
 194               $twits{$username}->retweet( { id => $id_map{ lc $nick }[$id] } );
 
 195             $success = $success->{id} if ref $success;
 
 197         ¬ice("Update failed") unless $success;
 
 199     return unless $success;
 
 202         ¬ice("Update caused an error: $@.  Aborted");
 
 206     foreach ( $data =~ /@([-\w]+)/ ) {
 
 210     ¬ice("Retweet sent");
 
 214     my ( $data, $server, $win ) = @_;
 
 216     return unless &logged_in($twit);
 
 218     $data =~ s/^\s+|\s+$//;
 
 220         ¬ice("Usage: /tweet <update>");
 
 224     &cmd_tweet_as( "$user\@$defservice $data", $server, $win );
 
 228     my ( $data, $server, $win ) = @_;
 
 230     return unless &logged_in($twit);
 
 232     $data =~ s/^\s+|\s+$//;
 
 233     $data =~ s/\s\s+/ /g;
 
 234     my ( $username, $data ) = split ' ', $data, 2;
 
 236     unless ( $username and $data ) {
 
 237         ¬ice("Usage: /tweet_as <username> <update>");
 
 241     return unless $username = &valid_username($username);
 
 243     $data = &shorten($data);
 
 245     return if &too_long($data);
 
 249         unless ( $twits{$username}->update($data) )
 
 251             ¬ice("Update failed");
 
 255     return unless $success;
 
 258         ¬ice("Update caused an error: $@.  Aborted.");
 
 262     foreach ( $data =~ /@([-\w]+)/ ) {
 
 266     my $away = &update_away($data);
 
 268     ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
 
 272     my ( $data, $server, $win ) = @_;
 
 274     return unless &logged_in($twit);
 
 276     $data =~ s/^\s+|\s+$//;
 
 278         ¬ice("Usage: /reply <nick[:num]> <update>");
 
 282     my ( $id, $data ) = split ' ', $data, 2;
 
 283     unless ( $id and $data ) {
 
 284         ¬ice("Usage: /reply <nick[:num]> <update>");
 
 288     &cmd_reply_as( "$user $id $data", $server, $win );
 
 292     my ( $data, $server, $win ) = @_;
 
 294     return unless &logged_in($twit);
 
 296     $data =~ s/^\s+|\s+$//;
 
 297     my ( $username, $id, $data ) = split ' ', $data, 3;
 
 299     unless ( $username and $data ) {
 
 300         ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
 
 304     return unless $username = &valid_username($username);
 
 307     $id =~ s/[^\w\d\-:]+//g;
 
 308     ( $nick, $id ) = split /:/, $id;
 
 309     unless ( exists $id_map{ lc $nick } ) {
 
 310         ¬ice("Can't find a tweet from $nick to reply to!");
 
 314     $id = $id_map{__indexes}{$nick} unless $id;
 
 315     unless ( $id_map{ lc $nick }[$id] ) {
 
 316         ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
 
 320     if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
 
 322         # remove any @nick at the beginning of the reply, as we'll add it anyway
 
 323         $data =~ s/^\s*\@?$nick\s*//;
 
 324         $data = "\@$nick " . $data;
 
 327     $data = &shorten($data);
 
 329     return if &too_long($data);
 
 334             $twits{$username}->update(
 
 337                     in_reply_to_status_id => $id_map{ lc $nick }[$id]
 
 342             ¬ice("Update failed");
 
 346     return unless $success;
 
 349         ¬ice("Update caused an error: $@.  Aborted");
 
 353     foreach ( $data =~ /@([-\w]+)/ ) {
 
 357     my $away = &update_away($data);
 
 359     ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
 
 363     my ( $usage_str, $api_name, $post_ref ) = @_;
 
 366         my ( $data, $server, $win ) = @_;
 
 368         return unless &logged_in($twit);
 
 370         $data =~ s/^\s+|\s+$//;
 
 372             ¬ice("Usage: $usage_str");
 
 378             unless ( $twit->$api_name($data) )
 
 380                 ¬ice("$api_name failed");
 
 384         return unless $success;
 
 387             ¬ice("$api_name caused an error.  Aborted.");
 
 391         &$post_ref($data) if $post_ref;
 
 396     my ( $data, $server, $win ) = @_;
 
 398     $data =~ s/^\s+|\s+$//g;
 
 399     $data = &normalize_username($data);
 
 400     if ( exists $twits{$data} ) {
 
 401         ¬ice("Switching to $data");
 
 402         $twit = $twits{$data};
 
 403         if ( $data =~ /(.*)\@(.*)/ ) {
 
 407             ¬ice("Couldn't figure out what service '$data' is on");
 
 410         ¬ice("Unknown user $data");
 
 415     my ( $data, $server, $win ) = @_;
 
 417     $data =~ s/^\s+|\s+$//g;
 
 418     $data = $user unless $data;
 
 419     return unless $data = &valid_username($data);
 
 421     ¬ice("Logging out $data...");
 
 422     eval { $twits{$data}->end_session(); };
 
 423     delete $twits{$data};
 
 426         &cmd_switch( ( keys %twits )[0], $server, $win );
 
 428         Irssi::timeout_remove($poll) if $poll;
 
 434     my ( $data, $server, $win ) = @_;
 
 436     print "logging in: $data" if &debug;
 
 438         print "manual data login" if &debug;
 
 439         ( $user, $pass ) = split ' ', $data, 2;
 
 440         unless ( Irssi::settings_get_bool("twirssi_use_oauth") or $pass ) {
 
 441             ¬ice("usage: /twitter_login <username>[\@<service>] <password>");
 
 444     } elsif ( Irssi::settings_get_bool("twirssi_use_oauth")
 
 445         and my $autouser = Irssi::settings_get_str("twitter_usernames") )
 
 447         print "oauth autouser login" if &debug;
 
 448         foreach my $user ( split /,/, $autouser ) {
 
 452     } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
 
 453         and my $autopass = Irssi::settings_get_str("twitter_passwords") )
 
 455         print "autouser login" if &debug;
 
 456         my @user = split /\s*,\s*/, $autouser;
 
 457         my @pass = split /\s*,\s*/, $autopass;
 
 459         # if a password ends with a '\', it was meant to escape the comma, and
 
 460         # it should be concatinated with the next one
 
 464             while ( $p =~ /\\$/ and @pass ) {
 
 465                 $p .= "," . shift @pass;
 
 470         if ( @user != @unescaped ) {
 
 471             ¬ice("Number of usernames doesn't match "
 
 472                   . "the number of passwords - auto-login failed" );
 
 475             while ( @user and @unescaped ) {
 
 477                 $p = shift @unescaped;
 
 483         ¬ice("/twitter_login requires either a username/password "
 
 484               . "or twitter_usernames and twitter_passwords to be set. "
 
 485               . "Note that if twirssi_use_oauth is true, passwords are "
 
 490     %friends = %nicks = ();
 
 493     if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
 
 494         ( $user, $service ) = ( $1, $2 );
 
 496         $service = Irssi::settings_get_str("twirssi_default_service");
 
 498     $defservice = $service = ucfirst lc $service;
 
 500     if ( $service eq 'Twitter'
 
 501         and Irssi::settings_get_bool("twirssi_use_oauth") )
 
 503         print "Attempting OAuth for $user\@$service" if &debug;
 
 505             if ( $service eq 'Identica' )
 
 507                 $twit = Net::Twitter->new(
 
 509                     traits   => [ 'API::REST', 'API::Search' ],
 
 511                     ssl      => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
 
 514                 $twit = Net::Twitter->new(
 
 515                     traits => [ 'API::REST', 'OAuth', 'API::Search' ],
 
 516                     consumer_key => 'BZVAvBma4GxdiRwXIvbnw',
 
 518                       '0T5kahwLyb34vciGZsgkA9lsjtGCQ05vxVE2APXM',
 
 520                     ssl    => !Irssi::settings_get_bool("twirssi_avoid_ssl"),
 
 526             if ( open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) )
 
 530                     next unless m/$user\@$service (\S+) (\S+)/i;
 
 531                     print "Trying cached oauth creds for $user\@$service"
 
 533                     $twit->access_token($1);
 
 534                     $twit->access_token_secret($2);
 
 540             unless ( $twit->authorized ) {
 
 542                 eval { $url = $twit->get_authorization_url; };
 
 545                     ¬ice( "ERROR: Failed to get OAuth authorization_url. " .
 
 549                 ¬ice( "Twirssi not autorized to access $service for $user.",
 
 550                          "Please authorize at the following url, then enter the pin",
 
 551                          "supplied with /twirssi_oauth $user\@$service <pin>",
 
 555                 $oauth{pending}{"$user\@$service"} = $twit;
 
 560         $twit = Net::Twitter->new(
 
 561             $service eq 'Identica' ? ( identica => 1 ) : (),
 
 565             ssl      => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1,
 
 570         ¬ice("Failed to create object!  Aborting.");
 
 574     return &verify_twitter_object( $server, $win, $user, $service, $twit );
 
 578     my ( $data, $server, $win ) = @_;
 
 579     my ( $key, $pin ) = split ' ', $data;
 
 580     my ( $user, $service );
 
 581     $key = &normalize_username($key);
 
 582     if ( $key =~ /^(.*)@(Twitter|Identica)$/ ) {
 
 583         ( $user, $service ) = ( $1, $2 );
 
 586     print "Applying pin to $key" if &debug;
 
 588     unless ( exists $oauth{pending}{$key} ) {
 
 589         ¬ice("There isn't a pending oauth request for $key. "
 
 590               . "Try /twitter_login first" );
 
 594     my $twit = $oauth{pending}{$key};
 
 595     my ( $access_token, $access_token_secret );
 
 597         ( $access_token, $access_token_secret ) =
 
 598           $twit->request_access_token( verifier => $pin );
 
 602         ¬ice("Invalid pin, try again.");
 
 606     delete $oauth{pending}{$key};
 
 608     my $store_file = Irssi::settings_get_str("twirssi_oauth_store");
 
 611         if ( open( OAUTH, $store_file ) ) {
 
 621         push @store, "$key $access_token $access_token_secret";
 
 623         if ( open( OAUTH, ">$store_file.new" ) ) {
 
 624             print OAUTH "$_\n" foreach @store;
 
 626             rename "$store_file.new", $store_file
 
 627               or ¬ice("Failed to rename $store_file.new: $!");
 
 629             ¬ice("Failed to write $store_file.new: $!");
 
 632         ¬ice("No persistant storage set for OAuth.  "
 
 633               . "Please /set twirssi_oauth_store to a writable filename." );
 
 636     return &verify_twitter_object( $server, $win, $user, $service, $twit );
 
 639 sub verify_twitter_object {
 
 640     my ( $server, $win, $user, $service, $twit ) = @_;
 
 642     if ( my $timeout = Irssi::settings_get_int("twitter_timeout")
 
 643         and $twit->can('ua') )
 
 645         $twit->ua->timeout($timeout);
 
 646         ¬ice("Twitter timeout set to $timeout");
 
 649     unless ( $twit->verify_credentials() ) {
 
 650         ¬ice("Login as $user\@$service failed");
 
 652         if ( not Irssi::settings_get_bool("twirssi_avoid_ssl") ) {
 
 654                 "It's possible you're missing one of the modules required for "
 
 655                   . "SSL logins.  Try setting twirssi_avoid_ssl to on.  See "
 
 656                   . "http://cpansearch.perl.org/src/GAAS/libwww-perl-5.831/README.SSL "
 
 657                   . "for the detailed requirements." );
 
 662             &cmd_switch( ( keys %twits )[0], $server, $win );
 
 667     my $rate_limit = $twit->rate_limit_status();
 
 668     if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
 
 670             "Rate limit exceeded, try again after $rate_limit->{reset_time}");
 
 675     print "saving object for $user\@$service" if &debug;
 
 676     $twits{"$user\@$service"} = $twit;
 
 677     Irssi::timeout_remove($poll) if $poll;
 
 678     $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
 
 679     ¬ice("Logged in as $user\@$service, loading friends list...");
 
 681     ¬ice( "loaded friends: ". scalar keys %friends );
 
 682     if ( Irssi::settings_get_bool("twirssi_first_run") ) {
 
 683         Irssi::settings_set_bool( "twirssi_first_run", 0 );
 
 691     my ( $data, $server, $win ) = @_;
 
 694         ¬ice("Usage: /twitter_add_follow_extra <username>");
 
 698     $data =~ s/^\s+|\s+$//;
 
 702     if ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
 
 703         ¬ice("Already following all replies by \@$data");
 
 707     $id_map{__fixreplies}{"$user\@$defservice"}{$data} = 1;
 
 708     ¬ice("Will now follow all replies by \@$data");
 
 712     my ( $data, $server, $win ) = @_;
 
 715         ¬ice("Usage: /twitter_del_follow_extra <username>");
 
 719     $data =~ s/^\s+|\s+$//;
 
 723     unless ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
 
 724         ¬ice("Wasn't following all replies by \@$data");
 
 728     delete $id_map{__fixreplies}{"$user\@$defservice"}{$data};
 
 729     ¬ice("Will no longer follow all replies by \@$data");
 
 732 sub cmd_list_follow {
 
 733     my ( $data, $server, $win ) = @_;
 
 736     foreach my $suser ( sort keys %{ $id_map{__fixreplies} } ) {
 
 738         foreach my $fruser ( sort keys %{ $id_map{__fixreplies}{$suser} } ) {
 
 739             $frusers = $frusers ? "$frusers, $fruser" : $fruser;
 
 743             ¬ice("Following all replies as \@$suser: $frusers");
 
 748         ¬ice("Not following all replies by anyone");
 
 753     my ( $data, $server, $win ) = @_;
 
 755     unless ( $twit and $twit->can('search') ) {
 
 756         ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
 
 757               . "doesn't support searches." );
 
 761     $data =~ s/^\s+|\s+$//;
 
 765         ¬ice("Usage: /twitter_subscribe <topic>");
 
 769     if ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
 
 770         ¬ice("Already had a subscription for '$data'");
 
 774     $id_map{__searches}{"$user\@$defservice"}{$data} = 1;
 
 775     ¬ice("Added subscription for '$data'");
 
 779     my ( $data, $server, $win ) = @_;
 
 781     unless ( $twit and $twit->can('search') ) {
 
 782         ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
 
 783               . "doesn't support searches." );
 
 786     $data =~ s/^\s+|\s+$//;
 
 790         ¬ice("Usage: /twitter_unsubscribe <topic>");
 
 794     unless ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
 
 795         ¬ice("No subscription found for '$data'");
 
 799     delete $id_map{__searches}{"$user\@$defservice"}{$data};
 
 800     ¬ice("Removed subscription for '$data'");
 
 803 sub cmd_list_search {
 
 804     my ( $data, $server, $win ) = @_;
 
 807     foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
 
 809         foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
 
 810             $topics = $topics ? "$topics, $topic" : $topic;
 
 814             ¬ice("Search subscriptions for \@$suser: $topics");
 
 819         ¬ice("No search subscriptions set up");
 
 824     my ( $data, $server, $win ) = @_;
 
 826     my $loc = Irssi::settings_get_str("twirssi_location");
 
 828         ¬ice("$loc isn't writable, can't upgrade."
 
 829               . "  Perhaps you need to /set twirssi_location?" );
 
 834     unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
 
 835         eval { use Digest::MD5; };
 
 838             ¬ice("Failed to load Digest::MD5."
 
 839                   . "  Try '/twirssi_upgrade nomd5' to skip MD5 verification" );
 
 843         $md5 = get("http://twirssi.com/md5sum");
 
 847             ¬ice("Failed to download md5sum from peeron!  Aborting.");
 
 851         unless ( open( CUR, $loc ) ) {
 
 852             ¬ice("Failed to read $loc."
 
 853                   . "  Check that /set twirssi_location is set to the correct location."
 
 858         my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
 
 861         if ( $cur_md5 eq $md5 ) {
 
 862             ¬ice("Current twirssi seems to be up to date.");
 
 868       Irssi::settings_get_bool("twirssi_upgrade_beta")
 
 869       ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
 
 870       : "http://twirssi.com/twirssi.pl";
 
 871     ¬ice("Downloading twirssi from $URL");
 
 872     LWP::Simple::getstore( $URL, "$loc.upgrade" );
 
 874     unless ( -s "$loc.upgrade" ) {
 
 875         ¬ice("Failed to save $loc.upgrade."
 
 876               . "  Check that /set twirssi_location is set to the correct location."
 
 881     unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
 
 882         unless ( open( NEW, "$loc.upgrade" ) ) {
 
 883             ¬ice("Failed to read $loc.upgrade."
 
 884                   . "  Check that /set twirssi_location is set to the correct location."
 
 889         my $new_md5 = Digest::MD5::md5_hex(<NEW>);
 
 892         if ( $new_md5 ne $md5 ) {
 
 893             ¬ice("MD5 verification failed. expected $md5, got $new_md5");
 
 898     rename $loc, "$loc.backup"
 
 899       or ¬ice("Failed to back up $loc: $!.  Aborting")
 
 901     rename "$loc.upgrade", $loc
 
 902       or ¬ice("Failed to rename $loc.upgrade: $!.  Aborting")
 
 905     my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
 
 906     if ( -e "$dir/autorun/$file" ) {
 
 907         ¬ice("Updating $dir/autorun/$file");
 
 908         unlink "$dir/autorun/$file"
 
 909           or ¬ice("Failed to remove old $file from autorun: $!");
 
 910         symlink "../$file", "$dir/autorun/$file"
 
 911           or ¬ice("Failed to create symlink in autorun directory: $!");
 
 914     ¬ice("Download complete.  Reload twirssi with /script load $file");
 
 923         while ( $page < 11 and $cursor ne "0" )
 
 925             print $fh "type:debug Loading friends page $page...\n"
 
 926               if ( $fh and &debug );
 
 928             if ( ref $twit =~ /^Net::Twitter/ ) {
 
 929                 $friends = $twit->friends( { cursor => $cursor } );
 
 930                 last unless $friends;
 
 931                 $cursor  = $friends->{next_cursor};
 
 932                 $friends = $friends->{users};
 
 934                 $friends = $twit->friends( { page => $page } );
 
 935                 last unless $friends;
 
 937             $new_friends{ $_->{screen_name} } = time foreach @$friends;
 
 943         print $fh "type:debug Error during friends list update.  Aborted.\n"
 
 948     my ( $added, $removed ) = ( 0, 0 );
 
 949     print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
 
 950     foreach ( keys %new_friends ) {
 
 951         next if exists $friends{$_};
 
 956     print $fh "type:debug Scanning for removed friends...\n"
 
 957       if ( $fh and &debug );
 
 958     foreach ( keys %friends ) {
 
 959         next if exists $new_friends{$_};
 
 964     return ( $added, $removed );
 
 968     print scalar localtime, " - get_updates starting" if &debug;
 
 971       Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
 
 974           ->print( "Can't find a window named '"
 
 975               . Irssi::settings_get_str('twitter_window')
 
 976               . "'.  Create it or change the value of twitter_window" );
 
 979     return unless &logged_in($twit);
 
 981     my ( $fh, $filename ) = File::Temp::tempfile();
 
 982     binmode( $fh, ":" . &get_charset );
 
 985     if ($child_pid) {    # parent
 
 986         Irssi::timeout_add_once( 5000, 'monitor_child',
 
 987             [ "$filename.done", 0 ] );
 
 988         Irssi::pidwait_add($child_pid);
 
 989     } elsif ( defined $child_pid ) {    # child
 
 998         foreach ( keys %twits ) {
 
 999             $error++ unless &do_updates( $fh, $_, $twits{$_}, \%context_cache );
 
1001             if ( $id_map{__fixreplies}{$_} ) {
 
1002                 my @frusers = sort keys %{ $id_map{__fixreplies}{$_} };
 
1005                   unless &get_timeline( $fh, $frusers[ $fix_replies_index{$_} ],
 
1006                     $_, $twits{$_}, \%context_cache );
 
1008                 $fix_replies_index{$_}++;
 
1009                 $fix_replies_index{$_} = 0
 
1010                   if $fix_replies_index{$_} >= @frusers;
 
1011                 print $fh "id:$fix_replies_index{$_} ",
 
1012                   "account:$_ type:fix_replies_index\n";
 
1016         print $fh "__friends__\n";
 
1018             time - $last_friends_poll >
 
1019             Irssi::settings_get_int('twitter_friends_poll') )
 
1021             print $fh "__updated ", time, "\n";
 
1022             my ( $added, $removed ) = &load_friends($fh);
 
1023             if ( $added + $removed ) {
 
1024                 print $fh "type:debug %R***%n Friends list updated: ",
 
1026                     sprintf( "%d added",   $added ),
 
1027                     sprintf( "%d removed", $removed ) ),
 
1032         foreach ( sort keys %friends ) {
 
1033             print $fh "$_ $friends{$_}\n";
 
1037             print $fh "type:debug Update encountered errors.  Aborted\n";
 
1038             print $fh "-- $last_poll";
 
1040             print $fh "-- $new_poll";
 
1043         rename $filename, "$filename.done";
 
1046         &ccrap("Failed to fork for updating: $!");
 
1048     print scalar localtime, " - get_updates ends" if &debug;
 
1052     my ( $fh, $username, $obj, $cache ) = @_;
 
1055         my $rate_limit = $obj->rate_limit_status();
 
1056         if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
 
1057             ¬ice("Rate limit exceeded for $username");
 
1062     print scalar localtime, " - Polling for updates for $username" if &debug;
 
1064     my $new_poll_id = 0;
 
1066         if ( $id_map{__last_id}{$username}{timeline} )
 
1068             $tweets = $obj->home_timeline( { count => 100 } );
 
1070             $tweets = $obj->home_timeline();
 
1075         print $fh "type:debug Error during home_timeline call: Aborted.\n";
 
1076         print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
 
1080     unless ( ref $tweets ) {
 
1081         if ( $obj->can("get_error") ) {
 
1082             my $error = "Unknown error";
 
1083             eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
 
1084             unless ($@) { $error = $obj->get_error() }
 
1086               "type:debug API Error during home_timeline call: Aborted\n";
 
1087             print $fh "type:debug : $_\n" foreach split /\n/, Dumper($error);
 
1091               "type:debug API Error during home_timeline call. Aborted.\n";
 
1096     foreach my $t ( reverse @$tweets ) {
 
1097         my $text = &get_text( $t, $obj );
 
1098         my $reply = "tweet";
 
1099         if (    Irssi::settings_get_bool("show_reply_context")
 
1100             and $t->{in_reply_to_screen_name} ne $username
 
1101             and $t->{in_reply_to_screen_name}
 
1102             and not exists $friends{ $t->{in_reply_to_screen_name} } )
 
1104             $nicks{ $t->{in_reply_to_screen_name} } = time;
 
1106             unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
 
1108                     $cache->{ $t->{in_reply_to_status_id} } =
 
1109                       $obj->show_status( $t->{in_reply_to_status_id} );
 
1113             $context = $cache->{ $t->{in_reply_to_status_id} };
 
1116                 my $ctext = &get_text( $context, $obj );
 
1117                 printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
 
1118                   $context->{id}, $username,
 
1119                   $context->{user}{screen_name}, $ctext;
 
1124           if $t->{user}{screen_name} eq $username
 
1125               and not Irssi::settings_get_bool("show_own_tweets");
 
1126         printf $fh "id:%s account:%s nick:%s type:%s %s\n",
 
1127           $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
 
1128         $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
 
1130     printf $fh "id:%s account:%s type:last_id timeline\n",
 
1131       $new_poll_id, $username;
 
1133     print scalar localtime, " - Polling for replies since ",
 
1134       $id_map{__last_id}{$username}{reply}
 
1138         if ( $id_map{__last_id}{$username}{reply} )
 
1140             $tweets = $obj->replies(
 
1141                 { since_id => $id_map{__last_id}{$username}{reply} } )
 
1144             $tweets = $obj->replies() || [];
 
1149         print $fh "type:debug Error during replies call.  Aborted.\n";
 
1153     foreach my $t ( reverse @$tweets ) {
 
1155           if exists $friends{ $t->{user}{screen_name} };
 
1157         my $text = &get_text( $t, $obj );
 
1158         printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
 
1159           $t->{id}, $username, $t->{user}{screen_name}, $text;
 
1160         $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
 
1162     printf $fh "id:%s account:%s type:last_id reply\n", $new_poll_id, $username;
 
1164     print scalar localtime, " - Polling for DMs" if &debug;
 
1167         if ( $id_map{__last_id}{$username}{dm} )
 
1169             $tweets = $obj->direct_messages(
 
1170                 { since_id => $id_map{__last_id}{$username}{dm} } )
 
1173             $tweets = $obj->direct_messages() || [];
 
1178         print $fh "type:debug Error during direct_messages call.  Aborted.\n";
 
1182     foreach my $t ( reverse @$tweets ) {
 
1183         my $text = decode_entities( $t->{text} );
 
1184         $text =~ s/[\n\r]/ /g;
 
1185         printf $fh "id:%s account:%s nick:%s type:dm %s\n",
 
1186           $t->{id}, $username, $t->{sender_screen_name}, $text;
 
1187         $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
 
1189     printf $fh "id:%s account:%s type:last_id dm\n", $new_poll_id, $username;
 
1191     print scalar localtime, " - Polling for subscriptions" if &debug;
 
1192     if ( $obj->can('search') and $id_map{__searches}{$username} ) {
 
1194         foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
 
1195             print $fh "type:debug searching for $topic since ",
 
1196               "$id_map{__searches}{$username}{$topic}\n";
 
1198                 $search = $obj->search(
 
1201                         since_id => $id_map{__searches}{$username}{$topic}
 
1208                   "type:debug Error during search($topic) call.  Aborted.\n";
 
1212             unless ( $search->{max_id} ) {
 
1213                 print $fh "type:debug Invalid search results when searching",
 
1214                   " for $topic. Aborted.\n";
 
1218             $id_map{__searches}{$username}{$topic} = $search->{max_id};
 
1219             $topic =~ s/ /\\ /g;
 
1220             printf $fh "id:%s account:%s type:searchid topic:%s\n",
 
1221               $search->{max_id}, $username, $topic;
 
1223             foreach my $t ( reverse @{ $search->{results} } ) {
 
1224                 my $text = &get_text( $t, $obj );
 
1225                 printf $fh "id:%s account:%s nick:%s type:search topic:%s %s\n",
 
1226                   $t->{id}, $username, $t->{from_user}, $topic, $text;
 
1227                 $new_poll_id = $t->{id}
 
1229                       or $t->{id} < $new_poll_id;
 
1234     print scalar localtime, " - Done" if &debug;
 
1240     my ( $fh, $target, $username, $obj, $cache ) = @_;
 
1242     my $last_id = $id_map{__last_id}{$username}{$target};
 
1244     print $fh "type:debug get_timeline("
 
1245       . "$fix_replies_index{$username}=$target > $last_id) started."
 
1246       . "  username = $username\n";
 
1248         $tweets = $obj->user_timeline(
 
1251                 ( $last_id ? ( since_id => $last_id ) : () ),
 
1258           "type:debug Error during user_timeline($target) call: Aborted.\n";
 
1259         print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
 
1265           "type:debug user_timeline($target) call returned undef!  Aborted\n";
 
1269     foreach my $t ( reverse @$tweets ) {
 
1270         my $text = &get_text( $t, $obj );
 
1271         my $reply = "tweet";
 
1272         if (    Irssi::settings_get_bool("show_reply_context")
 
1273             and $t->{in_reply_to_screen_name} ne $username
 
1274             and $t->{in_reply_to_screen_name}
 
1275             and not exists $friends{ $t->{in_reply_to_screen_name} } )
 
1277             $nicks{ $t->{in_reply_to_screen_name} } = time;
 
1279             unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
 
1281                     $cache->{ $t->{in_reply_to_status_id} } =
 
1282                       $obj->show_status( $t->{in_reply_to_status_id} );
 
1286             $context = $cache->{ $t->{in_reply_to_status_id} };
 
1289                 my $ctext = &get_text( $context, $obj );
 
1290                 printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
 
1291                   $context->{id}, $username,
 
1292                   $context->{user}{screen_name}, $ctext;
 
1296         printf $fh "id:%s account:%s nick:%s type:%s %s\n",
 
1297           $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
 
1298         $last_id = $t->{id} if $last_id < $t->{id};
 
1300     printf $fh "id:%s account:%s type:last_id_fixreplies %s\n",
 
1301       $last_id, $username, $target;
 
1308     my $filename = $data->[0];
 
1309     my $attempt  = $data->[1];
 
1311     print scalar localtime, " - checking child log at $filename ($attempt)"
 
1313     my ($new_last_poll);
 
1315     # reap any random leftover processes - work around a bug in irssi on gentoo
 
1316     waitpid( -1, WNOHANG );
 
1318     # first time we run we don't want to print out *everything*, so we just
 
1321     if ( open FILE, $filename ) {
 
1322         binmode FILE, ":" . &get_charset;
 
1326             last if /^__friends__/;
 
1327             unless (/\n$/) {    # skip partial lines
 
1328                                 # print "Skipping partial line: $_" if &debug;
 
1335             foreach my $key (qw/id account nick type topic/) {
 
1336                 if (s/^$key:((?:\S|\\ )+)\s*//) {
 
1338                     $meta{$key} =~ s/\\ / /g;
 
1342             if ( $meta{type} and $meta{type} eq 'fix_replies_index' ) {
 
1343                 $fix_replies_index{ $meta{account} } = $meta{id};
 
1344                 print "fix_replies_index for $meta{account} set to $meta{id}"
 
1349             if ( not $meta{type} or $meta{type} !~ /searchid|last_id/ ) {
 
1350                 if ( exists $meta{id} and exists $new_cache{ $meta{id} } ) {
 
1354                 $new_cache{ $meta{id} } = time;
 
1356                 if ( exists $meta{id} and exists $tweet_cache{ $meta{id} } ) {
 
1362             $meta{account} =~ s/\@(\w+)$//;
 
1363             $meta{service} = $1;
 
1365                 lc $meta{service} eq
 
1366                 lc Irssi::settings_get_str("twirssi_default_service") )
 
1368                 $account = "$meta{account}: "
 
1369                   if lc "$meta{account}\@$meta{service}" ne lc
 
1370                       "$user\@$defservice";
 
1372                 $account = "$meta{account}\@$meta{service}: ";
 
1376             if ($meta{type} ne 'dm' and $meta{nick} and $meta{id} )
 
1378                 $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100;
 
1379                 $id_map{ lc $meta{nick} }[$marker]           = $meta{id};
 
1380                 $id_map{__indexes}{ $meta{nick} }            = $marker;
 
1381                 $id_map{__tweets}{ lc $meta{nick} }[$marker] = $_;
 
1382                 $marker                                      = ":$marker";
 
1386               $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
 
1387             my $nick = "\@$meta{account}";
 
1388             if ( $_ =~ /\Q$nick\E(?:\W|$)/i
 
1389                 and Irssi::settings_get_bool("twirssi_hilights") )
 
1391                 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
 
1392                 $hilight = MSGLEVEL_HILIGHT;
 
1395             if ( $meta{type} =~ /tweet|reply/ ) {
 
1398                     ( MSGLEVEL_PUBLIC | $hilight ),
 
1399                     $meta{type}, $account, $meta{nick}, $marker, $_
 
1401             } elsif ( $meta{type} eq 'search' ) {
 
1404                     ( MSGLEVEL_PUBLIC | $hilight ),
 
1405                     $meta{type}, $account, $meta{topic},
 
1406                     $meta{nick}, $marker,  $_
 
1409                     exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
 
1411                     $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
 
1413                     $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
 
1416             } elsif ( $meta{type} eq 'dm' ) {
 
1419                     ( MSGLEVEL_MSGS | $hilight ),
 
1420                     $meta{type}, $account, $meta{nick}, $_
 
1422             } elsif ( $meta{type} eq 'searchid' ) {
 
1423                 print "Search '$meta{topic}' returned id $meta{id}" if &debug;
 
1426                     exists $id_map{__searches}{ $meta{account} }{ $meta{topic} }
 
1428                     $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
 
1430                     $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
 
1433                     print "Search '$meta{topic}' returned invalid id $meta{id}";
 
1435             } elsif ( $meta{type} eq 'last_id' ) {
 
1436                 $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} =
 
1438                   if $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} <
 
1440             } elsif ( $meta{type} eq 'last_id_fixreplies' ) {
 
1441                 $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} =
 
1443                   if $id_map{__last_id}{"$meta{account}\@$meta{service}"}{$_} <
 
1445             } elsif ( $meta{type} eq 'error' ) {
 
1446                 push @lines, [ MSGLEVEL_MSGS, $_ ];
 
1447             } elsif ( $meta{type} eq 'debug' ) {
 
1448                 print "$_" if &debug,;
 
1450                 print "Unknown line type $meta{type}: $_" if &debug,;
 
1456             if (/^__updated (\d+)$/) {
 
1457                 $last_friends_poll = $1;
 
1458                 print "Friend list updated" if &debug;
 
1463                 $new_last_poll = $1;
 
1464                 if ( $new_last_poll >= $last_poll ) {
 
1467                     print "Impossible!  ",
 
1468                       "new_last_poll=$new_last_poll < last_poll=$last_poll!"
 
1470                     undef $new_last_poll;
 
1474             my ( $f, $t ) = split ' ', $_;
 
1475             $nicks{$f} = $friends{$f} = $t;
 
1478         if ($new_last_poll) {
 
1479             print "new last_poll    = $new_last_poll" if &debug;
 
1480             print "new last_poll_id = ", Dumper( $id_map{__last_id} ) if &debug;
 
1482                 print "First call, not printing updates" if &debug;
 
1484                 foreach my $line (@lines) {
 
1485                     $window->printformat(
 
1487                         "twirssi_" . $line->[1],
 
1488                         @$line[ 2 .. $#$line - 1 ],
 
1489                         &hilight( $line->[-1] )
 
1496               or warn "Failed to remove $filename: $!"
 
1499             # commit the pending cache lines to the actual cache, now that
 
1500             # we've printed our output
 
1501             %tweet_cache = ( %tweet_cache, %new_cache );
 
1503             # keep enough cached tweets, to make sure we don't show duplicates.
 
1504             foreach ( keys %tweet_cache ) {
 
1505                 next if $tweet_cache{$_} >= $last_poll - 3600;
 
1506                 delete $tweet_cache{$_};
 
1508             $last_poll = $new_last_poll;
 
1510             # make sure the pid is removed from the waitpid list
 
1511             Irssi::pidwait_remove($child_pid);
 
1513             # and that we don't leave any zombies behind, somehow
 
1514             waitpid( -1, WNOHANG );
 
1519                 Irssi::settings_get_str("twirssi_replies_store") )
 
1521                 if ( open JSON, ">$file" ) {
 
1522                     print JSON JSON::Any->objToJson( \%id_map );
 
1525                     &ccrap("Failed to write replies to $file: $!");
 
1536     if ( $attempt < 24 ) {
 
1537         Irssi::timeout_add_once( 5000, 'monitor_child',
 
1538             [ $filename, $attempt + 1 ] );
 
1540         print "Giving up on polling $filename" if &debug;
 
1541         Irssi::pidwait_remove($child_pid);
 
1542         waitpid( -1, WNOHANG );
 
1543         unlink $filename unless &debug;
 
1545         return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
 
1548         my @time = localtime($last_poll);
 
1549         if ( time - $last_poll < 24 * 60 * 60 ) {
 
1550             $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
 
1552             $since = scalar localtime($last_poll);
 
1555         if ( not $failwhale and time - $last_poll > 60 * 60 ) {
 
1560                 q{  .--./ /  |  _.---.| },
 
1569         if ( time - $last_poll < 600 ) {
 
1570             &ccrap("Haven't been able to get updated tweets since $since");
 
1576     return Irssi::settings_get_bool("twirssi_debug");
 
1580     foreach my $msg (@_) {
 
1581         $window->print( "%R***%n $msg", MSGLEVEL_PUBLIC );
 
1586     foreach my $msg (@_) {
 
1587         $window->print( "%R***%n $msg", MSGLEVEL_CLIENTCRAP );
 
1594     if (    Irssi::settings_get_bool("tweet_to_away")
 
1596         and $data !~ /^[dD] / )
 
1599           Irssi::server_find_tag( Irssi::settings_get_str("bitlbee_server") );
 
1601             $server->send_raw("away :$data");
 
1604             &ccrap( "Can't find bitlbee server.",
 
1605                 "Update bitlbee_server or disable tweet_to_away" );
 
1615     my $noalert = shift;
 
1617     if ( length $data > 140 ) {
 
1618         ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" )
 
1626 sub valid_username {
 
1627     my $username = shift;
 
1629     $username = &normalize_username($username);
 
1631     unless ( exists $twits{$username} ) {
 
1632         ¬ice("Unknown username $username");
 
1642         ¬ice("Not logged in!  Use /twitter_login username pass!");
 
1650     my ( $complist, $window, $word, $linestart, $want_space ) = @_;
 
1653         $linestart =~ /^\/(?:retweet|twitter_reply)(?:_as)?\s*$/
 
1654         or ( Irssi::settings_get_bool("twirssi_use_reply_aliases")
 
1655             and $linestart =~ /^\/reply(?:_as)?\s*$/ )
 
1657     {    # /twitter_reply gets a nick:num
 
1659         @$complist = map { "$_:$id_map{__indexes}{$_}" }
 
1660           sort { $nicks{$b} <=> $nicks{$a} }
 
1662           keys %{ $id_map{__indexes} };
 
1666 /^\/(twitter_unfriend|twitter_add_follow_extra|twitter_del_follow_extra)\s*$/
 
1668     {    # /twitter_unfriend gets a nick
 
1670         push @$complist, grep /^\Q$word/i,
 
1671           sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
 
1674     # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first
 
1676     if ( $linestart =~ /^\/(?:tweet|dm)/ ) {
 
1677         my $prefix = $word =~ s/^@//;
 
1678         $prefix = 0 if $linestart eq '/dm' or $linestart eq '/dm_as';
 
1679         push @$complist, grep /^\Q$word/i,
 
1680           sort { $nicks{$b} <=> $nicks{$a} } keys %nicks;
 
1681         @$complist = map { "\@$_" } @$complist if $prefix;
 
1685 sub event_send_text {
 
1686     my ( $line, $server, $win ) = @_;
 
1687     my $awin = Irssi::active_win();
 
1689     # if the window where we got our text was the twitter window, and the user
 
1690     # wants to be lazy, tweet away!
 
1691     if ( ( $awin->get_active_name() eq $window->{name} )
 
1692         and Irssi::settings_get_bool("tweet_window_input") )
 
1694         &cmd_tweet( $line, $server, $win );
 
1699     my $poll = Irssi::settings_get_int("twitter_poll_interval");
 
1700     return $poll if $poll >= 60;
 
1705     my $charset = Irssi::settings_get_str("twirssi_charset");
 
1706     return "utf8" if $charset =~ /^\s*$/;
 
1713     if ( Irssi::settings_get_str("twirssi_nick_color") ) {
 
1714         my $c = Irssi::settings_get_str("twirssi_nick_color");
 
1715         $c = $irssi_to_mirc_colors{$c};
 
1716         $text =~ s/(^|\W)\@(\w+)/$1\cC$c\@$2\cO/g if $c;
 
1718     if ( Irssi::settings_get_str("twirssi_topic_color") ) {
 
1719         my $c = Irssi::settings_get_str("twirssi_topic_color");
 
1720         $c = $irssi_to_mirc_colors{$c};
 
1721         $text =~ s/(^|\W)(\#|\!)([-\w]+)/$1\cC$c$2$3\cO/g if $c;
 
1723     $text =~ s/[\n\r]/ /g;
 
1731     my $provider = Irssi::settings_get_str("short_url_provider");
 
1734             Irssi::settings_get_bool("twirssi_always_shorten")
 
1735             or &too_long( $data, 1 )
 
1741         if ( $provider eq 'Bitly' ) {
 
1742             @args[ 1, 2 ] = split ',',
 
1743               Irssi::settings_get_str("short_url_args"), 2;
 
1744             unless ( @args == 3 ) {
 
1746                     "WWW::Shorten::Bitly requires a username and API key.",
 
1747                     "Set short_url_args to username,API_key or change your",
 
1748                     "short_url_provider."
 
1750                 return decode &get_charset, $data;
 
1754         foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
 
1757                 my $short = makeashorterlink(@args);
 
1759                     $data =~ s/\Q$url/$short/g;
 
1761                     ¬ice("Failed to shorten $url!");
 
1767     return decode &get_charset, $data;
 
1770 sub normalize_username {
 
1773     my ( $username, $service ) = split /\@/, $user, 2;
 
1775         $service = ucfirst lc $service;
 
1778           ucfirst lc Irssi::settings_get_str("twirssi_default_service");
 
1779         unless ( exists $twits{"$username\@$service"} ) {
 
1781             foreach my $t ( sort keys %twits ) {
 
1782                 next unless $t =~ /^\Q$username\E\@(Twitter|Identica)/;
 
1788                 ¬ice("Can't find a logged in user '$user'");
 
1793     return "$username\@$service";
 
1799     my $text   = decode_entities( $tweet->{text} );
 
1800     if ( $tweet->{truncated} ) {
 
1801         if ( exists $tweet->{retweeted_status} ) {
 
1802             $text = "RT \@$tweet->{retweeted_status}{user}{screen_name}: "
 
1803               . "$tweet->{retweeted_status}{text}";
 
1804         } elsif ( $object->isa('Net::Twitter') ) {
 
1805             $text .= " -- http://twitter.com/$tweet->{user}{screen_name}"
 
1806               . "/status/$tweet->{id}";
 
1810     $text =~ s/[\n\r]/ /g;
 
1815 Irssi::signal_add( "send text", "event_send_text" );
 
1817 Irssi::theme_register(
 
1819         'twirssi_tweet',  '[$0%B@$1%n$2] $3',
 
1820         'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4',
 
1821         'twirssi_reply',  '[$0\--> %B@$1%n$2] $3',
 
1822         'twirssi_dm',     '[$0%r@$1%n (%WDM%n)] $2',
 
1823         'twirssi_error',  'ERROR: $0',
 
1827 Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 );
 
1828 Irssi::settings_add_str( "twirssi", "twirssi_charset",         "utf8" );
 
1829 Irssi::settings_add_str( "twirssi", "twitter_window",          "twitter" );
 
1830 Irssi::settings_add_str( "twirssi", "bitlbee_server",          "bitlbee" );
 
1831 Irssi::settings_add_str( "twirssi", "short_url_provider",      "TinyURL" );
 
1832 Irssi::settings_add_str( "twirssi", "short_url_args",          undef );
 
1833 Irssi::settings_add_str( "twirssi", "twitter_usernames",       undef );
 
1834 Irssi::settings_add_str( "twirssi", "twitter_passwords",       undef );
 
1835 Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
 
1836 Irssi::settings_add_str( "twirssi", "twirssi_nick_color",      "%B" );
 
1837 Irssi::settings_add_str( "twirssi", "twirssi_topic_color",     "%r" );
 
1838 Irssi::settings_add_str( "twirssi", "twirssi_retweet_format",
 
1839     'RT $n: "$t" ${-- $c$}' );
 
1840 Irssi::settings_add_str( "twirssi", "twirssi_location",
 
1841     Irssi::get_irssi_dir . "/scripts/twirssi.pl" );
 
1842 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
 
1843     Irssi::get_irssi_dir . "/scripts/twirssi.json" );
 
1844 Irssi::settings_add_str( "twirssi", "twirssi_oauth_store",
 
1845     Irssi::get_irssi_dir . "/scripts/twirssi.oauth" );
 
1847 Irssi::settings_add_int( "twirssi", "twitter_friends_poll", 600 );
 
1848 Irssi::settings_add_int( "twirssi", "twitter_timeout",      30 );
 
1850 Irssi::settings_add_bool( "twirssi", "twirssi_upgrade_beta",      0 );
 
1851 Irssi::settings_add_bool( "twirssi", "tweet_to_away",             0 );
 
1852 Irssi::settings_add_bool( "twirssi", "show_reply_context",        0 );
 
1853 Irssi::settings_add_bool( "twirssi", "show_own_tweets",           1 );
 
1854 Irssi::settings_add_bool( "twirssi", "twirssi_debug",             0 );
 
1855 Irssi::settings_add_bool( "twirssi", "twirssi_first_run",         1 );
 
1856 Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick",  1 );
 
1857 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
 
1858 Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts",   1 );
 
1859 Irssi::settings_add_bool( "twirssi", "twirssi_hilights",          1 );
 
1860 Irssi::settings_add_bool( "twirssi", "twirssi_always_shorten",    0 );
 
1861 Irssi::settings_add_bool( "twirssi", "tweet_window_input",        0 );
 
1862 Irssi::settings_add_bool( "twirssi", "twirssi_avoid_ssl",         0 );
 
1863 Irssi::settings_add_bool( "twirssi", "twirssi_use_oauth",         1 );
 
1865 $last_poll = time - &get_poll_time;
 
1866 $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
 
1869       ->print( "Couldn't find a window named '"
 
1870           . Irssi::settings_get_str('twitter_window')
 
1871           . "', trying to create it." );
 
1873       Irssi::Windowitem::window_create(
 
1874         Irssi::settings_get_str('twitter_window'), 1 );
 
1875     $window->set_name( Irssi::settings_get_str('twitter_window') );
 
1879     Irssi::command_bind( "dm",                         "cmd_direct" );
 
1880     Irssi::command_bind( "dm_as",                      "cmd_direct_as" );
 
1881     Irssi::command_bind( "tweet",                      "cmd_tweet" );
 
1882     Irssi::command_bind( "tweet_as",                   "cmd_tweet_as" );
 
1883     Irssi::command_bind( "retweet",                    "cmd_retweet" );
 
1884     Irssi::command_bind( "retweet_as",                 "cmd_retweet_as" );
 
1885     Irssi::command_bind( "twitter_reply",              "cmd_reply" );
 
1886     Irssi::command_bind( "twitter_reply_as",           "cmd_reply_as" );
 
1887     Irssi::command_bind( "twitter_login",              "cmd_login" );
 
1888     Irssi::command_bind( "twitter_logout",             "cmd_logout" );
 
1889     Irssi::command_bind( "twitter_switch",             "cmd_switch" );
 
1890     Irssi::command_bind( "twitter_subscribe",          "cmd_add_search" );
 
1891     Irssi::command_bind( "twitter_unsubscribe",        "cmd_del_search" );
 
1892     Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" );
 
1893     Irssi::command_bind( "twirssi_upgrade",            "cmd_upgrade" );
 
1894     Irssi::command_bind( "twirssi_oauth",              "cmd_oauth" );
 
1895     Irssi::command_bind( "twitter_updates",            "get_updates" );
 
1896     Irssi::command_bind( "twitter_add_follow_extra",   "cmd_add_follow" );
 
1897     Irssi::command_bind( "twitter_del_follow_extra",   "cmd_del_follow" );
 
1898     Irssi::command_bind( "twitter_list_follow_extra",  "cmd_list_follow" );
 
1899     Irssi::command_bind( "bitlbee_away",               "update_away" );
 
1900     if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) {
 
1901         Irssi::command_bind( "reply",    "cmd_reply" );
 
1902         Irssi::command_bind( "reply_as", "cmd_reply_as" );
 
1904     Irssi::command_bind(
 
1907             print "twits: ", join ", ",
 
1908               map { "u: $_->{username}\@" . ref($_) } values %twits;
 
1909             print "selected: $user\@$defservice";
 
1910             print "friends: ", join ", ", sort keys %friends;
 
1911             print "nicks: ",   join ", ", sort keys %nicks;
 
1912             print "searches: ", Dumper \%{ $id_map{__searches} };
 
1913             print "last poll: $last_poll";
 
1914             if ( open DUMP, ">/tmp/twirssi.cache.txt" ) {
 
1915                 print DUMP Dumper \%tweet_cache;
 
1917                 print "cache written out to /tmp/twirssi.cache.txt";
 
1921     Irssi::command_bind(
 
1925                 "Twirssi v$VERSION; "
 
1927                     $Net::Twitter::VERSION
 
1928                     ? "Net::Twitter v$Net::Twitter::VERSION. "
 
1932                     $Net::Identica::VERSION
 
1933                     ? "Net::Identica v$Net::Identica::VERSION. "
 
1937                   . JSON::Any::handler()
 
1938                   . ".  See details at http://twirssi.com/"
 
1942     Irssi::command_bind(
 
1945             "/twitter_follow <username>",
 
1947             sub { ¬ice("Following $_[0]"); $nicks{ $_[0] } = time; }
 
1950     Irssi::command_bind(
 
1953             "/twitter_unfriend <username>",
 
1955             sub { ¬ice("Stopped following $_[0]"); delete $nicks{ $_[0] }; }
 
1958     Irssi::command_bind(
 
1959         "twitter_device_updates",
 
1961             "/twitter_device_updates none|im|sms",
 
1962             "update_delivery_device",
 
1963             sub { ¬ice("Device updated to $_[0]"); }
 
1966     Irssi::command_bind(
 
1969             "/twitter_block <username>",
 
1971             sub { ¬ice("Blocked $_[0]"); }
 
1974     Irssi::command_bind(
 
1977             "/twitter_unblock <username>",
 
1979             sub { ¬ice("Unblock $_[0]"); }
 
1982     Irssi::signal_add_last( 'complete word' => \&sig_complete );
 
1984     ¬ice("  %Y<%C(%B^%C)%N                   TWIRSSI v%R$VERSION%N",
 
1985             "   %C(_(\\%N           http://twirssi.com/ for full docs",
 
1986             "    %Y||%C `%N Log in with /twitter_login, send updates with /tweet");
 
1988     my $file = Irssi::settings_get_str("twirssi_replies_store");
 
1989     if ( $file and -r $file ) {
 
1990         if ( open( JSON, $file ) ) {
 
1995                 my $ref = JSON::Any->jsonToObj($json);
 
1997                 my $num = keys %{ $id_map{__indexes} };
 
1998                 ¬ice( sprintf "Loaded old replies from %d contact%s.",
 
1999                     $num, ( $num == 1 ? "" : "s" ) );
 
2004             ¬ice("Failed to load old replies from $file: $!");
 
2008     if ( my $provider = Irssi::settings_get_str("short_url_provider") ) {
 
2009         ¬ice("Loading WWW::Shorten::$provider...");
 
2010         eval "use WWW::Shorten::$provider;";
 
2014                 "Failed to load WWW::Shorten::$provider - either clear",
 
2015                 "short_url_provider or install the CPAN module"
 
2020     if ( my $autouser = Irssi::settings_get_str("twitter_usernames") ) {
 
2027       ->print( "Create a window named "
 
2028           . Irssi::settings_get_str('twitter_window')
 
2029           . " or change the value of twitter_window.  Then, reload twirssi." );
 
2032 # vim: set sts=4 expandtab: