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 | |
---|
21 | use BarnOwl; |
---|
22 | use BarnOwl::Hooks; |
---|
23 | use BarnOwl::Module::Twitter::Handle; |
---|
24 | |
---|
25 | our @twitter_handles = (); |
---|
26 | our $default_handle = undef; |
---|
27 | my $user = BarnOwl::zephyr_getsender(); |
---|
28 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
29 | my $instance = "status"; |
---|
30 | my $opcode = "twitter"; |
---|
31 | my $use_reply_to = 0; |
---|
32 | my $next_service_to_poll = 0; |
---|
33 | |
---|
34 | my $desc = <<'END_DESC'; |
---|
35 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
36 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
37 | from your sender and mirror them to Twitter. |
---|
38 | |
---|
39 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
40 | messages with any value of that field. |
---|
41 | END_DESC |
---|
42 | BarnOwl::new_variable_string( |
---|
43 | 'twitter:class', |
---|
44 | { |
---|
45 | default => $class, |
---|
46 | summary => 'Class to watch for Twitter messages', |
---|
47 | description => $desc |
---|
48 | } |
---|
49 | ); |
---|
50 | BarnOwl::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 | ); |
---|
58 | BarnOwl::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 | ); |
---|
66 | |
---|
67 | BarnOwl::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 | |
---|
77 | sub fail { |
---|
78 | my $msg = shift; |
---|
79 | undef @twitter_handles; |
---|
80 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
81 | die("Twitter Error: $msg\n"); |
---|
82 | } |
---|
83 | |
---|
84 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
85 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
86 | my $raw_cfg = do {local $/; <$fh>}; |
---|
87 | close($fh); |
---|
88 | eval { |
---|
89 | $raw_cfg = from_json($raw_cfg); |
---|
90 | }; |
---|
91 | if($@) { |
---|
92 | fail("Unable to parse $conffile: $@"); |
---|
93 | } |
---|
94 | |
---|
95 | $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY"; |
---|
96 | |
---|
97 | for 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 | } |
---|
115 | |
---|
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 | }; |
---|
121 | } |
---|
122 | |
---|
123 | sub match { |
---|
124 | my $val = shift; |
---|
125 | my $pat = shift; |
---|
126 | return $pat eq "*" || ($val eq $pat); |
---|
127 | } |
---|
128 | |
---|
129 | sub handle_message { |
---|
130 | my $m = shift; |
---|
131 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
132 | if($m->sender eq $user |
---|
133 | && match($m->class, $class) |
---|
134 | && match($m->instance, $instance) |
---|
135 | && match($m->opcode, $opcode) |
---|
136 | && $m->auth eq 'YES') { |
---|
137 | for my $handle (@twitter_handles) { |
---|
138 | $handle->twitter($m->body); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | sub poll_messages { |
---|
144 | return unless @twitter_handles; |
---|
145 | |
---|
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}); |
---|
151 | } |
---|
152 | |
---|
153 | sub 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; |
---|
163 | } |
---|
164 | } |
---|
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 | } |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | sub 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; |
---|
185 | } |
---|
186 | } |
---|
187 | BarnOwl::message("No Twitter account named " . $account) unless $sent == 1 |
---|
188 | } |
---|
189 | elsif (defined $default_handle) { |
---|
190 | $default_handle->twitter_direct(@_); |
---|
191 | } |
---|
192 | else { |
---|
193 | $twitter_handles[0]->twitter_direct(@_); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | sub twitter_atreply { |
---|
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(@_); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
220 | summary => 'Update Twitter from BarnOwl', |
---|
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." |
---|
224 | . "\nOtherwise, prompt for a status message to use." |
---|
225 | }); |
---|
226 | |
---|
227 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
228 | summary => 'Send a Twitter direct message', |
---|
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)" |
---|
232 | }); |
---|
233 | |
---|
234 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
235 | { |
---|
236 | summary => 'Send a Twitter @ message', |
---|
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)" |
---|
240 | } |
---|
241 | ); |
---|
242 | |
---|
243 | |
---|
244 | sub cmd_twitter { |
---|
245 | my $cmd = shift; |
---|
246 | my $account = shift; |
---|
247 | if (defined $account) { |
---|
248 | if(@_) { |
---|
249 | my $status = join(" ", @_); |
---|
250 | twitter($account, $status); |
---|
251 | return; |
---|
252 | } |
---|
253 | } |
---|
254 | BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)}); |
---|
255 | } |
---|
256 | |
---|
257 | sub cmd_twitter_direct { |
---|
258 | my $cmd = shift; |
---|
259 | my $user = shift; |
---|
260 | die("Usage: $cmd USER\n") unless $user; |
---|
261 | my $account = shift; |
---|
262 | BarnOwl::start_edit_win("$cmd $user" . (defined $account ? " $account" : ""), sub{twitter_direct($account, $user, shift)}); |
---|
263 | } |
---|
264 | |
---|
265 | sub cmd_twitter_atreply { |
---|
266 | my $cmd = shift; |
---|
267 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
---|
268 | my $id = shift; |
---|
269 | my $account = shift; |
---|
270 | BarnOwl::start_edit_win("Reply to \@" . $user . (defined $account ? " on $account" : ""), sub { twitter_atreply($account, $user, $id, shift) }); |
---|
271 | } |
---|
272 | |
---|
273 | eval { |
---|
274 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
275 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
276 | }; |
---|
277 | if($@) { |
---|
278 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
279 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
280 | } |
---|
281 | |
---|
282 | BarnOwl::filter(qw{twitter type ^twitter$}); |
---|
283 | |
---|
284 | 1; |
---|