X-Git-Url: https://git.sommitrealweird.co.uk/twirssi-net-twitter-lite.git/blobdiff_plain/f0c04189c57f0444094480a9602fd41a3d2606a1..6788ac4739865440cf868a4640460e64bfb00dd3:/twirssi.pl diff --git a/twirssi.pl b/twirssi.pl index c5384cf..7147981 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 qw/3.05/; $Data::Dumper::Indent = 1; use vars qw($VERSION %IRSSI); -$VERSION = "2.4.0beta"; +$VERSION = "2.4.1beta"; %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; @@ -430,7 +431,7 @@ sub cmd_logout { return unless $data = &valid_username($data); ¬ice("Logging out $data..."); - $twits{$data}->end_session(); + eval { $twits{$data}->end_session(); }; delete $twits{$data}; undef $twit; if ( keys %twits ) { @@ -448,14 +449,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; @@ -493,7 +495,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; } @@ -507,32 +509,39 @@ 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") ) { + if ( $service eq 'Twitter' + and 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 ( $service eq 'Identica' ) + { + $twit = Net::Twitter->new( + identica => 1, + traits => [ 'API::REST', 'API::Search' ], + source => "twirssi", + ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"), + ); + } else { + $twit = Net::Twitter->new( + traits => [ 'API::REST', 'OAuth', 'API::Search' ], + consumer_key => 'BZVAvBma4GxdiRwXIvbnw', + consumer_secret => + '0T5kahwLyb34vciGZsgkA9lsjtGCQ05vxVE2APXM', + source => "twirssi", + ssl => !Irssi::settings_get_bool("twirssi_avoid_ssl"), + ); + } }; - if ( $twit ) { - if (open( OAUTH, Irssi::settings_get_str("twirssi_oauth_store") ) ) { + 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; + print "Trying cached oauth creds for $user\@$service" + if &debug; $twit->access_token($1); $twit->access_token_secret($2); last; @@ -541,17 +550,27 @@ sub cmd_login { } unless ( $twit->authorized ) { - ¬ice("Twirssi not autorized to access $service for $user."); - ¬ice("Please authorize at the following url, then enter the pin "); - ¬ice("supplied with /twirssi_oath $user\@$service "); - ¬ice($twit->get_authorization_url); + 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.", + "Please authorize at the following url, then enter the pin", + "supplied with /twirssi_oauth $user\@$service ", + $url + ); $oauth{pending}{"$user\@$service"} = $twit; return; } } - } else { - $twit = "Net::$service"->new( + } else { + $twit = Net::Twitter->new( + $service eq 'Identica' ? ( identica => 1 ) : (), username => $user, password => $pass, source => "twirssi", @@ -560,17 +579,17 @@ sub cmd_login { } 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 ); @@ -600,21 +619,21 @@ sub cmd_oauth { my $store_file = Irssi::settings_get_str("twirssi_oauth_store"); if ($store_file) { - my %store; + my @store; if ( open( OAUTH, $store_file ) ) { while () { chomp; - my ( $k, $v ) = split ' ', 2; - $store{$k} = $v; + next if /$key/i; + push @store, $_; } close OAUTH; } - $store{$key} = "$access_token $access_token_secret"; + push @store, "$key $access_token $access_token_secret"; if ( open( OAUTH, ">$store_file.new" ) ) { - print OAUTH "$_ $store{$_}\n" foreach keys %store; + print OAUTH "$_\n" foreach @store; close OAUTH; rename "$store_file.new", $store_file or ¬ice("Failed to rename $store_file.new: $!"); @@ -626,11 +645,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') ) @@ -660,7 +679,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; } @@ -671,7 +690,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 ); } @@ -933,7 +952,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; } @@ -1043,11 +1063,13 @@ sub get_updates { 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; - } + eval { + 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; @@ -1544,7 +1566,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{ | .-, | | |}, @@ -1553,10 +1575,7 @@ sub monitor_child { q{ \\ a |}, q{ ',.__. ,__.-'/}, q{ '--/_.'----'`} - ) - { - &ccrap($whale); - } + ); $failwhale = 1; } @@ -1571,11 +1590,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 { @@ -1821,11 +1844,11 @@ Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" ); 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 ); @@ -1965,10 +1988,9 @@ 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"); + ¬ice(" %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 ) { @@ -2002,9 +2024,7 @@ if ($window) { } } - if ( my $autouser = Irssi::settings_get_str("twitter_usernames") - and my $autopass = Irssi::settings_get_str("twitter_passwords") ) - { + if ( my $autouser = Irssi::settings_get_str("twitter_usernames") ) { &cmd_login(); &get_updates; }