Changeset 01d186f for perl/modules/Facebook/lib/BarnOwl/Module/Facebook.pm
- Timestamp:
- Sep 20, 2011, 11:15:32 PM (13 years ago)
- Branches:
- master, release-1.10, release-1.9
- Children:
- a4ae221
- Parents:
- 65c2b3c
- git-author:
- Edward Z. Yang <ezyang@mit.edu> (06/22/11 21:53:17)
- git-committer:
- Edward Z. Yang <ezyang@mit.edu> (09/20/11 23:15:32)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/modules/Facebook/lib/BarnOwl/Module/Facebook.pm
r8b62088 r01d186f 24 24 our $facebook_handle = undef; 25 25 26 # did not implement class monitoring27 # did not implement multiple accounts28 29 26 BarnOwl::new_variable_bool( 30 27 'facebook:poll', … … 32 29 default => 1, 33 30 summary => 'Poll Facebook for wall updates', 34 # TODO: Make thisconfigurable31 # XXX: Make poll time configurable 35 32 description => "If set, will poll Facebook every minute for updates.\n" 36 33 } 37 34 ); 38 35 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"); 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); 42 43 eval { $cfg = from_json($raw_cfg); }; 44 if ($@) { BarnOwl::admin_message('Facebook', "Unable to parse $conffile: $@"); } 45 } 46 eval { $facebook_handle = BarnOwl::Module::Facebook::Handle->new($cfg); }; 47 if ($@) { BarnOwl::error($@); } 44 48 } 45 49 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 } 50 init(); 85 51 86 52 # Should also add support for posting to other people's walls (this … … 89 55 BarnOwl::new_command('facebook' => \&cmd_facebook, { 90 56 summary => 'Post a status update to your wall from BarnOwl', 91 usage => 'facebook ',92 description => 'Post a status update to your wall .'57 usage => 'facebook [USER]', 58 description => 'Post a status update to your wall, or post on another user\'s wall. Autocomplete is supported.' 93 59 }); 94 60 95 # How do we allow people to specify the USER?96 61 #BarnOwl::new_command('facebook-message' => \&cmd_facebook_direct, { 97 62 # summary => 'Send a Facebook message', … … 103 68 summary => 'Comment on a wall post.', 104 69 usage => 'facebook-comment POST_ID', 105 description => 'Comment on a friend\'s wall post. Using r is recommended.'70 description => 'Comment on a friend\'s wall post.' 106 71 }); 107 72 … … 116 81 summary => 'Force a poll of Facebook.', 117 82 usage => 'facebook-poll', 118 description => 'Get updates from Facebook.'83 description => 'Get updates (news, friends) from Facebook.' 119 84 }); 120 121 # XXX: UI: probably should bug out immediately if we're not logged in.122 85 123 86 sub cmd_facebook { … … 125 88 my $user = shift; 126 89 90 return unless check_ready(); 91 127 92 BarnOwl::start_edit_win( 128 93 defined $user ? "Write something to $user..." : "What's on your mind?", 129 sub{ facebook($user, shift) }94 sub{ $facebook_handle->facebook($user, shift) } 130 95 ); 131 96 } … … 135 100 my $post_id = shift; 136 101 102 return unless check_ready(); 103 137 104 my $topic = $facebook_handle->get_topic($post_id); 138 105 139 # XXX UI should give some (better) indication /which/ conversation140 # is being commented on141 106 BarnOwl::start_edit_win("Write a comment on '$topic'...", 142 107 sub { $facebook_handle->facebook_comment($post_id, shift) }); … … 145 110 sub cmd_facebook_poll { 146 111 my $cmd = shift; 112 113 return unless check_ready(); 147 114 148 115 $facebook_handle->sleep(0); … … 154 121 my $url = shift; 155 122 123 if ($facebook_handle->{logged_in}) { 124 BarnOwl::message("Already logged in. (To force, run ':reload-module Facebook', or deauthorize BarnOwl.)"); 125 return; 126 } 127 156 128 $facebook_handle->facebook_auth($url); 157 129 } 158 130 131 sub check_ready { 132 if (!$facebook_handle->{logged_in}) { 133 BarnOwl::message("Need to login to Facebook first with ':facebook-auth'."); 134 return 0; 135 } 136 return 1; 137 } 138 159 139 BarnOwl::filter(qw{facebook type ^facebook$}); 160 161 # Autocompletion support162 140 163 141 sub complete_user { return keys %{$facebook_handle->{friends}}; }
Note: See TracChangeset
for help on using the changeset viewer.