source: perl/modules/Twitter/lib/BarnOwl/Module/Twitter.pm @ c53f5e8

Last change on this file since c53f5e8 was c53f5e8, checked in by Alex Dehnert <adehnert@mit.edu>, 8 years ago
Use SSL by default As per https://dev.twitter.com/discussions/24239 and https://twitter.com/perl_api/status/423210390684565504, the Twitter API now requires SSL connections, so go use them.
  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[e54f2fa]1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Twitter
7
8=head1 DESCRIPTION
9
10Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter
11
12=cut
13
14package BarnOwl::Module::Twitter;
15
[159aaad]16our $VERSION = 0.2;
[af3415c]17
[e54f2fa]18use JSON;
[efcd223]19use List::Util qw(first);
[e54f2fa]20
21use BarnOwl;
22use BarnOwl::Hooks;
[159aaad]23use BarnOwl::Module::Twitter::Handle;
[6e8eb1c]24use BarnOwl::Module::Twitter::Completion;
[e54f2fa]25
[d748296]26our @twitter_handles = ();
27our $default_handle = undef;
[188b745]28
[d689fc7]29my $desc = <<'END_DESC';
[d1bb4f3]30BarnOwl::Module::Twitter will watch for authentic zephyrs to
31-c $twitter:class -i $twitter:instance -O $twitter:opcode
32from your sender and mirror them to Twitter.
33
34A value of '*' in any of these fields acts a wildcard, accepting
35messages with any value of that field.
36END_DESC
[d689fc7]37BarnOwl::new_variable_string(
38    'twitter:class',
39    {
[40c9dac]40        default     => $ENV{USER},
[d689fc7]41        summary     => 'Class to watch for Twitter messages',
42        description => $desc
43    }
44);
45BarnOwl::new_variable_string(
46    'twitter:instance',
47    {
[40c9dac]48        default => 'status',
[d689fc7]49        summary => 'Instance on twitter:class to watch for Twitter messages.',
50        description => $desc
51    }
52);
53BarnOwl::new_variable_string(
54    'twitter:opcode',
55    {
[40c9dac]56        default => 'twitter',
[d689fc7]57        summary => 'Opcode for zephyrs that will be sent as twitter updates',
58        description => $desc
59    }
60);
[e54f2fa]61
[927c186]62BarnOwl::new_variable_bool(
63    'twitter:poll',
64    {
65        default => 1,
66        summary => 'Poll Twitter for incoming messages',
67        description => "If set, will poll Twitter every minute for normal updates,\n"
68        . 'and every two minutes for direct message'
69     }
70 );
71
[159aaad]72sub fail {
73    my $msg = shift;
74    undef @twitter_handles;
75    BarnOwl::admin_message('Twitter Error', $msg);
76    die("Twitter Error: $msg\n");
77}
78
[d775050]79my $conffile = BarnOwl::get_config_dir() . "/twitter";
[ab28a06]80
81if (open(my $fh, "<", "$conffile")) {
82    read_config($fh);
83    close($fh);
[e54f2fa]84}
85
[ab28a06]86sub read_config {
87    my $fh = shift;
[159aaad]88
[ab28a06]89    my $raw_cfg = do {local $/; <$fh>};
90    close($fh);
91    eval {
92        $raw_cfg = from_json($raw_cfg);
93    };
94    if($@) {
95        fail("Unable to parse $conffile: $@");
96    }
[e010ee0]97
[ab28a06]98    $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
99
100    # Perform some sanity checking on the configuration.
101  {
102      my %nicks;
103      my $default = 0;
104
105      for my $cfg (@$raw_cfg) {
106          if(! exists $cfg->{user}) {
107              fail("Account has no username set.");
108          }
109          my $user = $cfg->{user};
110          if(! exists $cfg->{password}) {
111              fail("Account $user has no password set.");
112          }
113          if(@$raw_cfg > 1&&
114             !exists($cfg->{account_nickname}) ) {
115              fail("Account $user has no account_nickname set.");
116          }
117          if($cfg->{account_nickname}) {
118              if($nicks{$cfg->{account_nickname}}++) {
119                  fail("Nickname " . $cfg->{account_nickname} . " specified more than once.");
120              }
121          }
122          if($cfg->{default} || $cfg->{default_sender}) {
123              if($default++) {
124                  fail("Multiple accounts marked as 'default'.");
125              }
126          }
127      }
128  }
129
130    # If there is only a single account, make publish_tweets default to
131    # true.
132    if (scalar @$raw_cfg == 1 && !exists($raw_cfg->[0]{publish_tweets})) {
133        $raw_cfg->[0]{publish_tweets} = 1;
[e010ee0]134    }
135
[ab28a06]136    for my $cfg (@$raw_cfg) {
137        my $twitter_args = { username   => $cfg->{user},
138                             password   => $cfg->{password},
139                             source     => 'barnowl', 
[c53f5e8]140                             ssl        => 1,
[ab28a06]141                         };
142        if (defined $cfg->{service}) {
143            my $service = $cfg->{service};
144            $twitter_args->{apiurl} = $service;
145            my $apihost = $service;
146            $apihost =~ s/^\s*http:\/\///;
147            $apihost =~ s/\/.*$//;
148            $apihost .= ':80' unless $apihost =~ /:\d+$/;
149            $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
150            my $apirealm = "Laconica API";
151            $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
152        } else {
153            $cfg->{service} = 'http://twitter.com';
154        }
[f0de278]155
[ab28a06]156        my $twitter_handle;
157        eval {
158            $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
159        };
160        if ($@) {
161            BarnOwl::error($@);
162            next;
163        }
164        push @twitter_handles, $twitter_handle;
[159aaad]165    }
[b56f2c3]166
[ab28a06]167    $default_handle = first {$_->{cfg}->{default}} @twitter_handles;
168    if (!$default_handle && @twitter_handles) {
169        $default_handle = $twitter_handles[0];
[39dd366]170    }
[efcd223]171
[b56f2c3]172}
173
[d1bb4f3]174sub match {
175    my $val = shift;
176    my $pat = shift;
177    return $pat eq "*" || ($val eq $pat);
178}
179
[e54f2fa]180sub handle_message {
181    my $m = shift;
[40c9dac]182    my ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
[176434d]183    if($m->type eq 'zephyr'
184       && $m->sender eq BarnOwl::zephyr_getsender()
[d1bb4f3]185       && match($m->class, $class)
186       && match($m->instance, $instance)
187       && match($m->opcode, $opcode)
[e54f2fa]188       && $m->auth eq 'YES') {
[159aaad]189        for my $handle (@twitter_handles) {
[efcd223]190            $handle->twitter($m->body) if $handle->{cfg}->{publish_tweets};
[159aaad]191        }
[e54f2fa]192    }
193}
194
[4ae10de]195sub poll_messages {
[b8a3e00]196    # If we are reloaded into a BarnOwl with the old
[4ae10de]197    # BarnOwl::Module::Twitter loaded, it still has a main loop hook
198    # that will call this function every second. If we just delete it,
199    # it will get the old version, which will call poll on each of our
200    # handles every second. However, they no longer include the time
201    # check, and so we will poll a handle every second until
202    # ratelimited.
203
204    # So we include an empty function here.
205}
206
[efcd223]207sub find_account {
208    my $name = shift;
209    my $handle = first {$_->{cfg}->{account_nickname} eq $name} @twitter_handles;
210    if ($handle) {
211        return $handle;
212    } else {
213        die("No such Twitter account: $name\n");
214    }
[927c186]215}
216
[8462b38]217sub find_account_default {
218    my $name = shift;
219    if(defined($name)) {
220        return find_account($name);
221    } else {
222        return $default_handle;
223    }
224}
225
[159aaad]226sub twitter {
227    my $account = shift;
228
229    my $sent = 0;
230    if (defined $account) {
[efcd223]231        my $handle = find_account($account);
232        $handle->twitter(@_);
[159aaad]233    } 
234    else {
235        # broadcast
236        for my $handle (@twitter_handles) {
[efcd223]237            $handle->twitter(@_) if $handle->{cfg}->{publish_tweets};
[159aaad]238        }
[8618438]239    }
240}
241
[4cf4067]242BarnOwl::new_command(twitter => \&cmd_twitter, {
243    summary     => 'Update Twitter from BarnOwl',
[159aaad]244    usage       => 'twitter [ACCOUNT] [MESSAGE]',
245    description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.'
246    . "\nIf no ACCOUNT is provided, update all services which have publishing enabled."
[4cf4067]247    . "\nOtherwise, prompt for a status message to use."
248   });
249
[927c186]250BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, {
251    summary     => 'Send a Twitter direct message',
[159aaad]252    usage       => 'twitter-direct USER [ACCOUNT]',
253    description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,'
254    . "\nor first service if no default is provided)"
[927c186]255   });
256
[6babb75]257BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); },
258    {
259    summary     => 'Send a Twitter @ message',
[159aaad]260    usage       => 'twitter-atreply USER [ACCOUNT]',
261    description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' 
[513da71]262    . "\nor first service if no default is provided)"
263    }
264);
265
[5214546]266BarnOwl::new_command( 'twitter-retweet' => sub { cmd_twitter_retweet(@_) },
267    {
268    summary     => 'Retweet the current Twitter message',
269    usage       => 'twitter-retweet [ACCOUNT]',
270    description => <<END_DESCRIPTION
271Retweet the current Twitter message using ACCOUNT (defaults to the
272account that received the tweet).
273END_DESCRIPTION
274    }
275);
276
[513da71]277BarnOwl::new_command( 'twitter-follow' => sub { cmd_twitter_follow(@_); },
278    {
279    summary     => 'Follow a user on Twitter',
280    usage       => 'twitter-follow USER [ACCOUNT]',
281    description => 'Follow USER on Twitter ACCOUNT (defaults to default_sender, or first service'
282    . "\nif no default is provided)"
[6babb75]283    }
284);
285
[513da71]286BarnOwl::new_command( 'twitter-unfollow' => sub { cmd_twitter_unfollow(@_); },
287    {
288    summary     => 'Stop following a user on Twitter',
289    usage       => 'twitter-unfollow USER [ACCOUNT]',
290    description => 'Stop following USER on Twitter ACCOUNT (defaults to default_sender, or first'
291    . "\nservice if no default is provided)"
292    }
293);
[6babb75]294
[f3e44eb]295BarnOwl::new_command('twitter-count-chars' => \&cmd_count_chars, {
296    summary     => 'Count the number of characters in the edit window',
297    usage       => 'twitter-count-chars',
298    description => <<END_DESCRIPTION
[a2640485]299Displays the number of characters entered in the edit window so far.
[f3e44eb]300END_DESCRIPTION
301   });
302
[f6e1262]303$BarnOwl::Hooks::getQuickstart->add( sub { twitter_quickstart(@_); } );
304
305sub twitter_quickstart {
306    return <<'EOF'
307@b[Twitter]:
308Add your Twitter account to ~/.owl/twitter, like:
309  {"user":"nelhage", "password":"sekrit"}
310Run :reload-module Twitter, then use :twitter to tweet.
311EOF
312}
313
[f3e44eb]314
[4cf4067]315sub cmd_twitter {
316    my $cmd = shift;
[159aaad]317    my $account = shift;
318    if (defined $account) {
319        if(@_) {
320            my $status = join(" ", @_);
321            twitter($account, $status);
322            return;
323        }
[4cf4067]324    }
[22fce654]325    BarnOwl::start_edit_win("What's happening?" . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
[4cf4067]326}
327
[927c186]328sub cmd_twitter_direct {
329    my $cmd = shift;
330    my $user = shift;
331    die("Usage: $cmd USER\n") unless $user;
[8462b38]332    my $account = find_account_default(shift);
333    BarnOwl::start_edit_win("$cmd $user " . ($account->nickname||""),
334                            sub { $account->twitter_direct($user, shift) });
[927c186]335}
336
[6babb75]337sub cmd_twitter_atreply {
338    my $cmd  = shift;
[acdd52e]339    my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n");
340    my $id   = shift;
[8462b38]341    my $account = find_account_default(shift);
342
343    BarnOwl::start_edit_win("Reply to \@" . $user . ($account->nickname ? (" on " . $account->nickname) : ""),
344                            sub { $account->twitter_atreply($user, $id, shift) });
[a2640485]345    BarnOwl::Editwin::insert_text("\@$user ");
[6babb75]346}
347
[5214546]348sub cmd_twitter_retweet {
349    my $cmd = shift;
350    my $account = shift;
351    my $m = BarnOwl::getcurmsg();
352    if(!$m || $m->type ne 'Twitter') {
353        die("$cmd must be used with a Twitter message selected.\n");
354    }
355
356    $account = $m->account unless defined($account);
357    find_account($account)->twitter_retweet($m);
[986c9b1]358    return;
[5214546]359}
360
[513da71]361sub cmd_twitter_follow {
362    my $cmd = shift;
363    my $user = shift;
364    die("Usage: $cmd USER\n") unless $user;
365    my $account = shift;
[8462b38]366    find_account_default($account)->twitter_follow($user);
[513da71]367}
368
369sub cmd_twitter_unfollow {
370    my $cmd = shift;
371    my $user = shift;
372    die("Usage: $cmd USER\n") unless $user;
373    my $account = shift;
[8462b38]374    find_account_default($account)->twitter_unfollow($user);
[513da71]375}
376
[f3e44eb]377use BarnOwl::Editwin qw(:all);
378sub cmd_count_chars {
379    my $cmd = shift;
380    my $text = save_excursion {
381        move_to_buffer_start();
382        set_mark();
383        move_to_buffer_end();
384        get_region();
385    };
386    my $len = length($text);
387    BarnOwl::message($len);
388    return $len;
389}
390
[6249a76d]391eval {
392    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
393};
394if($@) {
395    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
396}
397
398
399
[f93b81b]400BarnOwl::filter(qw{twitter type ^twitter$});
[8618438]401
[e54f2fa]4021;
Note: See TracBrowser for help on using the repository browser.