(no commit message)
[twirssi-net-twitter-lite.git] / twirssi.pl
index f0567d591a37c27be0ea3c458449cffe58e9169f..093a1b84bcab255efdb862e07dbd1a075503431e 100644 (file)
@@ -6,13 +6,13 @@ use HTML::Entities;
 use File::Temp;
 use LWP::Simple;
 use Data::Dumper;
-use Net::Twitter;
+use Net::Identica;
 $Data::Dumper::Indent = 1;
 
 use vars qw($VERSION %IRSSI);
 
-$VERSION = "2.0";
-my ($REV) = '$Rev: 443 $' =~ /(\d+)/;
+$VERSION = "2.0.6";
+my ($REV) = '$Rev: 483 $' =~ /(\d+)/;
 %IRSSI = (
     authors     => 'Dan Boger',
     contact     => 'zigdon@gmail.com',
@@ -21,7 +21,7 @@ my ($REV) = '$Rev: 443 $' =~ /(\d+)/;
       . 'Can optionally set your bitlbee /away message to same',
     license => 'GNU GPL v2',
     url     => 'http://twirssi.com',
-    changed => '$Date: 2009-01-29 18:25:38 -0800 (Thu, 29 Jan 2009) $',
+    changed => '$Date: 2009-02-20 14:37:28 -0800 (Fri, 20 Feb 2009) $',
 );
 
 my $window;
