r481 - apparently, returns return from evals. Adjusted code.
[twirssi-net-twitter-lite.git] / twirssi.pl
index 7ba9fe92b5175847a1d7e8a6ff5eaf0c7581ba57..fb995175709999d54f6fa2c7da5560e170ef777a 100644 (file)
@@ -11,8 +11,8 @@ $Data::Dumper::Indent = 1;
 
 use vars qw($VERSION %IRSSI);
 
-$VERSION = "2.0.2";
-my ($REV) = '$Rev: 454 $' =~ /(\d+)/;
+$VERSION = "2.0.5";
+my ($REV) = '$Rev: 481 $' =~ /(\d+)/;
 %IRSSI = (
     authors     => 'Dan Boger',
     contact     => 'zigdon@gmail.com',
@@ -21,7 +21,7 @@ my ($REV) = '$Rev: 454 $' =~ /(\d+)/;
       . 'Can optionally set your bitlbee /away message to same',
     license => 'GNU GPL v2',
     url     => 'http://twirssi.com',
-    changed => '$Date: 2009-02-04 13:35:29 -0800 (Wed, 04 Feb 2009) $',
+    changed => '$Date: 2009-02-19 14:22:45 -0800 (Thu, 19 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 ) {
@@ -140,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;
     }
 
@@ -237,6 +245,7 @@ sub cmd_reply_as {
 
     return if &too_long($data);
 
+    my $success = 1;
     eval {
         unless (
             $twits{$username}->update(
@@ -248,12 +257,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;
     }
 
@@ -280,13 +290,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.");
@@ -688,7 +700,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;
     }
 
@@ -850,7 +863,7 @@ sub monitor_child {
                 }
             }
 
-            if (not $meta{type} or $meta{type} ne 'searchid') {
+            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;
             }
@@ -874,7 +887,9 @@ sub monitor_child {
 
             my $hilight_color =
               $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") };
-            if ( $_ =~ /\@$meta{account}\W/i ) {
+            if ( ( $_ =~ /\@$meta{account}\W/i )
+                && Irssi::settings_get_bool("twirssi_hilights") )
+            {
                 $meta{nick} = "\cC$hilight_color$meta{nick}\cO";
                 $hilight = MSGLEVEL_HILIGHT;
             }
@@ -973,12 +988,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");
     }
 }
 
@@ -1093,8 +1119,16 @@ sub get_poll_time {
 sub hilight {
     my $text = shift;
 
-    $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g;
-    $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g;
+    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;
@@ -1122,6 +1156,8 @@ 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 );
@@ -1130,6 +1166,8 @@ 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;