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

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 69d6d3e3 was 69d6d3e3, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Use a client name of 'barnowl', which I just registered with Twitter.
  • Property mode set to 100644
File size: 3.7 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
22my $twitter;
23my $user     = BarnOwl::zephyr_getsender();
24my ($class)  = ($user =~ /(^[^@]+)/);
25my $instance = "status";
26my $opcode   = "twitter";
27
28sub fail {
29    my $msg = shift;
30    undef $twitter;
31    BarnOwl::admin_message('Twitter Error', $msg);
32    die("Twitter Error: $msg\n");
33}
34
35# Don't redefine variables if they already exist
36# This is a workaround for http://barnowl.mit.edu/trac/ticket/44
37# Which was fixed in svn r819
38if((BarnOwl::getvar('twitter:class')||'') eq '') {
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
47    BarnOwl::new_variable_string('twitter:class',
48                               {
49                                   default => $class,
50                                   summary => 'Class to watch for Twitter messages',
51                                   description => $desc
52                                  });
53    BarnOwl::new_variable_string('twitter:instance',
54                               {
55                                   default => $instance,
56                                   summary => 'Instance on twitter:class to watch for Twitter messages.',
57                                   description => $desc
58                                  });
59    BarnOwl::new_variable_string('twitter:opcode',
60                               {
61                                   default => $opcode,
62                                   summary => 'Opcode for zephyrs that will be sent as twitter updates',
63                                   description => $desc
64                                  });
65}
66
67my $conffile = BarnOwl::get_config_dir() . "/twitter";
68open(my $fh, "<", "$conffile") || fail("Unable to read $conffile");
69my $cfg = do {local $/; <$fh>};
70close($fh);
71eval {
72    $cfg = from_json($cfg);
73};
74if($@) {
75    fail("Unable to parse ~/.owl/twitter: $@");
76}
77
78$twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
79                              password   => $cfg->{password},
80                              clientname => 'barnowl');
81
82if(!defined($twitter->verify_credentials())) {
83    fail("Invalid twitter credentials");
84}
85
86sub match {
87    my $val = shift;
88    my $pat = shift;
89    return $pat eq "*" || ($val eq $pat);
90}
91
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
96       && match($m->class, $class)
97       && match($m->instance, $instance)
98       && match($m->opcode, $opcode)
99       && $m->auth eq 'YES') {
100        twitter($m->body);
101    }
102}
103
104sub twitter {
105    my $msg = shift;
106    if(defined $twitter) {
107        $twitter->update($msg);
108    }
109}
110
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
128eval {
129    $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
130};
131if($@) {
132    $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
133}
134
1351;
Note: See TracBrowser for help on using the repository browser.