X-Git-Url: https://git.sommitrealweird.co.uk//gitweb/?p=twirssi-net-twitter-lite.git;a=blobdiff_plain;f=twirssi.pl;h=757cb708f9b5c1b718f36fbacc0d66de0f944409;hp=9d374dc41fac23d14a9f24c641e4335c594ea15b;hb=5c590e8510d34cf0afe10493788f064f3f9816ec;hpb=8bfb8601326ddc503216c9dd3d81cecdd053256d diff --git a/twirssi.pl b/twirssi.pl index 9d374dc..757cb70 100644 --- a/twirssi.pl +++ b/twirssi.pl @@ -6,20 +6,13 @@ use HTML::Entities; use File::Temp; use LWP::Simple; use Data::Dumper; +use Net::Twitter; $Data::Dumper::Indent = 1; -BEGIN { - $ENV{JSON_ANY_ORDER} = "JSON Syck DWIW"; - require JSON::Any; - import JSON::Any; - require Net::Twitter; - import Net::Twitter; -} - use vars qw($VERSION %IRSSI); -$VERSION = "1.7.3"; -my ($REV) = '$Rev: 354 $' =~ /(\d+)/; +$VERSION = "2.0.2"; +my ($REV) = '$Rev: 452 $' =~ /(\d+)/; %IRSSI = ( authors => 'Dan Boger', contact => 'zigdon@gmail.com', @@ -27,8 +20,8 @@ my ($REV) = '$Rev: 354 $' =~ /(\d+)/; description => 'Send twitter updates using /tweet. ' . 'Can optionally set your bitlbee /away message to same', license => 'GNU GPL v2', - url => 'http://tinyurl.com/twirssi', - changed => '$Date: 2009-01-08 14:46:04 -0800 (Thu, 08 Jan 2009) $', + url => 'http://twirssi.com', + changed => '$Date: 2009-02-03 10:38:51 -0800 (Tue, 03 Feb 2009) $', ); my $window; @@ -36,11 +29,29 @@ my $twit; my %twits; my $user; my $poll; +my $last_poll; my %nicks; my %friends; -my $last_poll = time - 300; my %tweet_cache; my %id_map; +my %irssi_to_mirc_colors = ( + '%k' => '01', + '%r' => '05', + '%g' => '03', + '%y' => '07', + '%b' => '02', + '%m' => '06', + '%c' => '10', + '%w' => '15', + '%K' => '14', + '%R' => '04', + '%G' => '09', + '%Y' => '08', + '%B' => '12', + '%M' => '13', + '%C' => '11', + '%W' => '00', +); sub cmd_direct { my ( $data, $server, $win ) = @_; @@ -116,7 +127,9 @@ sub cmd_tweet_as { return unless &valid_username($username); - if ( 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); @@ -206,9 +219,12 @@ 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") ) { + + # 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_str("short_url_provider") ) { foreach my $url ( $data =~ /(https?:\/\/\S+[\w\/])/g ) { @@ -298,23 +314,18 @@ sub cmd_logout { my ( $data, $server, $win ) = @_; $data =~ s/^\s+|\s+$//g; + $data = $user unless $data; return unless &valid_username($data); - if ($data) { - ¬ice("Logging out $data..."); - $twits{$data}->end_session(); - delete $twits{$data}; + ¬ice("Logging out $data..."); + $twits{$data}->end_session(); + delete $twits{$data}; + undef $twit; + if ( keys %twits ) { + &cmd_switch( ( keys %twits )[0], $server, $win ); } else { - ¬ice("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; } } @@ -366,14 +377,16 @@ sub cmd_login { if ($twit) { my $rate_limit = $twit->rate_limit_status(); if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) { - ¬ice("Rate limit exceeded, try again later"); + ¬ice( + "Rate limit exceeded, try again after $rate_limit->{reset_time}" + ); $twit = undef; return; } $twits{$user} = $twit; Irssi::timeout_remove($poll) if $poll; - $poll = Irssi::timeout_add( 300 * 1000, \&get_updates, "" ); + $poll = Irssi::timeout_add( &get_poll_time * 1000, \&get_updates, "" ); ¬ice("Logged in as $user, loading friends list..."); &load_friends(); ¬ice( "loaded friends: ", scalar keys %friends ); @@ -394,6 +407,77 @@ sub cmd_login { } } +sub cmd_add_search { + my ( $data, $server, $win ) = @_; + + unless ( $twit and $twit->can('search') ) { + ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) " + . "doesn't support searches." ); + return; + } + + $data =~ s/^\s+|\s+$//; + $data = lc $data; + + unless ($data) { + ¬ice("Usage: /twitter_subscribe "); + return; + } + + if ( exists $id_map{__searches}{$user}{$data} ) { + ¬ice("Already had a subscription for '$data'"); + return; + } + + $id_map{__searches}{$user}{$data} = 1; + ¬ice("Added subscription for '$data'"); +} + +sub cmd_del_search { + my ( $data, $server, $win ) = @_; + + unless ( $twit and $twit->can('search') ) { + ¬ice("ERROR: Your version of Net::Twitter ($Net::Twitter::VERSION) " + . "doesn't support searches." ); + return; + } + $data =~ s/^\s+|\s+$//; + $data = lc $data; + + unless ($data) { + ¬ice("Usage: /twitter_unsubscribe "); + return; + } + + unless ( exists $id_map{__searches}{$user}{$data} ) { + ¬ice("No subscription found for '$data'"); + return; + } + + delete $id_map{__searches}{$user}{$data}; + ¬ice("Removed subscription for '$data'"); +} + +sub cmd_list_search { + my ( $data, $server, $win ) = @_; + + my $found = 0; + foreach my $suser ( sort keys %{ $id_map{__searches} } ) { + my $topics; + foreach my $topic ( sort keys %{ $id_map{__searches}{$suser} } ) { + $topics = $topics ? "$topics, $topic" : $topic; + } + if ($topics) { + $found = 1; + ¬ice("Search subscriptions for \@$suser: $topics"); + } + } + + unless ($found) { + ¬ice("No search subscriptions set up"); + } +} + sub cmd_upgrade { my ( $data, $server, $win ) = @_; @@ -590,6 +674,12 @@ sub get_updates { sub do_updates { my ( $fh, $username, $obj ) = @_; + my $rate_limit = $obj->rate_limit_status(); + if ( $rate_limit and $rate_limit->{remaining_hits} < 1 ) { + ¬ice("Rate limit exceeded for $username"); + return 1; + } + print scalar localtime, " - Polling for updates for $username" if &debug; my $tweets; eval { @@ -604,8 +694,11 @@ sub do_updates { unless ( ref $tweets ) { if ( $obj->can("get_error") ) { + my $error; + eval { $error = JSON::Any->jsonToObj( $obj->get_error() ) }; + if ($@) { $error = $obj->get_error() } print $fh "type:debug API Error during friends_timeline call: ", - JSON::Any->jsonToObj( $obj->get_error() ), " Aborted.\n"; + "$error Aborted.\n"; } else { print $fh "type:debug API Error during friends_timeline call. Aborted.\n"; @@ -615,9 +708,7 @@ sub do_updates { foreach my $t ( reverse @$tweets ) { my $text = decode_entities( $t->{text} ); - $text =~ s/%/%%/g; - $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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 @@ -632,9 +723,7 @@ sub do_updates { if ($context) { my $ctext = decode_entities( $context->{text} ); - $ctext =~ s/%/%%/g; - $ctext =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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; @@ -643,7 +732,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; } } @@ -670,9 +759,7 @@ sub do_updates { if exists $friends{ $t->{user}{screen_name} }; my $text = decode_entities( $t->{text} ); - $text =~ s/%/%%/g; - $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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; } @@ -691,12 +778,52 @@ sub do_updates { foreach my $t ( reverse @$tweets ) { my $text = decode_entities( $t->{text} ); - $text =~ s/%/%%/g; - $text =~ s/(^|\W)\@([-\w]+)/$1%B\@$2%n/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; } + + print scalar localtime, " - Polling for subscriptions" if &debug; + 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( + { + q => $topic, + since_id => $id_map{__searches}{$username}{$topic} + } + ); + }; + + if ($@) { + print $fh + "type:debug Error during search($topic) call. Aborted.\n"; + return 1; + } + + unless ( $search->{max_id} ) { + print $fh +"type:debug Invalid search results when searching for $topic.", + " Aborted.\n"; + return 1; + } + + $id_map{__searches}{$username}{$topic} = $search->{max_id}; + printf $fh "id:%d account:%s type:searchid topic:%s\n", + $search->{max_id}, $username, $topic; + + foreach my $t ( reverse @{ $search->{results} } ) { + my $text = decode_entities( $t->{text} ); + $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; + } + } + } + print scalar localtime, " - Done" if &debug; return 0; @@ -715,8 +842,9 @@ sub monitor_child { while () { chomp; last if /^__friends__/; + my $hilight = 0; my %meta; - foreach my $key (qw/id account nick type/) { + foreach my $key (qw/id account nick type topic/) { if (s/^$key:(\S+)\s*//) { $meta{$key} = $1; } @@ -741,14 +869,45 @@ sub monitor_child { $marker = ":$marker"; } - if ( $meta{type} eq 'tweet' ) { - push @lines, "[$account%B\@$meta{nick}%n$marker] $_\n",; - } elsif ( $meta{type} eq 'reply' ) { - push @lines, "[$account\\--> %B\@$meta{nick}%n$marker] $_\n",; + my $hilight_color = + $irssi_to_mirc_colors{ Irssi::settings_get_str("hilight_color") }; + if ( $_ =~ /\@$meta{account}\W/i ) { + $meta{nick} = "\cC$hilight_color$meta{nick}\cO"; + $hilight = MSGLEVEL_HILIGHT; + } + + if ( $meta{type} =~ /tweet|reply/ ) { + push @lines, + [ + ( MSGLEVEL_PUBLIC | $hilight ), + $meta{type}, $account, $meta{nick}, $marker, $_ + ]; + } elsif ( $meta{type} eq 'search' ) { + push @lines, + [ + ( MSGLEVEL_PUBLIC | $hilight ), + $meta{type}, $account, $meta{topic}, + $meta{nick}, $marker, $_ + ]; } elsif ( $meta{type} eq 'dm' ) { - push @lines, "[$account%B\@$meta{nick}%n (%WDM%n)] $_\n",; + push @lines, + [ + ( MSGLEVEL_MSGS | $hilight ), + $meta{type}, $account, $meta{nick}, $_ + ]; + } elsif ( $meta{type} eq 'searchid' ) { + 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}"; + } + print "Search '$meta{topic}' id set to $meta{id}" if &debug; } elsif ( $meta{type} eq 'error' ) { - push @lines, "ERROR: $_\n"; + push @lines, [ MSGLEVEL_MSGS, $_ ]; } elsif ( $meta{type} eq 'debug' ) { print "$_" if &debug,; } else { @@ -768,12 +927,12 @@ sub monitor_child { if ($new_last_poll) { print "new last_poll = $new_last_poll" if &debug; - foreach my $line (@lines) { - chomp $line; - $window->print( $line, MSGLEVEL_PUBLIC ); - foreach ( $line =~ /\@([-\w]+)/ ) { - $nicks{$1} = time; - } + for my $line (@lines) { + $window->printformat( + $line->[0], + "twirssi_" . $line->[1], + @$line[ 2 .. $#$line ] + ); } close FILE; @@ -846,11 +1005,12 @@ sub update_away { } sub too_long { - my $data = shift; + my $data = shift; + my $noalert = shift; if ( length $data > 140 ) { - ¬ice( - "Tweet too long (" . length($data) . " characters) - aborted" ); + ¬ice( "Tweet too long (" . length($data) . " characters) - aborted" ) + unless $noalert; return 1; } @@ -887,7 +1047,9 @@ sub sig_complete { and $linestart =~ /^\/reply(?:_as)?\s*$/ ) ) { # /twitter_reply gets a nick:num - @$complist = grep /^\Q$word/i, sort keys %{ $id_map{__indexes} }; + $word =~ s/^@//; + @$complist = map { "$_:$id_map{__indexes}{$_}" } grep /^\Q$word/i, + sort keys %{ $id_map{__indexes} }; } # /tweet, /tweet_as, /dm, /dm_as - complete @nicks (and nicks as the first @@ -907,14 +1069,42 @@ sub event_send_text { # if the window where we got our text was the twitter window, and the user # wants to be lazy, tweet away! - if ( ($awin->get_active_name() eq $window->{name}) - and Irssi::settings_get_bool("tweet_window_input") ) { - &cmd_tweet($line, $server, $win); + if ( ( $awin->get_active_name() eq $window->{name} ) + and Irssi::settings_get_bool("tweet_window_input") ) + { + &cmd_tweet( $line, $server, $win ); } } +sub get_poll_time { + my $poll = Irssi::settings_get_int("twitter_poll_interval"); + return $poll if $poll >= 60; + return 60; +} + +sub hilight { + my $text = shift; + + $text =~ s/(^|\W)\@([-\w]+)/$1\cC12\@$2\cO/g; + $text =~ s/(^|\W)\#([-\w]+)/$1\cC5\#$2\cO/g; + $text =~ s/[\n\r]/ /g; + + return $text; +} + Irssi::signal_add( "send text", "event_send_text" ); +Irssi::theme_register( + [ + 'twirssi_tweet', '[$0%B@$1%n$2] $3', + 'twirssi_search', '[$0%r$1%n:%B@$2%n$3] $4', + 'twirssi_reply', '[$0\--> %B@$1%n$2] $3', + 'twirssi_dm', '[$0%r@$1%n (%WDM%n)] $2', + 'twirssi_error', 'ERROR: $0', + ] +); + +Irssi::settings_add_int( "twirssi", "twitter_poll_interval", 300 ); 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" ); @@ -930,21 +1120,33 @@ 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", "tweet_window_input", 0 ); + +$last_poll = time - &get_poll_time; $window = Irssi::window_find_name( Irssi::settings_get_str('twitter_window') ); +if ( !$window ) { + $window = + Irssi::Windowitem::window_create( + Irssi::settings_get_str('twitter_window'), 1 ); + $window->set_name( Irssi::settings_get_str('twitter_window') ); +} if ($window) { - Irssi::command_bind( "dm", "cmd_direct" ); - Irssi::command_bind( "dm_as", "cmd_direct_as" ); - Irssi::command_bind( "tweet", "cmd_tweet" ); - Irssi::command_bind( "tweet_as", "cmd_tweet_as" ); - Irssi::command_bind( "twitter_reply", "cmd_reply" ); - Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" ); - Irssi::command_bind( "twitter_login", "cmd_login" ); - Irssi::command_bind( "twitter_logout", "cmd_logout" ); - Irssi::command_bind( "twitter_switch", "cmd_switch" ); - Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" ); + Irssi::command_bind( "dm", "cmd_direct" ); + Irssi::command_bind( "dm_as", "cmd_direct_as" ); + Irssi::command_bind( "tweet", "cmd_tweet" ); + Irssi::command_bind( "tweet_as", "cmd_tweet_as" ); + Irssi::command_bind( "twitter_reply", "cmd_reply" ); + Irssi::command_bind( "twitter_reply_as", "cmd_reply_as" ); + Irssi::command_bind( "twitter_login", "cmd_login" ); + Irssi::command_bind( "twitter_logout", "cmd_logout" ); + Irssi::command_bind( "twitter_switch", "cmd_switch" ); + Irssi::command_bind( "twitter_subscribe", "cmd_add_search" ); + Irssi::command_bind( "twitter_unsubscribe", "cmd_del_search" ); + Irssi::command_bind( "twitter_list_subscriptions", "cmd_list_search" ); + Irssi::command_bind( "twirssi_upgrade", "cmd_upgrade" ); if ( Irssi::settings_get_bool("twirssi_use_reply_aliases") ) { Irssi::command_bind( "reply", "cmd_reply" ); Irssi::command_bind( "reply_as", "cmd_reply_as" ); @@ -956,7 +1158,7 @@ if ($window) { map { "u: $_->{username}" } values %twits; print "friends: ", join ", ", sort keys %friends; print "nicks: ", join ", ", sort keys %nicks; - print "id_map: ", Dumper \%{ $id_map{__indexes} }; + print "searches: ", Dumper \%{ $id_map{__searches} }; print "last poll: $last_poll"; } ); @@ -1035,3 +1237,4 @@ if ($window) { . " or change the value of twitter_window. Then, reload twirssi." ); } +# vim: set sts=4 expandtab: