[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 | |
---|
[159aaad] | 16 | our $VERSION = 0.2; |
---|
[af3415c] | 17 | |
---|
[e54f2fa] | 18 | use Net::Twitter; |
---|
| 19 | use JSON; |
---|
| 20 | |
---|
| 21 | use BarnOwl; |
---|
| 22 | use BarnOwl::Hooks; |
---|
[159aaad] | 23 | use BarnOwl::Module::Twitter::Handle; |
---|
[e54f2fa] | 24 | |
---|
[d748296] | 25 | our @twitter_handles = (); |
---|
| 26 | our $default_handle = undef; |
---|
[9bedca0] | 27 | my $user = BarnOwl::zephyr_getsender(); |
---|
| 28 | my ($class) = ($user =~ /(^[^@]+)/); |
---|
| 29 | my $instance = "status"; |
---|
| 30 | my $opcode = "twitter"; |
---|
[d658c29] | 31 | my $use_reply_to = 0; |
---|
[159aaad] | 32 | my $next_service_to_poll = 0; |
---|
[188b745] | 33 | |
---|
[d689fc7] | 34 | my $desc = <<'END_DESC'; |
---|
[d1bb4f3] | 35 | BarnOwl::Module::Twitter will watch for authentic zephyrs to |
---|
| 36 | -c $twitter:class -i $twitter:instance -O $twitter:opcode |
---|
| 37 | from your sender and mirror them to Twitter. |
---|
| 38 | |
---|
| 39 | A value of '*' in any of these fields acts a wildcard, accepting |
---|
| 40 | messages with any value of that field. |
---|
| 41 | END_DESC |
---|
[d689fc7] | 42 | BarnOwl::new_variable_string( |
---|
| 43 | 'twitter:class', |
---|
| 44 | { |
---|
| 45 | default => $class, |
---|
| 46 | summary => 'Class to watch for Twitter messages', |
---|
| 47 | description => $desc |
---|
| 48 | } |
---|
| 49 | ); |
---|
| 50 | BarnOwl::new_variable_string( |
---|
| 51 | 'twitter:instance', |
---|
| 52 | { |
---|
| 53 | default => $instance, |
---|
| 54 | summary => 'Instance on twitter:class to watch for Twitter messages.', |
---|
| 55 | description => $desc |
---|
| 56 | } |
---|
| 57 | ); |
---|
| 58 | BarnOwl::new_variable_string( |
---|
| 59 | 'twitter:opcode', |
---|
| 60 | { |
---|
| 61 | default => $opcode, |
---|
| 62 | summary => 'Opcode for zephyrs that will be sent as twitter updates', |
---|
| 63 | description => $desc |
---|
| 64 | } |
---|
| 65 | ); |
---|
[e54f2fa] | 66 | |
---|
[927c186] | 67 | BarnOwl::new_variable_bool( |
---|
| 68 | 'twitter:poll', |
---|
| 69 | { |
---|
| 70 | default => 1, |
---|
| 71 | summary => 'Poll Twitter for incoming messages', |
---|
| 72 | description => "If set, will poll Twitter every minute for normal updates,\n" |
---|
| 73 | . 'and every two minutes for direct message' |
---|
| 74 | } |
---|
| 75 | ); |
---|
| 76 | |
---|
[159aaad] | 77 | sub fail { |
---|
| 78 | my $msg = shift; |
---|
| 79 | undef @twitter_handles; |
---|
| 80 | BarnOwl::admin_message('Twitter Error', $msg); |
---|
| 81 | die("Twitter Error: $msg\n"); |
---|
| 82 | } |
---|
| 83 | |
---|
[d775050] | 84 | my $conffile = BarnOwl::get_config_dir() . "/twitter"; |
---|
| 85 | open(my $fh, "<", "$conffile") || fail("Unable to read $conffile"); |
---|
[159aaad] | 86 | my $raw_cfg = do {local $/; <$fh>}; |
---|
[e54f2fa] | 87 | close($fh); |
---|
| 88 | eval { |
---|
[159aaad] | 89 | $raw_cfg = from_json($raw_cfg); |
---|
[e54f2fa] | 90 | }; |
---|
[9bedca0] | 91 | if($@) { |
---|
[159aaad] | 92 | fail("Unable to parse $conffile: $@"); |
---|
[e54f2fa] | 93 | } |
---|
| 94 | |
---|
[159aaad] | 95 | $raw_cfg = [$raw_cfg] unless UNIVERSAL::isa $raw_cfg, "ARRAY"; |
---|
| 96 | |
---|
| 97 | for my $cfg (@$raw_cfg) { |
---|
| 98 | my $twitter_args = { username => $cfg->{user} || $user, |
---|
| 99 | password => $cfg->{password}, |
---|
| 100 | source => 'barnowl', |
---|
| 101 | }; |
---|
| 102 | if (defined $cfg->{service}) { |
---|
| 103 | my $service = $cfg->{service}; |
---|
| 104 | $twitter_args->{apiurl} = $service; |
---|
| 105 | my $apihost = $service; |
---|
| 106 | $apihost =~ s/^\s*http:\/\///; |
---|
| 107 | $apihost =~ s/\/.*$//; |
---|
| 108 | $apihost .= ':80' unless $apihost =~ /:\d+$/; |
---|
| 109 | $twitter_args->{apihost} = $cfg->{apihost} || $apihost; |
---|
| 110 | my $apirealm = "Laconica API"; |
---|
| 111 | $twitter_args->{apirealm} = $cfg->{apirealm} || $apirealm; |
---|
| 112 | } else { |
---|
| 113 | $cfg->{service} = 'http://twitter.com'; |
---|
| 114 | } |
---|
[b56f2c3] | 115 | |
---|
[39dd366] | 116 | my $twitter_handle; |
---|
[d748296] | 117 | eval { |
---|
[39dd366] | 118 | $twitter_handle = BarnOwl::Module::Twitter::Handle->new($cfg, %$twitter_args); |
---|
[d748296] | 119 | }; |
---|
[39dd366] | 120 | if ($@) { |
---|
| 121 | BarnOwl::error($@); |
---|
| 122 | next; |
---|
| 123 | } |
---|
| 124 | push @twitter_handles, $twitter_handle; |
---|
| 125 | $default_handle = $twitter_handle if (!defined $twitter_handle && exists $cfg->{default_sender} && $cfg->{default_sender}); |
---|
[b56f2c3] | 126 | } |
---|
| 127 | |
---|
[d1bb4f3] | 128 | sub match { |
---|
| 129 | my $val = shift; |
---|
| 130 | my $pat = shift; |
---|
| 131 | return $pat eq "*" || ($val eq $pat); |
---|
| 132 | } |
---|
| 133 | |
---|
[e54f2fa] | 134 | sub handle_message { |
---|
| 135 | my $m = shift; |
---|
| 136 | ($class, $instance, $opcode) = map{BarnOwl::getvar("twitter:$_")} qw(class instance opcode); |
---|
| 137 | if($m->sender eq $user |
---|
[d1bb4f3] | 138 | && match($m->class, $class) |
---|
| 139 | && match($m->instance, $instance) |
---|
| 140 | && match($m->opcode, $opcode) |
---|
[e54f2fa] | 141 | && $m->auth eq 'YES') { |
---|
[159aaad] | 142 | for my $handle (@twitter_handles) { |
---|
[1c45137] | 143 | $handle->twitter($m->body) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets}); |
---|
[159aaad] | 144 | } |
---|
[e54f2fa] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[8618438] | 148 | sub poll_messages { |
---|
[d748296] | 149 | return unless @twitter_handles; |
---|
| 150 | |
---|
[159aaad] | 151 | my $handle = $twitter_handles[$next_service_to_poll]; |
---|
| 152 | $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles); |
---|
| 153 | |
---|
| 154 | $handle->poll_twitter() if (!exists $handle->{cfg}->{poll_for_tweets} || $handle->{cfg}->{poll_for_tweets}); |
---|
| 155 | $handle->poll_direct() if (!exists $handle->{cfg}->{poll_for_dms} || $handle->{cfg}->{poll_for_dms}); |
---|
[927c186] | 156 | } |
---|
| 157 | |
---|
[159aaad] | 158 | sub twitter { |
---|
| 159 | my $account = shift; |
---|
| 160 | |
---|
| 161 | my $sent = 0; |
---|
| 162 | if (defined $account) { |
---|
| 163 | for my $handle (@twitter_handles) { |
---|
| 164 | if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) { |
---|
| 165 | $handle->twitter(@_); |
---|
| 166 | $sent = 1; |
---|
| 167 | last; |
---|
[8618438] | 168 | } |
---|
| 169 | } |
---|
[159aaad] | 170 | BarnOwl::message("No Twitter account named " . $account) unless $sent == 1 |
---|
| 171 | } |
---|
| 172 | else { |
---|
| 173 | # broadcast |
---|
| 174 | for my $handle (@twitter_handles) { |
---|
| 175 | $handle->twitter(@_) if (!exists $handle->{cfg}->{publish_tweets} || $handle->{cfg}->{publish_tweets}); |
---|
| 176 | } |
---|
[8618438] | 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[159aaad] | 180 | sub twitter_direct { |
---|
| 181 | my $account = shift; |
---|
| 182 | |
---|
| 183 | my $sent = 0; |
---|
| 184 | if (defined $account) { |
---|
| 185 | for my $handle (@twitter_handles) { |
---|
| 186 | if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) { |
---|
| 187 | $handle->twitter_direct(@_); |
---|
| 188 | $sent = 1; |
---|
| 189 | last; |
---|
[927c186] | 190 | } |
---|
| 191 | } |
---|
[159aaad] | 192 | BarnOwl::message("No Twitter account named " . $account) unless $sent == 1 |
---|
[927c186] | 193 | } |
---|
[159aaad] | 194 | elsif (defined $default_handle) { |
---|
| 195 | $default_handle->twitter_direct(@_); |
---|
[9bedca0] | 196 | } |
---|
[159aaad] | 197 | else { |
---|
| 198 | $twitter_handles[0]->twitter_direct(@_); |
---|
[927c186] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[6babb75] | 202 | sub twitter_atreply { |
---|
[159aaad] | 203 | my $account = shift; |
---|
| 204 | |
---|
| 205 | my $sent = 0; |
---|
| 206 | if (defined $account) { |
---|
| 207 | for my $handle (@twitter_handles) { |
---|
| 208 | if (defined $handle->{cfg}->{account_nickname} && $account eq $handle->{cfg}->{account_nickname}) { |
---|
| 209 | $handle->twitter_atreply(@_); |
---|
| 210 | $sent = 1; |
---|
| 211 | last; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | BarnOwl::message("No Twitter account named " . $account) unless $sent == 1 |
---|
| 215 | } |
---|
| 216 | elsif (defined $default_handle) { |
---|
| 217 | $default_handle->twitter_atreply(@_); |
---|
| 218 | } |
---|
| 219 | else { |
---|
| 220 | $twitter_handles[0]->twitter_atreply(@_); |
---|
[acdd52e] | 221 | } |
---|
[6babb75] | 222 | } |
---|
| 223 | |
---|
[4cf4067] | 224 | BarnOwl::new_command(twitter => \&cmd_twitter, { |
---|
| 225 | summary => 'Update Twitter from BarnOwl', |
---|
[159aaad] | 226 | usage => 'twitter [ACCOUNT] [MESSAGE]', |
---|
| 227 | description => 'Update Twitter on ACCOUNT. If MESSAGE is provided, use it as your status.' |
---|
| 228 | . "\nIf no ACCOUNT is provided, update all services which have publishing enabled." |
---|
[4cf4067] | 229 | . "\nOtherwise, prompt for a status message to use." |
---|
| 230 | }); |
---|
| 231 | |
---|
[927c186] | 232 | BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { |
---|
| 233 | summary => 'Send a Twitter direct message', |
---|
[159aaad] | 234 | usage => 'twitter-direct USER [ACCOUNT]', |
---|
| 235 | description => 'Send a Twitter Direct Message to USER on ACCOUNT (defaults to default_sender,' |
---|
| 236 | . "\nor first service if no default is provided)" |
---|
[927c186] | 237 | }); |
---|
| 238 | |
---|
[6babb75] | 239 | BarnOwl::new_command( 'twitter-atreply' => sub { cmd_twitter_atreply(@_); }, |
---|
| 240 | { |
---|
| 241 | summary => 'Send a Twitter @ message', |
---|
[159aaad] | 242 | usage => 'twitter-atreply USER [ACCOUNT]', |
---|
| 243 | description => 'Send a Twitter @reply Message to USER on ACCOUNT (defaults to default_sender,' |
---|
| 244 | . "or first service if no default is provided)" |
---|
[6babb75] | 245 | } |
---|
| 246 | ); |
---|
| 247 | |
---|
| 248 | |
---|
[4cf4067] | 249 | sub cmd_twitter { |
---|
| 250 | my $cmd = shift; |
---|
[159aaad] | 251 | my $account = shift; |
---|
| 252 | if (defined $account) { |
---|
| 253 | if(@_) { |
---|
| 254 | my $status = join(" ", @_); |
---|
| 255 | twitter($account, $status); |
---|
| 256 | return; |
---|
| 257 | } |
---|
[4cf4067] | 258 | } |
---|
[159aaad] | 259 | BarnOwl::start_edit_win('What are you doing?' . (defined $account ? " ($account)" : ""), sub{twitter($account, shift)}); |
---|
[4cf4067] | 260 | } |
---|
| 261 | |
---|
[927c186] | 262 | sub cmd_twitter_direct { |
---|
| 263 | my $cmd = shift; |
---|
| 264 | my $user = shift; |
---|
| 265 | die("Usage: $cmd USER\n") unless $user; |
---|
[159aaad] | 266 | my $account = shift; |
---|
| 267 | BarnOwl::start_edit_win("$cmd $user" . (defined $account ? " $account" : ""), sub{twitter_direct($account, $user, shift)}); |
---|
[927c186] | 268 | } |
---|
| 269 | |
---|
[6babb75] | 270 | sub cmd_twitter_atreply { |
---|
| 271 | my $cmd = shift; |
---|
[acdd52e] | 272 | my $user = shift || die("Usage: $cmd USER [In-Reply-To ID]\n"); |
---|
| 273 | my $id = shift; |
---|
[159aaad] | 274 | my $account = shift; |
---|
| 275 | BarnOwl::start_edit_win("Reply to \@" . $user . (defined $account ? " on $account" : ""), sub { twitter_atreply($account, $user, $id, shift) }); |
---|
[6babb75] | 276 | } |
---|
| 277 | |
---|
[72b61dd] | 278 | eval { |
---|
| 279 | $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message"); |
---|
[8618438] | 280 | $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages"); |
---|
[72b61dd] | 281 | }; |
---|
| 282 | if($@) { |
---|
| 283 | $BarnOwl::Hooks::receiveMessage->add(\&handle_message); |
---|
[8618438] | 284 | $BarnOwl::Hooks::mainLoop->add(\&poll_messages); |
---|
[72b61dd] | 285 | } |
---|
[e54f2fa] | 286 | |
---|
[f93b81b] | 287 | BarnOwl::filter(qw{twitter type ^twitter$}); |
---|
[8618438] | 288 | |
---|
[e54f2fa] | 289 | 1; |
---|