| 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 | # did not implement class monitoring |
|---|
| 27 | # did not implement multiple accounts |
|---|
| 28 | |
|---|
| 29 | BarnOwl::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 | |
|---|
| 39 | sub 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 | |
|---|
| 50 | my $conffile = BarnOwl::get_config_dir() . "/facebook"; |
|---|
| 51 | |
|---|
| 52 | if (open(my $fh, "<", "$conffile")) { |
|---|
| 53 | read_config($fh); |
|---|
| 54 | close($fh); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | sub 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 |
|---|
| 82 | sub 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. |
|---|
| 89 | BarnOwl::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 | |
|---|
| 102 | BarnOwl::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 | |
|---|
| 108 | BarnOwl::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 | |
|---|
| 115 | BarnOwl::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 | |
|---|
| 123 | sub 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 | |
|---|
| 133 | sub 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 | |
|---|
| 145 | sub cmd_facebook_poll { |
|---|
| 146 | my $cmd = shift; |
|---|
| 147 | |
|---|
| 148 | $facebook_handle->sleep(0); |
|---|
| 149 | return; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | sub cmd_facebook_auth { |
|---|
| 153 | my $cmd = shift; |
|---|
| 154 | my $url = shift; |
|---|
| 155 | |
|---|
| 156 | $facebook_handle->facebook_auth($url); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | BarnOwl::filter(qw{facebook type ^facebook$}); |
|---|
| 160 | |
|---|
| 161 | # Autocompletion support |
|---|
| 162 | |
|---|
| 163 | sub complete_user { return keys %{$facebook_handle->{friends}}; } |
|---|
| 164 | BarnOwl::Completion::register_completer(facebook => sub { complete_user(@_) }); |
|---|
| 165 | |
|---|
| 166 | 1; |
|---|