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

release-1.10release-1.7release-1.8release-1.9
Last change on this file since 9601640 was e54f2fa, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
initial import
  • Property mode set to 100644
File size: 2.3 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
33if(BarnOwl::getvar('twitter:class') eq '') {
34    BarnOwl::new_variable_string('twitter:class',
35                               {
36                                   default => $class,
37                                   summary => 'Class to watch for Twitter messages'
38                                  });
39    BarnOwl::new_variable_string('twitter:instance',
40                               {
41                                   default => $instance,
42                                   summary => 'Instance on twitter:class to watch for Twitter messages'
43                                  });
44    BarnOwl::new_variable_string('twitter:opcode',
45                               {
46                                   default => $opcode,
47                                   summary => 'Opcode for zephyrs that will be sent as twitter updates'
48                                  });
49}
50
51open(my $fh, "<", "$ENV{HOME}/.owl/twitter") || fail("Unable to read ~/.owl/twitter");
52my $cfg = do {local $/; <$fh>};
53close($fh);
54eval {
55    $cfg = from_json($cfg);
56};
57if(@!) {
58    fail("Unable to parse ~/.owl/twitter: @!");
59}
60
61my $twitter  = Net::Twitter->new(username   => $cfg->{user} || $user,
62                                 password   => $cfg->{password},
63                                 clientname => 'BarnOwl');
64
65if(!defined($twitter->verify_credentials())) {
66    fail("Invalid twitter credentials found");
67}
68
69sub handle_message {
70    my $m = shift;
71    ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode);
72    if($m->sender eq $user
73       && $m->class eq $class
74       && $m->instance eq $instance
75       && $m->opcode eq $opcode
76       && $m->auth eq 'YES') {
77        twitter($m->body);
78    }
79}
80
81sub twitter {
82    my $msg = shift;
83    $twitter->update($msg);
84}
85
86$BarnOwl::Hooks::receiveMessage->add(\&handle_message);
87
881;
Note: See TracBrowser for help on using the repository browser.