| 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 | use Ouch; |
|---|
| 38 | |
|---|
| 39 | use Scalar::Util qw(weaken); |
|---|
| 40 | |
|---|
| 41 | use BarnOwl; |
|---|
| 42 | use BarnOwl::Message::Facebook; |
|---|
| 43 | |
|---|
| 44 | our $app_id = 235537266461636; # for application 'barnowl' |
|---|
| 45 | |
|---|
| 46 | # Unfortunately, Facebook does not offer a comment stream, in the same |
|---|
| 47 | # way we can get a post stream using the news feed. This makes it a bit |
|---|
| 48 | # difficult to de-duplicate comments we have already seen. We use a |
|---|
| 49 | # simple heuristic to fix this: we check if the comment's time is dated |
|---|
| 50 | # from before our last update, and don't re-post if it's dated before. |
|---|
| 51 | # Be somewhat forgiving, since it's better to duplicate a post than to |
|---|
| 52 | # drop one. Furthermore, we must use Facebook's idea of time, since the |
|---|
| 53 | # server BarnOwl is running on may be desynchronized. So we need to |
|---|
| 54 | # utilize Facebook's idea of time, not ours. We do this by looking at |
|---|
| 55 | # all of the timestamps we see while processing an update, and take the |
|---|
| 56 | # latest one and increment it by one second. |
|---|
| 57 | # |
|---|
| 58 | # What properties do we get with this setup? |
|---|
| 59 | # |
|---|
| 60 | # - We get comment updates only for the latest N posts on a news feed. |
|---|
| 61 | # Any later ones, you have to use Facebook's usual mechanisms (e.g. |
|---|
| 62 | # email notifications). |
|---|
| 63 | # |
|---|
| 64 | # - Processing a poll is relatively expensive, since we have to |
|---|
| 65 | # iterate over N new posts. It might be worthwhile polling for new |
|---|
| 66 | # comments less frequently than polling for new posts. |
|---|
| 67 | |
|---|
| 68 | sub new { |
|---|
| 69 | my $class = shift; |
|---|
| 70 | my $cfg = shift; |
|---|
| 71 | |
|---|
| 72 | my $self = { |
|---|
| 73 | 'cfg' => $cfg, |
|---|
| 74 | 'facebook' => undef, |
|---|
| 75 | |
|---|
| 76 | # Ideally this should be done using Facebook realtime updates, |
|---|
| 77 | # but we can't assume that the BarnOwl lives on a publically |
|---|
| 78 | # addressable server (XXX maybe we can setup an option for this.) |
|---|
| 79 | 'friend_timer' => undef, |
|---|
| 80 | |
|---|
| 81 | # Initialized with our 'time', but will be synced to Facebook |
|---|
| 82 | # soon enough. (Subtractive amount is just to preseed with some |
|---|
| 83 | # values.) XXX Remove subtraction altogether. |
|---|
| 84 | 'last_poll' => time - 60 * 60, |
|---|
| 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,read_friendlists,rsvp_event,user_events&redirect_uri=http://www.facebook.com/connect/login_success.html&response_type=token', |
|---|
| 95 | # minified to fit in most terminal windows. |
|---|
| 96 | # Be careful about updating these values, since BarnOwl will not |
|---|
| 97 | # notice that it is missing necessary permissions until it |
|---|
| 98 | # attempt to perform an operation which fails due to lack of |
|---|
| 99 | # permissions. |
|---|
| 100 | 'login_url' => 'http://goo.gl/rcM9s', |
|---|
| 101 | |
|---|
| 102 | 'logged_in' => 0, |
|---|
| 103 | |
|---|
| 104 | # would need another hash for topic de-dup |
|---|
| 105 | 'topics' => {}, |
|---|
| 106 | |
|---|
| 107 | # deduplicated map of names to user ids |
|---|
| 108 | 'friends' => {}, |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | bless($self, $class); |
|---|
| 112 | |
|---|
| 113 | $self->{facebook} = Facebook::Graph->new( app_id => $app_id ); |
|---|
| 114 | if ( defined $self->{cfg}->{token} ) { |
|---|
| 115 | $self->facebook_do_auth; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | return $self; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | =head2 sleep N |
|---|
| 122 | |
|---|
| 123 | Stop polling Facebook for N seconds. |
|---|
| 124 | |
|---|
| 125 | =cut |
|---|
| 126 | |
|---|
| 127 | sub sleep { |
|---|
| 128 | my $self = shift; |
|---|
| 129 | my $delay = shift; |
|---|
| 130 | |
|---|
| 131 | # prevent reference cycles |
|---|
| 132 | my $weak = $self; |
|---|
| 133 | weaken($weak); |
|---|
| 134 | |
|---|
| 135 | # Stop any existing timers. |
|---|
| 136 | if (defined $self->{friend_timer}) { |
|---|
| 137 | $self->{friend_timer}->stop; |
|---|
| 138 | $self->{friend_timer} = undef; |
|---|
| 139 | } |
|---|
| 140 | if (defined $self->{timer}) { |
|---|
| 141 | $self->{timer}->stop; |
|---|
| 142 | $self->{timer} = undef; |
|---|
| 143 | } |
|---|
| 144 | if (defined $self->{message_timer}) { |
|---|
| 145 | # XXX doesn't do anything right now |
|---|
| 146 | $self->{message_timer}->stop; |
|---|
| 147 | $self->{message_timer} = undef; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | $self->{friend_timer} = BarnOwl::Timer->new({ |
|---|
| 151 | name => "Facebook friend poll", |
|---|
| 152 | after => $delay, |
|---|
| 153 | interval => 60 * 60 * 24, |
|---|
| 154 | cb => sub { $weak->poll_friends if $weak } |
|---|
| 155 | }); |
|---|
| 156 | $self->{timer} = BarnOwl::Timer->new({ |
|---|
| 157 | name => "Facebook poll", |
|---|
| 158 | after => $delay, |
|---|
| 159 | interval => 90, |
|---|
| 160 | cb => sub { $weak->poll_facebook if $weak } |
|---|
| 161 | }); |
|---|
| 162 | # XXX implement message polling |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | sub check_result { |
|---|
| 166 | my $self = shift; |
|---|
| 167 | if (kiss 400) { |
|---|
| 168 | # Ugh, no easy way of accessing the JSON error type |
|---|
| 169 | # which is OAuthException. |
|---|
| 170 | $self->{logged_in} = 0; |
|---|
| 171 | $self->facebook_do_auth; |
|---|
| 172 | return 0; |
|---|
| 173 | } elsif (hug) { |
|---|
| 174 | my $code = $@->code; |
|---|
| 175 | warn "Poll failed with $code: $@"; |
|---|
| 176 | return 0; |
|---|
| 177 | } |
|---|
| 178 | return 1; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | sub poll_friends { |
|---|
| 182 | my $self = shift; |
|---|
| 183 | |
|---|
| 184 | return unless BarnOwl::getvar('facebook:poll') eq 'on'; |
|---|
| 185 | return unless $self->{logged_in}; |
|---|
| 186 | |
|---|
| 187 | my $friends = eval { $self->{facebook}->fetch('me/friends'); }; |
|---|
| 188 | return unless $self->check_result; |
|---|
| 189 | |
|---|
| 190 | $self->{friends} = {}; |
|---|
| 191 | |
|---|
| 192 | for my $friend ( @{$friends->{data}} ) { |
|---|
| 193 | if (defined $self->{friends}{$friend->{name}}) { |
|---|
| 194 | # XXX We should try a little harder here, rather than just |
|---|
| 195 | # tacking on a number. Ideally, we should be able to |
|---|
| 196 | # calculate some extra piece of information that the user |
|---|
| 197 | # needs to disambiguate between the two users. An old |
|---|
| 198 | # version of Facebook used to disambiguate with your primary |
|---|
| 199 | # network (so you might have Edward Yang (MIT) and Edward |
|---|
| 200 | # Yang (Cambridge), the idea being that users in the same |
|---|
| 201 | # network would probably have already disambiguated |
|---|
| 202 | # themselves with middle names or nicknames. We no longer |
|---|
| 203 | # get network information, since Facebook axed that |
|---|
| 204 | # information, but the Education/Work fields may still be |
|---|
| 205 | # a reasonable approximation (but which one do you pick?! |
|---|
| 206 | # The most recent one.) Since getting this information |
|---|
| 207 | # involves extra queries, there are also caching and |
|---|
| 208 | # efficiency concerns (though hopefully you don't have too |
|---|
| 209 | # many friends with the same name). Furthermore, accessing |
|---|
| 210 | # this information requires a pretty hefty extra set of |
|---|
| 211 | # permissions requests, which we don't currently ask for. |
|---|
| 212 | # It may just be better to let users specify custom |
|---|
| 213 | # aliases for Facebook users, which are added into this |
|---|
| 214 | # hash. See also username support. |
|---|
| 215 | warn "Duplicate friend name " . $friend->{name}; |
|---|
| 216 | my $name = $friend->{name}; |
|---|
| 217 | my $i = 2; |
|---|
| 218 | while (defined $self->{friends}{$friend->{name} . ' ' . $i}) { $i++; } |
|---|
| 219 | $self->{friends}{$friend->{name} . ' ' . $i} = $friend->{id}; |
|---|
| 220 | } else { |
|---|
| 221 | $self->{friends}{$friend->{name}} = $friend->{id}; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | # XXX We should also have support for usernames, and not just real |
|---|
| 226 | # names. However, since this data is not returned by the friends |
|---|
| 227 | # query, it would require a rather expensive set of queries. We |
|---|
| 228 | # might try to preserve old data, but all-in-all it's a bit |
|---|
| 229 | # complicated. One possible way of fixing this is to construct a |
|---|
| 230 | # custom FQL query that joins the friends table and the users table. |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | sub poll_facebook { |
|---|
| 234 | my $self = shift; |
|---|
| 235 | |
|---|
| 236 | return unless BarnOwl::getvar('facebook:poll') eq 'on'; |
|---|
| 237 | return unless $self->{logged_in}; |
|---|
| 238 | |
|---|
| 239 | # XXX Oh no! This blocks the user interface. Not good. |
|---|
| 240 | # Ideally, we should have some worker thread for polling facebook. |
|---|
| 241 | # But BarnOwl is probably not thread-safe >_< |
|---|
| 242 | |
|---|
| 243 | my $old_topics = $self->{topics}; |
|---|
| 244 | $self->{topics} = {}; |
|---|
| 245 | |
|---|
| 246 | my $updates = eval { |
|---|
| 247 | $self->{facebook} |
|---|
| 248 | ->query |
|---|
| 249 | ->from("my_news") |
|---|
| 250 | # Not using this, because we want to pick up comment |
|---|
| 251 | # updates. We need to manually de-duplicate, though. |
|---|
| 252 | # ->where_since( "@" . $self->{last_poll} ) |
|---|
| 253 | # Facebook doesn't actually give us that many results. |
|---|
| 254 | # But it can't hurt to ask! |
|---|
| 255 | ->limit_results( 200 ) |
|---|
| 256 | ->request |
|---|
| 257 | ->as_hashref |
|---|
| 258 | }; |
|---|
| 259 | return unless $self->check_result; |
|---|
| 260 | |
|---|
| 261 | my $new_last_poll = $self->{last_poll}; |
|---|
| 262 | for my $post ( reverse @{$updates->{data}} ) { |
|---|
| 263 | # No app invites, thanks! (XXX make configurable) |
|---|
| 264 | if ($post->{type} eq 'link' && $post->{application}) { |
|---|
| 265 | next; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | # XXX Filtering out interest groups for now |
|---|
| 269 | # A more reasonable strategy may be to show their |
|---|
| 270 | # posts, but not the comments. |
|---|
| 271 | if (defined $post->{from}{category}) { |
|---|
| 272 | next; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | # There can be multiple recipients! Strange! Pick the first one. |
|---|
| 276 | my $name = $post->{to}{data}[0]{name} || $post->{from}{name}; |
|---|
| 277 | my $name_id = $post->{to}{data}[0]{id} || $post->{from}{id}; |
|---|
| 278 | my $post_id = $post->{id}; |
|---|
| 279 | |
|---|
| 280 | my $topic; |
|---|
| 281 | if (defined $old_topics->{$post_id}) { |
|---|
| 282 | $topic = $old_topics->{$post_id}; |
|---|
| 283 | $self->{topics}->{$post_id} = $topic; |
|---|
| 284 | } else { |
|---|
| 285 | my @keywords = keywords($post->{name} || $post->{message}); |
|---|
| 286 | $topic = $keywords[0] || 'personal'; |
|---|
| 287 | $topic =~ s/ /-/g; |
|---|
| 288 | $self->{topics}->{$post_id} = $topic; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | # Only handle post if it's new |
|---|
| 292 | my $created_time = str2time($post->{created_time}); |
|---|
| 293 | if ($created_time >= $self->{last_poll}) { |
|---|
| 294 | # XXX indexing is fragile |
|---|
| 295 | my $msg = BarnOwl::Message->new( |
|---|
| 296 | type => 'Facebook', |
|---|
| 297 | sender => $post->{from}{name}, |
|---|
| 298 | sender_id => $post->{from}{id}, |
|---|
| 299 | name => $name, |
|---|
| 300 | name_id => $name_id, |
|---|
| 301 | direction => 'in', |
|---|
| 302 | body => $self->format_body($post), |
|---|
| 303 | post_id => $post_id, |
|---|
| 304 | topic => $topic, |
|---|
| 305 | time => asctime(localtime $created_time), |
|---|
| 306 | # XXX The intent is to get the 'Comment' link, which also |
|---|
| 307 | # serves as a canonical link to the post. The {name} |
|---|
| 308 | # field should equal 'Comment'. |
|---|
| 309 | permalink => $post->{actions}[0]{link}, |
|---|
| 310 | ); |
|---|
| 311 | BarnOwl::queue_message($msg); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | # This will interleave times (they'll all be organized by parent |
|---|
| 315 | # post), but since we don't expect too many updates between |
|---|
| 316 | # polls this is pretty acceptable. |
|---|
| 317 | my $updated_time = str2time($post->{updated_time}); |
|---|
| 318 | if ($updated_time >= $self->{last_poll} && defined $post->{comments}{data}) { |
|---|
| 319 | for my $comment ( @{$post->{comments}{data}} ) { |
|---|
| 320 | my $comment_time = str2time($comment->{created_time}); |
|---|
| 321 | if ($comment_time < $self->{last_poll}) { |
|---|
| 322 | next; |
|---|
| 323 | } |
|---|
| 324 | my $msg = BarnOwl::Message->new( |
|---|
| 325 | type => 'Facebook', |
|---|
| 326 | sender => $comment->{from}{name}, |
|---|
| 327 | sender_id => $comment->{from}{id}, |
|---|
| 328 | name => $name, |
|---|
| 329 | name_id => $name_id, |
|---|
| 330 | direction => 'in', |
|---|
| 331 | body => $comment->{message}, |
|---|
| 332 | post_id => $post_id, |
|---|
| 333 | topic => $topic, |
|---|
| 334 | time => asctime(localtime $comment_time), |
|---|
| 335 | ); |
|---|
| 336 | BarnOwl::queue_message($msg); |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | if ($updated_time + 1 > $new_last_poll) { |
|---|
| 340 | $new_last_poll = $updated_time + 1; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | # old_topics gets GC'd |
|---|
| 344 | |
|---|
| 345 | $self->{last_poll} = $new_last_poll; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | sub format_body { |
|---|
| 349 | my $self = shift; |
|---|
| 350 | |
|---|
| 351 | my $post = shift; |
|---|
| 352 | |
|---|
| 353 | # XXX implement optional URL minification |
|---|
| 354 | if ($post->{type} eq 'status') { |
|---|
| 355 | return $post->{message}; |
|---|
| 356 | } elsif ($post->{type} eq 'link' || $post->{type} eq 'video' || $post->{type} eq 'photo') { |
|---|
| 357 | return $post->{name} |
|---|
| 358 | . ($post->{caption} ? " (" . $post->{caption} . ")\n" : "\n") |
|---|
| 359 | . $post->{link} |
|---|
| 360 | . ($post->{description} ? "\n\n" . $post->{description} : "") |
|---|
| 361 | . ($post->{message} ? "\n\n" . $post->{message} : ""); |
|---|
| 362 | } else { |
|---|
| 363 | return "(unknown post type " . $post->{type} . ")"; |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | # Invariant: we don't become logged out between entering text field |
|---|
| 368 | # and actually processing the request. XXX I don't think this actually |
|---|
| 369 | # holds, but such a case would rarely happen. |
|---|
| 370 | |
|---|
| 371 | sub facebook { |
|---|
| 372 | my $self = shift; |
|---|
| 373 | |
|---|
| 374 | my $user = shift; |
|---|
| 375 | my $msg = shift; |
|---|
| 376 | |
|---|
| 377 | if (defined $user) { |
|---|
| 378 | $user = $self->{friends}{$user} || $user; |
|---|
| 379 | eval { $self->{facebook}->add_post( $user )->set_message( $msg )->publish; }; |
|---|
| 380 | return unless $self->check_result; |
|---|
| 381 | } else { |
|---|
| 382 | eval { $self->{facebook}->add_post->set_message( $msg )->publish; }; |
|---|
| 383 | return unless $self->check_result; |
|---|
| 384 | } |
|---|
| 385 | $self->sleep(0); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | sub facebook_comment { |
|---|
| 389 | my $self = shift; |
|---|
| 390 | |
|---|
| 391 | my $post_id = shift; |
|---|
| 392 | my $msg = shift; |
|---|
| 393 | |
|---|
| 394 | eval { $self->{facebook}->add_comment( $post_id )->set_message( $msg )->publish; }; |
|---|
| 395 | return unless $self->check_result; |
|---|
| 396 | $self->sleep(0); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | sub facebook_auth { |
|---|
| 400 | my $self = shift; |
|---|
| 401 | |
|---|
| 402 | my $url = shift; |
|---|
| 403 | |
|---|
| 404 | if (!defined $url) { |
|---|
| 405 | $self->facebook_do_auth; |
|---|
| 406 | return; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | # http://www.facebook.com/connect/login_success.html#access_token=TOKEN&expires_in=0 |
|---|
| 410 | $url =~ /access_token=([^&]+)/; # XXX Ew regex |
|---|
| 411 | |
|---|
| 412 | if (!defined $1) { |
|---|
| 413 | BarnOwl::message("Invalid URL."); |
|---|
| 414 | return; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | $self->{cfg}->{token} = $1; |
|---|
| 418 | if ($self->facebook_do_auth) { |
|---|
| 419 | my $raw_cfg = to_json($self->{cfg}); |
|---|
| 420 | BarnOwl::admin_message('Facebook', "Add this as the contents of your ~/.owl/facebook file:\n$raw_cfg"); |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | sub facebook_do_auth { |
|---|
| 425 | my $self = shift; |
|---|
| 426 | if ( ! defined $self->{cfg}->{token} ) { |
|---|
| 427 | BarnOwl::admin_message('Facebook', "Login to Facebook at ".$self->{login_url} |
|---|
| 428 | . "\nand run command ':facebook-auth URL' with the URL you are redirected to." |
|---|
| 429 | . "\n\nWhat does Barnowl use these permissions for? As a desktop" |
|---|
| 430 | . "\nmessaging application, we need persistent read/write access to your" |
|---|
| 431 | . "\nnews feed and your inbox. Other permissions are for pending" |
|---|
| 432 | . "\nfeatures: we intend on adding support for event streaming, RSVP," |
|---|
| 433 | . "\nand BarnOwl filtering on friend lists." |
|---|
| 434 | ); |
|---|
| 435 | return 0; |
|---|
| 436 | } |
|---|
| 437 | $self->{facebook}->access_token($self->{cfg}->{token}); |
|---|
| 438 | # Do a quick check to see if things are working |
|---|
| 439 | my $result = eval { $self->{facebook}->query()->find('me')->select_fields('name')->request->as_hashref; }; |
|---|
| 440 | if ($@) { |
|---|
| 441 | BarnOwl::admin_message('Facebook', "Failed to authenticate! Login to Facebook at ".$self->{login_url} |
|---|
| 442 | . "\nand run command ':facebook-auth URL' with the URL you are redirected to."); |
|---|
| 443 | return 0; |
|---|
| 444 | } else { |
|---|
| 445 | my $name = $result->{'name'}; |
|---|
| 446 | BarnOwl::admin_message('Facebook', "Successfully logged in to Facebook as $name!"); |
|---|
| 447 | $self->{logged_in} = 1; |
|---|
| 448 | $self->sleep(0); # start polling |
|---|
| 449 | return 1; |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | 1; |
|---|