[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; |
---|
[8618438] | 21 | use BarnOwl::Message::Twitter; |
---|
| 22 | use HTML::Entities; |
---|
[e54f2fa] | 23 | |
---|
[9bedca0] | 24 | my $twitter; |
---|
| 25 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 26 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 27 | my $instance = "status"; |
---|
| 28 | my $opcode = "twitter"; |
---|
| 29 | |
---|
[e54f2fa] | 30 | sub fail { |
---|
| 31 | my $msg = shift; |
---|
[9bedca0] | 32 | undef $twitter; |
---|
[e54f2fa] | 33 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
[4cf4067] | 34 | die("Twitter Error: $msg\n"); |
---|
[e54f2fa] | 35 | } |
---|
| 36 | |
---|
[d775050] | 37 | # Don't redefine variables if they already exist |
---|
[4cf4067] | 38 | # This is a workaround for http://barnowl.mit.edu/trac/ticket/44 |
---|
[d775050] | 39 | # Which was fixed in svn r819 |
---|
[30f0680] | 40 | if((BarnOwl::getvar('twitter:class')||'') eq '') { |
---|
[d1bb4f3] | 41 | my $desc = <<'END_DESC'; |
---|
| 42 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
| 43 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
| 44 | from your sender and mirror them to Twitter. |
---|
| 45 | |
---|
| 46 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
| 47 | messages with any value of that field. |
---|
| 48 | END_DESC |
---|
[e54f2fa] | 49 | BarnOwl::new_variable_string('twitter:class', |
---|
| 50 | { |
---|
| 51 | default => $class, |
---|
[d1bb4f3] | 52 | summary => 'Class to watch for Twitter messages', |
---|
| 53 | description => $desc |
---|
[e54f2fa] | 54 | }); |
---|
| 55 | BarnOwl::new_variable_string('twitter:instance', |
---|
| 56 | { |
---|
| 57 | default => $instance, |
---|
[d1bb4f3] | 58 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
| 59 | description => $desc |
---|
[e54f2fa] | 60 | }); |
---|
| 61 | BarnOwl::new_variable_string('twitter:opcode', |
---|
| 62 | { |
---|
| 63 | default => $opcode, |
---|
[d1bb4f3] | 64 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
| 65 | description => $desc |
---|
[e54f2fa] | 66 | }); |
---|
| 67 | } |
---|
| 68 | |
---|
[d775050] | 69 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 70 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[e54f2fa] | 71 | my $cfg = do {local $/; <$fh>}; |
---|
| 72 | close($fh); |
---|
| 73 | eval { |
---|
| 74 | $cfg = from_json($cfg); |
---|
| 75 | }; |
---|
[9bedca0] | 76 | if($@) { |
---|
| 77 | fail("Unable to parse ~/.owl/twitter: $@"); |
---|
[e54f2fa] | 78 | } |
---|
| 79 | |
---|
[9bedca0] | 80 | $twitter = Net::Twitter->new(username => $cfg->{user} || $user, |
---|
| 81 | password => $cfg->{password}, |
---|
[104f7d9] | 82 | source => 'barnowl'); |
---|
[e54f2fa] | 83 | |
---|
[51a7fc5] | 84 | eval { |
---|
| 85 | $twitter->{ua}->timeout(1); |
---|
| 86 | }; |
---|
| 87 | |
---|
[e54f2fa] | 88 | if(!defined($twitter->verify_credentials())) { |
---|
[d775050] | 89 | fail("Invalid twitter credentials"); |
---|
[e54f2fa] | 90 | } |
---|
| 91 | |
---|
[d1bb4f3] | 92 | sub match { |
---|
| 93 | my $val = shift; |
---|
| 94 | my $pat = shift; |
---|
| 95 | return $pat eq "*" || ($val eq $pat); |
---|
| 96 | } |
---|
| 97 | |
---|
[e54f2fa] | 98 | sub handle_message { |
---|
| 99 | my $m = shift; |
---|
| 100 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
| 101 | if($m->sender eq $user |
---|
[d1bb4f3] | 102 | && match($m->class, $class) |
---|
| 103 | && match($m->instance, $instance) |
---|
| 104 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 105 | && $m->auth eq 'YES') { |
---|
| 106 | twitter($m->body); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[8618438] | 110 | my $last_poll = 0; |
---|
| 111 | my $last_id = undef; |
---|
| 112 | unless(defined($last_id)) { |
---|
| 113 | $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | sub poll_messages { |
---|
| 117 | return unless ( time - $last_poll ) >= 45; |
---|
| 118 | $last_poll = time; |
---|
| 119 | my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); |
---|
| 120 | unless(defined($timeline)) { |
---|
| 121 | BarnOwl::error("Twitter returned error ... rate-limited?"); |
---|
| 122 | # Sleep for 15 minutes |
---|
| 123 | $last_poll = time + 60*15; |
---|
| 124 | return; |
---|
| 125 | }; |
---|
| 126 | if ( scalar @$timeline ) { |
---|
| 127 | for my $tweet ( reverse @$timeline ) { |
---|
| 128 | if ( $tweet->{id} <= $last_id ) { |
---|
| 129 | next; |
---|
| 130 | } |
---|
| 131 | my $msg = BarnOwl::Message->new( |
---|
| 132 | type => 'Twitter', |
---|
| 133 | sender => $tweet->{user}{screen_name}, |
---|
| 134 | recipient => $cfg->{user} || $user, |
---|
| 135 | direction => 'in', |
---|
| 136 | source => decode_entities($tweet->{source}), |
---|
[5aabe2d7] | 137 | location => decode_entities($tweet->{user}{location}) || "", |
---|
[8618438] | 138 | body => decode_entities($tweet->{text}) |
---|
| 139 | ); |
---|
| 140 | BarnOwl::queue_message($msg); |
---|
| 141 | } |
---|
| 142 | $last_id = $timeline->[0]{id}; |
---|
| 143 | } else { |
---|
| 144 | # BarnOwl::message("No new tweets..."); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[e54f2fa] | 148 | sub twitter { |
---|
| 149 | my $msg = shift; |
---|
[9bedca0] | 150 | if(defined $twitter) { |
---|
| 151 | $twitter->update($msg); |
---|
| 152 | } |
---|
[e54f2fa] | 153 | } |
---|
| 154 | |
---|
[4cf4067] | 155 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 156 | summary => 'Update Twitter from BarnOwl', |
---|
| 157 | usage => 'twitter [message]', |
---|
| 158 | description => 'Update Twitter. If MESSAGE is provided, use it as your status.' |
---|
| 159 | . "\nOtherwise, prompt for a status message to use." |
---|
| 160 | }); |
---|
| 161 | |
---|
| 162 | sub cmd_twitter { |
---|
| 163 | my $cmd = shift; |
---|
| 164 | if(@_) { |
---|
| 165 | my $status = join(" ", @_); |
---|
| 166 | twitter($status); |
---|
| 167 | } else { |
---|
| 168 | BarnOwl::start_edit_win('What are you doing?', \&twitter); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[72b61dd] | 172 | eval { |
---|
| 173 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 174 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 175 | }; |
---|
| 176 | if($@) { |
---|
| 177 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 178 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 179 | } |
---|
[e54f2fa] | 180 | |
---|
[5aabe2d7] | 181 | BarnOwl::filter('twitter type ^twitter$'); |
---|
[8618438] | 182 | |
---|
[e54f2fa] | 183 | 1; |
---|