Ignore:
Timestamp:
Sep 19, 2011, 1:31:34 PM (13 years ago)
Author:
Edward Z. Yang <ezyang@mit.edu>
Children:
ac45403
Parents:
a8e1fcf
git-author:
Edward Z. Yang <ezyang@mit.edu> (06/22/11 21:53:17)
git-committer:
Edward Z. Yang <ezyang@mit.edu> (09/19/11 13:31:34)
Message:
Improve docs, error handling and refactor.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/Facebook/lib/BarnOwl/Module/Facebook.pm

    rc833c3d rbc56a0d  
    2424our $facebook_handle = undef;
    2525
    26 # did not implement class monitoring
    27 # did not implement multiple accounts
    28 
    2926BarnOwl::new_variable_bool(
    3027    'facebook:poll',
     
    3229        default => 1,
    3330        summary => 'Poll Facebook for wall updates',
    34         # TODO: Make this configurable
     31        # XXX: Make poll time configurable
    3532        description => "If set, will poll Facebook every minute for updates.\n"
    3633     }
    3734 );
    3835
    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");
     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);
     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($@); }
    4448}
    4549
    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 }
     50init();
    8551
    8652# Should also add support for posting to other people's walls (this
     
    8955BarnOwl::new_command('facebook' => \&cmd_facebook, {
    9056    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.'
    9359});
    9460
    95 # How do we allow people to specify the USER?
    9661#BarnOwl::new_command('facebook-message' => \&cmd_facebook_direct, {
    9762#    summary     => 'Send a Facebook message',
     
    10368    summary     => 'Comment on a wall post.',
    10469    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.'
    10671});
    10772
     
    11681    summary     => 'Force a poll of Facebook.',
    11782    usage       => 'facebook-poll',
    118     description => 'Get updates from Facebook.'
     83    description => 'Get updates (news, friends) from Facebook.'
    11984});
    120 
    121 # XXX: UI: probably should bug out immediately if we're not logged in.
    12285
    12386sub cmd_facebook {
     
    12588    my $user = shift;
    12689
     90    return unless check_ready();
     91
    12792    BarnOwl::start_edit_win(
    12893        defined $user ? "Write something to $user..." : "What's on your mind?",
    129         sub{ facebook($user, shift) }
     94        sub{ $facebook_handle->facebook($user, shift) }
    13095    );
    13196}
     
    135100    my $post_id = shift;
    136101
     102    return unless check_ready();
     103
    137104    my $topic = $facebook_handle->get_topic($post_id);
    138105
    139     # XXX UI should give some (better) indication /which/ conversation
    140     # is being commented on
    141106    BarnOwl::start_edit_win("Write a comment on '$topic'...",
    142107                            sub { $facebook_handle->facebook_comment($post_id, shift) });
     
    145110sub cmd_facebook_poll {
    146111    my $cmd = shift;
     112
     113    return unless check_ready();
    147114
    148115    $facebook_handle->sleep(0);
     
    154121    my $url = shift;
    155122
     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
    156128    $facebook_handle->facebook_auth($url);
    157129}
    158130
     131sub 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
    159139BarnOwl::filter(qw{facebook type ^facebook$});
    160 
    161 # Autocompletion support
    162140
    163141sub complete_user { return keys %{$facebook_handle->{friends}}; }
Note: See TracChangeset for help on using the changeset viewer.