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

release-1.10release-1.9
Last change on this file was 7efa163, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Wordwrap, write the config, style wibbles. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[24bd860]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',
[01d186f]31        # XXX: Make poll time configurable
[24bd860]32        description => "If set, will poll Facebook every minute for updates.\n"
33     }
34 );
35
[01d186f]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);
[24bd860]42
43        eval { $cfg = from_json($raw_cfg); };
[01d186f]44        if ($@) { BarnOwl::admin_message('Facebook', "Unable to parse $conffile: $@"); }
[24bd860]45    }
[01d186f]46    eval { $facebook_handle = BarnOwl::Module::Facebook::Handle->new($cfg); };
47    if ($@) { BarnOwl::error($@); }
[24bd860]48}
49
[01d186f]50init();
[24bd860]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',
[01d186f]57    usage       => 'facebook [USER]',
58    description => 'Post a status update to your wall, or post on another user\'s wall. Autocomplete is supported.'
[24bd860]59});
60
61#BarnOwl::new_command('facebook-message' => \&cmd_facebook_direct, {
62#    summary     => 'Send a Facebook message',
[7efa163]63#    usage       => 'facebook-message USER',
64#    description => 'Send a private Facebook message to USER.'
[24bd860]65#});
66
67BarnOwl::new_command('facebook-comment' => \&cmd_facebook_comment, {
68    summary     => 'Comment on a wall post.',
[eb497a9]69    usage       => 'facebook-comment POST_ID',
[01d186f]70    description => 'Comment on a friend\'s wall post.'
[24bd860]71});
72
73BarnOwl::new_command('facebook-auth' => \&cmd_facebook_auth, {
74    summary     => 'Authenticate as a Facebook user.',
[e1ed6f4]75    usage       => 'facebook-auth [URL]',
[24bd860]76    description => 'Authenticate as a Facebook user.  URL should be the page'
[e1ed6f4]77                ."\nFacebook redirects you to after OAuth login.  If no URL"
78                ."\nis specified, output instructions for logging in."
[24bd860]79});
80
[3199d8e]81BarnOwl::new_command('facebook-poll' => \&cmd_facebook_poll, {
82    summary     => 'Force a poll of Facebook.',
83    usage       => 'facebook-poll',
[01d186f]84    description => 'Get updates (news, friends) from Facebook.'
[3199d8e]85});
86
[24bd860]87sub cmd_facebook {
88    my $cmd = shift;
[8b62088]89    my $user = shift;
90
[01d186f]91    return unless check_ready();
92
[8b62088]93    BarnOwl::start_edit_win(
94        defined $user ? "Write something to $user..." : "What's on your mind?",
[7efa163]95        sub { $facebook_handle->facebook($user, shift) }
[8b62088]96    );
[24bd860]97}
98
99sub cmd_facebook_comment {
100    my $cmd  = shift;
[eb497a9]101    my $post_id = shift;
[99f0a77]102
[01d186f]103    return unless check_ready();
104
[395d304]105    BarnOwl::start_edit_win(
106        "Write a comment...",
107        sub { $facebook_handle->facebook_comment($post_id, shift) }
108    );
[24bd860]109}
110
[3199d8e]111sub cmd_facebook_poll {
112    my $cmd = shift;
113
[01d186f]114    return unless check_ready();
115
[3199d8e]116    $facebook_handle->sleep(0);
117    return;
118}
119
[24bd860]120sub cmd_facebook_auth {
121    my $cmd = shift;
122    my $url = shift;
123
[01d186f]124    if ($facebook_handle->{logged_in}) {
125        BarnOwl::message("Already logged in. (To force, run ':reload-module Facebook', or deauthorize BarnOwl.)");
126        return;
127    }
128
[24bd860]129    $facebook_handle->facebook_auth($url);
130}
131
[01d186f]132sub check_ready {
133    if (!$facebook_handle->{logged_in}) {
[7efa163]134        BarnOwl::message("You need to login to Facebook first with ':facebook-auth'.");
[01d186f]135        return 0;
136    }
137    return 1;
138}
[24bd860]139
[01d186f]140BarnOwl::filter(qw{facebook type ^facebook$});
[7efa163]141BarnOwl::bindkey(qw(recv f command facebook));
[8b62088]142
143sub complete_user { return keys %{$facebook_handle->{friends}}; }
144BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) });
145
[24bd860]1461;
Note: See TracBrowser for help on using the repository browser.