release-1.10release-1.7release-1.8release-1.9
Last change
on this file since 104672f 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
|
Rev | Line | |
---|
[e54f2fa] | 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 | if(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 | |
---|
| 51 | open(my $fh, "<", "$ENV{HOME}/.owl/twitter") || fail("Unable to read ~/.owl/twitter"); |
---|
| 52 | my $cfg = do {local $/; <$fh>}; |
---|
| 53 | close($fh); |
---|
| 54 | eval { |
---|
| 55 | $cfg = from_json($cfg); |
---|
| 56 | }; |
---|
| 57 | if(@!) { |
---|
| 58 | fail("Unable to parse ~/.owl/twitter: @!"); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | my $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
| 62 | password => $cfg->{password}, |
---|
| 63 | clientname => 'BarnOwl'); |
---|
| 64 | |
---|
| 65 | if(!defined($twitter->verify_credentials())) { |
---|
| 66 | fail("Invalid twitter credentials found"); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | sub 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 | |
---|
| 81 | sub twitter { |
---|
| 82 | my $msg = shift; |
---|
| 83 | $twitter->update($msg); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
| 87 | |
---|
| 88 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.