| 1 | package POE::Component::Server::Twirc; | ||
| 2 | 2 | ▶ | use MooseX::POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 2 | ▶ | use LWP::UserAgent::POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use POE qw(Component::Server::IRC); | ||
| 6 | use Net::Twitter; | ||
| 7 | use String::Truncate elide => { marker => '…' }; | ||
| 8 | use POE::Component::Server::Twirc::LogAppender; | ||
| 9 | use POE::Component::Server::Twirc::State; | ||
| 10 | use Encode qw/decode/; | ||
| 11 | use Try::Tiny; | ||
| 12 | use Scalar::Util qw/reftype/; | ||
| 13 | use AnyEvent; | ||
| 14 | use AnyEvent::Twitter::Stream; | ||
| 15 | use HTML::Entities; | ||
| 16 | use Regexp::Common qw/URI/; | ||
| 17 | |||
| 18 | with 'MooseX::Log::Log4perl'; | ||
| 19 | |||
| 20 | our $VERSION = '0.17'; | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | POE::Component::Server::Twirc - Twitter/IRC gateway | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use POE::Component::Server::Twirc; | ||
| 29 | |||
| 30 | POE::Component::Server::Twirc->new; | ||
| 31 | |||
| 32 | POE::Kernel->run; | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | C<POE::Component::Server::Twirc> provides an IRC/Twitter gateway. Twitter friends are | ||
| 37 | added to a channel and messages they post on twitter appear as channel | ||
| 38 | messages in IRC. The IRC interface supports several Twitter features, | ||
| 39 | including posting status updates, following and un-following Twitter feeds, | ||
| 40 | enabling and disabling device notifications, sending direct messages, and | ||
| 41 | querying information about specific Twitter users. | ||
| 42 | |||
| 43 | Friends who are also followers are given "voice" as a visual clue in IRC. | ||
| 44 | |||
| 45 | =head1 METHODS | ||
| 46 | |||
| 47 | =head2 new | ||
| 48 | |||
| 49 | Spawns a POE component encapsulating the Twitter/IRC gateway. | ||
| 50 | |||
| 51 | Arguments: | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | |||
| 56 | =item irc_server_name | ||
| 57 | |||
| 58 | (Optional) The name of the IRC server. Defaults to C<twitter.irc>. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | has irc_server_name => isa => 'Str', is => 'ro', default => 'twitter.irc'; | ||
| 63 | |||
| 64 | =item irc_server_port | ||
| 65 | |||
| 66 | (Optional) The port number the IRC server binds to. Defaults to 6667. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | has irc_server_port => isa => 'Int', is => 'ro', default => 6667; | ||
| 71 | |||
| 72 | =item irc_server_bindaddr | ||
| 73 | |||
| 74 | (Optional) The local address to bind to. Defaults to '127.0.0.1'. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | # will be defaulted to INADDR_ANY by POE::Wheel::SocketFactory | ||
| 79 | has irc_server_bindaddr => isa => 'Str', is => 'ro', default => '127.0.0.1'; | ||
| 80 | |||
| 81 | =item irc_mask | ||
| 82 | |||
| 83 | (Optional) The IRC user/host mask used to restrict connecting users. Defaults to C<*@127.0.0.1>. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | has irc_mask => isa => 'Str', is => 'ro', default => '*@127.0.0.1'; | ||
| 88 | |||
| 89 | |||
| 90 | =item irc_password | ||
| 91 | |||
| 92 | (Optional) Password used to authenticate to the IRC server. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | has irc_password => isa => 'Str', is => 'ro'; | ||
| 97 | |||
| 98 | |||
| 99 | =item irc_botname | ||
| 100 | |||
| 101 | (Optional) The name of the channel operator bot. Defaults to C<tweeter>. Select a name | ||
| 102 | that does not conflict with friends, followers, or your own IRC nick. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | has irc_botname => isa => 'Str', is => 'ro', default => 'tweeter'; | ||
| 107 | |||
| 108 | |||
| 109 | =item irc_botircname | ||
| 110 | |||
| 111 | (Optional) Text to be used as the channel operator bot's IRC full name. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | has irc_botircname => isa => 'Str', is => 'ro', default => 'Your friendly Twitter agent'; | ||
| 116 | |||
| 117 | |||
| 118 | =item irc_channel | ||
| 119 | |||
| 120 | (Optional) The name of the channel to use. Defaults to C<&twitter>. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | has irc_channel => isa => 'Str', is => 'ro', default => '&twitter'; | ||
| 125 | |||
| 126 | |||
| 127 | =item twitter_args | ||
| 128 | |||
| 129 | (Optional) A hashref of extra arguments to pass to C<< Net::Twitter->new >>. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | has twitter_args => isa => 'HashRef', is => 'ro', default => sub { {} }; | ||
| 134 | |||
| 135 | =item extra_net_twitter_traits | ||
| 136 | |||
| 137 | (Optional) Additional traits used to construct the Net::Twitter instance. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | has extra_net_twitter_traits => is => 'ro', default => sub { [] }; | ||
| 142 | |||
| 143 | =item selection_count | ||
| 144 | |||
| 145 | (Optional) How many favorites candidates to display for selection. Defaults to 3. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | has selection_count => isa => 'Int', is => 'ro', default => 3; | ||
| 150 | |||
| 151 | =item truncate_to | ||
| 152 | |||
| 153 | (Optional) When displaying tweets for selection, they will be truncated to this length. | ||
| 154 | Defaults to 60. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | has truncate_to => isa => 'Int', is => 'ro', default => 60; | ||
| 159 | |||
| 160 | |||
| 161 | =item log_channel | ||
| 162 | |||
| 163 | (Optional) If specified, twirc will post log messages to this channel. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | has log_channel => isa => 'Str', is => 'ro'; | ||
| 168 | |||
| 169 | =item state_file | ||
| 170 | |||
| 171 | (Optional) File used to store state information between sessions, including last message read for | ||
| 172 | replies, direct messages, and timelines. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | has state_file => isa => 'Str', is => 'ro'; | ||
| 177 | |||
| 178 | =item plugins | ||
| 179 | |||
| 180 | (Optional) An array of plugin objects. | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | has plugins => isa => 'ArrayRef[Object]', is => 'ro', default => sub { [] }; | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | has irc_nickname => isa => 'Str', is => 'rw', init_arg => undef; | ||
| 192 | |||
| 193 | has _twitter => is => 'rw', isa => 'Object', lazy => 1, default => sub { | ||
| 194 | my $self = shift; | ||
| 195 | |||
| 196 | Net::Twitter->new( | ||
| 197 | $self->_net_twitter_opts, | ||
| 198 | access_token => $self->state->access_token, | ||
| 199 | access_token_secret => $self->state->access_token_secret, | ||
| 200 | ); | ||
| 201 | }; | ||
| 202 | |||
| 203 | has ircd => isa => 'POE::Component::Server::IRC', is => 'rw', weak_ref => 1; | ||
| 204 | |||
| 205 | has _users_by_nick => | ||
| 206 | traits => [qw/Hash/], | ||
| 207 | isa => 'HashRef[HashRef|Object]', | ||
| 208 | is => 'rw', | ||
| 209 | init_arg => undef, | ||
| 210 | lazy => 1, | ||
| 211 | default => sub { +{ map { lc($$_{screen_name}) => $_ } shift->get_users } }, | ||
| 212 | handles => { | ||
| 213 | set_user_by_nick => 'set', | ||
| 214 | get_user_by_nick => 'get', | ||
| 215 | delete_user_by_nick => 'delete', | ||
| 216 | user_nicks => 'keys', | ||
| 217 | }; | ||
| 218 | |||
| 219 | has joined => init_arg => undef, isa => 'Bool', is => 'rw', default => 0; | ||
| 220 | has stash => init_arg => undef, isa => 'HashRef', is => 'rw', predicate => 'has_stash', clearer => 'clear_stash'; | ||
| 221 | |||
| 222 | has state => | ||
| 223 | isa => 'POE::Component::Server::Twirc::State', | ||
| 224 | is => 'rw', | ||
| 225 | lazy => 1, | ||
| 226 | handles => [qw/set_user_by_id get_user_by_id delete_user_by_id get_users/], | ||
| 227 | default => sub { POE::Component::Server::Twirc::State->new }; | ||
| 228 | |||
| 229 | has client_encoding => isa => 'Str', is => 'rw', default => sub { 'utf-8' }; | ||
| 230 | |||
| 231 | has reconnect_delay => is => 'rw', isa => 'Num', default => 0; | ||
| 232 | has twitter_stream_watcher => is => 'rw', clearer => 'disconnect_twitter_stream', | ||
| 233 | predicate => 'has_twitter_stream_watcher'; | ||
| 234 | |||
| 235 | has authenticated_user => is => 'rw', isa => 'HashRef', init_arg => undef; | ||
| 236 | |||
| 237 | # force build of users by nick hash early | ||
| 238 | sub BUILD { shift->_users_by_nick } | ||
| 239 | |||
| 240 | sub get_authenticated_user { | ||
| 241 | my $self = shift; | ||
| 242 | |||
| 243 | if ( my $r = $self->twitter(verify_credentials => { include_entities => 1 }) ) { | ||
| 244 | $self->authenticated_user($r); | ||
| 245 | if ( my $status = delete $$r{status} ) { | ||
| 246 | $$status{user} = $r; | ||
| 247 | $self->set_topic($self->formatted_status_text($status)); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | $self->log->fatal("Failed to get authenticated user data from twitter (verify_credentials)"); | ||
| 252 | $self->call('poco_shutdown'); | ||
| 253 | } | ||
| 254 | }; | ||
| 255 | |||
| 256 | sub twitter { | ||
| 257 | my ($self, $method, @args) = @_; | ||
| 258 | |||
| 259 | my $r = try { $self->_twitter->$method(@args) } | ||
| 260 | catch { | ||
| 261 | $self->log->error("twitter errer: $_"); | ||
| 262 | if ( blessed $_ && $_->can('code') && $_->code == 502 ) { | ||
| 263 | $_ = 'Fail Whale'; | ||
| 264 | } | ||
| 265 | s/ at .* line \d+//; | ||
| 266 | $self->twitter_error("$method -> $_"); | ||
| 267 | undef; | ||
| 268 | }; | ||
| 269 | |||
| 270 | return $r; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub post_ircd { | ||
| 274 | my $self = shift; | ||
| 275 | $self->ircd->yield(@_); | ||
| 276 | } | ||
| 277 | |||
| 278 | sub bot_says { | ||
| 279 | my ($self, $channel, $text) = @_; | ||
| 280 | |||
| 281 | $self->post_ircd('daemon_cmd_privmsg', $self->irc_botname, $channel, $text); | ||
| 282 | }; | ||
| 283 | |||
| 284 | sub bot_notice { | ||
| 285 | my ($self, $channel, $text) = @_; | ||
| 286 | |||
| 287 | $self->post_ircd(daemon_cmd_notice => $self->irc_botname, $channel, $text); | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | sub twitter_error { | ||
| 292 | my ($self, $text) = @_; | ||
| 293 | |||
| 294 | $self->bot_notice($self->irc_channel, "Twitter error: $text"); | ||
| 295 | }; | ||
| 296 | |||
| 297 | # set topic from status, iff newest status | ||
| 298 | sub set_topic { | ||
| 299 | my ($self, $text) = @_; | ||
| 300 | |||
| 301 | $self->post_ircd(daemon_cmd_topic => $self->irc_botname, $self->irc_channel, $text); | ||
| 302 | }; | ||
| 303 | |||
| 304 | # match any nick | ||
| 305 | sub nicks_alternation { | ||
| 306 | my $self = shift; | ||
| 307 | |||
| 308 | return join '|', map quotemeta, $self->user_nicks; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub add_user { | ||
| 312 | my ($self, $user) = @_; | ||
| 313 | |||
| 314 | my $nick = $$user{screen_name}; | ||
| 315 | $self->log->trace("add_user: $nick"); | ||
| 316 | |||
| 317 | # handle nick changes | ||
| 318 | if ( my $current_user = $self->get_user_by_id($$user{id}) ) { | ||
| 319 | $self->post_ircd(daemon_cmd_nick => $$current_user{screen_name}, $nick) | ||
| 320 | if $nick ne $$current_user{screen_name}; | ||
| 321 | } | ||
| 322 | |||
| 323 | $$user{FRESH} = time; | ||
| 324 | $self->set_user_by_id($user->{id}, $user); | ||
| 325 | $self->set_user_by_nick(lc($user->{screen_name}), $user); | ||
| 326 | |||
| 327 | unless ( $self->ircd->state_nick_exists($nick) ) { | ||
| 328 | $self->post_ircd(add_spoofed_nick => { nick => $nick, ircname => $$user{name} }); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | sub delete_user { | ||
| 333 | my ($self, $user) = @_; | ||
| 334 | |||
| 335 | $self->delete_user_by_id($user->{id}); | ||
| 336 | $self->delete_user_by_nick(lc($user->{screen_name})); | ||
| 337 | } | ||
| 338 | |||
| 339 | sub _twitter_auth { | ||
| 340 | # ROT13: Gjvggre qbrf abg jnag pbafhzre xrl/frperg vapyhqrq va bcra | ||
| 341 | # fbhepr nccf. Gurl frrz gb guvax cebcevrgnel pbqr vf fnsre orpnhfr | ||
| 342 | # gur pbafhzre perqragvnyf ner boshfpngrq. Fb, jr'yy boshfpngr gurz | ||
| 343 | # urer jvgu ebg13 naq jr'yy or "frpher" whfg yvxr n cebcevrgnel ncc. | ||
| 344 | ( grep tr/a-zA-Z/n-za-mN-ZA-M/, map $_, | ||
| 345 | pbafhzre_xrl => 'ntqifMSFhMC0NdSWmBWgtN', | ||
| 346 | pbafhzre_frperg => 'CDDA2pAiDcjb6saxt0LLwezCBV97VPYGAF0LMa0oH', | ||
| 347 | ), | ||
| 348 | } | ||
| 349 | |||
| 350 | sub _net_twitter_opts { | ||
| 351 | my $self = shift; | ||
| 352 | |||
| 353 | my %config = ( | ||
| 354 | $self->_twitter_auth, | ||
| 355 | traits => [qw/API::RESTv1_1 OAuth RetryOnError/], | ||
| 356 | useragent_class => 'LWP::UserAgent::POE', | ||
| 357 | useragent => "twirc/$VERSION", | ||
| 358 | decode_html_entities => 1, | ||
| 359 | ssl => 1, | ||
| 360 | %{ $self->twitter_args }, | ||
| 361 | ); | ||
| 362 | |||
| 363 | foreach my $plugin (@{$self->plugins}){ | ||
| 364 | if ($plugin->can('plugin_traits')) { | ||
| 365 | push @{ $config{traits} }, $plugin->plugin_traits(); | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | my %unique_traits = map { $_ => undef } | ||
| 370 | @{ $config{traits} }, | ||
| 371 | @{ $self->extra_net_twitter_traits }; | ||
| 372 | $config{traits} = [ keys %unique_traits ]; | ||
| 373 | |||
| 374 | return %config; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub max_reconnect_delay () { 600 } # ten minutes | ||
| 378 | sub twitter_stream_timeout () { 65 } # should get activity every 30 seconds | ||
| 379 | sub friends_stale_after () { 7*24*3600 } # 1 week | ||
| 380 | |||
| 381 | sub is_user_stale { | ||
| 382 | my ( $self, $user ) = @_; | ||
| 383 | |||
| 384 | return time - $user->{FRESH} > $self->friends_stale_after; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub followers_stale_after () { 24*3600 } # 1 day | ||
| 388 | sub are_followers_stale { | ||
| 389 | my $self = shift; | ||
| 390 | |||
| 391 | return time - $self->state->followers_updated_at > $self->followers_stale_after; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub add_follower_id { | ||
| 395 | my ( $self, $id ) = @_; | ||
| 396 | |||
| 397 | $self->state->followers->{$id} = undef; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub remove_follower_id { | ||
| 401 | my ( $self, $id ) = @_; | ||
| 402 | |||
| 403 | delete $self->state->followers->{$id}; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub is_follower_id { | ||
| 407 | my ( $self, $id ) = @_; | ||
| 408 | |||
| 409 | return exists $self->state->followers->{$id}; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub twitter_screen_name { shift->authenticated_user->{screen_name} } | ||
| 413 | sub twitter_id { shift->authenticated_user->{id} } | ||
| 414 | |||
| 415 | sub formatted_status_text { | ||
| 416 | my ( $self, $status ) = @_; | ||
| 417 | |||
| 418 | my $is_retweet = !!$$status{retweeted_status}; | ||
| 419 | my $s = $$status{retweeted_status} || $status; | ||
| 420 | my $text = $$s{text}; | ||
| 421 | for my $e ( reverse @{$$s{entities}{urls} || []} ) { | ||
| 422 | my ($start, $end) = @{$$e{indices}}; | ||
| 423 | substr $text, $start, $end - $start, "[$$e{display_url}]($$e{url})"; | ||
| 424 | } | ||
| 425 | |||
| 426 | decode_entities($text); | ||
| 427 | |||
| 428 | # When the status is a retweet from verify_credentials, it doesn't have a user element | ||
| 429 | my $orig_author = $$s{user}{screen_name} || $$status{entities}{user_mentions}[0]{screen_name}; | ||
| 430 | $text = "RT \@$orig_author: $text" if $is_retweet; | ||
| 431 | |||
| 432 | return $text; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub connect_twitter_stream { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | $self->log->trace('connect_twitter_stream'); | ||
| 439 | |||
| 440 | my $w; $w = AnyEvent::Twitter::Stream->new( | ||
| 441 | $self->_twitter_auth, | ||
| 442 | token => $self->state->access_token, | ||
| 443 | token_secret => $self->state->access_token_secret, | ||
| 444 | method => 'userstream', | ||
| 445 | timeout => $self->twitter_stream_timeout, | ||
| 446 | on_connect => sub { | ||
| 447 | $self->twitter_stream_watcher($w); | ||
| 448 | $self->log->info('Connected to Twitter'); | ||
| 449 | $self->bot_notice($self->irc_channel, "Twitter stream connected"); | ||
| 450 | $self->reconnect_delay(0); | ||
| 451 | }, | ||
| 452 | on_eof => sub { | ||
| 453 | $self->log->trace("on_eof"); | ||
| 454 | $self->bot_notice($self->irc_channel, "Twitter stream disconnected"); | ||
| 455 | $self->connect_twitter_stream if $self->has_twitter_stream_watcher; | ||
| 456 | }, | ||
| 457 | on_error => sub { | ||
| 458 | my $e = shift; | ||
| 459 | $self->log->error("on_error: $e"); | ||
| 460 | $self->bot_notice($self->irc_channel, "Twitter stream error: $e"); | ||
| 461 | if ( $e =~ /^420:/ ) { | ||
| 462 | $self->log->fatal("excessive login rate; shutting down"); | ||
| 463 | $self->call('poco_shutdown'); | ||
| 464 | } | ||
| 465 | |||
| 466 | # progressively backoff on reconnection attepts to max_reconnect_delay | ||
| 467 | if ( my $delay = $self->reconnect_delay ) { | ||
| 468 | $self->log->debug("delaying $delay seconds before reconnecting"); | ||
| 469 | } | ||
| 470 | my $t; $t = AE::timer $self->reconnect_delay, 0, sub { | ||
| 471 | undef $t; | ||
| 472 | my $next_delay = $self->reconnect_delay * 2 || 1; | ||
| 473 | $next_delay = $self->max_reconnect_delay if $next_delay > $self->max_reconnect_delay; | ||
| 474 | $self->reconnect_delay($next_delay); | ||
| 475 | $self->connect_twitter_stream; | ||
| 476 | }; | ||
| 477 | }, | ||
| 478 | on_keepalive => sub { | ||
| 479 | $self->log->trace("on_keepalive"); | ||
| 480 | }, | ||
| 481 | on_friends => sub { | ||
| 482 | $self->log->trace("on_friends: ", JSON->new->encode(@_)); | ||
| 483 | $self->yield(friends_ids => shift); | ||
| 484 | }, | ||
| 485 | on_event => sub { | ||
| 486 | my $msg = shift; | ||
| 487 | |||
| 488 | $self->log->trace("on_event: $$msg{event}"); | ||
| 489 | $self->yield(on_event => $msg); | ||
| 490 | }, | ||
| 491 | on_tweet => sub { | ||
| 492 | my $msg = shift; | ||
| 493 | |||
| 494 | $self->log->trace("on_tweet"); | ||
| 495 | |||
| 496 | if ( exists $$msg{sender} ) { | ||
| 497 | $self->log->debug('received old style direct_message'); | ||
| 498 | $self->yield(on_direct_message => $msg); | ||
| 499 | } | ||
| 500 | elsif ( exists $$msg{text} ) { | ||
| 501 | $self->yield(on_tweet => $msg); | ||
| 502 | } | ||
| 503 | elsif ( exists $$msg{direct_message} ) { | ||
| 504 | $self->yield(on_direct_message => $$msg{direct_message}); | ||
| 505 | } | ||
| 506 | elsif ( exists $$msg{limit} ) { | ||
| 507 | $self->log->warn("track limit: $$msg{limit}{track}"); | ||
| 508 | $self->bot_notice($self->irc_channel, | ||
| 509 | "Track limit received - $$msg{limit}{track} statuses missed."); | ||
| 510 | } | ||
| 511 | elsif ( exists $$msg{scrub_geo} ) { | ||
| 512 | # $$msg{scrub_geo} = {"user_id":14090452,"user_id_str":"14090452","up_to_status_id":23260136625,"up_to_status_id_str":"23260136625"} | ||
| 513 | my $e = $$msg{scrub_geo}; | ||
| 514 | $self->log->info("scrub_geo: user_id=$$e{user_id}, up_to_status_id=$$e{up_to_status_id}"); | ||
| 515 | } | ||
| 516 | else { | ||
| 517 | $self->log->error("unexpected message: ", JSON->new->pretty($msg)); | ||
| 518 | $self->bot_notice($self->irc_channel, "Unexpected twitter packet, see the log for details"); | ||
| 519 | } | ||
| 520 | }, | ||
| 521 | on_delete => sub { | ||
| 522 | $self->log->trace("on_delete"); | ||
| 523 | }, | ||
| 524 | ); | ||
| 525 | } | ||
| 526 | |||
| 527 | sub START { | ||
| 528 | my ($self) = @_; | ||
| 529 | |||
| 530 | $self->ircd( | ||
| 531 | POE::Component::Server::IRC->spawn( | ||
| 532 | config => { | ||
| 533 | servername => $self->irc_server_name, | ||
| 534 | nicklen => 15, | ||
| 535 | network => 'SimpleNET' | ||
| 536 | }, | ||
| 537 | inline_states => { | ||
| 538 | _stop => sub { $self->log->trace('[ircd:stop]') }, | ||
| 539 | }, | ||
| 540 | ) | ||
| 541 | ); | ||
| 542 | |||
| 543 | # register ircd to receive events | ||
| 544 | $self->post_ircd('register' ); | ||
| 545 | $self->ircd->add_auth( | ||
| 546 | mask => $self->irc_mask, | ||
| 547 | password => $self->irc_password, | ||
| 548 | ); | ||
| 549 | $self->post_ircd('add_listener', port => $self->irc_server_port, | ||
| 550 | bindaddr => $self->irc_server_bindaddr); | ||
| 551 | |||
| 552 | # add super user | ||
| 553 | $self->post_ircd(add_spoofed_nick => { | ||
| 554 | nick => $self->irc_botname, | ||
| 555 | ircname => $self->irc_botircname, | ||
| 556 | }); | ||
| 557 | $self->post_ircd(daemon_cmd_join => $self->irc_botname, $self->irc_channel); | ||
| 558 | |||
| 559 | # logging | ||
| 560 | if ( $self->log_channel ) { | ||
| 561 | $self->post_ircd(daemon_cmd_join => $self->irc_botname, $self->log_channel); | ||
| 562 | my $logger = Log::Log4perl->get_logger(''); | ||
| 563 | my $appender = Log::Log4perl::Appender->new( | ||
| 564 | 'POE::Component::Server::Twirc::LogAppender', | ||
| 565 | name => 'twirc-logger', | ||
| 566 | ircd => $self->ircd, | ||
| 567 | irc_botname => $self->irc_botname, | ||
| 568 | irc_channel => $self->log_channel, | ||
| 569 | ); | ||
| 570 | $logger->add_appender($appender); | ||
| 571 | } | ||
| 572 | |||
| 573 | POE::Kernel->sig(TERM => 'poco_shutdown'); | ||
| 574 | POE::Kernel->sig(INT => 'poco_shutdown'); | ||
| 575 | |||
| 576 | $self->get_authenticated_user; | ||
| 577 | $self->connect_twitter_stream; | ||
| 578 | |||
| 579 | return $self; | ||
| 580 | } | ||
| 581 | |||
| 582 | # Without detaching the ircd child session, the application will not | ||
| 583 | # shut down. Bug in PoCo::Server::IRC? | ||
| 584 | event _child => sub { | ||
| 585 | my ($self, $kernel, $event, $child) = @_[OBJECT, KERNEL, ARG0, ARG1]; | ||
| 586 | |||
| 587 | $self->log->trace("[_child] $event $child"); | ||
| 588 | $kernel->detach_child($child) if $event eq 'create'; | ||
| 589 | }; | ||
| 590 | |||
| 591 | event poco_shutdown => sub { | ||
| 592 | my ($self) = @_; | ||
| 593 | |||
| 594 | $self->log->trace("[poco_shutdown]"); | ||
| 595 | $self->disconnect_twitter_stream; | ||
| 596 | $_[KERNEL]->alarm_remove_all(); | ||
| 597 | $self->post_ircd('unregister'); | ||
| 598 | $self->post_ircd('shutdown'); | ||
| 599 | $_[KERNEL]->call($self->_twitter->ua->{poco_alias}, 'shutdown'); | ||
| 600 | if ( $self->state_file ) { | ||
| 601 | try { $self->state->store($self->state_file) } | ||
| 602 | catch { | ||
| 603 | s/ at .*//s; | ||
| 604 | $self->log->error($_); | ||
| 605 | $self->bot_notice($self->irc_channel, "Error storing state file: $_"); | ||
| 606 | }; | ||
| 607 | } | ||
| 608 | |||
| 609 | # TODO: Why does twirc often fail to shut down? | ||
| 610 | # This is surely the WRONG thing to do, but hit the big red kill switch. | ||
| 611 | exit 0; | ||
| 612 | }; | ||
| 613 | |||
| 614 | ######################################################################## | ||
| 615 | # IRC events | ||
| 616 | ######################################################################## | ||
| 617 | |||
| 618 | event ircd_daemon_nick => sub { | ||
| 619 | my ($self, $sender, $nick) = @_[OBJECT, SENDER, ARG0]; | ||
| 620 | |||
| 621 | $self->log->trace("[ircd_daemon_nick] $nick"); | ||
| 622 | |||
| 623 | # if it's a nick change, we only get ARG0 and ARG1 | ||
| 624 | return unless defined $_[ARG2]; | ||
| 625 | return if $self->ircd->_state_user_route($nick) eq 'spoofed'; | ||
| 626 | |||
| 627 | $self->irc_nickname($nick); | ||
| 628 | |||
| 629 | # Abuse! Calling the private implementation of ircd to force-join the connecting | ||
| 630 | # user to the twitter channel. ircd set's it's heap to $self: see ircd's perldoc. | ||
| 631 | $sender->get_heap->_daemon_cmd_join($nick, $self->irc_channel); | ||
| 632 | |||
| 633 | # Give the user half ops (just a visual cue) | ||
| 634 | $self->post_ircd(daemon_cmd_mode => $self->irc_botname, $self->irc_channel, '+h', $nick); | ||
| 635 | }; | ||
| 636 | |||
| 637 | event ircd_daemon_join => sub { | ||
| 638 | my($self, $sender, $user, $ch) = @_[OBJECT, SENDER, ARG0, ARG1]; | ||
| 639 | |||
| 640 | $self->log->trace("[ircd_daemon_join] $user, $ch"); | ||
| 641 | return unless my($nick) = $user =~ /^([^!]+)!/; | ||
| 642 | return if $self->ircd->_state_user_route($nick) eq 'spoofed'; | ||
| 643 | |||
| 644 | if ( $ch eq $self->irc_channel ) { | ||
| 645 | $self->joined(1); | ||
| 646 | $self->log->trace(" joined!"); | ||
| 647 | return; | ||
| 648 | } | ||
| 649 | elsif ( $self->log_channel && $ch eq $self->log_channel ) { | ||
| 650 | my $appender = Log::Log4perl->appender_by_name('twirc-logger'); | ||
| 651 | $appender->dump_history; | ||
| 652 | } | ||
| 653 | else { | ||
| 654 | $self->log->trace(" ** part **"); | ||
| 655 | # only one channel allowed | ||
| 656 | $sender->get_heap()->_daemon_cmd_part($nick, $ch); | ||
| 657 | } | ||
| 658 | }; | ||
| 659 | |||
| 660 | event ircd_daemon_part => sub { | ||
| 661 | my($self, $user_name, $ch) = @_[OBJECT, ARG0, ARG1]; | ||
| 662 | |||
| 663 | return unless my($nick) = $user_name =~ /^([^!]+)!/; | ||
| 664 | return if $nick eq $self->irc_botname; | ||
| 665 | |||
| 666 | if ( my $user = $self->get_user_by_nick(lc $nick) ) { | ||
| 667 | $self->delete_user($user); | ||
| 668 | } | ||
| 669 | |||
| 670 | $self->joined(0) if $ch eq $self->irc_channel && $nick eq $self->irc_nickname; | ||
| 671 | }; | ||
| 672 | |||
| 673 | event ircd_daemon_quit => sub { | ||
| 674 | my($self, $user) = @_[OBJECT, ARG0]; | ||
| 675 | |||
| 676 | $self->log->trace("[ircd_daemon_quit]"); | ||
| 677 | return unless my($nick) = $user =~ /^([^!]+)!/; | ||
| 678 | return unless $nick eq $self->irc_nickname; | ||
| 679 | |||
| 680 | $self->joined(0); | ||
| 681 | $self->yield('poco_shutdown'); | ||
| 682 | }; | ||
| 683 | |||
| 684 | event ircd_daemon_public => sub { | ||
| 685 | my ($self, $user, $channel, $text) = @_[OBJECT, ARG0, ARG1, ARG2]; | ||
| 686 | |||
| 687 | return unless $channel eq $self->irc_channel; | ||
| 688 | |||
| 689 | $text = decode($self->client_encoding, $text); | ||
| 690 | |||
| 691 | $text =~ s/\s+$//; | ||
| 692 | |||
| 693 | my $nick = ( $user =~ m/^(.*)!/)[0]; | ||
| 694 | |||
| 695 | $self->log->trace("[ircd_daemon_public] $nick: $text"); | ||
| 696 | return unless $nick eq $self->irc_nickname; | ||
| 697 | |||
| 698 | # give any command handler a shot | ||
| 699 | if ( $self->has_stash ) { | ||
| 700 | $self->log->debug("stash exists..."); | ||
| 701 | my $handler = delete $self->stash->{handler}; | ||
| 702 | if ( $handler ) { | ||
| 703 | return if $self->$handler($channel, $text); # handled | ||
| 704 | } | ||
| 705 | else { | ||
| 706 | $self->log->error("stash exsits with no handler"); | ||
| 707 | } | ||
| 708 | # the user ignored a command completion request, kill it | ||
| 709 | $self->clear_stash; | ||
| 710 | } | ||
| 711 | |||
| 712 | for my $plugin ( @{$self->plugins} ) { | ||
| 713 | $plugin->preprocess($self, $channel, $nick, \$text) && last | ||
| 714 | if $plugin->can('preprocess'); | ||
| 715 | } | ||
| 716 | |||
| 717 | # treat "nick: ..." as "post @nick ..." | ||
| 718 | my $nick_alternation = $self->nicks_alternation; | ||
| 719 | $text =~ s/^(?:post\s+)?($nick_alternation):\s+/post \@$1 /i; | ||
| 720 | |||
| 721 | my ($command, $argstr) = split /\s+/, $text, 2; | ||
| 722 | if ( $command =~ /^\w+$/ ) { | ||
| 723 | my $event = "cmd_$command"; | ||
| 724 | |||
| 725 | # Give each plugin a opportunity: | ||
| 726 | # - Plugins return true if they swallow the event; false to continue | ||
| 727 | # the processing chain. | ||
| 728 | # - Plugins can modify the text, so pass a ref. | ||
| 729 | for my $plugin ( @{$self->plugins} ) { | ||
| 730 | $plugin->$event($self, $channel, $nick, \$argstr) && return | ||
| 731 | if $plugin->can($event); | ||
| 732 | } | ||
| 733 | if ( $self->can($event) ) { | ||
| 734 | $self->yield($event, $channel, $argstr); | ||
| 735 | } | ||
| 736 | else { | ||
| 737 | $self->bot_says($channel, qq/I don't understand "$command". Try "help"./) | ||
| 738 | } | ||
| 739 | } | ||
| 740 | else { | ||
| 741 | $self->bot_says($channel, qq/That doesn't look like a command. Try "help"./); | ||
| 742 | } | ||
| 743 | }; | ||
| 744 | |||
| 745 | event ircd_daemon_privmsg => sub { | ||
| 746 | my ($self, $user, $target_nick, $text) = @_[OBJECT, ARG0..ARG2]; | ||
| 747 | |||
| 748 | # owning user is the only one allowed to send direct messages | ||
| 749 | my $me = $self->irc_nickname; | ||
| 750 | return unless $user =~ /^\Q$me\E!/; | ||
| 751 | |||
| 752 | $text = decode($self->client_encoding, $text); | ||
| 753 | |||
| 754 | unless ( $self->get_user_by_nick(lc $target_nick) ) { | ||
| 755 | # TODO: handle the error the way IRC would?? (What channel?) | ||
| 756 | $self->bot_says($self->irc_channel, qq/You don't appear to be following $target_nick; message not sent./); | ||
| 757 | return; | ||
| 758 | } | ||
| 759 | |||
| 760 | $self->twitter(new_direct_message => { user => $target_nick, text => $text }); | ||
| 761 | }; | ||
| 762 | |||
| 763 | event friend_join => sub { | ||
| 764 | my ( $self, $friend ) = @_[OBJECT, ARG0]; | ||
| 765 | |||
| 766 | my $nick = $$friend{screen_name}; | ||
| 767 | $self->log->trace("friend_join: $nick"); | ||
| 768 | |||
| 769 | $self->post_ircd(add_spoofed_nick => { nick => $nick, ircname => $$friend{name} }) | ||
| 770 | unless $self->ircd->state_nick_exists($nick); | ||
| 771 | |||
| 772 | $self->post_ircd(daemon_cmd_join => $nick, $self->irc_channel); | ||
| 773 | if ( $self->is_follower_id($$friend{id}) ) { | ||
| 774 | $self->post_ircd(daemon_cmd_mode => $self->irc_botname, $self->irc_channel, '+v', $nick); | ||
| 775 | } | ||
| 776 | }; | ||
| 777 | |||
| 778 | event lookup_friends => sub { | ||
| 779 | my ( $self, $ids ) = @_[OBJECT, ARG0]; | ||
| 780 | |||
| 781 | return unless @$ids; | ||
| 782 | |||
| 783 | my $fresh = time; | ||
| 784 | my $r = $self->twitter(lookup_users => { user_id => $ids }); | ||
| 785 | for my $friend ( @{$r || []} ) { | ||
| 786 | delete $friend->{status}; | ||
| 787 | $self->add_user($friend); | ||
| 788 | $self->yield(friend_join => $friend); | ||
| 789 | } | ||
| 790 | |||
| 791 | $self->state->store($self->state_file) if $self->state_file; | ||
| 792 | }; | ||
| 793 | |||
| 794 | event get_followers_ids => sub { | ||
| 795 | my ( $self ) = $_[OBJECT]; | ||
| 796 | |||
| 797 | my %followers; | ||
| 798 | for ( my $cursor = -1; $cursor; ) { | ||
| 799 | if ( my $r = $self->twitter(followers_ids => { cursor => $cursor }) ) { | ||
| 800 | for my $id ( @{$$r{ids}} ) { | ||
| 801 | $followers{$id} = undef; | ||
| 802 | } | ||
| 803 | $cursor = $$r{next_cursor}; | ||
| 804 | } | ||
| 805 | else { | ||
| 806 | $cursor = 0; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | $self->state->followers(\%followers); | ||
| 811 | $self->state->followers_updated_at(time); | ||
| 812 | |||
| 813 | $self->yield('set_voice'); | ||
| 814 | }; | ||
| 815 | |||
| 816 | event set_voice => sub { | ||
| 817 | my $self = $_[OBJECT]; | ||
| 818 | |||
| 819 | for my $user ( $self->get_users ) { | ||
| 820 | my $mode = $self->is_follower_id($$user{id}) ? '+v' : '-v'; | ||
| 821 | |||
| 822 | $self->post_ircd(daemon_cmd_mode => $self->irc_botname, $self->irc_channel, $mode, | ||
| 823 | $$user{screen_name}); | ||
| 824 | } | ||
| 825 | }; | ||
| 826 | |||
| 827 | ######################################################################## | ||
| 828 | # Twitter events | ||
| 829 | ######################################################################## | ||
| 830 | |||
| 831 | event friends_ids => sub { | ||
| 832 | my ( $self, $kernel, $friends_ids ) = @_[OBJECT, KERNEL, ARG0]; | ||
| 833 | |||
| 834 | my $buffer = []; | ||
| 835 | for my $id ( @$friends_ids ) { | ||
| 836 | my $friend = $self->state->twitter_users->{$id}; | ||
| 837 | if ( !$friend || $self->is_user_stale($friend) ) { | ||
| 838 | push @$buffer, $id; | ||
| 839 | if ( @$buffer == 100 ) { | ||
| 840 | $self->yield(lookup_friends => [ @$buffer ]); | ||
| 841 | $buffer = []; | ||
| 842 | $kernel->run_one_timeslice; | ||
| 843 | } | ||
| 844 | } | ||
| 845 | else { | ||
| 846 | $self->yield(friend_join => $friend); | ||
| 847 | } | ||
| 848 | } | ||
| 849 | |||
| 850 | $self->yield(lookup_friends => $buffer); | ||
| 851 | $self->yield('get_followers_ids'); | ||
| 852 | }; | ||
| 853 | |||
| 854 | event on_tweet => sub { | ||
| 855 | my ( $self, $status ) = @_[OBJECT, ARG0]; | ||
| 856 | |||
| 857 | # add or freshen user | ||
| 858 | $self->add_user($$status{user}); | ||
| 859 | |||
| 860 | my $nick = $$status{user}{screen_name}; | ||
| 861 | my $text = $self->formatted_status_text($status); | ||
| 862 | if ( $nick eq $self->irc_nickname ) { | ||
| 863 | $self->set_topic($text); | ||
| 864 | } | ||
| 865 | |||
| 866 | unless ( $self->ircd->state_is_chan_member($nick, $self->irc_channel) ) { | ||
| 867 | $self->post_ircd(daemon_cmd_join => $nick, $self->irc_channel); | ||
| 868 | } | ||
| 869 | |||
| 870 | $self->log->trace("on_tweet: <$nick> $text"); | ||
| 871 | $self->post_ircd(daemon_cmd_privmsg => $nick, $self->irc_channel, $_) for split /[\r\n]+/, $text; | ||
| 872 | }; | ||
| 873 | |||
| 874 | event on_event => sub { | ||
| 875 | my ( $self, $msg ) = @_[OBJECT, ARG0]; | ||
| 876 | |||
| 877 | ### Potential events: | ||
| 878 | # | ||
| 879 | ## implemented: | ||
| 880 | # retweet | ||
| 881 | # follow unfollow | ||
| 882 | # block unblock | ||
| 883 | # favorite unfavorite | ||
| 884 | # | ||
| 885 | ## unimplemented: | ||
| 886 | # user_update | ||
| 887 | # list_created list_updated list_destroyed | ||
| 888 | # list_member_added list_member_removed | ||
| 889 | # list_user_subscribed list_user_unsubscribed | ||
| 890 | |||
| 891 | my $method = "on_event_$$msg{event}"; | ||
| 892 | return $self->$method($msg) if $self->can($method); | ||
| 893 | |||
| 894 | $self->bot_notice($self->irc_channel, "Unhandled Twitter stream event: $$msg{event}"); | ||
| 895 | $self->log->debug("unhandled event", JSON->new->pretty->encode($msg)); | ||
| 896 | }; | ||
| 897 | |||
| 898 | sub on_event_follow { | ||
| 899 | my ( $self, $event ) = @_; | ||
| 900 | |||
| 901 | if ( my $source = $$event{source} ) { | ||
| 902 | my $target = $$event{target} || return; | ||
| 903 | |||
| 904 | # new friend | ||
| 905 | if ( $$source{id} eq $self->twitter_id ) { | ||
| 906 | $self->yield(friend_join => $target); | ||
| 907 | $self->bot_notice($self->irc_channel, qq/Now following $$target{screen_name}./); | ||
| 908 | } | ||
| 909 | |||
| 910 | # new follower | ||
| 911 | elsif ( $$target{id} eq $self->twitter_id ) { | ||
| 912 | $self->bot_notice($self->irc_channel, qq`\@$$source{screen_name} "$$source{name}" ` | ||
| 913 | . qq`is following you https://twitter.com/$$source{screen_name}`); | ||
| 914 | $self->add_follower_id($$source{id}); | ||
| 915 | } | ||
| 916 | } | ||
| 917 | } | ||
| 918 | |||
| 919 | sub on_event_favorite { shift->_favorite_or_retweet(favorited => @_) } | ||
| 920 | sub on_event_unfavorite { shift->_favorite_or_retweet(unfavorited => @_) } | ||
| 921 | sub on_event_retweet { shift->_favorite_or_retweet(retweeted => @_) } | ||
| 922 | sub _favorite_or_retweet { | ||
| 923 | my ( $self, $verb, $event ) = @_; | ||
| 924 | |||
| 925 | my $status = $$event{target_object}; | ||
| 926 | my $who = $$event{source}{id} eq $self->twitter_id ? 'You' : $$event{source}{screen_name}; | ||
| 927 | my $whom = $$event{target}{id} eq $self->twitter_id ? 'your' : "$$event{target}{screen_name}'s"; | ||
| 928 | my $link = "https://twitter.com/$$status{user}{screen_name}/status/$$status{id}"; | ||
| 929 | my $text = $self->formatted_status_text($status); | ||
| 930 | |||
| 931 | $self->bot_notice($self->irc_channel, | ||
| 932 | elide(qq/$who $verb $whom "$text"/, 80, { marker => '…"' }) . " [$link]"); | ||
| 933 | } | ||
| 934 | |||
| 935 | sub on_event_block { | ||
| 936 | my ( $self, $event ) = @_; | ||
| 937 | |||
| 938 | my $target = $$event{target}; | ||
| 939 | if ( $self->get_user_by_id($$target{id}) ) { | ||
| 940 | $self->post_ircd(daemon_cmd_mode => | ||
| 941 | $self->irc_botname, $self->irc_channel, '-v', $$target{screen_name}); | ||
| 942 | $self->remove_follower_id($$target{id}); | ||
| 943 | } | ||
| 944 | $self->bot_notice($self->irc_channel, qq/You blocked $$target{screen_name}./); | ||
| 945 | } | ||
| 946 | |||
| 947 | sub on_event_unblock { | ||
| 948 | my ( $self, $event ) = @_; | ||
| 949 | |||
| 950 | my $target = $$event{target}; | ||
| 951 | if ( $self->get_user_by_id($$target{id}) ) { | ||
| 952 | $self->post_ircd(daemon_cmd_mode => | ||
| 953 | $self->irc_botname, $self->irc_channel, '+v', $$target{screen_name}); | ||
| 954 | } | ||
| 955 | $self->bot_notice($self->irc_channel, qq/You unblocked $$target{screen_name}./); | ||
| 956 | } | ||
| 957 | |||
| 958 | sub on_event_list_member_added { shift->_list_add_or_remove(qw/added to/, @_) } | ||
| 959 | sub on_event_list_member_removed { shift->_list_add_or_remove(qw/removed from/, @_) } | ||
| 960 | sub _list_add_or_remove { | ||
| 961 | my ( $self, $verb, $preposition, $event ) = @_; | ||
| 962 | |||
| 963 | my $list = $$event{target_object}; | ||
| 964 | my $who = $$event{source}{id} eq $self->twitter_id ? 'You' : $$event{source}{screen_name}; | ||
| 965 | my $whom = $$event{target}{id} eq $self->twitter_id ? 'you' : $$event{target}{screen_name}; | ||
| 966 | my $link = "https://twitter.com$$list{uri}"; | ||
| 967 | |||
| 968 | $self->bot_notice($self->irc_channel, "$who $verb $whom $preposition list [$$list{name}]($link)"); | ||
| 969 | } | ||
| 970 | |||
| 971 | event on_direct_message => sub { | ||
| 972 | my ( $self, $msg ) = @_[OBJECT, ARG0]; | ||
| 973 | |||
| 974 | if ( $$msg{recipient_screen_name} ne $self->twitter_screen_name ) { | ||
| 975 | $self->log->info('direct message sent to @', $$msg{recipient_screen_name}); | ||
| 976 | return; | ||
| 977 | } | ||
| 978 | |||
| 979 | my $nick = $$msg{sender_screen_name}; | ||
| 980 | my $sender = $$msg{sender}; | ||
| 981 | |||
| 982 | unless ( $self->ircd->state_nick_exists($nick) ) { | ||
| 983 | # This shouldn't happen - twitter only allows direct messages to followers, so | ||
| 984 | # we *should* already have $nick on board. | ||
| 985 | $self->post_ircd(add_spoofed_nick => { nick => $nick, ircname => $$sender{name} }); | ||
| 986 | $self->add_user($sender); | ||
| 987 | } | ||
| 988 | |||
| 989 | my $text = $self->formatted_status_text($msg); | ||
| 990 | $self->post_ircd(daemon_cmd_privmsg => $nick, $self->irc_nickname, $_) | ||
| 991 | for split /\r?\n/, $text; | ||
| 992 | }; | ||
| 993 | |||
| 994 | ######################################################################## | ||
| 995 | # Commands | ||
| 996 | ######################################################################## | ||
| 997 | |||
| 998 | =head2 COMMANDS | ||
| 999 | |||
| 1000 | Commands are entered as public messages in the IRC channel in the form: | ||
| 1001 | |||
| 1002 | command arg1 arg2 ... argn | ||
| 1003 | |||
| 1004 | Where the arguments, if any, depend upon the command. | ||
| 1005 | |||
| 1006 | =over 4 | ||
| 1007 | |||
| 1008 | =item post I<status> | ||
| 1009 | |||
| 1010 | Post a status update. E.g., | ||
| 1011 | |||
| 1012 | post Now cooking tweets with twirc! | ||
| 1013 | |||
| 1014 | =cut | ||
| 1015 | |||
| 1016 | event cmd_post => sub { | ||
| 1017 | my ($self, $channel, $text) = @_[OBJECT, ARG0, ARG1]; | ||
| 1018 | |||
| 1019 | $self->log->trace("[cmd_post_status]"); | ||
| 1020 | |||
| 1021 | return if $self->status_text_too_long($channel, $text); | ||
| 1022 | |||
| 1023 | my $status = $self->twitter(update => $text) || return; | ||
| 1024 | |||
| 1025 | $self->log->trace(" update returned $status"); | ||
| 1026 | }; | ||
| 1027 | |||
| 1028 | sub status_text_too_long { | ||
| 1029 | my ( $self, $channel, $text ) = @_; | ||
| 1030 | |||
| 1031 | if ( (my $n = $self->_calc_text_length($text) - 140) > 0 ) { | ||
| 1032 | $self->bot_says($channel, "$n characters too long."); | ||
| 1033 | return $n; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | return; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | sub _calc_text_length { | ||
| 1040 | my ( $self, $text ) = @_; | ||
| 1041 | |||
| 1042 | my $http_urls = $text =~ s/$RE{URI}{HTTP}//g; | ||
| 1043 | my $https_urls = $text =~ s/$RE{URI}{HTTP}{-scheme => 'https'}//g; | ||
| 1044 | |||
| 1045 | return length($text) + $http_urls * 20 + $https_urls * 21; | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | =item follow I<id> | ||
| 1049 | |||
| 1050 | Follow a new Twitter user, I<id>. In Twitter parlance, this creates a friendship. | ||
| 1051 | |||
| 1052 | =cut | ||
| 1053 | |||
| 1054 | event cmd_follow => sub { | ||
| 1055 | my ($self, $channel, $id) = @_[OBJECT, ARG0, ARG1]; | ||
| 1056 | |||
| 1057 | if ( $id !~ /^\w+$/ ) { | ||
| 1058 | $self->bot_says($channel, qq/"$id" doesn't look like a user ID to me./); | ||
| 1059 | return; | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | $self->twitter(create_friend => $id); | ||
| 1063 | }; | ||
| 1064 | |||
| 1065 | =item unfollow I<id> | ||
| 1066 | |||
| 1067 | Stop following Twitter user I<id>. In Twitter, parlance, this destroys a | ||
| 1068 | friendship. | ||
| 1069 | |||
| 1070 | =cut | ||
| 1071 | |||
| 1072 | event cmd_unfollow => sub { | ||
| 1073 | my ($self, $channel, $id) = @_[OBJECT, ARG0, ARG1]; | ||
| 1074 | |||
| 1075 | my $user = $self->get_user_by_nick(lc $id); | ||
| 1076 | unless ( $user ) { | ||
| 1077 | $self->bot_says($channel, qq/You don't appear to be following $id./); | ||
| 1078 | return; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | $self->twitter(destroy_friend => $id) || return; | ||
| 1082 | |||
| 1083 | $self->post_ircd(daemon_cmd_part => $id, $self->irc_channel); | ||
| 1084 | $self->post_ircd(del_spooked_nick => $id); | ||
| 1085 | $self->bot_notice($channel, qq/No longer following $id./); | ||
| 1086 | $self->delete_user($user); | ||
| 1087 | }; | ||
| 1088 | |||
| 1089 | =item block I<id> | ||
| 1090 | |||
| 1091 | Block Twitter user I<id>. | ||
| 1092 | |||
| 1093 | =cut | ||
| 1094 | |||
| 1095 | event cmd_block => sub { | ||
| 1096 | my ($self, $channel, $id) = @_[OBJECT, ARG0, ARG1]; | ||
| 1097 | |||
| 1098 | if ( $id !~ /^\w+$/ ) { | ||
| 1099 | $self->bot_says($channel, qq/"$id" doesn't look like a user ID to me./); | ||
| 1100 | return; | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | $self->twitter(create_block => { screen_name => $id }); | ||
| 1104 | }; | ||
| 1105 | |||
| 1106 | =item unblock I<id> | ||
| 1107 | |||
| 1108 | Stop blocking Twitter user I<id>. | ||
| 1109 | |||
| 1110 | =cut | ||
| 1111 | |||
| 1112 | event cmd_unblock => sub { | ||
| 1113 | my ( $self, $channel, $id ) = @_[OBJECT, ARG0, ARG1]; | ||
| 1114 | |||
| 1115 | if ( $id !~ /^\w+$/ ) { | ||
| 1116 | $self->bot_says($self->irc_channel, qq/"$id" doesn't look like a Twitter screen name to me./); | ||
| 1117 | return; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | $self->twitter(destroy_block => { screen_name => $id}); | ||
| 1121 | }; | ||
| 1122 | |||
| 1123 | =item whois I<id> | ||
| 1124 | |||
| 1125 | Displays information about Twitter user I<id>, including name, location, and | ||
| 1126 | description. | ||
| 1127 | |||
| 1128 | =cut | ||
| 1129 | |||
| 1130 | event cmd_whois => sub { | ||
| 1131 | my ($self, $channel, $nick) = @_[OBJECT, ARG0, ARG1]; | ||
| 1132 | |||
| 1133 | $self->log->trace("[cmd_whois] $nick"); | ||
| 1134 | |||
| 1135 | my $user = $self->get_user_by_nick(lc $nick); | ||
| 1136 | unless ( $user ) { | ||
| 1137 | $self->log->trace(" $nick not in users; fetching"); | ||
| 1138 | $user = $self->twitter(show_user => { screen_name => $nick }); | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | if ( $user ) { | ||
| 1142 | $self->bot_says( | ||
| 1143 | $channel, | ||
| 1144 | sprintf('%s [%s]: %s, %s', @{$user}{qw/screen_name id name location description url/}) | ||
| 1145 | ); | ||
| 1146 | } | ||
| 1147 | else { | ||
| 1148 | $self->bot_says($channel, "I don't know $nick."); | ||
| 1149 | } | ||
| 1150 | }; | ||
| 1151 | |||
| 1152 | =item notify I<on|off> I<id ...> | ||
| 1153 | |||
| 1154 | Turns device notifications on or off for the list of Twitter IDs. | ||
| 1155 | |||
| 1156 | =cut | ||
| 1157 | |||
| 1158 | event cmd_notify => sub { | ||
| 1159 | my ($self, $channel, $argstr) = @_[OBJECT, ARG0, ARG1]; | ||
| 1160 | |||
| 1161 | my @nicks = split /\s+/, $argstr; | ||
| 1162 | my $onoff = shift @nicks; | ||
| 1163 | |||
| 1164 | unless ( $onoff && $onoff =~ /^on|off$/ ) { | ||
| 1165 | $self->bot_says($channel, "Usage: notify on|off nick[ nick [...]]"); | ||
| 1166 | return; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | my $method = $onoff eq 'on' ? 'enable_notifications' : 'disable_notifications'; | ||
| 1170 | for my $nick ( @nicks ) { | ||
| 1171 | $self->twitter($method => { id => $nick }); | ||
| 1172 | } | ||
| 1173 | }; | ||
| 1174 | |||
| 1175 | =item favorite I<screen_name> [I<count>] | ||
| 1176 | |||
| 1177 | Mark a tweet as a favorite. Specify the user by I<screen_name> and select from a | ||
| 1178 | list of recent tweets. Optionally, specify the number of tweets to display for | ||
| 1179 | selection with I<count> (Defaults to 3.) | ||
| 1180 | |||
| 1181 | =cut | ||
| 1182 | |||
| 1183 | event cmd_favorite => sub { | ||
| 1184 | my ($self, $channel, $args) = @_[OBJECT, ARG0, ARG1]; | ||
| 1185 | |||
| 1186 | my ($nick, $count) = split /\s+/, $args; | ||
| 1187 | $count ||= $self->selection_count; | ||
| 1188 | |||
| 1189 | $self->log->trace("[cmd_favorite] $nick"); | ||
| 1190 | |||
| 1191 | my $recent = $self->twitter(user_timeline => { screen_name => $nick, count => $count }) || return; | ||
| 1192 | if ( @$recent == 0 ) { | ||
| 1193 | $self->bot_says($channel, "$nick has no recent tweets"); | ||
| 1194 | return; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | $self->stash({ | ||
| 1198 | handler => '_handle_favorite', | ||
| 1199 | candidates => [ map $$_{id_str}, @$recent ], | ||
| 1200 | }); | ||
| 1201 | |||
| 1202 | $self->bot_says($channel, 'Which tweet?'); | ||
| 1203 | for ( 1..@$recent ) { | ||
| 1204 | $self->bot_says($channel, "[$_] " . elide($recent->[$_ - 1]{text}, $self->truncate_to)); | ||
| 1205 | } | ||
| 1206 | }; | ||
| 1207 | |||
| 1208 | sub _handle_favorite { | ||
| 1209 | my ($self, $channel, $index) = @_; | ||
| 1210 | |||
| 1211 | $self->log->trace("[handle_favorite] $index"); | ||
| 1212 | |||
| 1213 | my @candidates = @{$self->stash->{candidates} || []}; | ||
| 1214 | if ( $index =~ /^\d+$/ && 0 < $index && $index <= @candidates ) { | ||
| 1215 | $self->twitter(create_favorite => { id => $candidates[$index - 1] }); | ||
| 1216 | $self->clear_stash; | ||
| 1217 | return 1; # handled | ||
| 1218 | } | ||
| 1219 | return 0; # unhandled | ||
| 1220 | }; | ||
| 1221 | |||
| 1222 | =item rate_limit_status | ||
| 1223 | |||
| 1224 | Displays the remaining number of API requests available in the current hour. | ||
| 1225 | |||
| 1226 | =cut | ||
| 1227 | |||
| 1228 | event cmd_rate_limit_status => sub { | ||
| 1229 | my ($self, $channel) = @_[OBJECT, ARG0]; | ||
| 1230 | |||
| 1231 | if ( my $r = $self->twitter('rate_limit_status') ) { | ||
| 1232 | my $reset_time = sprintf "%02d:%02d:%02d", (localtime $r->{reset_time_in_seconds})[2,1,0]; | ||
| 1233 | my $seconds_remaning = $r->{reset_time_in_seconds} - time; | ||
| 1234 | my $time_remaning = sprintf "%d:%02d", int($seconds_remaning / 60), $seconds_remaning % 60; | ||
| 1235 | $self->bot_says($channel, sprintf "%s API calls remaining for the next %s (until %s), hourly limit is %s", | ||
| 1236 | $$r{remaining_hits}, | ||
| 1237 | $time_remaning, | ||
| 1238 | $reset_time, | ||
| 1239 | $$r{hourly_limit}, | ||
| 1240 | ); | ||
| 1241 | } | ||
| 1242 | }; | ||
| 1243 | |||
| 1244 | =item retweet I<screen_name> [I<count>] | ||
| 1245 | |||
| 1246 | Re-tweet another user's status. Specify the user by I<screen_name> and select from a | ||
| 1247 | list of recent tweets. Optionally, specify the number of tweets to display for | ||
| 1248 | selection with I<count> (Defaults to 3.) | ||
| 1249 | |||
| 1250 | =cut | ||
| 1251 | |||
| 1252 | event cmd_retweet => sub { | ||
| 1253 | my ( $self, $channel, $args ) = @_[OBJECT, ARG0, ARG1]; | ||
| 1254 | |||
| 1255 | unless ( defined $args ) { | ||
| 1256 | $self->bot_says($channel, 'usage: retweet nick [-N]'); | ||
| 1257 | return; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | my ( $nick, $count ) = split /\s+/, $args; | ||
| 1261 | |||
| 1262 | $count ||= $self->selection_count; | ||
| 1263 | |||
| 1264 | my $recent = $self->twitter(user_timeline => { screen_name => $nick, count => $count }) || return; | ||
| 1265 | if ( @$recent == 0 ) { | ||
| 1266 | $self->bot_says($channel, "$nick has no recent tweets"); | ||
| 1267 | return; | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | $self->stash({ | ||
| 1271 | handler => '_handle_retweet', | ||
| 1272 | candidates => [ map $$_{id_str}, @$recent ], | ||
| 1273 | }); | ||
| 1274 | |||
| 1275 | $self->bot_says($channel, 'Which tweet?'); | ||
| 1276 | for ( 1..@$recent ) { | ||
| 1277 | $self->bot_says($channel, "[$_] " . elide($recent->[$_ - 1]{text}, $self->truncate_to)); | ||
| 1278 | } | ||
| 1279 | }; | ||
| 1280 | |||
| 1281 | =item rt I<screen_name> [I<count>] | ||
| 1282 | |||
| 1283 | An alias for the C<retweet> command. | ||
| 1284 | |||
| 1285 | =cut | ||
| 1286 | |||
| 1287 | event cmd_rt => sub { shift->cmd_retweet(@_) }; | ||
| 1288 | |||
| 1289 | sub _handle_retweet { | ||
| 1290 | my ($self, $channel, $index) = @_; | ||
| 1291 | |||
| 1292 | my @candidates = @{$self->stash->{candidates} || []}; | ||
| 1293 | if ( $index =~ /^\d+$/ && 0 < $index && $index <= @candidates ) { | ||
| 1294 | $self->twitter(retweet => { id => $candidates[$index - 1] }); | ||
| 1295 | $self->clear_stash; | ||
| 1296 | return 1; # handled | ||
| 1297 | } | ||
| 1298 | return 0; # unhandled | ||
| 1299 | }; | ||
| 1300 | |||
| 1301 | =item reply I<screen_name> [I<-count>] I<message> | ||
| 1302 | |||
| 1303 | Reply to another user's status. Specify the user by I<screen_name> and select | ||
| 1304 | from a list of recent tweets. Optionally, specify the number of tweets to | ||
| 1305 | display for selection with I<-count> (Defaults to 3.) Note that the count | ||
| 1306 | parameter is prefixed with a dash. | ||
| 1307 | |||
| 1308 | =cut | ||
| 1309 | |||
| 1310 | event cmd_reply => sub { | ||
| 1311 | my ( $self, $channel, $args ) = @_[OBJECT, ARG0, ARG1]; | ||
| 1312 | |||
| 1313 | unless ( defined $args ) { | ||
| 1314 | $self->bot_says($channel, "usage: reply nick [-N] message-text"); | ||
| 1315 | return; | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | my ( $nick, $count, $message ) = $args =~ / | ||
| 1319 | ^@?(\S+) # nick; strip leading @ if there is one | ||
| 1320 | \s+ | ||
| 1321 | (?:-(\d+)\s+)? # optional count: -N | ||
| 1322 | (.*) # the message | ||
| 1323 | /x; | ||
| 1324 | unless ( defined $nick && defined $message ) { | ||
| 1325 | $self->bot_says($channel, "usage: reply nick [-N] message-text"); | ||
| 1326 | return; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | $message = "\@$nick $message"; | ||
| 1330 | return if $self->status_text_too_long($channel, $message); | ||
| 1331 | |||
| 1332 | $count ||= $self->selection_count; | ||
| 1333 | |||
| 1334 | my $recent = $self->twitter(user_timeline => { screen_name => $nick, count => $count }) || return; | ||
| 1335 | if ( @$recent == 0 ) { | ||
| 1336 | $self->bot_says($channel, "$nick has no recent tweets"); | ||
| 1337 | return; | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | $self->stash({ | ||
| 1341 | handler => '_handle_reply', | ||
| 1342 | candidates => [ map $_->{id_str}, @$recent ], | ||
| 1343 | message => $message, | ||
| 1344 | }); | ||
| 1345 | |||
| 1346 | $self->bot_says($channel, 'Which tweet?'); | ||
| 1347 | for ( 1..@$recent ) { | ||
| 1348 | $self->bot_says($channel, "[$_] " . elide($recent->[$_ - 1]{text}, $self->truncate_to)); | ||
| 1349 | } | ||
| 1350 | }; | ||
| 1351 | |||
| 1352 | sub _handle_reply { | ||
| 1353 | my ($self, $channel, $index) = @_; | ||
| 1354 | |||
| 1355 | my @candidates = @{$self->stash->{candidates} || []}; | ||
| 1356 | if ( $index =~ /^\d+$/ && 0 < $index && $index <= @candidates ) { | ||
| 1357 | $self->twitter(update => { | ||
| 1358 | status => $self->stash->{message}, | ||
| 1359 | in_reply_to_status_id => $candidates[$index - 1], | ||
| 1360 | }); | ||
| 1361 | $self->clear_stash; | ||
| 1362 | return 1; # handled | ||
| 1363 | } | ||
| 1364 | return 0; # unhandled | ||
| 1365 | }; | ||
| 1366 | |||
| 1367 | =item report_spam | ||
| 1368 | |||
| 1369 | Report 1 or more screen names as spammers. | ||
| 1370 | |||
| 1371 | =cut | ||
| 1372 | |||
| 1373 | event cmd_report_spam => sub { | ||
| 1374 | my ( $self, $channel, $args ) = @_[OBJECT, ARG0, ARG1]; | ||
| 1375 | |||
| 1376 | unless ( $args ) { | ||
| 1377 | $self->bot_says($channel, "spam requires list of 1 or more spammers"); | ||
| 1378 | return; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | for my $spammer ( split /\s+/, $args ) { | ||
| 1382 | $self->yield(report_spam_helper => $spammer); | ||
| 1383 | } | ||
| 1384 | }; | ||
| 1385 | |||
| 1386 | event report_spam_helper => sub { | ||
| 1387 | my ( $self, $spammer ) = @_[OBJECT, ARG0]; | ||
| 1388 | |||
| 1389 | $self->twitter(report_spam => { screen_name => $spammer }); | ||
| 1390 | }; | ||
| 1391 | |||
| 1392 | =item add I<screen_name> to I<list-slug> | ||
| 1393 | |||
| 1394 | Add a user to one of your lists. | ||
| 1395 | |||
| 1396 | =cut | ||
| 1397 | |||
| 1398 | event cmd_add => sub { $_[OBJECT]->_add_remove_list_member(qw/add to/, @_[ARG0, ARG1]) }; | ||
| 1399 | |||
| 1400 | sub _add_remove_list_member { | ||
| 1401 | my ( $self, $verb, $preposition, $channel, $args ) = @_; | ||
| 1402 | |||
| 1403 | my ( $nick, $slug ) = ($args || '') =~ / | ||
| 1404 | ^@?(\w+) # nick; strip leading @ if there is one | ||
| 1405 | \s+$preposition\s+ | ||
| 1406 | ([-\w]+) # the list-slug | ||
| 1407 | \s*$ | ||
| 1408 | /x; | ||
| 1409 | |||
| 1410 | unless ( defined $nick ) { | ||
| 1411 | $self->bot_says($channel, "usage: $verb <nick> $preposition <list-slug>"); | ||
| 1412 | return; | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | $self->twitter($verb . '_list_member' => { | ||
| 1416 | owner_id => $self->twitter_id, | ||
| 1417 | slug => $slug, | ||
| 1418 | screen_name => $nick, | ||
| 1419 | }); | ||
| 1420 | }; | ||
| 1421 | |||
| 1422 | =item remove I<screen_name> from I<list-slug> | ||
| 1423 | |||
| 1424 | Add a user to one of your lists. | ||
| 1425 | |||
| 1426 | =cut | ||
| 1427 | |||
| 1428 | event cmd_remove => sub { $_[OBJECT]->_add_remove_list_member(qw/remove from/, @_[ARG0, ARG1]) }; | ||
| 1429 | |||
| 1430 | =item help | ||
| 1431 | |||
| 1432 | Display a simple help message | ||
| 1433 | |||
| 1434 | =cut | ||
| 1435 | |||
| 1436 | event cmd_help => sub { | ||
| 1437 | my ($self, $channel, $argstr)=@_[OBJECT, ARG0, ARG1]; | ||
| 1438 | $self->bot_says($channel, "Available commands:"); | ||
| 1439 | $self->bot_says($channel, join ' ' => sort qw/ | ||
| 1440 | post follow unfollow block unblock whois notify favorite | ||
| 1441 | rate_limit_status retweet report_spam | ||
| 1442 | /); | ||
| 1443 | $self->bot_says($channel, '/msg nick for a direct message.') | ||
| 1444 | }; | ||
| 1445 | |||
| 1446 | 1; | ||
| 1447 | |||
| 1448 | __END__ | ||
| 1449 | |||
| 1450 | =item /msg I<id> I<text> | ||
| 1451 | |||
| 1452 | Sends a direct message to Twitter user I<id> using an IRC private message. | ||
| 1453 | |||
| 1454 | =back | ||
| 1455 | |||
| 1456 | =head1 SEE ALSO | ||
| 1457 | |||
| 1458 | L<App::Twirc> | ||
| 1459 | |||
| 1460 | =head1 AUTHOR | ||
| 1461 | |||
| 1462 | Marc Mims <marc@questright.com> | ||
| 1463 | |||
| 1464 | =head1 CONTRIBUTORS | ||
| 1465 | |||
| 1466 | Adam Prime <adam.prime@utoronto.ca> (@adamprime) | ||
| 1467 | |||
| 1468 | =head1 LICENSE | ||
| 1469 | |||
| 1470 | Copyright (c) 2008 Marc Mims | ||
| 1471 | |||
| 1472 | You may distribute this code and/or modify it under the same terms as Perl itself. | ||