Changes in / [e4951ea:e4e1dcb]


Ignore:
Location:
lib/BarnOwl/Module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/BarnOwl/Module/Twitter.pm

    r36546fa r22fce654  
    246246);
    247247
     248BarnOwl::new_command( 'twitter-retweet' => sub { cmd_twitter_retweet(@_) },
     249    {
     250    summary     => 'Retweet the current Twitter message',
     251    usage       => 'twitter-retweet [ACCOUNT]',
     252    description => <<END_DESCRIPTION
     253Retweet the current Twitter message using ACCOUNT (defaults to the
     254account that received the tweet).
     255END_DESCRIPTION
     256    }
     257);
     258
    248259BarnOwl::new_command( 'twitter-follow' => sub { cmd_twitter_follow(@_); },
    249260    {
     
    274285        }
    275286    }
    276     BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
     287    BarnOwl::start_edit_win("What's happening?" . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
    277288}
    278289
     
    296307}
    297308
     309sub cmd_twitter_retweet {
     310    my $cmd = shift;
     311    my $account = shift;
     312    my $m = BarnOwl::getcurmsg();
     313    if(!$m || $m->type ne 'Twitter') {
     314        die("$cmd must be used with a Twitter message selected.\n");
     315    }
     316
     317    $account = $m->account unless defined($account);
     318    find_account($account)->twitter_retweet($m);
     319}
     320
    298321sub cmd_twitter_follow {
    299322    my $cmd = shift;
  • lib/BarnOwl/Module/Twitter/Handle.pm

    re4951ea re4951ea  
    1515
    1616use Net::Twitter::Lite;
     17BEGIN {
     18    # Backwards compatibility with version of Net::Twitter::Lite that
     19    # lack home_timeline.
     20    if(!defined(*Net::Twitter::Lite::home_timeline{CODE})) {
     21        *Net::Twitter::Lite::home_timeline =
     22          \&Net::Twitter::Lite::friends_timeline;
     23    }
     24}
    1725use HTML::Entities;
    1826
     
    7179    $self->{twitter}  = Net::Twitter::Lite->new(%twitter_args);
    7280
    73     my $timeline = eval { $self->{twitter}->friends_timeline({count => 1}) };
    74     warn "$@" if $@;
     81    my $timeline = eval { $self->{twitter}->home_timeline({count => 1}) };
     82    warn "$@\n" if $@;
    7583
    7684    if(!defined($timeline)) {
     
    8694        $self->{last_direct} = $self->{twitter}->direct_messages()->[0]{id};
    8795    };
    88     warn "$@" if $@;
     96    warn "$@\n" if $@;
    8997    $self->{last_direct} = 1 unless defined($self->{last_direct});
    9098
     
    92100        $self->{twitter}->{ua}->timeout(1);
    93101    };
    94     warn "$@" if $@;
     102    warn "$@\n" if $@;
    95103
    96104    $self->sleep(0);
     
    129137}
    130138
     139=head2 twitter_command COMMAND ARGS...
     140
     141Call the specified method on $self->{twitter} with an extended
     142timeout. This is intended for interactive commands, with the theory
     143that if the user explicitly requested an action, it is slightly more
     144acceptable to hang the UI for a second or two than to fail just
     145because Twitter is being slightly slow. Polling commands should be
     146called normally, with the default (short) timeout, to prevent
     147background Twitter suckage from hosing the UI normally.
     148
     149=cut
     150
     151sub twitter_command {
     152    my $self = shift;
     153    my $cmd = shift;
     154
     155    eval { $self->{twitter}->{ua}->timeout(5); };
     156    my $result = eval {
     157        $self->{twitter}->$cmd(@_);
     158    };
     159    my $error = $@;
     160    eval { $self->{twitter}->{ua}->timeout(1); };
     161    if ($error) {
     162        die($error);
     163    }
     164    return $result;
     165}
     166
    131167sub twitter_error {
    132168    my $self = shift;
    133169
    134170    my $ratelimit = eval { $self->{twitter}->rate_limit_status };
    135     warn "$@" if $@;
     171    warn "$@\n" if $@;
    136172    unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') {
    137173        # Twitter's probably just sucking, try again later.
     
    157193    BarnOwl::debug("Polling " . $self->{cfg}->{account_nickname});
    158194
    159     my $timeline = eval { $self->{twitter}->friends_timeline( { since_id => $self->{last_id} } ) };
    160     warn "$@" if $@;
     195    my $timeline = eval { $self->{twitter}->home_timeline( { since_id => $self->{last_id} } ) };
     196    warn "$@\n" if $@;
    161197    unless(defined($timeline) && ref($timeline) eq 'ARRAY') {
    162198        $self->twitter_error();
     
    166202    if ($self->{cfg}->{show_mentions}) {
    167203        my $mentions = eval { $self->{twitter}->mentions( { since_id => $self->{last_id} } ) };
    168         warn "$@" if $@;
     204        warn "$@\n" if $@;
    169205        unless (defined($mentions) && ref($mentions) eq 'ARRAY') {
    170206            $self->twitter_error();
     
    211247
    212248    my $direct = eval { $self->{twitter}->direct_messages( { since_id => $self->{last_direct} } ) };
    213     warn "$@" if $@;
     249    warn "$@\n" if $@;
    214250    unless(defined($direct) && ref($direct) eq 'ARRAY') {
    215251        $self->twitter_error();
     
    228264                location  => decode_entities($tweet->{sender}{location}||""),
    229265                body      => decode_entities($tweet->{text}),
    230                 isprivate => 'true',
     266                private => 'true',
    231267                service   => $self->{cfg}->{service},
    232268                account   => $self->{cfg}->{account_nickname},
     
    249285        $self->twitter_direct($1, $2);
    250286    } elsif(defined $self->{twitter}) {
    251         if(defined($reply_to)) {
    252             $self->{twitter}->update({
    253                 status => $msg,
    254                 in_reply_to_status_id => $reply_to
    255                });
    256         } else {
    257             $self->{twitter}->update($msg);
     287        if(length($msg) > 140) {
     288            die("Twitter: Message over 140 characters long.\n");
    258289        }
     290        $self->twitter_command('update', {
     291            status => $msg,
     292            defined($reply_to) ? (in_reply_to_status_id => $reply_to) : ()
     293           });
    259294    }
    260295}
     
    266301    my $msg = shift;
    267302    if(defined $self->{twitter}) {
    268         $self->{twitter}->new_direct_message({
     303        $self->twitter_command('new_direct_message', {
    269304            user => $who,
    270305            text => $msg
     
    277312                direction => 'out',
    278313                body      => $msg,
    279                 isprivate => 'true',
     314                private => 'true',
    280315                service   => $self->{cfg}->{service},
    281316               );
     
    298333}
    299334
     335sub twitter_retweet {
     336    my $self = shift;
     337    my $msg = shift;
     338
     339    if($msg->service ne $self->{cfg}->{service}) {
     340        die("Cannot retweet a message from a different service.\n");
     341    }
     342    $self->twitter_command(retweet => $msg->{status_id});
     343}
     344
    300345sub twitter_follow {
    301346    my $self = shift;
     
    303348    my $who = shift;
    304349
    305     my $user = $self->{twitter}->create_friend($who);
     350    my $user = $self->twitter_command('create_friend', $who);
    306351    # returns a string on error
    307352    if (defined $user && !ref $user) {
     
    317362    my $who = shift;
    318363
    319     my $user = $self->{twitter}->destroy_friend($who);
     364    my $user = $self->twitter_command('destroy_friend', $who);
    320365    # returns a string on error
    321366    if (defined $user && !ref $user) {
Note: See TracChangeset for help on using the changeset viewer.