X-Git-Url: https://git.sommitrealweird.co.uk/twirssi-net-twitter-lite.git/blobdiff_plain/6143e982ea2f215f5059c262d7904228a588ea32..HEAD:/twirssi.pl diff --git a/twirssi.pl b/twirssi.pl index 840a20e..7099013 100644 --- a/twirssi.pl +++ b/twirssi.pl @@ -8,11 +8,12 @@ use LWP::Simple; use Data::Dumper; use Encode; use POSIX qw/:sys_wait_h/; +use Net::Twitter::Lite $Data::Dumper::Indent = 1; use vars qw($VERSION %IRSSI); -$VERSION = "2.4.0beta"; +$VERSION = "2.4.2"; %IRSSI = ( authors => 'Dan Boger', contact => 'zigdon@gmail.com', @@ -21,7 +22,7 @@ $VERSION = "2.4.0beta"; . 'Can optionally set your bitlbee /away message to same', license => 'GNU GPL v2', url => 'http://twirssi.com', - changed => '$Date: 2009-08-07 01:24:53 -0700 (Fri, 07 Aug 2009) $', + changed => 'Fri Jan 22 14:40:48 PST 2010', ); my $window; @@ -130,11 +131,6 @@ sub cmd_retweet { 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+$//; @@ -295,13 +291,6 @@ sub cmd_reply { 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 ." ); - return; - } - return unless &logged_in($twit); $data =~ s/^\s+|\s+$//; @@ -430,9 +419,7 @@ sub cmd_logout { return unless $data = &valid_username($data); ¬ice("Logging out $data..."); - eval { - $twits{$data}->end_session(); - }; + eval { $twits{$data}->end_session(); }; delete $twits{$data}; undef $twit; if ( keys %twits ) { @@ -450,14 +437,15 @@ sub cmd_login { if ($data) { print "manual data login" if &debug; ( $user, $pass ) = split ' ', $data, 2; - unless (Irssi::settings_get_bool("twirssi_use_oauth") or $pass) { + unless ( Irssi::settings_get_bool("twirssi_use_oauth") or $pass ) { ¬ice("usage: /twitter_login [\@] "); return; } - } elsif ( Irssi::settings_get_bool("twirssi_use_oauth") and - my $autouser = Irssi::settings_get_str("twitter_usernames") ) { + } elsif ( Irssi::settings_get_bool("twirssi_use_oauth") + and my $autouser = Irssi::settings_get_str("twitter_usernames") ) + { print "oauth autouser login" if &debug; - foreach my $user (split /,/, $autouser) { + foreach my $user ( split /,/, $autouser ) { &cmd_login($user); } return; @@ -495,7 +483,7 @@ sub cmd_login { ¬ice("/twitter_login requires either a username/password " . "or twitter_usernames and twitter_passwords to be set. " . "Note that if twirssi_use_oauth is true, passwords are " - . "not required"); + . "not required" ); return; } @@ -509,80 +497,22 @@ sub cmd_login { } $defservice = $service = ucfirst lc $service; - print "Loading Net::$service" if &debug; - eval "use Net::$service 3.05"; - if ($@) { - ¬ice( - "Failed to load Net::$service when trying to log in as $user: $@"); - return; - } - - if ( Irssi::settings_get_bool("twirssi_use_oauth") ) { - print "Attempting OAuth for $user\@$service" if &debug; - eval { - $twit = "Net::$service"->new( - traits => [ 'API::REST', 'OAuth' ], - consumer_key => 'BZVAvBma4GxdiRwXIvbnw', - consumer_secret => '0T5kahwLyb34vciGZsgkA9lsjtGCQ05vxVE2APXM', - source => "twirssi", - ssl => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1, - ); - }; - - if ( $twit ) { - if (open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) ) { - while () { - chomp; - next unless m/$user\@$service (\S+) (\S+)/i; - print "Trying cached oauth creds for $user\@$service" if &debug; - $twit->access_token($1); - $twit->access_token_secret($2); - last; - } - close OAUTH; - } - - unless ( $twit->authorized ) { - my $url; - eval { - $url = $twit->get_authorization_url; - }; - - if ($@) { - ¬ice("ERROR: Failed to get OAuth authorization_url. Try again later."); - return; - } - - ¬ice("Twirssi not autorized to access $service for $user."); - ¬ice("Please authorize at the following url, then enter the pin "); - ¬ice("supplied with /twirssi_oauth $user\@$service "); - ¬ice($url); - - $oauth{pending}{"$user\@$service"} = $twit; - return; - } - } - } else { - $twit = "Net::$service"->new( - username => $user, - password => $pass, - source => "twirssi", - ssl => Irssi::settings_get_bool("twirssi_avoid_ssl") ? 0 : 1, - ); - } - + $twit = Net::Twitter::Lite->new( + username => $user, + password => $pass, + ); unless ($twit) { - ¬ice("Failed to create Net::$service object! Aborting."); + ¬ice("Failed to create object! Aborting."); return; } - return &verify_twitter_object($server, $win, $user, $service, $twit); + return &verify_twitter_object( $server, $win, $user, $service, $twit ); } sub cmd_oauth { - my ($data, $server, $win) = @_; + my ( $data, $server, $win ) = @_; my ( $key, $pin ) = split ' ', $data; - my ($user, $service); + my ( $user, $service ); $key = &normalize_username($key); if ( $key =~ /^(.*)@(Twitter|Identica)$/ ) { ( $user, $service ) = ( $1, $2 ); @@ -638,11 +568,11 @@ sub cmd_oauth { . "Please /set twirssi_oauth_store to a writable filename." ); } - return &verify_twitter_object($server, $win, $user, $service, $twit); + return &verify_twitter_object( $server, $win, $user, $service, $twit ); } sub verify_twitter_object { - my ($server, $win, $user, $service, $twit) = @_; + my ( $server, $win, $user, $service, $twit ) = @_; if ( my $timeout = Irssi::settings_get_int("twitter_timeout") and $twit->can('ua') ) @@ -672,7 +602,7 @@ sub verify_twitter_object { 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}" ); + "Rate limit exceeded, try again after $rate_limit->{reset_time}"); $twit = undef; return; } @@ -683,7 +613,7 @@ sub verify_twitter_object { $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 ); + ¬ice( "loaded friends: " . scalar keys %friends ); if ( Irssi::settings_get_bool("twirssi_first_run") ) { Irssi::settings_set_bool( "twirssi_first_run", 0 ); } @@ -945,7 +875,8 @@ sub load_friends { }; if ($@) { - print $fh "type:debug Error during friends list update. Aborted.\n"; + print $fh "type:debug Error during friends list update. Aborted.\n" + if $fh; return; } @@ -983,7 +914,7 @@ sub get_updates { return unless &logged_in($twit); my ( $fh, $filename ) = File::Temp::tempfile(); - binmode( $fh, ":utf8" ); + binmode( $fh, ":" . &get_charset ); $child_pid = fork(); if ($child_pid) { # parent @@ -1097,9 +1028,31 @@ sub do_updates { return undef; } + my @ignore_tags = + Irssi::settings_get_str("twirssi_ignored_tags") + ? split /\s*,\s*/, Irssi::settings_get_str("twirssi_ignored_tags") + : (); + my @strip_tags = + Irssi::settings_get_str("twirssi_stripped_tags") + ? split /\s*,\s*/, Irssi::settings_get_str("twirssi_stripped_tags") + : (); foreach my $t ( reverse @$tweets ) { my $text = &get_text( $t, $obj ); my $reply = "tweet"; + + my $match = 0; + foreach my $tag (@ignore_tags) { + next unless $text =~ /\b\Q$tag\E\b/i; + $match = 1; + $text = "(ignored: $tag) $text" if &debug; + last; + } + next if not &debug and $match; + + foreach my $tag (@strip_tags) { + $text =~ s/\b\Q$tag\E\b//gi; + } + if ( Irssi::settings_get_bool("show_reply_context") and $t->{in_reply_to_screen_name} ne $username and $t->{in_reply_to_screen_name} @@ -1220,6 +1173,7 @@ sub do_updates { } $id_map{__searches}{$username}{$topic} = $search->{max_id}; + $topic =~ s/ /%20/g; printf $fh "id:%s account:%s type:searchid topic:%s\n", $search->{max_id}, $username, $topic; @@ -1322,7 +1276,7 @@ sub monitor_child { # pretend if ( open FILE, $filename ) { - binmode FILE, ":utf8"; + binmode FILE, ":" . &get_charset; my @lines; my %new_cache; while () { @@ -1336,8 +1290,9 @@ sub monitor_child { my %meta; foreach my $key (qw/id account nick type topic/) { - if (s/^$key:(\S+)\s*//) { + if (s/^$key:((?:\S|\\ )+)\s*//) { $meta{$key} = $1; + $meta{$key} =~ s/%20/ /g; } } @@ -1375,11 +1330,7 @@ sub monitor_child { } my $marker = ""; - if ( $meta{type} ne 'dm' - and Irssi::settings_get_bool("twirssi_track_replies") - and $meta{nick} - and $meta{id} ) - { + if ( $meta{type} ne 'dm' and $meta{nick} and $meta{id} ) { $marker = ( $id_map{__indexes}{ $meta{nick} } + 1 ) % 100; $id_map{ lc $meta{nick} }[$marker] = $meta{id}; $id_map{__indexes}{ $meta{nick} } = $marker; @@ -1516,7 +1467,7 @@ sub monitor_child { Irssi::pidwait_remove($child_pid); # and that we don't leave any zombies behind, somehow - wait(); + waitpid( -1, WNOHANG ); # save id_map hash if ( keys %id_map @@ -1544,7 +1495,7 @@ sub monitor_child { } else { print "Giving up on polling $filename" if &debug; Irssi::pidwait_remove($child_pid); - wait(); + waitpid( -1, WNOHANG ); unlink $filename unless &debug; return unless Irssi::settings_get_bool("twirssi_notify_timeouts"); @@ -1558,7 +1509,7 @@ sub monitor_child { } if ( not $failwhale and time - $last_poll > 60 * 60 ) { - foreach my $whale ( + &ccrap( q{ v v v}, q{ | | v | v}, q{ | .-, | | |}, @@ -1567,10 +1518,7 @@ sub monitor_child { q{ \\ a |}, q{ ',.__. ,__.-'/}, q{ '--/_.'----'`} - ) - { - &ccrap($whale); - } + ); $failwhale = 1; } @@ -1585,11 +1533,15 @@ sub debug { } sub notice { - $window->print( "%R***%n @_", MSGLEVEL_PUBLIC ); + foreach my $msg (@_) { + $window->print( "%R***%n $msg", MSGLEVEL_PUBLIC ); + } } sub ccrap { - $window->print( "%R***%n @_", MSGLEVEL_CLIENTCRAP ); + foreach my $msg (@_) { + $window->print( "%R***%n $msg", MSGLEVEL_CLIENTCRAP ); + } } sub update_away { @@ -1705,6 +1657,12 @@ sub get_poll_time { return 60; } +sub get_charset { + my $charset = Irssi::settings_get_str("twirssi_charset"); + return "utf8" if $charset =~ /^\s*$/; + return $charset; +} + sub hilight { my $text = shift; @@ -1745,7 +1703,7 @@ sub shorten { "Set short_url_args to username,API_key or change your", "short_url_provider." ); - return decode "utf8", $data; + return decode &get_charset, $data; } } @@ -1762,7 +1720,7 @@ sub shorten { } } - return decode "utf8", $data; + return decode &get_charset, $data; } sub normalize_username { @@ -1799,7 +1757,7 @@ sub get_text { if ( exists $tweet->{retweeted_status} ) { $text = "RT \@$tweet->{retweeted_status}{user}{screen_name}: " . "$tweet->{retweeted_status}{text}"; - } elsif ( $object->isa('Net::Twitter') ) { + } elsif ( $object->isa('Net::Twitter::Lite') ) { $text .= " -- http://twitter.com/$tweet->{user}{screen_name}" . "/status/$tweet->{id}"; } @@ -1823,6 +1781,7 @@ Irssi::theme_register( ); Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 ); +Irssi::settings_add_str( "twirssi", "twirssi_charset", "utf8" ); Irssi::settings_add_str( "twirssi", "twitter_window", "twitter" ); Irssi::settings_add_str( "twirssi", "bitlbee_server", "bitlbee" ); Irssi::settings_add_str( "twirssi", "short_url_provider", "TinyURL" ); @@ -1832,14 +1791,16 @@ Irssi::settings_add_str( "twirssi", "twitter_passwords", undef ); Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" ); Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" ); Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" ); +Irssi::settings_add_str( "twirssi", "twirssi_ignored_tags", "" ); +Irssi::settings_add_str( "twirssi", "twirssi_stripped_tags", "" ); Irssi::settings_add_str( "twirssi", "twirssi_retweet_format", 'RT $n: "$t" ${-- $c$}' ); Irssi::settings_add_str( "twirssi", "twirssi_location", - "$ENV{HOME}/.irssi/scripts/twirssi.pl" ); + Irssi::get_irssi_dir . "/scripts/twirssi.pl" ); Irssi::settings_add_str( "twirssi", "twirssi_replies_store", - "$ENV{HOME}/.irssi/scripts/twirssi.json" ); + Irssi::get_irssi_dir . "/scripts/twirssi.json" ); Irssi::settings_add_str( "twirssi", "twirssi_oauth_store", - "$ENV{HOME}/.irssi/scripts/twirssi.oauth" ); + Irssi::get_irssi_dir . "/scripts/twirssi.oauth" ); Irssi::settings_add_int( "twirssi", "twitter_friends_poll", 600 ); Irssi::settings_add_int( "twirssi", "twitter_timeout", 30 ); @@ -1850,7 +1811,6 @@ 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 ); @@ -1922,8 +1882,8 @@ if ($window) { ¬ice( "Twirssi v$VERSION; " . ( - $Net::Twitter::VERSION - ? "Net::Twitter v$Net::Twitter::VERSION. " + $Net::Twitter::Lite::VERSION + ? "Net::Twitter v$Net::Twitter::Lite::VERSION. " : "" ) . ( @@ -1979,10 +1939,11 @@ if ($window) { ); Irssi::signal_add_last( 'complete word' => \&sig_complete ); - ¬ice(" %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N"); - ¬ice(" %C(_(\\%N http://twirssi.com/ for full docs"); ¬ice( - " %Y||%C `%N Log in with /twitter_login, send updates with /tweet"); + " %Y<%C(%B^%C)%N TWIRSSI v%R$VERSION%N", + " %C(_(\\%N http://twirssi.com/ for full docs", + " %Y||%C `%N Log in with /twitter_login, send updates with /tweet" + ); my $file = Irssi::settings_get_str("twirssi_replies_store"); if ( $file and -r $file ) {