source: lib/BarnOwl/Module/Twitter.pm @ 69d6d3e3

release-1.7release-1.8release-1.9
Last change on this file since 69d6d3e3 was 69d6d3e3, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
Use a client name of 'barnowl', which I just registered with Twitter.
  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[e54f2fa]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
[9bedca0]22my $twitter;
23my $user     = BarnOwl::zephyr_getsender();
24my ($class)  = ($user =~ /(^[^@]+)/);
25my $instance = "status";
26my $opcode   = "twitter";
27
[e54f2fa]28sub fail {
29    my $msg = shift;
[9bedca0]30    undef $twitter;
[e54f2fa]31    BarnOwl::admin_message('Twitter Error', $msg);
[4cf4067]32    die("Twitter Error: $msg\n");
[e54f2fa]33}
34
[d775050]35# Don't redefine variables if they already exist
[4cf4067]36# This is a workaround for http://barnowl.mit.edu/trac/ticket/44
[d775050]37# Which was fixed in svn r819
[30f0680]38if((BarnOwl::getvar('twitter:class')||'') eq '') {
[d1bb4f3]39    my $desc = <<'END_DESC';
40BarnOwl::Module::Twitter will watch for authentic zephyrs to
41-c $twitter:class -i $twitter:instance -O $twitter:opcode
42from your sender and mirror them to Twitter.
43
44A value of '*' in any of these fields acts a wildcard, accepting
45messages with any value of that field.
46END_DESC
[e54f2fa]47    BarnOwl::new_variable_string('twitter:class',
48                               {
49                                   default => $class,
[d1bb4f3]50                                   summary => 'Class to watch for Twitter messages',
51                                   description => $desc
[e54f2fa]52                                  });
53    BarnOwl::new_variable_string('twitter:instance',
54                               {
55                                   default => $instance,
[d1bb4f3]56                                   summary => 'Instance on twitter:class to watch for Twitter messages.',
57                                   description => $desc
[e54f2fa]58                                  });
59    BarnOwl::new_variable_string('twitter:opcode',
60                               {
61                                   default => $opcode,
[d1bb4f3]62                                   summary => 'Opcode for zephyrs that will be sent as twitter updates',
63                                   description => $desc
[e54f2fa]64                                  });
65}
66
[d775050]67my $conffile = BarnOwl::get_config_dir() . "/twitter";
68open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
[e54f2fa]69my $cfg = do {local $/; <$fh>};
70close($fh);
71eval {
72    $cfg = from_json($cfg);
73};
[9bedca0]74if($@) {
75    fail("Unable to parse ~/.owl/twitter: $@");
[e54f2fa]76}
77
[9bedca0]78$twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
79                              password   => $cfg->{password},
[69d6d3e3]80                              clientname => 'barnowl');
[e54f2fa]81
82if(!defined($twitter->verify_credentials())) {
[d775050]83    fail("Invalid twitter credentials");
[e54f2fa]84}
85
[d1bb4f3]86sub match {
87    my $val = shift;
88    my $pat = shift;
89    return $pat eq "*" || ($val eq $pat);
90}
91
[e54f2fa]92sub handle_message {
93    my $m = shift;
94    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
95    if($m->sender eq $user
[d1bb4f3]96       && match($m->class, $class)
97       && match($m->instance, $instance)
98       && match($m->opcode, $opcode)
[e54f2fa]99       && $m->auth eq 'YES') {
100        twitter($m->body);
101    }
102}
103
104sub twitter {
105    my $msg = shift;
[9bedca0]106    if(defined $twitter) {
107        $twitter->update($msg);
108    }
[e54f2fa]109}
110
[4cf4067]111BarnOwl::new_command(twitter => \&cmd_twitter, {
112    summary     => 'Update Twitter from BarnOwl',
113    usage       => 'twitter [message]',
114    description => 'Update Twitter. If MESSAGE is provided, use it as your status.'
115    . "\nOtherwise, prompt for a status message to use."
116   });
117
118sub cmd_twitter {
119    my $cmd = shift;
120    if(@_) {
121        my $status = join(" ", @_);
122        twitter($status);
123    } else {
124      BarnOwl::start_edit_win('What are you doing?', \&twitter);
125    }
126}
127
[72b61dd]128eval {
129    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
130};
131if($@) {
132    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
133}
[e54f2fa]134
1351;
Note: See TracBrowser for help on using the repository browser.