source: lib/BarnOwl/Module/Twitter.pm @ 4cf4067

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 4cf4067 was 4cf4067, checked in by nelhage@mit.edu <nelhage@mit.edu>, 16 years ago
Add a `twitter' command
  • Property mode set to 100644
File size: 3.0 KB
Line 
1use warnings;
2use strict;
3
4=head1 NAME
5
6BarnOwl::Module::Twitter
7
8=head1 DESCRIPTION
9
10Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter
11
12=cut
13
14package BarnOwl::Module::Twitter;
15
16use Net::Twitter;
17use JSON;
18
19use BarnOwl;
20use BarnOwl::Hooks;
21
22sub fail {
23    my $msg = shift;
24    BarnOwl::admin_message('Twitter Error', $msg);
25    die("Twitter Error: $msg\n");
26}
27
28my $user     = BarnOwl::zephyr_getsender();
29my ($class)  = ($user =~ /(^[^@]+)/);
30my $instance = "status";
31my $opcode   = "twitter";
32
33# Don't redefine variables if they already exist
34# This is a workaround for http://barnowl.mit.edu/trac/ticket/44
35# Which was fixed in svn r819
36if(BarnOwl::getvar('twitter:class') eq '') {
37    BarnOwl::new_variable_string('twitter:class',
38                               {
39                                   default => $class,
40                                   summary => 'Class to watch for Twitter messages'
41                                  });
42    BarnOwl::new_variable_string('twitter:instance',
43                               {
44                                   default => $instance,
45                                   summary => 'Instance on twitter:class to watch for Twitter messages'
46                                  });
47    BarnOwl::new_variable_string('twitter:opcode',
48                               {
49                                   default => $opcode,
50                                   summary => 'Opcode for zephyrs that will be sent as twitter updates'
51                                  });
52}
53
54my $conffile = BarnOwl::get_config_dir() . "/twitter";
55open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
56my $cfg = do {local $/; <$fh>};
57close($fh);
58eval {
59    $cfg = from_json($cfg);
60};
61if(@!) {
62    fail("Unable to parse ~/.owl/twitter: @!");
63}
64
65my $twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
66                                 password   => $cfg->{password},
67                                 clientname => 'BarnOwl');
68
69if(!defined($twitter->verify_credentials())) {
70    fail("Invalid twitter credentials");
71}
72
73sub handle_message {
74    my $m = shift;
75    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
76    if($m->sender eq $user
77       && $m->class eq $class
78       && $m->instance eq $instance
79       && $m->opcode eq $opcode
80       && $m->auth eq 'YES') {
81        twitter($m->body);
82    }
83}
84
85sub twitter {
86    my $msg = shift;
87    $twitter->update($msg);
88}
89
90BarnOwl::new_command(twitter => \&cmd_twitter, {
91    summary     => 'Update Twitter from BarnOwl',
92    usage       => 'twitter [message]',
93    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
94    . "\nOtherwise, prompt for a status message to use."
95   });
96
97sub cmd_twitter {
98    my $cmd = shift;
99    if(@_) {
100        my $status = join(" ", @_);
101        twitter($status);
102    } else {
103      BarnOwl::start_edit_win('What are you doing?', \&twitter);
104    }
105}
106
107$BarnOwl::Hooks::receiveMessage->add(\&handle_message);
108
1091;
Note: See TracBrowser for help on using the repository browser.