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

Last change on this file since 303dbc1 was 6c3d4ad, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Rename postid to post_id. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 4.0 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    BarnOwl::start_edit_win("What's on your mind?", sub{ facebook(shift) });
126    # User prompt for other person's wall is "Write something..." which
127    # we will ostensibly check for soon.
128}
129
130sub cmd_facebook_comment {
131    my $cmd  = shift;
132    my $post_id = shift;
133
134    my $topic = $facebook_handle->get_topic($post_id);
135
136    # XXX UI should give some (better) indication /which/ conversation
137    # is being commented on
138    BarnOwl::start_edit_win("Write a comment on '$topic'...",
139                            sub { $facebook_handle->facebook_comment($post_id, shift) });
140}
141
142sub cmd_facebook_poll {
143    my $cmd = shift;
144
145    $facebook_handle->sleep(0);
146    return;
147}
148
149sub cmd_facebook_auth {
150    my $cmd = shift;
151    my $url = shift;
152
153    $facebook_handle->facebook_auth($url);
154}
155
156BarnOwl::filter(qw{facebook type ^facebook$});
157
1581;
Note: See TracBrowser for help on using the repository browser.