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

release-1.9
Last change on this file since 65c2b3c was 8b62088, checked in by Edward Z. Yang <ezyang@mit.edu>, 10 years ago
Add autocomplete and wall-posting support. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 4.1 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 POST_ID',
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
115BarnOwl::new_command('facebook-poll' => \&cmd_facebook_poll, {
116    summary     => 'Force a poll of Facebook.',
117    usage       => 'facebook-poll',
118    description => 'Get updates from Facebook.'
119});
120
121# XXX: UI: probably should bug out immediately if we're not logged in.
122
123sub cmd_facebook {
124    my $cmd = shift;
125    my $user = shift;
126
127    BarnOwl::start_edit_win(
128        defined $user ? "Write something to $user..." : "What's on your mind?",
129        sub{ facebook($user, shift) }
130    );
131}
132
133sub cmd_facebook_comment {
134    my $cmd  = shift;
135    my $post_id = shift;
136
137    my $topic = $facebook_handle->get_topic($post_id);
138
139    # XXX UI should give some (better) indication /which/ conversation
140    # is being commented on
141    BarnOwl::start_edit_win("Write a comment on '$topic'...",
142                            sub { $facebook_handle->facebook_comment($post_id, shift) });
143}
144
145sub cmd_facebook_poll {
146    my $cmd = shift;
147
148    $facebook_handle->sleep(0);
149    return;
150}
151
152sub cmd_facebook_auth {
153    my $cmd = shift;
154    my $url = shift;
155
156    $facebook_handle->facebook_auth($url);
157}
158
159BarnOwl::filter(qw{facebook type ^facebook$});
160
161# Autocompletion support
162
163sub complete_user { return keys %{$facebook_handle->{friends}}; }
164BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) });
165
1661;
Note: See TracBrowser for help on using the repository browser.