source: lib/BarnOwl/Module/Twitter.pm @ d775050

release-1.10release-1.7release-1.8release-1.9
Last change on this file since d775050 was d775050, checked in by nelhage@mit.edu <nelhage@mit.edu>, 16 years ago
Fix this up slightly
  • Property mode set to 100644
File size: 2.5 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;
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
90$BarnOwl::Hooks::receiveMessage->add(\&handle_message);
91
921;
Note: See TracBrowser for help on using the repository browser.