| 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("Twitter Error: $msg\n"); |
|---|
| 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 | my $desc = <<'END_DESC'; |
|---|
| 38 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
|---|
| 39 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
|---|
| 40 | from your sender and mirror them to Twitter. |
|---|
| 41 | |
|---|
| 42 | A value of '*' in any of these fields acts a wildcard, accepting |
|---|
| 43 | messages with any value of that field. |
|---|
| 44 | END_DESC |
|---|
| 45 | BarnOwl::new_variable_string('twitter:class', |
|---|
| 46 | { |
|---|
| 47 | default => $class, |
|---|
| 48 | summary => 'Class to watch for Twitter messages', |
|---|
| 49 | description => $desc |
|---|
| 50 | }); |
|---|
| 51 | BarnOwl::new_variable_string('twitter:instance', |
|---|
| 52 | { |
|---|
| 53 | default => $instance, |
|---|
| 54 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
|---|
| 55 | description => $desc |
|---|
| 56 | }); |
|---|
| 57 | BarnOwl::new_variable_string('twitter:opcode', |
|---|
| 58 | { |
|---|
| 59 | default => $opcode, |
|---|
| 60 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
|---|
| 61 | description => $desc |
|---|
| 62 | }); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
|---|
| 66 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
|---|
| 67 | my $cfg = do {local $/; <$fh>}; |
|---|
| 68 | close($fh); |
|---|
| 69 | eval { |
|---|
| 70 | $cfg = from_json($cfg); |
|---|
| 71 | }; |
|---|
| 72 | if(@!) { |
|---|
| 73 | fail("Unable to parse ~/.owl/twitter: @!"); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | my $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
|---|
| 77 | password => $cfg->{password}, |
|---|
| 78 | clientname => 'BarnOwl'); |
|---|
| 79 | |
|---|
| 80 | if(!defined($twitter->verify_credentials())) { |
|---|
| 81 | fail("Invalid twitter credentials"); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | sub match { |
|---|
| 85 | my $val = shift; |
|---|
| 86 | my $pat = shift; |
|---|
| 87 | return $pat eq "*" || ($val eq $pat); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | sub handle_message { |
|---|
| 91 | my $m = shift; |
|---|
| 92 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
|---|
| 93 | if($m->sender eq $user |
|---|
| 94 | && match($m->class, $class) |
|---|
| 95 | && match($m->instance, $instance) |
|---|
| 96 | && match($m->opcode, $opcode) |
|---|
| 97 | && $m->auth eq 'YES') { |
|---|
| 98 | twitter($m->body); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | sub twitter { |
|---|
| 103 | my $msg = shift; |
|---|
| 104 | $twitter->update($msg); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
|---|
| 108 | summary => 'Update Twitter from BarnOwl', |
|---|
| 109 | usage => 'twitter [message]', |
|---|
| 110 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
|---|
| 111 | . "\nOtherwise, prompt for a status message to use." |
|---|
| 112 | }); |
|---|
| 113 | |
|---|
| 114 | sub cmd_twitter { |
|---|
| 115 | my $cmd = shift; |
|---|
| 116 | if(@_) { |
|---|
| 117 | my $status = join(" ", @_); |
|---|
| 118 | twitter($status); |
|---|
| 119 | } else { |
|---|
| 120 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
|---|
| 125 | |
|---|
| 126 | 1; |
|---|