source: perl/modules/Facebook/lib/BarnOwl/Module/Facebook.pm @ a0c06dc

Last change on this file since a0c06dc was ee98987, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Initial commit of Facebook module implementation. This module was inspired by Kevin Riggle's Facebook-hacked-onto-Twitter implementation, but was rewritten to stand alone as its own module and use the new Facebook Graph API. It requires the Facebook::Graph CPAN module. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 3.6 KB
Line 
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Facebook
7
8=head1 DESCRIPTION
9
10Integration with Facebook wall posting and commenting.
11
12=cut
13
14package BarnOwl::Module::Facebook;
15
16our $VERSION = 0.1;
17
18use JSON;
19
20use BarnOwl;
21use BarnOwl::Hooks;
22use BarnOwl::Module::Facebook::Handle;
23
24our $facebook_handle = undef;
25
26# did not implement class monitoring
27# did not implement multiple accounts
28
29BarnOwl::new_variable_bool(
30    'facebook:poll',
31    {
32        default => 1,
33        summary => 'Poll Facebook for wall updates',
34        # TODO: Make this configurable
35        description => "If set, will poll Facebook every minute for updates.\n"
36     }
37 );
38
39sub fail {
40    my $msg = shift;
41    # reset global state here
42    BarnOwl::admin_message('Facebook Error', $msg);
43    die("Facebook Error: $msg\n");
44}
45
46# We only load up when the conf file is present, to reduce resource
47# usage.  Though, probably not by very much, so maybe a 'facebook-init'
48# command would be more appropriate.
49
50my $conffile = BarnOwl::get_config_dir() . "/facebook";
51
52if (open(my $fh, "<", "$conffile")) {
53    read_config($fh);
54    close($fh);
55}
56
57sub read_config {
58    my $fh = shift;
59    my $raw_cfg = do {local $/; <$fh>};
60    close($fh);
61
62    my $cfg;
63    if ($raw_cfg) {
64        eval { $cfg = from_json($raw_cfg); };
65        if($@) {
66            fail("Unable to parse $conffile: $@");
67        }
68    } else {
69        $cfg = {};
70    }
71
72    eval {
73        $facebook_handle = BarnOwl::Module::Facebook::Handle->new($cfg);
74    };
75    if ($@) {
76        BarnOwl::error($@);
77        next;
78    }
79}
80
81# Ostensibly here as a convenient shortcut for Perl hackery
82sub facebook {
83    $facebook_handle->facebook(@_);
84}
85
86# Should also add support for posting to other people's walls (this
87# is why inline MESSAGE is not supported... yet).  However, see below:
88# specifying USER is UI problematic.
89BarnOwl::new_command('facebook' => \&cmd_facebook, {
90    summary     => 'Post a status update to your wall from BarnOwl',
91    usage       => 'facebook',
92    description => 'Post a status update to your wall.'
93});
94
95# How do we allow people to specify the USER?
96#BarnOwl::new_command('facebook-message' => \&cmd_facebook_direct, {
97#    summary     => 'Send a Facebook message',
98#    usage       => 'twitter-direct USER',
99#    description => 'Send a private Facebook Message to USER.'
100#});
101
102BarnOwl::new_command('facebook-comment' => \&cmd_facebook_comment, {
103    summary     => 'Comment on a wall post.',
104    usage       => 'facebook-comment POSTID',
105    description => 'Comment on a friend\'s wall post.  Using r is recommended.'
106});
107
108BarnOwl::new_command('facebook-auth' => \&cmd_facebook_auth, {
109    summary     => 'Authenticate as a Facebook user.',
110    usage       => 'facebook-auth URL',
111    description => 'Authenticate as a Facebook user.  URL should be the page'
112                ."\nFacebook redirects you to after OAuth login."
113});
114
115# XXX: UI: probably should bug out immediately if we're not logged in.
116
117sub cmd_facebook {
118    my $cmd = shift;
119    BarnOwl::start_edit_win("What's on your mind?", sub{ facebook(shift) });
120    # User prompt for other person's wall is "Write something..." which
121    # we will ostensibly check for soon.
122}
123
124sub cmd_facebook_comment {
125    my $cmd  = shift;
126    my $postid   = shift;
127
128    # XXX UI should give some (better) indication /which/ conversation
129    # is being commented on
130    BarnOwl::start_edit_win("Write a comment... ($postid)",
131                            sub { $facebook_handle->facebook_comment($postid, shift) });
132}
133
134sub cmd_facebook_auth {
135    my $cmd = shift;
136    my $url = shift;
137
138    $facebook_handle->facebook_auth($url);
139}
140
141BarnOwl::filter(qw{facebook type ^facebook$});
142
1431;
Note: See TracBrowser for help on using the repository browser.