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