1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Twitter |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Twitter; |
---|
15 | |
---|
16 | our $VERSION = 0.2; |
---|
17 | |
---|
18 | use Net::Twitter; |
---|
19 | use JSON; |
---|
20 | use List::Util qw(first); |
---|
21 | |
---|
22 | use BarnOwl; |
---|
23 | use BarnOwl::Hooks; |
---|
24 | use BarnOwl::Module::Twitter::Handle; |
---|
25 | |
---|
26 | our @twitter_handles = (); |
---|
27 | our $default_handle = undef; |
---|
28 | my $user = BarnOwl::zephyr_getsender(); |
---|
29 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
30 | my $instance = "status"; |
---|
31 | my $opcode = "twitter"; |
---|
32 | my $use_reply_to = 0; |
---|
33 | my $next_service_to_poll = 0; |
---|
34 | |
---|
35 | my $desc = <<'END_DESC'; |
---|
36 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
37 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
38 | from your sender and mirror them to Twitter. |
---|
39 | |
---|
40 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
41 | messages with any value of that field. |
---|
42 | END_DESC |
---|
43 | BarnOwl::new_variable_string( |
---|
44 | 'twitter:class', |
---|
45 | { |
---|
46 | default => $class, |
---|
47 | summary => 'Class to watch for Twitter messages', |
---|
48 | description => $desc |
---|
49 | } |
---|
50 | ); |
---|
51 | BarnOwl::new_variable_string( |
---|
52 | 'twitter:instance', |
---|
53 | { |
---|
54 | default => $instance, |
---|
55 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
56 | description => $desc |
---|
57 | } |
---|
58 | ); |
---|
59 | BarnOwl::new_variable_string( |
---|
60 | 'twitter:opcode', |
---|
61 | { |
---|
62 | default => $opcode, |
---|
63 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
64 | description => $desc |
---|
65 | } |
---|
66 | ); |
---|
67 | |
---|
68 | BarnOwl::new_variable_bool( |
---|
69 | 'twitter:poll', |
---|
70 | { |
---|
71 | default => 1, |
---|
72 | summary => 'Poll Twitter for incoming messages', |
---|
73 | description => "If set, will poll Twitter every minute for normal updates,\n" |
---|
74 | . 'and every two minutes for direct message' |
---|
75 | } |
---|
76 | ); |
---|
77 | |
---|
78 | sub fail { |
---|
79 | my $msg = shift; |
---|
80 | undef @twitter_handles; |
---|
81 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
82 | die("Twitter Error: $msg\n"); |
---|
83 | } |
---|
84 | |
---|
85 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
86 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
87 | my $raw_cfg = do {local $/; <$fh>}; |
---|
88 | close($fh); |
---|
89 | eval { |
---|
90 | $raw_cfg = from_json($raw_cfg); |
---|
91 | }; |
---|
92 | if($@) { |
---|
93 | fail("Unable to parse $conffile: $@"); |
---|
94 | } |
---|
95 | |
---|
96 | $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY"; |
---|
97 | |
---|
98 | # Perform some sanity checking on the configuration. |
---|
99 | { |
---|
100 | my %nicks; |
---|
101 | my $default = 0; |
---|
102 | |
---|
103 | for my $cfg (@$raw_cfg) { |
---|
104 | if(! exists $cfg->{user}) { |
---|
105 | fail("Account has no username set."); |
---|
106 | } |
---|
107 | my $user = $cfg->{user}; |
---|
108 | if(! exists $cfg->{password}) { |
---|
109 | fail("Account $user has no password set."); |
---|
110 | } |
---|
111 | if(@$raw_cfg > 1&& |
---|
112 | !exists($cfg->{account_nickname}) ) { |
---|
113 | fail("Account $user has no account_nickname set."); |
---|
114 | } |
---|
115 | if($cfg->{account_nickname}) { |
---|
116 | if($nicks{$cfg->{account_nickname}}++) { |
---|
117 | fail("Nickname " . $cfg->{account_nickname} . " specified more than once."); |
---|
118 | } |
---|
119 | } |
---|
120 | if($cfg->{default} || $cfg->{default_sender}) { |
---|
121 | if($default++) { |
---|
122 | fail("Multiple accounts marked as 'default'."); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | # If there is only a single account, make publish_tweets default to |
---|
129 | # true. |
---|
130 | if (scalar @$raw_cfg == 1 && !exists($raw_cfg->[0]{publish_tweets})) { |
---|
131 | $raw_cfg->[0]{publish_tweets} = 1; |
---|
132 | } |
---|
133 | |
---|
134 | for my $cfg (@$raw_cfg) { |
---|
135 | my $twitter_args = { username => $cfg->{user} || $user, |
---|
136 | password => $cfg->{password}, |
---|
137 | source => 'barnowl', |
---|
138 | }; |
---|
139 | if (defined $cfg->{service}) { |
---|
140 | my $service = $cfg->{service}; |
---|
141 | $twitter_args->{apiurl} = $service; |
---|
142 | my $apihost = $service; |
---|
143 | $apihost =~ s/^\s*http:\/\///; |
---|
144 | $apihost =~ s/\/.*$//; |
---|
145 | $apihost .= ':80' unless $apihost =~ /:\d+$/; |
---|
146 | $twitter_args->{apihost} = $cfg->{apihost} || $apihost; |
---|
147 | my $apirealm = "Laconica API"; |
---|
148 | $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm; |
---|
149 | } else { |
---|
150 | $cfg->{service} = 'http://twitter.com'; |
---|
151 | } |
---|
152 | |
---|
153 | my $twitter_handle; |
---|
154 | eval { |
---|
155 | $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args); |
---|
156 | }; |
---|
157 | if ($@) { |
---|
158 | BarnOwl::error($@); |
---|
159 | next; |
---|
160 | } |
---|
161 | push @twitter_handles, $twitter_handle; |
---|
162 | } |
---|
163 | |
---|
164 | $default_handle = first {$_->{cfg}->{default}} @twitter_handles; |
---|
165 | if (!$default_handle && @twitter_handles) { |
---|
166 | $default_handle = $twitter_handles[0]; |
---|
167 | } |
---|
168 | |
---|
169 | sub match { |
---|
170 | my $val = shift; |
---|
171 | my $pat = shift; |
---|
172 | return $pat eq "*" || ($val eq $pat); |
---|
173 | } |
---|
174 | |
---|
175 | sub handle_message { |
---|
176 | my $m = shift; |
---|
177 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
178 | if($m->sender eq $user |
---|
179 | && match($m->class, $class) |
---|
180 | && match($m->instance, $instance) |
---|
181 | && match($m->opcode, $opcode) |
---|
182 | && $m->auth eq 'YES') { |
---|
183 | for my $handle (@twitter_handles) { |
---|
184 | $handle->twitter($m->body) if $handle->{cfg}->{publish_tweets}; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | sub poll_messages { |
---|
190 | return unless @twitter_handles; |
---|
191 | |
---|
192 | my $handle = $twitter_handles[$next_service_to_poll]; |
---|
193 | $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles); |
---|
194 | |
---|
195 | $handle->poll_twitter() if $handle->{cfg}->{poll_for_tweets}; |
---|
196 | $handle->poll_direct() if $handle->{cfg}->{poll_for_dms}; |
---|
197 | } |
---|
198 | |
---|
199 | sub find_account { |
---|
200 | my $name = shift; |
---|
201 | my $handle = first {$_->{cfg}->{account_nickname} eq $name} @twitter_handles; |
---|
202 | if ($handle) { |
---|
203 | return $handle; |
---|
204 | } else { |
---|
205 | die("No such Twitter account: $name\n"); |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | sub find_account_default { |
---|
210 | my $name = shift; |
---|
211 | if(defined($name)) { |
---|
212 | return find_account($name); |
---|
213 | } else { |
---|
214 | return $default_handle; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | sub twitter { |
---|
219 | my $account = shift; |
---|
220 | |
---|
221 | my $sent = 0; |
---|
222 | if (defined $account) { |
---|
223 | my $handle = find_account($account); |
---|
224 | $handle->twitter(@_); |
---|
225 | } |
---|
226 | else { |
---|
227 | # broadcast |
---|
228 | for my $handle (@twitter_handles) { |
---|
229 | $handle->twitter(@_) if $handle->{cfg}->{publish_tweets}; |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
235 | summary => 'Update Twitter from BarnOwl', |
---|
236 | usage => 'twitter [ACCOUNT] [MESSAGE]', |
---|
237 | description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.' |
---|
238 | . "\nIf no ACCOUNT is provided, update all services which have publishing enabled." |
---|
239 | . "\nOtherwise, prompt for a status message to use." |
---|
240 | }); |
---|
241 | |
---|
242 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
243 | summary => 'Send a Twitter direct message', |
---|
244 | usage => 'twitter-direct USER [ACCOUNT]', |
---|
245 | description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,' |
---|
246 | . "\nor first service if no default is provided)" |
---|
247 | }); |
---|
248 | |
---|
249 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
250 | { |
---|
251 | summary => 'Send a Twitter @ message', |
---|
252 | usage => 'twitter-atreply USER [ACCOUNT]', |
---|
253 | description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' |
---|
254 | . "\nor first service if no default is provided)" |
---|
255 | } |
---|
256 | ); |
---|
257 | |
---|
258 | BarnOwl::new_command( 'twitter-follow' => sub { cmd_twitter_follow(@_); }, |
---|
259 | { |
---|
260 | summary => 'Follow a user on Twitter', |
---|
261 | usage => 'twitter-follow USER [ACCOUNT]', |
---|
262 | description => 'Follow USER on Twitter ACCOUNT (defaults to default_sender, or first service' |
---|
263 | . "\nif no default is provided)" |
---|
264 | } |
---|
265 | ); |
---|
266 | |
---|
267 | BarnOwl::new_command( 'twitter-unfollow' => sub { cmd_twitter_unfollow(@_); }, |
---|
268 | { |
---|
269 | summary => 'Stop following a user on Twitter', |
---|
270 | usage => 'twitter-unfollow USER [ACCOUNT]', |
---|
271 | description => 'Stop following USER on Twitter ACCOUNT (defaults to default_sender, or first' |
---|
272 | . "\nservice if no default is provided)" |
---|
273 | } |
---|
274 | ); |
---|
275 | |
---|
276 | sub cmd_twitter { |
---|
277 | my $cmd = shift; |
---|
278 | my $account = shift; |
---|
279 | if (defined $account) { |
---|
280 | if(@_) { |
---|
281 | my $status = join(" ", @_); |
---|
282 | twitter($account, $status); |
---|
283 | return; |
---|
284 | } |
---|
285 | } |
---|
286 | BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)}); |
---|
287 | } |
---|
288 | |
---|
289 | sub cmd_twitter_direct { |
---|
290 | my $cmd = shift; |
---|
291 | my $user = shift; |
---|
292 | die("Usage: $cmd USER\n") unless $user; |
---|
293 | my $account = find_account_default(shift); |
---|
294 | BarnOwl::start_edit_win("$cmd $user " . ($account->nickname||""), |
---|
295 | sub { $account->twitter_direct($user, shift) }); |
---|
296 | } |
---|
297 | |
---|
298 | sub cmd_twitter_atreply { |
---|
299 | my $cmd = shift; |
---|
300 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
---|
301 | my $id = shift; |
---|
302 | my $account = find_account_default(shift); |
---|
303 | |
---|
304 | BarnOwl::start_edit_win("Reply to \@" . $user . ($account->nickname ? (" on " . $account->nickname) : ""), |
---|
305 | sub { $account->twitter_atreply($user, $id, shift) }); |
---|
306 | } |
---|
307 | |
---|
308 | sub cmd_twitter_follow { |
---|
309 | my $cmd = shift; |
---|
310 | my $user = shift; |
---|
311 | die("Usage: $cmd USER\n") unless $user; |
---|
312 | my $account = shift; |
---|
313 | find_account_default($account)->twitter_follow($user); |
---|
314 | } |
---|
315 | |
---|
316 | sub cmd_twitter_unfollow { |
---|
317 | my $cmd = shift; |
---|
318 | my $user = shift; |
---|
319 | die("Usage: $cmd USER\n") unless $user; |
---|
320 | my $account = shift; |
---|
321 | find_account_default($account)->twitter_unfollow($user); |
---|
322 | } |
---|
323 | |
---|
324 | eval { |
---|
325 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
326 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
327 | }; |
---|
328 | if($@) { |
---|
329 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
330 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
331 | } |
---|
332 | |
---|
333 | BarnOwl::filter(qw{twitter type ^twitter$}); |
---|
334 | |
---|
335 | 1; |
---|