[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); |
---|
[4cf4067] | 25 | die("Twitter Error: $msg\n"); |
---|
[e54f2fa] | 26 | } |
---|
| 27 | |
---|
| 28 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 29 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 30 | my $instance = "status"; |
---|
| 31 | my $opcode = "twitter"; |
---|
| 32 | |
---|
[d775050] | 33 | # Don't redefine variables if they already exist |
---|
[4cf4067] | 34 | # This is a workaround for http://barnowl.mit.edu/trac/ticket/44 |
---|
[d775050] | 35 | # Which was fixed in svn r819 |
---|
[30f0680] | 36 | if((BarnOwl::getvar('twitter:class')||'') eq '') { |
---|
[d1bb4f3] | 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 |
---|
[e54f2fa] | 45 | BarnOwl::new_variable_string('twitter:class', |
---|
| 46 | { |
---|
| 47 | default => $class, |
---|
[d1bb4f3] | 48 | summary => 'Class to watch for Twitter messages', |
---|
| 49 | description => $desc |
---|
[e54f2fa] | 50 | }); |
---|
| 51 | BarnOwl::new_variable_string('twitter:instance', |
---|
| 52 | { |
---|
| 53 | default => $instance, |
---|
[d1bb4f3] | 54 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
| 55 | description => $desc |
---|
[e54f2fa] | 56 | }); |
---|
| 57 | BarnOwl::new_variable_string('twitter:opcode', |
---|
| 58 | { |
---|
| 59 | default => $opcode, |
---|
[d1bb4f3] | 60 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
| 61 | description => $desc |
---|
[e54f2fa] | 62 | }); |
---|
| 63 | } |
---|
| 64 | |
---|
[d775050] | 65 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 66 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[e54f2fa] | 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())) { |
---|
[d775050] | 81 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 82 | } |
---|
| 83 | |
---|
[d1bb4f3] | 84 | sub match { |
---|
| 85 | my $val = shift; |
---|
| 86 | my $pat = shift; |
---|
| 87 | return $pat eq "*" || ($val eq $pat); |
---|
| 88 | } |
---|
| 89 | |
---|
[e54f2fa] | 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 |
---|
[d1bb4f3] | 94 | && match($m->class, $class) |
---|
| 95 | && match($m->instance, $instance) |
---|
| 96 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 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 | |
---|
[4cf4067] | 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 | |
---|
[e54f2fa] | 124 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
| 125 | |
---|
| 126 | 1; |
---|