r484 - Add binmode so that we don't break incoming UTF8 tweets
[twirssi-net-twitter-lite.git] / twirssi.pl
index b4094ad49ce5341e9e8225d63eb23756708e59ba..0d4cb26751b00588a171562dd3a620ab24e8d788 100644 (file)
@@ -11,8 +11,8 @@ $Data::Dumper::Indent = 1;
 
 use vars qw($VERSION %IRSSI);
 
-$VERSION = "2.0.3";
-my ($REV) = '$Rev: 475 $' =~ /(\d+)/;
+$VERSION = "2.0.6";
+my ($REV) = '$Rev: 484 $' =~ /(\d+)/;
 %IRSSI = (
     authors     => 'Dan Boger',
     contact     => 'zigdon@gmail.com',
@@ -21,7 +21,7 @@ my ($REV) = '$Rev: 475 $' =~ /(\d+)/;
       . 'Can optionally set your bitlbee /away message to same',
     license => 'GNU GPL v2',
     url     => 'http://twirssi.com',
-    changed => '$Date: 2009-02-16 20:36:03 -0800 (Mon, 16 Feb 2009) $',
+    changed => '$Date: 2009-02-21 13:53:25 -0800 (Sat, 21 Feb 2009) $',
 );
 
 my $window;
@@ -81,20 +81,25 @@ 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;
-    } else {
-        &notice("DM sent to $target");
-        $nicks{$target} = time;
     }
 }
 
@@ -141,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;
     }
 
@@ -227,17 +234,20 @@ sub cmd_reply_as {
         $data = "\@$nick " . $data;
     }
 
-    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;
-            };
+    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(
@@ -249,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;
     }
 
@@ -281,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.");
@@ -490,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; };
@@ -628,6 +634,7 @@ sub get_updates {
     return unless &logged_in($twit);
 
     my ( $fh, $filename ) = File::Temp::tempfile();
+    binmode($fh, ":utf8");
     my $pid = fork();
 
     if ($pid) {    # parent
@@ -689,7 +696,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;
     }
 
@@ -976,12 +984,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");
     }
 }
 
@@ -1099,11 +1118,12 @@ sub hilight {
     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;
+        $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");
-        $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g;
+        $c = $irssi_to_mirc_colors{$c};
+        $text =~ s/(^|\W)\#([-\w]+)/$1\cC$c\#$2\cO/g if $c;
     }
     $text =~ s/[\n\r]/ /g;
 
@@ -1142,6 +1162,7 @@ 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 );