@@ -81,21 +81,26 @@ sub cmd_direct_as {
     return unless &valid_username($username);
 
     eval {
-        unless ( $twits{$username}
+        if ( $twits{$username}
             ->new_direct_message( { user => $target, text => $text } ) )
         {
+            &notice("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;
             &notice("DM to $target failed");
-            return;
         }
     };
 
     if ($@) {
-        &notice("DM caused an error.  Aborted");
+        &notice("DM caused an error: $@");
         return;
     }
-
-    &notice("DM sent to $target");
-    $nicks{$target} = time;
 }
 
 sub cmd_tweet {
@@ -118,6 +123,7 @@ sub cmd_tweet_as {
     return unless &logged_in($twit);
 
     $data =~ s/^\s+|\s+$//;
+    $data =~ s/\s\s+/ /g;
     my ( $username, $data ) = split ' ', $data, 2;
 
     unless ( $username and $data ) {
@@ -127,7 +133,9 @@ sub cmd_tweet_as {
 
     return unless &valid_username($username);
 
-    if ( &too_long($data) and Irssi::settings_get_str("short_url_provider") ) {
+    if ( &too_long( $data, 1 )
+        and Irssi::settings_get_str("short_url_provider") )
+    {
         foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
             eval {
                 my $short = makeashorterlink($url);
@@ -138,16 +146,18 @@ sub cmd_tweet_as {
 
     return if &too_long($data);
 
+    my $success = 1;
     eval {
         unless ( $twits{$username}->update($data) )
         {
             &notice("Update failed");
-            return;
+            $success = 0;
         }
     };
+    return unless $success;
 
     if ($@) {
-        &notice("Update caused an error.  Aborted.");
+        &notice("Update caused an error: $@.  Aborted.");
         return;
     }
 
@@ -217,21 +227,27 @@ sub cmd_reply_as {
         return;
     }
 
-    # remove any @nick at the beginning of the reply, as we'll add it anyway
-    $data =~ s/^\s*\@?$nick\s*//;
-    $data = "\@$nick " . $data;
+    if ( Irssi::settings_get_bool("twirssi_replies_autonick") ) {
 
-    if ( Irssi::settings_get_str("short_url_provider") ) {
-        foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
-            eval {
-                my $short = makeashorterlink($url);
-                $data =~ s/\Q$url/$short/g;
-            };
+        # remove any @nick at the beginning of the reply, as we'll add it anyway
+        $data =~ s/^\s*\@?$nick\s*//;
+        $data = "\@$nick " . $data;
+    }
+
+    if ( &too_long( $data, 1 ) ) {
+        if ( Irssi::settings_get_str("short_url_provider") ) {
+            foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) {
+                eval {
+                    my $short = makeashorterlink($url);
+                    $data =~ s/\Q$url/$short/g;
+                };
+            }
         }
     }
 
     return if &too_long($data);
 
+    my $success = 1;
     eval {
         unless (
             $twits{$username}->update(
@@ -243,12 +259,13 @@ sub cmd_reply_as {
           )
         {
             &notice("Update failed");
-            return;
+            $success = 0;
         }
     };
+    return unless $success;
 
     if ($@) {
-        &notice("Update caused an error.  Aborted");
+        &notice("Update caused an error: $@.  Aborted");
         return;
     }
 
@@ -275,13 +292,15 @@ sub gen_cmd {
             return;
         }
 
+        my $success = 1;
         eval {
             unless ( $twit->$api_name($data) )
             {
                 &notice("$api_name failed");
-                return;
+                $success = 0;
             }
         };
+        return unless $success;
 
         if ($@) {
             &notice("$api_name caused an error.  Aborted.");
@@ -309,23 +328,18 @@ sub cmd_logout {
     my ( $data, $server, $win ) = @_;
 
     $data =~ s/^\s+|\s+$//g;
+    $data = $user unless $data;
     return unless &valid_username($data);
 
-    if ($data) {
-        &notice("Logging out $data...");
-        $twits{$data}->end_session();
-        delete $twits{$data};
+    &notice("Logging out $data...");
+    $twits{$data}->end_session();
+    delete $twits{$data};
+    undef $twit;
+    if ( keys %twits ) {
+        &cmd_switch( ( keys %twits )[0], $server, $win );
     } else {
-        &notice("Logging out $user...");
-        $twit->end_session();
-        undef $twit;
-        delete $twits{$user};
-        if ( keys %twits ) {
-            &cmd_switch( ( keys %twits )[0], $server, $win );
-        } else {
-            Irssi::timeout_remove($poll) if $poll;
-            undef $poll;
-        }
+        Irssi::timeout_remove($poll) if $poll;
+        undef $poll;
     }
 }
 
@@ -359,7 +373,7 @@ sub cmd_login {
 
     %friends = %nicks = ();
 
-    $twit = Net::Twitter->new(
+    $twit = Net::Identica->new(
         username => $user,
         password => $pass,
         source   => "twirssi"
@@ -411,13 +425,19 @@ sub cmd_add_search {
     my ( $data, $server, $win ) = @_;
 
     unless ( $twit and $twit->can('search') ) {
-        &notice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
+        &notice("ERROR: Your version of Net::Identica ($Net::Identica::VERSION) "
               . "doesn't support searches." );
         return;
     }
 
     $data =~ s/^\s+|\s+$//;
     $data = lc $data;
+
+    unless ($data) {
+        &notice("Usage: /twitter_subscribe <topic>");
+        return;
+    }
+
     if ( exists $id_map{__searches}{$user}{$data} ) {
         &notice("Already had a subscription for '$data'");
         return;
@@ -431,12 +451,18 @@ sub cmd_del_search {
     my ( $data, $server, $win ) = @_;
 
     unless ( $twit and $twit->can('search') ) {
-        &notice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) "
+        &notice("ERROR: Your version of Net::Identica ($Net::Identica::VERSION) "
               . "doesn't support searches." );
         return;
     }
     $data =~ s/^\s+|\s+$//;
     $data = lc $data;
+
+    unless ($data) {
+        &notice("Usage: /twitter_unsubscribe <topic>");
+        return;
+    }
+
     unless ( exists $id_map{__searches}{$user}{$data} ) {
         &notice("No subscription found for '$data'");
         return;
@@ -477,13 +503,6 @@ sub cmd_upgrade {
         return;
     }
 
-    if ( not -x "/usr/bin/md5sum" and not $data ) {
-        &notice(
-"/usr/bin/md5sum can't be found - try '/twirssi_upgrade nomd5' to skip MD5 verification"
-        );
-        return;
-    }
-
     my $md5;
     unless ($data) {
         eval { use Digest::MD5; };
@@ -676,7 +695,8 @@ sub do_updates {
     };
 
     if ($@) {
-        print $fh "type:debug Error during friends_timeline call.  Aborted.\n";
+        print $fh
+          "type:debug Error during friends_timeline call: $@.  Aborted.\n";
         return 1;
     }
 
@@ -696,8 +716,7 @@ sub do_updates {
 
     foreach my $t ( reverse @$tweets ) {
         my $text = decode_entities( $t->{text} );
-        $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-        $text =~ s/[\n\r]/ /g;
+        $text = &hilight($text);
         my $reply = "tweet";
         if (    Irssi::settings_get_bool("show_reply_context")
             and $t->{in_reply_to_screen_name} ne $username
@@ -712,8 +731,7 @@ sub do_updates {
 
             if ($context) {
                 my $ctext = decode_entities( $context->{text} );
-                $ctext =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-                $ctext =~ s/[\n\r]/ /g;
+                $ctext = &hilight($ctext);
                 printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
                   $context->{id}, $username,
                   $context->{user}{screen_name}, $ctext;
@@ -722,7 +740,7 @@ sub do_updates {
                 print $fh "type:debug request to get context failed: $@";
             } else {
                 print $fh
-"type:debug Failed to get context from $t->{in_reply_to_screen_name}"
+"type:debug Failed to get context from $t->{in_reply_to_screen_name}\n"
                   if &debug;
             }
         }
@@ -749,8 +767,7 @@ sub do_updates {
           if exists $friends{ $t->{user}{screen_name} };
 
         my $text = decode_entities( $t->{text} );
-        $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-        $text =~ s/[\n\r]/ /g;
+        $text = &hilight($text);
         printf $fh "id:%d account:%s nick:%s type:tweet %s\n",
           $t->{id}, $username, $t->{user}{screen_name}, $text;
     }
@@ -769,8 +786,7 @@ sub do_updates {
 
     foreach my $t ( reverse @$tweets ) {
         my $text = decode_entities( $t->{text} );
-        $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-        $text =~ s/[\n\r]/ /g;
+        $text = &hilight($text);
         printf $fh "id:%d account:%s nick:%s type:dm %s\n",
           $t->{id}, $username, $t->{sender_screen_name}, $text;
     }
@@ -779,6 +795,8 @@ sub do_updates {
     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(
                     {
@@ -796,7 +814,8 @@ sub do_updates {
 
             unless ( $search->{max_id} ) {
                 print $fh
-"type:debug Invalid search results when searching for $topic.  Aborted.\n";
+"type:debug Invalid search results when searching for $topic.",
+                  "  Aborted.\n";
                 return 1;
             }
 
@@ -806,9 +825,7 @@ sub do_updates {
 
             foreach my $t ( reverse @{ $search->{results} } ) {
                 my $text = decode_entities( $t->{text} );
-                $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-                $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g;
-                $text =~ s/[\n\r]/ /g;
+                $text = &hilight($text);
                 printf $fh "id:%d account:%s nick:%s type:search topic:%s %s\n",
                   $t->{id}, $username, $t->{from_user}, $topic, $text;
             }
@@ -841,8 +858,11 @@ sub monitor_child {
                 }
             }
 
-            next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
-            $tweet_cache{ $meta{id} } = time;
+            if ( not $meta{type} or $meta{type} ne 'searchid' ) {
+                next if exists $meta{id} and exists $tweet_cache{ $meta{id} };
+                $tweet_cache{ $meta{id} } = time;
+            }
+
             my $account = "";
             if ( $meta{account} ne $user ) {
                 $account = "$meta{account}: ";
@@ -862,7 +882,9 @@ sub monitor_child {
 
             my $hilight_color =
               $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
-            if ( $_ =~ /\@($meta{account})\W/ ) {
+            if ( ( $_ =~ /\@$meta{account}\W/i )
+                && Irssi::settings_get_bool("twirssi_hilights") )
+            {
                 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
                 $hilight = MSGLEVEL_HILIGHT;
             }
@@ -880,6 +902,12 @@ sub monitor_child {
                     $meta{type}, $account, $meta{topic},
                     $meta{nick}, $marker,  $_
                   ];
+                if ( $meta{id} >
+                    $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
+                {
+                    $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
+                      $meta{id};
+                }
             } elsif ( $meta{type} eq 'dm' ) {
                 push @lines,
                   [
@@ -887,9 +915,15 @@ sub monitor_child {
                     $meta{type}, $account, $meta{nick}, $_
                   ];
             } elsif ( $meta{type} eq 'searchid' ) {
-                $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
-                  $meta{id};
-                print "Search '$meta{topic}' id set to $meta{id}" if &debug;
+                print "Search '$meta{topic}' returned id $meta{id}" if &debug;
+                if ( $meta{id} >=
+                    $id_map{__searches}{ $meta{account} }{ $meta{topic} } )
+                {
+                    $id_map{__searches}{ $meta{account} }{ $meta{topic} } =
+                      $meta{id};
+                } elsif (&debug) {
+                    print "Search '$meta{topic}' returned invalid id $meta{id}";
+                }
             } elsif ( $meta{type} eq 'error' ) {
                 push @lines, [ MSGLEVEL_MSGS, $_ ];
             } elsif ( $meta{type} eq 'debug' ) {
@@ -949,12 +983,23 @@ sub monitor_child {
 
     close FILE;
 
-    if ( $attempt < 12 ) {
+    if ( $attempt < 24 ) {
         Irssi::timeout_add_once( 5000, 'monitor_child',
             [ $filename, $attempt + 1 ] );
     } else {
-        &notice("Giving up on polling $filename");
+        print "Giving up on polling $filename" if &debug;
         unlink $filename unless &debug;
+
+        return unless Irssi::settings_get_bool("twirssi_notify_timeouts");
+
+        my $since;
+        my @time = localtime($last_poll);
+        if ( time - $last_poll < 24 * 60 * 60 ) {
+            $since = sprintf( "%d:%02d", @time[ 2, 1 ] );
+        } else {
+            $since = scalar localtime($last_poll);
+        }
+        &notice("Haven't been able to get updated tweets since $since");
     }
 }
 
@@ -1066,6 +1111,24 @@ sub get_poll_time {
     return 60;
 }
 
+sub hilight {
+    my $text = shift;
+
+    if ( Irssi::settings_get_str("twirssi_nick_color") ) {
+        my $c = Irssi::settings_get_str("twirssi_nick_color");
+        $c = $irssi_to_mirc_colors{$c};
+        $text =~ s/(^|\W)\@([-\w]+)/$1\cC$c\@$2\cO/g if $c;
+    }
+    if ( Irssi::settings_get_str("twirssi_topic_color") ) {
+        my $c = Irssi::settings_get_str("twirssi_topic_color");
+        $c = $irssi_to_mirc_colors{$c};
+        $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
+    }
+    $text =~ s/[\n\r]/ /g;
+
+    return $text;
+}
+
 Irssi::signal_add( "send text", "event_send_text" );
 
 Irssi::theme_register(
@@ -1088,13 +1151,18 @@ Irssi::settings_add_str( "twirssi", "twitter_usernames", undef );
 Irssi::settings_add_str( "twirssi", "twitter_passwords", undef );
 Irssi::settings_add_str( "twirssi", "twirssi_replies_store",
     ".irssi/scripts/twirssi.json" );
+Irssi::settings_add_str( "twirssi", "twirssi_nick_color",  "%B" );
+Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
 Irssi::settings_add_bool( "twirssi", "tweet_to_away",             0 );
 Irssi::settings_add_bool( "twirssi", "show_reply_context",        0 );
 Irssi::settings_add_bool( "twirssi", "show_own_tweets",           1 );
 Irssi::settings_add_bool( "twirssi", "twirssi_debug",             0 );
 Irssi::settings_add_bool( "twirssi", "twirssi_first_run",         1 );
 Irssi::settings_add_bool( "twirssi", "twirssi_track_replies",     1 );
+Irssi::settings_add_bool( "twirssi", "twirssi_replies_autonick",  1 );
 Irssi::settings_add_bool( "twirssi", "twirssi_use_reply_aliases", 0 );
+Irssi::settings_add_bool( "twirssi", "twirssi_notify_timeouts",   1 );
+Irssi::settings_add_bool( "twirssi", "twirssi_hilights",          1 );
 Irssi::settings_add_bool( "twirssi", "tweet_window_input",        0 );
 
 $last_poll = time - &get_poll_time;
@@ -1139,7 +1207,7 @@ if ($window) {
         "twirssi_version",
         sub {
             &notice("Twirssi v$VERSION (r$REV); "
-                  . "Net::Twitter v$Net::Twitter::VERSION. "
+                  . "Net::Identica v$Net::Identica::VERSION. "
                   . "JSON in use: "
                   . JSON::Any::handler()
                   . ".  See details at http://twirssi.com/" );