[ee98987] | 1 | use warnings; |
---|
| 2 | use strict; |
---|
| 3 | |
---|
| 4 | =head1 NAME |
---|
| 5 | |
---|
| 6 | BarnOwl::Module::Facebook |
---|
| 7 | |
---|
| 8 | =head1 DESCRIPTION |
---|
| 9 | |
---|
| 10 | Integration with Facebook wall posting and commenting. |
---|
| 11 | |
---|
| 12 | =cut |
---|
| 13 | |
---|
| 14 | package BarnOwl::Module::Facebook; |
---|
| 15 | |
---|
| 16 | our $VERSION = 0.1; |
---|
| 17 | |
---|
| 18 | use JSON; |
---|
| 19 | |
---|
| 20 | use BarnOwl; |
---|
| 21 | use BarnOwl::Hooks; |
---|
| 22 | use BarnOwl::Module::Facebook::Handle; |
---|
| 23 | |
---|
| 24 | our $facebook_handle = undef; |
---|
| 25 | |
---|
| 26 | BarnOwl::new_variable_bool( |
---|
| 27 | 'facebook:poll', |
---|
| 28 | { |
---|
| 29 | default => 1, |
---|
| 30 | summary => 'Poll Facebook for wall updates', |
---|
[bc56a0d] | 31 | # XXX: Make poll time configurable |
---|
[ee98987] | 32 | description => "If set, will poll Facebook every minute for updates.\n" |
---|
| 33 | } |
---|
| 34 | ); |
---|
| 35 | |
---|
[bc56a0d] | 36 | sub 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); |
---|
[ee98987] | 42 | |
---|
| 43 | eval { $cfg = from_json($raw_cfg); }; |
---|
[bc56a0d] | 44 | if ($@) { BarnOwl::admin_message('Facebook', "Unable to parse $conffile: $@"); } |
---|
[ee98987] | 45 | } |
---|
[bc56a0d] | 46 | eval { $facebook_handle = BarnOwl::Module::Facebook::Handle->new($cfg); }; |
---|
| 47 | if ($@) { BarnOwl::error($@); } |
---|
[ee98987] | 48 | } |
---|
| 49 | |
---|
[bc56a0d] | 50 | init(); |
---|
[ee98987] | 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. |
---|
| 55 | BarnOwl::new_command('facebook' => \&cmd_facebook, { |
---|
| 56 | summary => 'Post a status update to your wall from BarnOwl', |
---|
[bc56a0d] | 57 | usage => 'facebook [USER]', |
---|
| 58 | description => 'Post a status update to your wall, or post on another user\'s wall. Autocomplete is supported.' |
---|
[ee98987] | 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 | |
---|
| 67 | BarnOwl::new_command('facebook-comment' => \&cmd_facebook_comment, { |
---|
| 68 | summary => 'Comment on a wall post.', |
---|
[2226f91] | 69 | usage => 'facebook-comment POST_ID', |
---|
[bc56a0d] | 70 | description => 'Comment on a friend\'s wall post.' |
---|
[ee98987] | 71 | }); |
---|
| 72 | |
---|
| 73 | BarnOwl::new_command('facebook-auth' => \&cmd_facebook_auth, { |
---|
| 74 | summary => 'Authenticate as a Facebook user.', |
---|
[9776805] | 75 | usage => 'facebook-auth [URL]', |
---|
[ee98987] | 76 | description => 'Authenticate as a Facebook user. URL should be the page' |
---|
[9776805] | 77 | ."\nFacebook redirects you to after OAuth login. If no URL" |
---|
| 78 | ."\nis specified, output instructions for logging in." |
---|
[ee98987] | 79 | }); |
---|
| 80 | |
---|
[f75a9ca] | 81 | BarnOwl::new_command('facebook-poll' => \&cmd_facebook_poll, { |
---|
| 82 | summary => 'Force a poll of Facebook.', |
---|
| 83 | usage => 'facebook-poll', |
---|
[bc56a0d] | 84 | description => 'Get updates (news, friends) from Facebook.' |
---|
[f75a9ca] | 85 | }); |
---|
| 86 | |
---|
[ee98987] | 87 | sub cmd_facebook { |
---|
| 88 | my $cmd = shift; |
---|
[c833c3d] | 89 | my $user = shift; |
---|
| 90 | |
---|
[bc56a0d] | 91 | return unless check_ready(); |
---|
| 92 | |
---|
[c833c3d] | 93 | BarnOwl::start_edit_win( |
---|
| 94 | defined $user ? "Write something to $user..." : "What's on your mind?", |
---|
[bc56a0d] | 95 | sub{ $facebook_handle->facebook($user, shift) } |
---|
[c833c3d] | 96 | ); |
---|
[ee98987] | 97 | } |
---|
| 98 | |
---|
| 99 | sub cmd_facebook_comment { |
---|
| 100 | my $cmd = shift; |
---|
[2226f91] | 101 | my $post_id = shift; |
---|
[9da6022] | 102 | |
---|
[bc56a0d] | 103 | return unless check_ready(); |
---|
| 104 | |
---|
[69066f6] | 105 | BarnOwl::start_edit_win( |
---|
| 106 | "Write a comment...", |
---|
| 107 | sub { $facebook_handle->facebook_comment($post_id, shift) } |
---|
| 108 | ); |
---|
[ee98987] | 109 | } |
---|
| 110 | |
---|
[f75a9ca] | 111 | sub cmd_facebook_poll { |
---|
| 112 | my $cmd = shift; |
---|
| 113 | |
---|
[bc56a0d] | 114 | return unless check_ready(); |
---|
| 115 | |
---|
[f75a9ca] | 116 | $facebook_handle->sleep(0); |
---|
| 117 | return; |
---|
| 118 | } |
---|
| 119 | |
---|
[ee98987] | 120 | sub cmd_facebook_auth { |
---|
| 121 | my $cmd = shift; |
---|
| 122 | my $url = shift; |
---|
| 123 | |
---|
[bc56a0d] | 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 | |
---|
[ee98987] | 129 | $facebook_handle->facebook_auth($url); |
---|
| 130 | } |
---|
| 131 | |
---|
[bc56a0d] | 132 | sub check_ready { |
---|
| 133 | if (!$facebook_handle->{logged_in}) { |
---|
| 134 | BarnOwl::message("Need to login to Facebook first with ':facebook-auth'."); |
---|
| 135 | return 0; |
---|
| 136 | } |
---|
| 137 | return 1; |
---|
| 138 | } |
---|
[ee98987] | 139 | |
---|
[bc56a0d] | 140 | BarnOwl::filter(qw{facebook type ^facebook$}); |
---|
[c833c3d] | 141 | |
---|
| 142 | sub complete_user { return keys %{$facebook_handle->{friends}}; } |
---|
| 143 | BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) }); |
---|
| 144 | |
---|
[ee98987] | 145 | 1; |
---|