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

Last change on this file since 2f6c263 was 2f6c263, checked in by Edward Z. Yang <ezyang@mit.edu>, 10 years ago
Improve docs, error handling and refactor. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 3.8 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
26BarnOwl::new_variable_bool(
27    'facebook:poll',
28    {
29        default => 1,
30        summary => 'Poll Facebook for wall updates',
31        # XXX: Make poll time configurable
32        description => "If set, will poll Facebook every minute for updates.\n"
33     }
34 );
35
36sub init {
37    my $conffile = BarnOwl::get_config_dir() . "/facebook";
38    my $cfg = {};
39    if (open(my $fh, "<", "$conffile")) {
40        my $raw_cfg = do {local $/; <$fh>};
41        close($fh);
42
43        eval { $cfg = from_json($raw_cfg); };
44        if ($@) { BarnOwl::admin_message('Facebook', "Unable to parse $conffile: $@"); }
45    }
46    eval { $facebook_handle = BarnOwl::Module::Facebook::Handle->new($cfg); };
47    if ($@) { BarnOwl::error($@); }
48}
49
50init();
51
52# Should also add support for posting to other people's walls (this
53# is why inline MESSAGE is not supported... yet).  However, see below:
54# specifying USER is UI problematic.
55BarnOwl::new_command('facebook' => \&cmd_facebook, {
56    summary     => 'Post a status update to your wall from BarnOwl',
57    usage       => 'facebook [USER]',
58    description => 'Post a status update to your wall, or post on another user\'s wall. Autocomplete is supported.'
59});
60
61#BarnOwl::new_command('facebook-message' => \&cmd_facebook_direct, {
62#    summary     => 'Send a Facebook message',
63#    usage       => 'twitter-direct USER',
64#    description => 'Send a private Facebook Message to USER.'
65#});
66
67BarnOwl::new_command('facebook-comment' => \&cmd_facebook_comment, {
68    summary     => 'Comment on a wall post.',
69    usage       => 'facebook-comment POST_ID',
70    description => 'Comment on a friend\'s wall post.'
71});
72
73BarnOwl::new_command('facebook-auth' => \&cmd_facebook_auth, {
74    summary     => 'Authenticate as a Facebook user.',
75    usage       => 'facebook-auth URL',
76    description => 'Authenticate as a Facebook user.  URL should be the page'
77                ."\nFacebook redirects you to after OAuth login."
78});
79
80BarnOwl::new_command('facebook-poll' => \&cmd_facebook_poll, {
81    summary     => 'Force a poll of Facebook.',
82    usage       => 'facebook-poll',
83    description => 'Get updates (news, friends) from Facebook.'
84});
85
86sub cmd_facebook {
87    my $cmd = shift;
88    my $user = shift;
89
90    return unless check_ready();
91
92    BarnOwl::start_edit_win(
93        defined $user ? "Write something to $user..." : "What's on your mind?",
94        sub{ $facebook_handle->facebook($user, shift) }
95    );
96}
97
98sub cmd_facebook_comment {
99    my $cmd  = shift;
100    my $post_id = shift;
101
102    return unless check_ready();
103
104    my $topic = $facebook_handle->get_topic($post_id);
105
106    BarnOwl::start_edit_win("Write a comment on '$topic'...",
107                            sub { $facebook_handle->facebook_comment($post_id, shift) });
108}
109
110sub cmd_facebook_poll {
111    my $cmd = shift;
112
113    return unless check_ready();
114
115    $facebook_handle->sleep(0);
116    return;
117}
118
119sub cmd_facebook_auth {
120    my $cmd = shift;
121    my $url = shift;
122
123    if ($facebook_handle->{logged_in}) {
124        BarnOwl::message("Already logged in. (To force, run ':reload-module Facebook', or deauthorize BarnOwl.)");
125        return;
126    }
127
128    $facebook_handle->facebook_auth($url);
129}
130
131sub check_ready {
132    if (!$facebook_handle->{logged_in}) {
133        BarnOwl::message("Need to login to Facebook first with ':facebook-auth'.");
134        return 0;
135    }
136    return 1;
137}
138
139BarnOwl::filter(qw{facebook type ^facebook$});
140
141sub complete_user { return keys %{$facebook_handle->{friends}}; }
142BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) });
143
1441;
Note: See TracBrowser for help on using the repository browser.