+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};
+ }
+ printf $fh "id:%s account:%s type:last_id reply\n", $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for DMs" if &debug;
+ $new_poll_id = 0;
+ eval {
+ if ( $id_map{__last_id}{$username}{dm} )
+ {
+ $tweets = $obj->direct_messages(
+ { since_id => $id_map{__last_id}{$username}{dm} } )
+ || [];
+ } else {
+ $tweets = $obj->direct_messages() || [];
+ }
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during direct_messages call. Aborted.\n";
+ return undef;
+ }
+
+ foreach my $t ( reverse @$tweets ) {
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/[\n\r]/ /g;
+ printf $fh "id:%s account:%s nick:%s type:dm %s\n",
+ $t->{id}, $username, $t->{sender_screen_name}, $text;
+ $new_poll_id = $t->{id} if $new_poll_id < $t->{id};
+ }
+ printf $fh "id:%s account:%s type:last_id dm\n", $new_poll_id, $username;
+
+ print scalar localtime, " - Polling for subscriptions" if &debug;
+ if ( $obj->can('search') and $id_map{__searches}{$username} ) {
+ my $search;
+ foreach my $topic ( sort keys %{ $id_map{__searches}{$username} } ) {
+ print $fh "type:debug searching for $topic since ",
+ "$id_map{__searches}{$username}{$topic}\n";
+ eval {
+ $search = $obj->search(
+ {
+ q => $topic,
+ since_id => $id_map{__searches}{$username}{$topic}
+ }
+ );
+ };
+
+ if ($@) {
+ print $fh
+ "type:debug Error during search($topic) call. Aborted.\n";
+ return undef;
+ }
+
+ unless ( $search->{max_id} ) {
+ print $fh "type:debug Invalid search results when searching",
+ " for $topic. Aborted.\n";
+ return undef;
+ }
+
+ $id_map{__searches}{$username}{$topic} = $search->{max_id};
+ printf $fh "id:%s account:%s type:searchid topic:%s\n",
+ $search->{max_id}, $username, $topic;
+
+ foreach my $t ( reverse @{ $search->{results} } ) {
+ my $text = decode_entities( $t->{text} );
+ $text =~ s/[\n\r]/ /g;
+ printf $fh "id:%s account:%s nick:%s type:search topic:%s %s\n",
+ $t->{id}, $username, $t->{from_user}, $topic, $text;
+ $new_poll_id = $t->{id}
+ if not $new_poll_id
+ or $t->{id} < $new_poll_id;
+ }
+ }
+ }
+
+ print scalar localtime, " - Done" if &debug;
+
+ return 1;
+}
+
+sub get_timeline {
+ my ( $fh, $target, $username, $obj, $cache ) = @_;
+ print $fh "type:debug get_timeline($fix_replies_index{$username}=$target) started. username = $username\n";
+ my $tweets;
+ eval {
+ $tweets = $obj->user_timeline({id => $target});
+ };
+
+ if ($@) {
+ print $fh "type:debug Error during user_timeline($target) call: Aborted.\n";
+ print $fh "type:debug : $_\n" foreach split /\n/, Dumper($@);
+ return undef;
+ }
+
+ unless ($tweets) {
+ print $fh "type:debug user_timeline($target) call returned undef! Aborted\n";
+ return 1;
+ }
+
+ 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";
+ }
+ }
+ 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;
+ }
+
+ return 1;
+}
+
+sub monitor_child {
+ my ($data) = @_;