+ return unless &logged_in($twit);
+
+ my ( $target, $text ) = split ' ', $data, 2;
+ unless ( $target and $text ) {
+ ¬ice("Usage: /dm <nick> <message>");
+ return;
+ }
+
+ &cmd_direct_as( "$user $data", $server, $win );
+}
+
+sub cmd_direct_as {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ my ( $username, $target, $text ) = split ' ', $data, 3;
+ unless ( $username and $target and $text ) {
+ ¬ice("Usage: /dm_as <username> <nick> <message>");
+ return;
+ }
+
+ return unless $username = &valid_username($username);
+
+ eval {
+ if ( $twits{$username}
+ ->new_direct_message( { user => $target, text => $text } ) )
+ {
+ ¬ice("DM sent to $target");
+ $nicks{$target} = time;
+ } else {
+ my $error;
+ eval {
+ $error = JSON::Any->jsonToObj( $twits{$username}->get_error() );
+ $error = $error->{error};
+ };
+ die $error if $error;
+ ¬ice("DM to $target failed");
+ }
+ };
+
+ if ($@) {
+ ¬ice("DM caused an error: $@");
+ return;
+ }
+}
+
+sub cmd_retweet {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ unless ($data) {
+ ¬ice("Usage: /retweet <nick[:num]> [comment]");
+ return;
+ }
+
+ my ( $id, $data ) = split ' ', $data, 2;
+
+ &cmd_retweet_as( "$user $id $data", $server, $win );
+}
+
+sub cmd_retweet_as {
+ my ( $data, $server, $win ) = @_;
+
+ unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
+ ¬ice("twirssi_track_replies is required in order to reteet.");
+ return;
+ }
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ my ( $username, $id, $data ) = split ' ', $data, 3;
+
+ unless ($username) {
+ ¬ice("Usage: /retweet_as <username> <nick[:num]> [comment]");
+ return;
+ }
+
+ return unless $username = &valid_username($username);
+
+ my $nick;
+ $id =~ s/[^\w\d\-:]+//g;
+ ( $nick, $id ) = split /:/, $id;
+ unless ( exists $id_map{ lc $nick } ) {
+ ¬ice("Can't find a tweet from $nick to retweet!");
+ return;
+ }
+
+ $id = $id_map{__indexes}{$nick} unless $id;
+ unless ( $id_map{ lc $nick }[$id] ) {
+ ¬ice("Can't find a tweet numbered $id from $nick to retweet!");
+ return;
+ }
+
+ unless ( $id_map{__tweets}{ lc $nick }[$id] ) {
+ ¬ice("The text of this tweet isn't saved, sorry!");
+ return;
+ }
+
+# Irssi::settings_add_str( "twirssi", "twirssi_retweet_format", 'RT $n: $t ${-- $c$}' );
+ my $text = Irssi::settings_get_str("twirssi_retweet_format");
+ $text =~ s/\$n/\@$nick/g;
+ if ($data) {
+ $text =~ s/\${|\$}//g;
+ $text =~ s/\$c/$data/;
+ } else {
+ $text =~ s/\${.*?\$}//;
+ }
+ $text =~ s/\$t/$id_map{__tweets}{ lc $nick }[$id]/;
+
+ $data = &shorten($text);
+
+ return if &too_long($data);
+
+ my $success = 1;
+ eval {
+ unless (
+ $twits{$username}->update(
+ {
+ status => $data,
+
+ # in_reply_to_status_id => $id_map{ lc $nick }[$id]
+ }
+ )
+ )
+ {
+ ¬ice("Update failed");
+ $success = 0;
+ }
+ };
+ return unless $success;
+
+ if ($@) {
+ ¬ice("Update caused an error: $@. Aborted");
+ return;
+ }
+
+ foreach ( $data =~ /@([-\w]+)/ ) {
+ $nicks{$1} = time;
+ }
+
+ ¬ice("Retweet sent");
+}
+
+sub cmd_tweet {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ unless ($data) {
+ ¬ice("Usage: /tweet <update>");
+ return;
+ }
+
+ &cmd_tweet_as( "$user\@$defservice $data", $server, $win );
+}
+
+sub cmd_tweet_as {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ $data =~ s/\s\s+/ /g;
+ my ( $username, $data ) = split ' ', $data, 2;
+
+ unless ( $username and $data ) {
+ ¬ice("Usage: /tweet_as <username> <update>");
+ return;
+ }
+
+ return unless $username = &valid_username($username);
+
+ $data = &shorten($data);
+
+ return if &too_long($data);
+
+ my $success = 1;
+ eval {
+ unless ( $twits{$username}->update($data) )
+ {
+ ¬ice("Update failed");
+ $success = 0;
+ }
+ };
+ return unless $success;
+
+ if ($@) {
+ ¬ice("Update caused an error: $@. Aborted.");
+ return;
+ }
+
+ foreach ( $data =~ /@([-\w]+)/ ) {
+ $nicks{$1} = time;
+ }
+
+ my $away = &update_away($data);
+
+ ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
+}
+
+sub cmd_reply {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ unless ($data) {
+ ¬ice("Usage: /reply <nick[:num]> <update>");
+ return;
+ }
+
+ my ( $id, $data ) = split ' ', $data, 2;
+ unless ( $id and $data ) {
+ ¬ice("Usage: /reply <nick[:num]> <update>");
+ return;
+ }
+
+ &cmd_reply_as( "$user $id $data", $server, $win );
+}
+
+sub cmd_reply_as {
+ my ( $data, $server, $win ) = @_;
+
+ unless ( Irssi::settings_get_bool("twirssi_track_replies") ) {
+ ¬ice("twirssi_track_replies is required in order to reply to "
+ . "specific tweets. Either enable it, or just use /tweet "
+ . "\@username <text>." );
+ return;
+ }
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ my ( $username, $id, $data ) = split ' ', $data, 3;
+
+ unless ( $username and $data ) {
+ ¬ice("Usage: /reply_as <username> <nick[:num]> <update>");
+ return;
+ }
+
+ return unless $username = &valid_username($username);
+
+ my $nick;
+ $id =~ s/[^\w\d\-:]+//g;
+ ( $nick, $id ) = split /:/, $id;
+ unless ( exists $id_map{ lc $nick } ) {
+ ¬ice("Can't find a tweet from $nick to reply to!");
+ return;
+ }
+
+ $id = $id_map{__indexes}{$nick} unless $id;
+ unless ( $id_map{ lc $nick }[$id] ) {
+ ¬ice("Can't find a tweet numbered $id from $nick to reply to!");
+ return;
+ }
+
+ if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
+
+ # remove any @nick at the beginning of the reply, as we'll add it anyway
+ $data =~ s/^\s*\@?$nick\s*//;
+ $data = "\@$nick " . $data;
+ }
+
+ $data = &shorten($data);
+
+ return if &too_long($data);
+
+ my $success = 1;
+ eval {
+ unless (
+ $twits{$username}->update(
+ {
+ status => $data,
+ in_reply_to_status_id => $id_map{ lc $nick }[$id]
+ }
+ )
+ )
+ {
+ ¬ice("Update failed");
+ $success = 0;
+ }
+ };
+ return unless $success;
+
+ if ($@) {
+ ¬ice("Update caused an error: $@. Aborted");
+ return;
+ }
+
+ foreach ( $data =~ /@([-\w]+)/ ) {
+ $nicks{$1} = time;
+ }
+
+ my $away = &update_away($data);
+
+ ¬ice( "Update sent" . ( $away ? " (and away msg set)" : "" ) );
+}
+
+sub gen_cmd {
+ my ( $usage_str, $api_name, $post_ref ) = @_;
+
+ return sub {
+ my ( $data, $server, $win ) = @_;
+
+ return unless &logged_in($twit);
+
+ $data =~ s/^\s+|\s+$//;
+ unless ($data) {
+ ¬ice("Usage: $usage_str");
+ return;
+ }
+
+ my $success = 1;
+ eval {
+ unless ( $twit->$api_name($data) )
+ {
+ ¬ice("$api_name failed");
+ $success = 0;
+ }
+ };
+ return unless $success;
+
+ if ($@) {
+ ¬ice("$api_name caused an error. Aborted.");
+ return;
+ }
+
+ &$post_ref($data) if $post_ref;
+ }
+}
+
+sub cmd_switch {
+ my ( $data, $server, $win ) = @_;
+
+ $data =~ s/^\s+|\s+$//g;
+ $data = &normalize_username($data);
+ if ( exists $twits{$data} ) {
+ ¬ice("Switching to $data");
+ $twit = $twits{$data};
+ if ( $data =~ /(.*)\@(.*)/ ) {
+ $user = $1;
+ $defservice = $2;
+ } else {
+ ¬ice("Couldn't figure out what service '$data' is on");
+ }
+ } else {
+ ¬ice("Unknown user $data");
+ }
+}
+
+sub cmd_logout {
+ my ( $data, $server, $win ) = @_;
+
+ $data =~ s/^\s+|\s+$//g;
+ $data = $user unless $data;
+ return unless $data = &valid_username($data);
+
+ ¬ice("Logging out $data...");
+ $twits{$data}->end_session();
+ delete $twits{$data};
+ undef $twit;
+ if ( keys %twits ) {
+ &cmd_switch( ( keys %twits )[0], $server, $win );
+ } else {
+ Irssi::timeout_remove($poll) if $poll;
+ undef $poll;
+ }
+}
+
+sub cmd_login {
+ my ( $data, $server, $win ) = @_;
+ my $pass;
+ if ($data) {
+ ( $user, $pass ) = split ' ', $data, 2;
+ unless ($pass) {
+ ¬ice("usage: /twitter_login <username>[\@<service>] <password>");
+ return;
+ }
+ } elsif ( my $autouser = Irssi::settings_get_str("twitter_usernames")
+ and my $autopass = Irssi::settings_get_str("twitter_passwords") )
+ {
+ my @user = split /\s*,\s*/, $autouser;
+ my @pass = split /\s*,\s*/, $autopass;
+
+ # if a password ends with a '\', it was meant to escape the comma, and
+ # it should be concatinated with the next one
+ my @unescaped;
+ while (@pass) {
+ my $p = shift @pass;
+ while ( $p =~ /\\$/ and @pass ) {
+ $p .= "," . shift @pass;
+ }
+ push @unescaped, $p;
+ }
+
+ if ( @user != @unescaped ) {
+ ¬ice("Number of usernames doesn't match "
+ . "the number of passwords - auto-login failed" );
+ } else {
+ my ( $u, $p );
+ while ( @user and @unescaped ) {
+ $u = shift @user;
+ $p = shift @unescaped;
+ &cmd_login("$u $p");
+ }
+ return;
+ }
+ } else {
+ ¬ice("/twitter_login requires either a username and password "
+ . "or twitter_usernames and twitter_passwords to be set." );
+ return;
+ }
+
+ %friends = %nicks = ();
+
+ my $service;
+ if ( $user =~ /^(.*)@(twitter|identica)$/ ) {
+ ( $user, $service ) = ( $1, $2 );
+ } else {
+ $service = Irssi::settings_get_str("twirssi_default_service");
+ }
+ $defservice = $service = ucfirst lc $service;
+
+ eval "use Net::$service";
+ if ($@) {
+ ¬ice(
+ "Failed to load Net::$service when trying to log in as $user: $@");
+ return;
+ }
+
+ $twit = "Net::$service"->new(
+ username => $user,
+ password => $pass,
+ source => "twirssi",
+ ssl => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1,
+ );
+
+ unless ($twit) {
+ ¬ice("Failed to create Net::$service object! Aborting.");
+ return;
+ }
+
+ if ( my $timeout = Irssi::settings_get_int("twitter_timeout")
+ and $twit->can('ua') )
+ {
+ $twit->ua->timeout($timeout);
+ }
+
+ unless ( $twit->verify_credentials() ) {
+ ¬ice("Login as $user\@$service failed");
+
+ if ( not Irssi::settings_get_bool("twirssi_avoid_ssl") ) {
+ ¬ice(
+ "It's possible you're missing one of the modules required for "
+ . "SSL logins. Try setting twirssi_avoid_ssl to on. See "
+ . "http://cpansearch.perl.org/src/GAAS/libwww-perl-5.831/README.SSL "
+ . "for the detailed requirements." );
+ }
+
+ $twit = undef;
+ if ( keys %twits ) {
+ &cmd_switch( ( keys %twits )[0], $server, $win );
+ }
+ return;
+ }
+
+ if ($twit) {
+ my $rate_limit = $twit->rate_limit_status();
+ if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
+ ¬ice(
+ "Rate limit exceeded, try again after $rate_limit->{reset_time}"
+ );
+ $twit = undef;
+ return;
+ }
+
+ $twits{"$user\@$service"} = $twit;
+ Irssi::timeout_remove($poll) if $poll;
+ $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" );
+ ¬ice("Logged in as $user\@$service, loading friends list...");
+ &load_friends();
+ ¬ice( "loaded friends: ", scalar keys %friends );
+ if ( Irssi::settings_get_bool("twirssi_first_run") ) {
+ Irssi::settings_set_bool( "twirssi_first_run", 0 );
+ }
+ %nicks = %friends;
+ $nicks{$user} = 0;
+ return 1;
+ } else {
+ ¬ice("Login failed");
+ }
+}
+
+sub cmd_add_follow {
+ my ( $data, $server, $win ) = @_;
+
+ unless ($data) {
+ ¬ice("Usage: /twitter_add_follow_extra <username>");
+ return;
+ }
+
+ $data =~ s/^\s+|\s+$//;
+ $data =~ s/^\@//;
+ $data = lc $data;
+
+ if ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
+ ¬ice("Already following all replies by \@$data");
+ return;
+ }
+
+ $id_map{__fixreplies}{"$user\@$defservice"}{$data} = 1;
+ ¬ice("Will now follow all replies by \@$data");
+}
+
+sub cmd_del_follow {
+ my ( $data, $server, $win ) = @_;
+
+ unless ($data) {
+ ¬ice("Usage: /twitter_del_follow_extra <username>");
+ return;
+ }
+
+ $data =~ s/^\s+|\s+$//;
+ $data =~ s/^\@//;
+ $data = lc $data;
+
+ unless ( exists $id_map{__fixreplies}{"$user\@$defservice"}{$data} ) {
+ ¬ice("Wasn't following all replies by \@$data");
+ return;
+ }
+
+ delete $id_map{__fixreplies}{"$user\@$defservice"}{$data};
+ ¬ice("Will no longer follow all replies by \@$data");
+}
+
+sub cmd_list_follow {
+ my ( $data, $server, $win ) = @_;
+
+ my $found = 0;
+ foreach my $suser ( sort keys %{ $id_map{__fixreplies} } ) {
+ my $frusers;
+ foreach my $fruser ( sort keys %{ $id_map{__fixreplies}{$suser} } ) {
+ $frusers = $frusers ? "$frusers, $fruser" : $fruser;
+ }
+ if ($frusers) {
+ $found = 1;
+ ¬ice("Following all replies as \@$suser: $frusers");
+ }
+ }
+
+ unless ($found) {
+ ¬ice("Not following all replies by anyone");
+ }
+}
+
+sub cmd_add_search {
+ my ( $data, $server, $win ) = @_;
+
+ unless ( $twit and $twit->can('search') ) {
+ ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
+ . "doesn't support searches." );
+ return;
+ }
+
+ $data =~ s/^\s+|\s+$//;
+ $data = lc $data;
+
+ unless ($data) {
+ ¬ice("Usage: /twitter_subscribe <topic>");
+ return;
+ }
+
+ if ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
+ ¬ice("Already had a subscription for '$data'");
+ return;
+ }
+
+ $id_map{__searches}{"$user\@$defservice"}{$data} = 1;
+ ¬ice("Added subscription for '$data'");
+}
+
+sub cmd_del_search {
+ my ( $data, $server, $win ) = @_;
+
+ unless ( $twit and $twit->can('search') ) {
+ ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
+ . "doesn't support searches." );
+ return;
+ }
+ $data =~ s/^\s+|\s+$//;
+ $data = lc $data;
+
+ unless ($data) {
+ ¬ice("Usage: /twitter_unsubscribe <topic>");
+ return;
+ }
+
+ unless ( exists $id_map{__searches}{"$user\@$defservice"}{$data} ) {
+ ¬ice("No subscription found for '$data'");
+ return;
+ }
+
+ delete $id_map{__searches}{"$user\@$defservice"}{$data};
+ ¬ice("Removed subscription for '$data'");
+}
+
+sub cmd_list_search {
+ my ( $data, $server, $win ) = @_;
+
+ my $found = 0;
+ foreach my $suser ( sort keys %{ $id_map{__searches} } ) {
+ my $topics;
+ foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) {
+ $topics = $topics ? "$topics, $topic" : $topic;
+ }
+ if ($topics) {
+ $found = 1;
+ ¬ice("Search subscriptions for \@$suser: $topics");
+ }
+ }
+
+ unless ($found) {
+ ¬ice("No search subscriptions set up");
+ }
+}
+
+sub cmd_upgrade {
+ my ( $data, $server, $win ) = @_;
+
+ my $loc = Irssi::settings_get_str("twirssi_location");
+ unless ( -w $loc ) {
+ ¬ice(
+"$loc isn't writable, can't upgrade. Perhaps you need to /set twirssi_location?"
+ );
+ return;
+ }
+
+ my $md5;
+ unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
+ eval { use Digest::MD5; };
+
+ if ($@) {
+ ¬ice(
+"Failed to load Digest::MD5. Try '/twirssi_upgrade nomd5' to skip MD5 verification"
+ );
+ return;
+ }
+
+ $md5 = get("http://twirssi.com/md5sum");
+ chomp $md5;
+ $md5 =~ s/ .*//;
+ unless ($md5) {
+ ¬ice("Failed to download md5sum from peeron! Aborting.");
+ return;
+ }
+
+ unless ( open( CUR, $loc ) ) {
+ ¬ice(
+"Failed to read $loc. Check that /set twirssi_location is set to the correct location."
+ );
+ return;
+ }
+
+ my $cur_md5 = Digest::MD5::md5_hex(<CUR>);
+ close CUR;
+
+ if ( $cur_md5 eq $md5 ) {
+ ¬ice("Current twirssi seems to be up to date.");
+ return;
+ }
+ }
+
+ my $URL =
+ Irssi::settings_get_bool("twirssi_upgrade_beta")
+ ? "http://github.com/zigdon/twirssi/raw/master/twirssi.pl"
+ : "http://twirssi.com/twirssi.pl";
+ ¬ice("Downloading twirssi from $URL");
+ LWP::Simple::getstore( $URL, "$loc.upgrade" );
+
+ unless ( -s "$loc.upgrade" ) {
+ ¬ice("Failed to save $loc.upgrade."
+ . " Check that /set twirssi_location is set to the correct location."
+ );
+ return;
+ }
+
+ unless ( $data or Irssi::settings_get_bool("twirssi_upgrade_beta") ) {
+ unless ( open( NEW, "$loc.upgrade" ) ) {
+ ¬ice("Failed to read $loc.upgrade."
+ . " Check that /set twirssi_location is set to the correct location."
+ );
+ return;
+ }
+
+ my $new_md5 = Digest::MD5::md5_hex(<NEW>);
+ close NEW;
+
+ if ( $new_md5 ne $md5 ) {
+ ¬ice("MD5 verification failed. expected $md5, got $new_md5");
+ return;
+ }
+ }
+
+ rename $loc, "$loc.backup"
+ or ¬ice("Failed to back up $loc: $!. Aborting")
+ and return;
+ rename "$loc.upgrade", $loc
+ or ¬ice("Failed to rename $loc.upgrade: $!. Aborting")
+ and return;
+
+ my ( $dir, $file ) = ( $loc =~ m{(.*)/([^/]+)$} );
+ if ( -e "$dir/autorun/$file" ) {
+ ¬ice("Updating $dir/autorun/$file");
+ unlink "$dir/autorun/$file"
+ or ¬ice("Failed to remove old $file from autorun: $!");
+ symlink "../$file", "$dir/autorun/$file"
+ or ¬ice("Failed to create symlink in autorun directory: $!");
+ }
+
+ ¬ice("Download complete. Reload twirssi with /script load $file");
+}
+
+sub load_friends {
+ my $fh = shift;
+ my $page = 1;
+ my %new_friends;
+ eval {
+ while (1)
+ {
+ print $fh "type:debug Loading friends page $page...\n"
+ if ( $fh and &debug );
+ my $friends = $twit->friends( { page => $page } );
+ last unless $friends;
+ $new_friends{ $_->{screen_name} } = time foreach @$friends;
+ $page++;
+ last if @$friends == 0 or $page == 10;
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during friends list update. Aborted.\n";
+ return;
+ }
+
+ my ( $added, $removed ) = ( 0, 0 );
+ print $fh "type:debug Scanning for new friends...\n" if ( $fh and &debug );
+ foreach ( keys %new_friends ) {
+ next if exists $friends{$_};
+ $friends{$_} = time;
+ $added++;
+ }
+
+ print $fh "type:debug Scanning for removed friends...\n"
+ if ( $fh and &debug );
+ foreach ( keys %friends ) {
+ next if exists $new_friends{$_};
+ delete $friends{$_};
+ $removed++;
+ }
+
+ return ( $added, $removed );
+}
+
+sub get_updates {
+ print scalar localtime, " - get_updates starting" if &debug;
+
+ $window =
+ Irssi::window_find_name( Irssi::settings_get_str('twitter_window') );
+ unless ($window) {
+ Irssi::active_win()
+ ->print( "Can't find a window named '"
+ . Irssi::settings_get_str('twitter_window')
+ . "'. Create it or change the value of twitter_window" );
+ }
+
+ return unless &logged_in($twit);
+
+ my ( $fh, $filename ) = File::Temp::tempfile();
+ binmode( $fh, ":utf8" );
+ $child_pid = fork();
+
+ if ($child_pid) { # parent
+ Irssi::timeout_add_once( 5000, 'monitor_child',
+ [ "$filename.done", 0 ] );
+ Irssi::pidwait_add($child_pid);
+ } elsif ( defined $child_pid ) { # child
+ close STDIN;
+ close STDOUT;
+ close STDERR;
+
+ my $new_poll = time;
+
+ my $error = 0;
+ my %context_cache;
+ foreach ( keys %twits ) {
+ $error++ unless &do_updates( $fh, $_, $twits{$_}, \%context_cache );
+
+ if ($id_map{__fixreplies}{$_}) {
+ my @frusers = sort keys %{$id_map{__fixreplies}{$_}};
+
+ $error++ unless &get_timeline( $fh, $frusers[$fix_replies_index{$_}], $_, $twits{$_}, \%context_cache );
+
+ $fix_replies_index{$_}++;
+ $fix_replies_index{$_} = 0 if $fix_replies_index{$_} >= @frusers;
+ print $fh "id:$fix_replies_index{$_} account:$_ type:fix_replies_index\n";
+ }
+ }
+
+
+ print $fh "__friends__\n";
+ if (
+ time - $last_friends_poll >
+ Irssi::settings_get_int('twitter_friends_poll') )
+ {
+ print $fh "__updated ", time, "\n";
+ my ( $added, $removed ) = &load_friends($fh);
+ if ( $added + $removed ) {
+ print $fh "type:debug %R***%n Friends list updated: ",
+ join( ", ",
+ sprintf( "%d added", $added ),
+ sprintf( "%d removed", $removed ) ),
+ "\n";
+ }
+ }
+
+ foreach ( sort keys %friends ) {
+ print $fh "$_ $friends{$_}\n";
+ }
+
+ if ($error) {
+ print $fh "type:debug Update encountered errors. Aborted\n";
+ print $fh "-- $last_poll";
+ } else {
+ print $fh "-- $new_poll";
+ }
+ close $fh;
+ rename $filename, "$filename.done";
+ exit;
+ } else {
+ &ccrap("Failed to fork for updating: $!");
+ }
+ print scalar localtime, " - get_updates ends" if &debug;
+}
+
+sub do_updates {
+ my ( $fh, $username, $obj, $cache ) = @_;
+
+ my $rate_limit = $obj->rate_limit_status();
+ if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) {
+ ¬ice("Rate limit exceeded for $username");
+ return undef;
+ }
+
+ print scalar localtime, " - Polling for updates for $username" if &debug;
+ my $tweets;
+ my $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{timeline} )
+ {
+ $tweets = $obj->friends_timeline( { count => 100 } );
+ } else {
+ $tweets = $obj->friends_timeline();
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during friends_timeline call: Aborted.\n";
+ print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
+ return undef;
+ }
+
+ unless ( ref $tweets ) {
+ if ( $obj->can("get_error") ) {
+ my $error = "Unknown error";
+ eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) };
+ unless ($@) { $error = $obj->get_error() }
+ print $fh
+ "type:debug API Error during friends_timeline call: Aborted\n";
+ print $fh "type:debug : $_\n" foreach split /\n/, Dumper($error);
+
+ } else {
+ print $fh
+ "type:debug API Error during friends_timeline call. Aborted.\n";
+ }
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/[\n\r]/ /g;
+ my $reply = "tweet";
+ if ( Irssi::settings_get_bool("show_reply_context")
+ and $t->{in_reply_to_screen_name} ne $username
+ and $t->{in_reply_to_screen_name}
+ and not exists $friends{ $t->{in_reply_to_screen_name} } )
+ {
+ $nicks{ $t->{in_reply_to_screen_name} } = time;
+ my $context;
+ unless ( $cache->{ $t->{in_reply_to_status_id} } ) {
+ eval {
+ $cache->{ $t->{in_reply_to_status_id} } =
+ $obj->show_status( $t->{in_reply_to_status_id} );
+ };
+
+ }
+ $context = $cache->{ $t->{in_reply_to_status_id} };
+
+ if ($context) {
+ my $ctext = decode_entities( $context->{text} );
+ $ctext =~ s/[\n\r]/ /g;
+ if ( $context->{truncated} and ref($obj) ne 'Net::Identica' ) {
+ $ctext .=
+ " -- http://twitter.com/$context->{user}{screen_name}"
+ . "/status/$context->{id}";
+ }
+ printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
+ $context->{id}, $username,
+ $context->{user}{screen_name}, $ctext;
+ $reply = "reply";
+ }
+ }
+ next
+ if $t->{user}{screen_name} eq $username
+ and not Irssi::settings_get_bool("show_own_tweets");
+ if ( $t->{truncated} and ref($obj) ne 'Net::Identica' ) {
+ $text .= " -- http://twitter.com/$t->{user}{screen_name}"
+ . "/status/$t->{id}";
+ }
+ printf $fh "id:%s account:%s nick:%s type:%s %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
+ }
+ printf $fh "id:%s account:%s type:last_id timeline\n",
+ $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for replies since ",
+ $id_map{__last_id}{$username}{reply}
+ if &debug;
+ $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{reply} )
+ {
+ $tweets = $obj->replies(
+ { since_id => $id_map{__last_id}{$username}{reply} } )
+ || [];
+ } else {
+ $tweets = $obj->replies() || [];
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during replies call. Aborted.\n";
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ next
+ if exists $friends{ $t->{user}{screen_name} };
+
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/[\n\r]/ /g;
+ if ( $t->{truncated} ) {
+ $text .= " -- http://twitter.com/$t->{user}{screen_name}"
+ . "/status/$t->{id}";
+ }
+ printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
+ $t->{id}, $username, $t->{user}{screen_name}, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};