source: lib/BarnOwl/Module/Twitter.pm @ d748296

release-1.10release-1.7release-1.8release-1.9
Last change on this file since d748296 was d748296, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Better error handling for invalid credentials in the main module.
  • Property mode set to 100644
File size: 8.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 Net::Twitter;
19use JSON;
20
21use BarnOwl;
22use BarnOwl::Hooks;
[159aaad]23use BarnOwl::Module::Twitter::Handle;
[e54f2fa]24
[d748296]25our @twitter_handles = ();
26our $default_handle = undef;
[9bedca0]27my $user     = BarnOwl::zephyr_getsender();
28my ($class)  = ($user =~ /(^[^@]+)/);
29my $instance = "status";
30my $opcode   = "twitter";
[d658c29]31my $use_reply_to = 0;
[159aaad]32my $next_service_to_poll = 0;
[188b745]33
[d689fc7]34my $desc = <<'END_DESC';
[d1bb4f3]35BarnOwl::Module::Twitter will watch for authentic zephyrs to
36-c $twitter:class -i $twitter:instance -O $twitter:opcode
37from your sender and mirror them to Twitter.
38
39A value of '*' in any of these fields acts a wildcard, accepting
40messages with any value of that field.
41END_DESC
[d689fc7]42BarnOwl::new_variable_string(
43    'twitter:class',
44    {
45        default     => $class,
46        summary     => 'Class to watch for Twitter messages',
47        description => $desc
48    }
49);
50BarnOwl::new_variable_string(
51    'twitter:instance',
52    {
53        default => $instance,
54        summary => 'Instance on twitter:class to watch for Twitter messages.',
55        description => $desc
56    }
57);
58BarnOwl::new_variable_string(
59    'twitter:opcode',
60    {
61        default => $opcode,
62        summary => 'Opcode for zephyrs that will be sent as twitter updates',
63        description => $desc
64    }
65);
[e54f2fa]66
[927c186]67BarnOwl::new_variable_bool(
68    'twitter:poll',
69    {
70        default => 1,
71        summary => 'Poll Twitter for incoming messages',
72        description => "If set, will poll Twitter every minute for normal updates,\n"
73        . 'and every two minutes for direct message'
74     }
75 );
76
[159aaad]77sub fail {
78    my $msg = shift;
79    undef @twitter_handles;
80    BarnOwl::admin_message('Twitter Error', $msg);
81    die("Twitter Error: $msg\n");
82}
83
[d775050]84my $conffile = BarnOwl::get_config_dir() . "/twitter";
85open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
[159aaad]86my $raw_cfg = do {local $/; <$fh>};
[e54f2fa]87close($fh);
88eval {
[159aaad]89    $raw_cfg = from_json($raw_cfg);
[e54f2fa]90};
[9bedca0]91if($@) {
[159aaad]92    fail("Unable to parse $conffile: $@");
[e54f2fa]93}
94
[159aaad]95$raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY";
96
97for my $cfg (@$raw_cfg) {
98    my $twitter_args = { username   => $cfg->{user} || $user,
99                        password   => $cfg->{password},
100                        source     => 'barnowl', 
101                    };
102    if (defined $cfg->{service}) {
103        my $service = $cfg->{service};
104        $twitter_args->{apiurl} = $service;
105        my $apihost = $service;
106        $apihost =~ s/^\s*http:\/\///;
107        $apihost =~ s/\/.*$//;
108        $apihost .= ':80' unless $apihost =~ /:\d+$/;
109        $twitter_args->{apihost} = $cfg->{apihost} || $apihost;
110        my $apirealm = "Laconica API";
111        $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm;
112    } else {
113        $cfg->{service} = 'http://twitter.com';
114    }
[b56f2c3]115
[d748296]116    eval {
117        my $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args);
118        push @twitter_handles, $twitter_handle;
119        $default_handle = $twitter_handle if (!defined $twitter_handle && exists $cfg->{default_sender} && $cfg->{default_sender});
120    };
[b56f2c3]121}
122
[d1bb4f3]123sub match {
124    my $val = shift;
125    my $pat = shift;
126    return $pat eq "*" || ($val eq $pat);
127}
128
[e54f2fa]129sub handle_message {
130    my $m = shift;
131    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
132    if($m->sender eq $user
[d1bb4f3]133       && match($m->class, $class)
134       && match($m->instance, $instance)
135       && match($m->opcode, $opcode)
[e54f2fa]136       && $m->auth eq 'YES') {
[159aaad]137        for my $handle (@twitter_handles) {
138            $handle->twitter($m->body);
139        }
[e54f2fa]140    }
141}
142
[8618438]143sub poll_messages {
[d748296]144    return unless @twitter_handles;
145
[159aaad]146    my $handle = $twitter_handles[$next_service_to_poll];
147    $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles);
148   
149    $handle->poll_twitter() if (!exists $handle->{cfg}->{poll_for_tweets} || $handle->{cfg}->{poll_for_tweets});
150    $handle->poll_direct() if (!exists $handle->{cfg}->{poll_for_dms} || $handle->{cfg}->{poll_for_dms});
[927c186]151}
152
[159aaad]153sub twitter {
154    my $account = shift;
155
156    my $sent = 0;
157    if (defined $account) {
158        for my $handle (@twitter_handles) {
159            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
160                $handle->twitter(@_);
161                $sent = 1;
162                last;
[8618438]163            }
164        }
[159aaad]165        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
166    } 
167    else {
168        # broadcast
169        for my $handle (@twitter_handles) {
170            $handle->twitter(@_) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets});
171        }
[8618438]172    }
173}
174
[159aaad]175sub twitter_direct {
176    my $account = shift;
177
178    my $sent = 0;
179    if (defined $account) {
180        for my $handle (@twitter_handles) {
181            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
182                $handle->twitter_direct(@_);
183                $sent = 1;
184                last;
[927c186]185            }
186        }
[159aaad]187        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
[927c186]188    }
[159aaad]189    elsif (defined $default_handle) {
190        $default_handle->twitter_direct(@_);
[9bedca0]191    }
[159aaad]192    else {
193        $twitter_handles[0]->twitter_direct(@_);
[927c186]194    }
195}
196
[6babb75]197sub twitter_atreply {
[159aaad]198    my $account = shift;
199
200    my $sent = 0;
201    if (defined $account) {
202        for my $handle (@twitter_handles) {
203            if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) {
204                $handle->twitter_atreply(@_);
205                $sent = 1;
206                last;
207            }
208        }
209        BarnOwl::message("No Twitter account named " . $account) unless $sent == 1
210    }
211    elsif (defined $default_handle) {
212        $default_handle->twitter_atreply(@_);
213    }
214    else {
215        $twitter_handles[0]->twitter_atreply(@_);
[acdd52e]216    }
[6babb75]217}
218
[4cf4067]219BarnOwl::new_command(twitter => \&cmd_twitter, {
220    summary     => 'Update Twitter from BarnOwl',
[159aaad]221    usage       => 'twitter [ACCOUNT] [MESSAGE]',
222    description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.'
223    . "\nIf no ACCOUNT is provided, update all services which have publishing enabled."
[4cf4067]224    . "\nOtherwise, prompt for a status message to use."
225   });
226
[927c186]227BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, {
228    summary     => 'Send a Twitter direct message',
[159aaad]229    usage       => 'twitter-direct USER [ACCOUNT]',
230    description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,'
231    . "\nor first service if no default is provided)"
[927c186]232   });
233
[6babb75]234BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); },
235    {
236    summary     => 'Send a Twitter @ message',
[159aaad]237    usage       => 'twitter-atreply USER [ACCOUNT]',
238    description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' 
239    . "or first service if no default is provided)"
[6babb75]240    }
241);
242
243
[4cf4067]244sub cmd_twitter {
245    my $cmd = shift;
[159aaad]246    my $account = shift;
247    if (defined $account) {
248        if(@_) {
249            my $status = join(" ", @_);
250            twitter($account, $status);
251            return;
252        }
[4cf4067]253    }
[159aaad]254    BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)});
[4cf4067]255}
256
[927c186]257sub cmd_twitter_direct {
258    my $cmd = shift;
259    my $user = shift;
260    die("Usage: $cmd USER\n") unless $user;
[159aaad]261    my $account = shift;
262    BarnOwl::start_edit_win("$cmd $user" . (defined $account ? " $account" : ""), sub{twitter_direct($account, $user, shift)});
[927c186]263}
264
[6babb75]265sub cmd_twitter_atreply {
266    my $cmd  = shift;
[acdd52e]267    my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n");
268    my $id   = shift;
[159aaad]269    my $account = shift;
270    BarnOwl::start_edit_win("Reply to \@" . $user . (defined $account ? " on $account" : ""), sub { twitter_atreply($account, $user, $id, shift) });
[6babb75]271}
272
[72b61dd]273eval {
274    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
[8618438]275    $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
[72b61dd]276};
277if($@) {
278    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
[8618438]279    $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
[72b61dd]280}
[e54f2fa]281
[f93b81b]282BarnOwl::filter(qw{twitter type ^twitter$});
[8618438]283
[e54f2fa]2841;
Note: See TracBrowser for help on using the repository browser.