[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 | # 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 POSTID', |
---|
| 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 | |
---|
[f75a9ca] | 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 | |
---|
[ee98987] | 121 | # XXX: UI: probably should bug out immediately if we're not logged in. |
---|
| 122 | |
---|
| 123 | sub 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 | |
---|
| 130 | sub cmd_facebook_comment { |
---|
| 131 | my $cmd = shift; |
---|
| 132 | my $postid = shift; |
---|
| 133 | |
---|
| 134 | # XXX UI should give some (better) indication /which/ conversation |
---|
| 135 | # is being commented on |
---|
| 136 | BarnOwl::start_edit_win("Write a comment... ($postid)", |
---|
| 137 | sub { $facebook_handle->facebook_comment($postid, shift) }); |
---|
| 138 | } |
---|
| 139 | |
---|
[f75a9ca] | 140 | sub cmd_facebook_poll { |
---|
| 141 | my $cmd = shift; |
---|
| 142 | |
---|
| 143 | $facebook_handle->sleep(0); |
---|
| 144 | return; |
---|
| 145 | } |
---|
| 146 | |
---|
[ee98987] | 147 | sub cmd_facebook_auth { |
---|
| 148 | my $cmd = shift; |
---|
| 149 | my $url = shift; |
---|
| 150 | |
---|
| 151 | $facebook_handle->facebook_auth($url); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | BarnOwl::filter(qw{facebook type ^facebook$}); |
---|
| 155 | |
---|
| 156 | 1; |
---|