1 | use warnings; |
---|
2 | use strict; |
---|
3 | |
---|
4 | =head1 NAME |
---|
5 | |
---|
6 | BarnOwl::Module::Twitter |
---|
7 | |
---|
8 | =head1 DESCRIPTION |
---|
9 | |
---|
10 | Post outgoing zephyrs from -c $USER -i status -O TWITTER to Twitter |
---|
11 | |
---|
12 | =cut |
---|
13 | |
---|
14 | package BarnOwl::Module::Twitter; |
---|
15 | |
---|
16 | use Net::Twitter; |
---|
17 | use JSON; |
---|
18 | |
---|
19 | use BarnOwl; |
---|
20 | use BarnOwl::Hooks; |
---|
21 | |
---|
22 | sub fail { |
---|
23 | my $msg = shift; |
---|
24 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
25 | die; |
---|
26 | } |
---|
27 | |
---|
28 | my $user = BarnOwl::zephyr_getsender(); |
---|
29 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
30 | my $instance = "status"; |
---|
31 | my $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 |
---|
36 | if(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 | |
---|
54 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
55 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
56 | my $cfg = do {local $/; <$fh>}; |
---|
57 | close($fh); |
---|
58 | eval { |
---|
59 | $cfg = from_json($cfg); |
---|
60 | }; |
---|
61 | if(@!) { |
---|
62 | fail("Unable to parse ~/.owl/twitter: @!"); |
---|
63 | } |
---|
64 | |
---|
65 | my $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
66 | password => $cfg->{password}, |
---|
67 | clientname => 'BarnOwl'); |
---|
68 | |
---|
69 | if(!defined($twitter->verify_credentials())) { |
---|
70 | fail("Invalid twitter credentials"); |
---|
71 | } |
---|
72 | |
---|
73 | sub 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 | |
---|
85 | sub twitter { |
---|
86 | my $msg = shift; |
---|
87 | $twitter->update($msg); |
---|
88 | } |
---|
89 | |
---|
90 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
91 | |
---|
92 | 1; |
---|