1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Facebook::Handle |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Contains everything needed to send and receive messages from Facebook |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Facebook::Handle; |
---|
15 | |
---|
16 | use Facebook::Graph; |
---|
17 | |
---|
18 | use List::Util qw(reduce); |
---|
19 | |
---|
20 | eval { require Lingua::EN::Keywords; }; |
---|
21 | if ($@) { |
---|
22 | *keywords = sub { |
---|
23 | # stupidly pick the longest one, and only return one. |
---|
24 | my $sentence = shift; |
---|
25 | $sentence =~ s/[[:punct:]]//g; |
---|
26 | my @words = split(' ', lc($sentence)); |
---|
27 | return () unless @words; |
---|
28 | return (reduce{ length($a) > length($b) ? $a : $b } @words,); |
---|
29 | }; |
---|
30 | } else { |
---|
31 | *keywords = \&Lingua::EN::Keywords::keywords; |
---|
32 | } |
---|
33 | |
---|
34 | use JSON; |
---|
35 | use Date::Parse; |
---|
36 | use POSIX; |
---|
37 | |
---|
38 | use Scalar::Util qw(weaken); |
---|
39 | |
---|
40 | use BarnOwl; |
---|
41 | use BarnOwl::Message::Facebook; |
---|
42 | |
---|
43 | our $app_id = 235537266461636; # for application 'barnowl' |
---|
44 | |
---|
45 | # Unfortunately, Facebook does not offer a comment stream, in the same |
---|
46 | # way we can get a post stream using the news feed. This makes it a bit |
---|
47 | # difficult to de-duplicate comments we have already seen. We use a |
---|
48 | # simple heuristic to fix this: we check if the comment's time is dated |
---|
49 | # from before our last update, and don't re-post if it's dated before. |
---|
50 | # Be somewhat forgiving, since it's better to duplicate a post than to |
---|
51 | # drop one. Furthermore, we must use Facebook's idea of time, since the |
---|
52 | # server BarnOwl is running on may be desynchronized. So we need to |
---|
53 | # utilize Facebook's idea of time, not ours. We do this by looking at |
---|
54 | # all of the timestamps we see while processing an update, and take the |
---|
55 | # latest one and increment it by one second. |
---|
56 | # |
---|
57 | # What properties do we get with this setup? |
---|
58 | # |
---|
59 | # - We get comment updates only for the latest N posts on a news feed. |
---|
60 | # Any later ones, you have to use Facebook's usual mechanisms (e.g. |
---|
61 | # email notifications). |
---|
62 | # |
---|
63 | # - Processing a poll is relatively expensive, since we have to |
---|
64 | # iterate over N new posts. It might be worthwhile polling for new |
---|
65 | # comments less frequently than polling for new posts. |
---|
66 | |
---|
67 | sub fail { |
---|
68 | my $self = shift; |
---|
69 | my $msg = shift; |
---|
70 | undef $self->{facebook}; |
---|
71 | die("[Facebook] Error: $msg\n"); |
---|
72 | } |
---|
73 | |
---|
74 | sub new { |
---|
75 | my $class = shift; |
---|
76 | my $cfg = shift; |
---|
77 | |
---|
78 | my $self = { |
---|
79 | 'cfg' => $cfg, |
---|
80 | 'facebook' => undef, |
---|
81 | |
---|
82 | # Initialized with our 'time', but will be synced to Facebook |
---|
83 | # soon enough. |
---|
84 | 'last_poll' => time - 60 * 60 * 24 * 2, |
---|
85 | 'timer' => undef, |
---|
86 | |
---|
87 | # Message polling not implemented yet |
---|
88 | #'last_message_poll' => time, |
---|
89 | #'message_timer' => undef, |
---|
90 | |
---|
91 | # yeah yeah, inelegant, I know. You can try using |
---|
92 | # $fb->authorize, but at time of writing (1.0300) they didn't support |
---|
93 | # the response_type parameter. |
---|
94 | # 'login_url' => 'https://www.facebook.com/dialog/oauth?client_id=235537266461636&scope=read_stream,read_mailbox,publish_stream,offline_access&redirect_uri=http://www.facebook.com/connect/login_success.html&response_type=token', |
---|
95 | # minified to fit in most terminal windows. |
---|
96 | 'login_url' => 'http://goo.gl/yA42G', |
---|
97 | |
---|
98 | 'logged_in' => 0, |
---|
99 | |
---|
100 | # would need another hash for topic de-dup |
---|
101 | 'topics' => {}, |
---|
102 | }; |
---|
103 | |
---|
104 | bless($self, $class); |
---|
105 | |
---|
106 | $self->{facebook} = Facebook::Graph->new( app_id => $app_id ); |
---|
107 | $self->facebook_do_auth; |
---|
108 | |
---|
109 | return $self; |
---|
110 | } |
---|
111 | |
---|
112 | =head2 sleep N |
---|
113 | |
---|
114 | Stop polling Facebook for N seconds. |
---|
115 | |
---|
116 | =cut |
---|
117 | |
---|
118 | sub sleep { |
---|
119 | my $self = shift; |
---|
120 | my $delay = shift; |
---|
121 | |
---|
122 | # prevent reference cycles |
---|
123 | my $weak = $self; |
---|
124 | weaken($weak); |
---|
125 | |
---|
126 | # Stop any existing timers. |
---|
127 | if (defined $self->{timer}) { |
---|
128 | $self->{timer}->stop; |
---|
129 | $self->{timer} = undef; |
---|
130 | } |
---|
131 | if (defined $self->{message_timer}) { |
---|
132 | # XXX doesn't do anything right now |
---|
133 | $self->{message_timer}->stop; |
---|
134 | $self->{message_timer} = undef; |
---|
135 | } |
---|
136 | |
---|
137 | $self->{timer} = BarnOwl::Timer->new({ |
---|
138 | name => "Facebook poll", |
---|
139 | after => $delay, |
---|
140 | interval => 90, |
---|
141 | cb => sub { $weak->poll_facebook if $weak } |
---|
142 | }); |
---|
143 | # XXX implement message polling |
---|
144 | } |
---|
145 | |
---|
146 | sub die_on_error { |
---|
147 | my $self = shift; |
---|
148 | my $error = shift; |
---|
149 | |
---|
150 | die "$error" if $error; |
---|
151 | } |
---|
152 | |
---|
153 | sub poll_facebook { |
---|
154 | my $self = shift; |
---|
155 | |
---|
156 | #return unless ( time - $self->{last_poll} ) >= 60; |
---|
157 | return unless BarnOwl::getvar('facebook:poll') eq 'on'; |
---|
158 | return unless $self->{logged_in}; |
---|
159 | |
---|
160 | #BarnOwl::message("Polling Facebook..."); |
---|
161 | |
---|
162 | # XXX Oh no! This blocks the user interface. Not good. |
---|
163 | # Ideally, we should have some worker thread for polling facebook. |
---|
164 | # But BarnOwl is probably not thread-safe >_< |
---|
165 | |
---|
166 | my $old_topics = $self->{topics}; |
---|
167 | $self->{topics} = {}; |
---|
168 | |
---|
169 | my $updates = eval { |
---|
170 | $self->{facebook} |
---|
171 | ->query |
---|
172 | ->from("my_news") |
---|
173 | # Not using this, because we want to pick up comment |
---|
174 | # updates. We need to manually de-dup, though. |
---|
175 | # ->where_since( "@" . $self->{last_poll} ) |
---|
176 | ->limit_results( 200 ) |
---|
177 | ->request() |
---|
178 | ->as_hashref() |
---|
179 | }; |
---|
180 | $self->die_on_error($@); |
---|
181 | |
---|
182 | my $new_last_poll = $self->{last_poll}; |
---|
183 | for my $post ( reverse @{$updates->{data}} ) { |
---|
184 | # No app invites, thanks! (XXX make configurable) |
---|
185 | if ($post->{type} eq 'link' && $post->{application}) { |
---|
186 | next; |
---|
187 | } |
---|
188 | |
---|
189 | # XXX Filtering out interest groups for now |
---|
190 | # A more reasonable strategy may be to show their |
---|
191 | # posts, but not the comments. |
---|
192 | if (defined $post->{from}{category}) { |
---|
193 | next; |
---|
194 | } |
---|
195 | |
---|
196 | # XXX Need to somehow access Facebook's user hiding |
---|
197 | # mechanism |
---|
198 | |
---|
199 | # There can be multiple recipients! Strange! Pick the first one. |
---|
200 | my $name = $post->{to}{data}[0]{name} || $post->{from}{name}; |
---|
201 | my $name_id = $post->{to}{data}[0]{id} || $post->{from}{id}; |
---|
202 | my $post_id = $post->{id}; |
---|
203 | |
---|
204 | # Only handle post if it's new |
---|
205 | my $created_time = str2time($post->{created_time}); |
---|
206 | if ($created_time >= $self->{last_poll}) { |
---|
207 | my @keywords = keywords($post->{name} || $post->{message}); |
---|
208 | my $topic = $keywords[0] || 'personal'; |
---|
209 | $self->{topics}->{$post_id} = $topic; |
---|
210 | # XXX indexing is fragile |
---|
211 | my $msg = BarnOwl::Message->new( |
---|
212 | type => 'Facebook', |
---|
213 | sender => $post->{from}{name}, |
---|
214 | sender_id => $post->{from}{id}, |
---|
215 | name => $name, |
---|
216 | name_id => $name_id, |
---|
217 | direction => 'in', |
---|
218 | body => $self->format_body($post), |
---|
219 | post_id => $post_id, |
---|
220 | topic => $topic, |
---|
221 | time => asctime(localtime $created_time), |
---|
222 | # XXX The intent is to get the 'Comment' link, which also |
---|
223 | # serves as a canonical link to the post. The {name} |
---|
224 | # field should equal 'Comment'. |
---|
225 | zsig => $post->{actions}[0]{link}, |
---|
226 | ); |
---|
227 | BarnOwl::queue_message($msg); |
---|
228 | } else { |
---|
229 | $self->{topics}->{$post_id} = $old_topics->{$post_id} || 'personal'; |
---|
230 | } |
---|
231 | |
---|
232 | # This will have funky interleaving of times (they'll all be |
---|
233 | # sorted linearly), but since we don't expect too many updates between |
---|
234 | # polls this is pretty acceptable. |
---|
235 | my $updated_time = str2time($post->{updated_time}); |
---|
236 | if ($updated_time >= $self->{last_poll} && defined $post->{comments}{data}) { |
---|
237 | for my $comment ( @{$post->{comments}{data}} ) { |
---|
238 | my $comment_time = str2time($comment->{created_time}); |
---|
239 | if ($comment_time < $self->{last_poll}) { |
---|
240 | next; |
---|
241 | } |
---|
242 | my $msg = BarnOwl::Message->new( |
---|
243 | type => 'Facebook', |
---|
244 | sender => $comment->{from}{name}, |
---|
245 | sender_id => $comment->{from}{id}, |
---|
246 | name => $name, |
---|
247 | name_id => $name_id, |
---|
248 | direction => 'in', |
---|
249 | body => $comment->{message}, |
---|
250 | post_id => $post_id, |
---|
251 | topic => $self->get_topic($post_id), |
---|
252 | time => asctime(localtime $comment_time), |
---|
253 | ); |
---|
254 | BarnOwl::queue_message($msg); |
---|
255 | } |
---|
256 | } |
---|
257 | if ($updated_time + 1 > $new_last_poll) { |
---|
258 | $new_last_poll = $updated_time + 1; |
---|
259 | } |
---|
260 | } |
---|
261 | # old_topics gets GC'd |
---|
262 | |
---|
263 | $self->{last_poll} = $new_last_poll; |
---|
264 | } |
---|
265 | |
---|
266 | sub format_body { |
---|
267 | my $self = shift; |
---|
268 | |
---|
269 | my $post = shift; |
---|
270 | |
---|
271 | # XXX implement optional URL minification |
---|
272 | if ($post->{type} eq 'status') { |
---|
273 | return $post->{message}; |
---|
274 | } elsif ($post->{type} eq 'link' || $post->{type} eq 'video' || $post->{type} eq 'photo') { |
---|
275 | return $post->{name} |
---|
276 | . ($post->{caption} ? " (" . $post->{caption} . ")\n" : "\n") |
---|
277 | . $post->{link} |
---|
278 | . ($post->{description} ? "\n\n" . $post->{description} : "") |
---|
279 | . ($post->{message} ? "\n\n" . $post->{message} : ""); |
---|
280 | } else { |
---|
281 | return "(unknown post type " . $post->{type} . ")"; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | sub facebook { |
---|
286 | my $self = shift; |
---|
287 | |
---|
288 | my $msg = shift; |
---|
289 | my $reply_to = shift; |
---|
290 | |
---|
291 | if (!defined $self->{facebook} || !$self->{logged_in}) { |
---|
292 | BarnOwl::admin_message('Facebook', 'You are not currently logged into Facebook.'); |
---|
293 | return; |
---|
294 | } |
---|
295 | $self->{facebook}->add_post->set_message( $msg )->publish; |
---|
296 | $self->sleep(0); |
---|
297 | } |
---|
298 | |
---|
299 | sub facebook_comment { |
---|
300 | my $self = shift; |
---|
301 | |
---|
302 | my $post_id = shift; |
---|
303 | my $msg = shift; |
---|
304 | |
---|
305 | $self->{facebook}->add_comment( $post_id )->set_message( $msg )->publish; |
---|
306 | $self->sleep(0); |
---|
307 | } |
---|
308 | |
---|
309 | sub facebook_auth { |
---|
310 | my $self = shift; |
---|
311 | |
---|
312 | my $url = shift; |
---|
313 | # http://www.facebook.com/connect/login_success.html#access_token=TOKEN&expires_in=0 |
---|
314 | $url =~ /access_token=([^&]+)/; # XXX Ew regex |
---|
315 | |
---|
316 | $self->{cfg}->{token} = $1; |
---|
317 | if ($self->facebook_do_auth) { |
---|
318 | my $raw_cfg = to_json($self->{cfg}); |
---|
319 | BarnOwl::admin_message('Facebook', "Add this as the contents of your ~/.owl/facebook file:\n$raw_cfg"); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | sub facebook_do_auth { |
---|
324 | my $self = shift; |
---|
325 | if ( ! defined $self->{cfg}->{token} ) { |
---|
326 | BarnOwl::admin_message('Facebook', "Login to Facebook at ".$self->{login_url} |
---|
327 | . "\nand run command ':facebook-auth URL' with the URL you are redirected to."); |
---|
328 | return 0; |
---|
329 | } |
---|
330 | $self->{facebook}->access_token($self->{cfg}->{token}); |
---|
331 | # Do a quick check to see if things are working |
---|
332 | my $result = eval { $self->{facebook}->fetch('me'); }; |
---|
333 | if ($@) { |
---|
334 | BarnOwl::admin_message('Facebook', "Failed to authenticate! Login to Facebook at ".$self->{login_url} |
---|
335 | . "\nand run command ':facebook-auth URL' with the URL you are redirected to."); |
---|
336 | return 0; |
---|
337 | } else { |
---|
338 | my $name = $result->{'name'}; |
---|
339 | BarnOwl::admin_message('Facebook', "Successfully logged in to Facebook as $name!"); |
---|
340 | $self->{logged_in} = 1; |
---|
341 | $self->sleep(0); # start polling |
---|
342 | return 1; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | sub get_topic { |
---|
347 | my $self = shift; |
---|
348 | |
---|
349 | my $post_id = shift; |
---|
350 | |
---|
351 | return $self->{topics}->{$post_id} || 'personal'; |
---|
352 | } |
---|
353 | |
---|
354 | 1; |
---|