[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 | |
---|
[9bedca0] | 22 | my $twitter; |
---|
| 23 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 24 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 25 | my $instance = "status"; |
---|
| 26 | my $opcode = "twitter"; |
---|
| 27 | |
---|
[e54f2fa] | 28 | sub fail { |
---|
| 29 | my $msg = shift; |
---|
[9bedca0] | 30 | undef $twitter; |
---|
[e54f2fa] | 31 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
[4cf4067] | 32 | die("Twitter Error: $msg\n"); |
---|
[e54f2fa] | 33 | } |
---|
| 34 | |
---|
[d775050] | 35 | # Don't redefine variables if they already exist |
---|
[4cf4067] | 36 | # This is a workaround for http://barnowl.mit.edu/trac/ticket/44 |
---|
[d775050] | 37 | # Which was fixed in svn r819 |
---|
[30f0680] | 38 | if((BarnOwl::getvar('twitter:class')||'') eq '') { |
---|
[d1bb4f3] | 39 | my $desc = <<'END_DESC'; |
---|
| 40 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
| 41 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
| 42 | from your sender and mirror them to Twitter. |
---|
| 43 | |
---|
| 44 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
| 45 | messages with any value of that field. |
---|
| 46 | END_DESC |
---|
[e54f2fa] | 47 | BarnOwl::new_variable_string('twitter:class', |
---|
| 48 | { |
---|
| 49 | default => $class, |
---|
[d1bb4f3] | 50 | summary => 'Class to watch for Twitter messages', |
---|
| 51 | description => $desc |
---|
[e54f2fa] | 52 | }); |
---|
| 53 | BarnOwl::new_variable_string('twitter:instance', |
---|
| 54 | { |
---|
| 55 | default => $instance, |
---|
[d1bb4f3] | 56 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
| 57 | description => $desc |
---|
[e54f2fa] | 58 | }); |
---|
| 59 | BarnOwl::new_variable_string('twitter:opcode', |
---|
| 60 | { |
---|
| 61 | default => $opcode, |
---|
[d1bb4f3] | 62 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
| 63 | description => $desc |
---|
[e54f2fa] | 64 | }); |
---|
| 65 | } |
---|
| 66 | |
---|
[d775050] | 67 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 68 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[e54f2fa] | 69 | my $cfg = do {local $/; <$fh>}; |
---|
| 70 | close($fh); |
---|
| 71 | eval { |
---|
| 72 | $cfg = from_json($cfg); |
---|
| 73 | }; |
---|
[9bedca0] | 74 | if($@) { |
---|
| 75 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
[e54f2fa] | 76 | } |
---|
| 77 | |
---|
[9bedca0] | 78 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
| 79 | password => $cfg->{password}, |
---|
[104f7d9] | 80 | source => 'barnowl'); |
---|
[e54f2fa] | 81 | |
---|
| 82 | if(!defined($twitter->verify_credentials())) { |
---|
[d775050] | 83 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 84 | } |
---|
| 85 | |
---|
[d1bb4f3] | 86 | sub match { |
---|
| 87 | my $val = shift; |
---|
| 88 | my $pat = shift; |
---|
| 89 | return $pat eq "*" || ($val eq $pat); |
---|
| 90 | } |
---|
| 91 | |
---|
[e54f2fa] | 92 | sub handle_message { |
---|
| 93 | my $m = shift; |
---|
| 94 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
| 95 | if($m->sender eq $user |
---|
[d1bb4f3] | 96 | && match($m->class, $class) |
---|
| 97 | && match($m->instance, $instance) |
---|
| 98 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 99 | && $m->auth eq 'YES') { |
---|
| 100 | twitter($m->body); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | sub twitter { |
---|
| 105 | my $msg = shift; |
---|
[9bedca0] | 106 | if(defined $twitter) { |
---|
| 107 | $twitter->update($msg); |
---|
| 108 | } |
---|
[e54f2fa] | 109 | } |
---|
| 110 | |
---|
[4cf4067] | 111 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 112 | summary => 'Update Twitter from BarnOwl', |
---|
| 113 | usage => 'twitter [message]', |
---|
| 114 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
| 115 | . "\nOtherwise, prompt for a status message to use." |
---|
| 116 | }); |
---|
| 117 | |
---|
| 118 | sub cmd_twitter { |
---|
| 119 | my $cmd = shift; |
---|
| 120 | if(@_) { |
---|
| 121 | my $status = join(" ", @_); |
---|
| 122 | twitter($status); |
---|
| 123 | } else { |
---|
| 124 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
[72b61dd] | 128 | eval { |
---|
| 129 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
| 130 | }; |
---|
| 131 | if($@) { |
---|
| 132 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
| 133 | } |
---|
[e54f2fa] | 134 | |
---|
| 135 | 1; |
---|