| 1 | package POEx::IRC::Client::Lite; | ||
| 2 | $POEx::IRC::Client::Lite::VERSION = '0.003001'; | ||
| 3 | use strictures 2; | ||
| 4 | |||
| 5 | use Carp 'confess'; | ||
| 6 | |||
| 7 | use POE; | ||
| 8 | use POEx::IRC::Backend; | ||
| 9 | |||
| 10 | use IRC::Message::Object 'ircmsg'; | ||
| 11 | use IRC::Toolkit::Case; | ||
| 12 | use IRC::Toolkit::CTCP; | ||
| 13 | |||
| 14 | use POE::Filter::IRCv3; | ||
| 15 | |||
| 16 | use Scalar::Util 'blessed'; | ||
| 17 | |||
| 18 | use Types::Standard -all; | ||
| 19 | |||
| 20 | use MooX::Role::Pluggable::Constants; | ||
| 21 | |||
| 22 | |||
| 23 | use Moo; | ||
| 24 | with 'MooX::Role::POE::Emitter'; | ||
| 25 | |||
| 26 | |||
| 27 | =for Pod::Coverage has_(\w+) | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | |||
| 32 | has server => ( | ||
| 33 | required => 1, | ||
| 34 | is => 'ro', | ||
| 35 | isa => Str, | ||
| 36 | writer => 'set_server', | ||
| 37 | ); | ||
| 38 | |||
| 39 | has nick => ( | ||
| 40 | required => 1, | ||
| 41 | is => 'ro', | ||
| 42 | isa => Str, | ||
| 43 | writer => 'set_nick', | ||
| 44 | ); | ||
| 45 | |||
| 46 | after set_nick => sub { | ||
| 47 | my ($self, $nick) = @_; | ||
| 48 | if ($self->_has_conn && $self->conn->has_wheel) { | ||
| 49 | ## Try to change IRC nickname as well. | ||
| 50 | $self->nick($nick) | ||
| 51 | } | ||
| 52 | }; | ||
| 53 | |||
| 54 | has bindaddr => ( | ||
| 55 | lazy => 1, | ||
| 56 | is => 'ro', | ||
| 57 | isa => Defined, | ||
| 58 | writer => 'set_bindaddr', | ||
| 59 | predicate => 'has_bindaddr', | ||
| 60 | default => sub { | ||
| 61 | my ($self) = @_; | ||
| 62 | $self->has_ipv6 && $self->ipv6 ? | ||
| 63 | '::0' : '0.0.0.0' | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | has ipv6 => ( | ||
| 68 | lazy => 1, | ||
| 69 | is => 'ro', | ||
| 70 | isa => Bool, | ||
| 71 | writer => 'set_ipv6', | ||
| 72 | predicate => 'has_ipv6', | ||
| 73 | default => sub { 0 }, | ||
| 74 | ); | ||
| 75 | |||
| 76 | has ssl => ( | ||
| 77 | lazy => 1, | ||
| 78 | is => 'ro', | ||
| 79 | isa => Bool, | ||
| 80 | writer => 'set_ssl', | ||
| 81 | default => sub { 0 }, | ||
| 82 | ); | ||
| 83 | |||
| 84 | has pass => ( | ||
| 85 | lazy => 1, | ||
| 86 | is => 'ro', | ||
| 87 | isa => Str, | ||
| 88 | writer => 'set_pass', | ||
| 89 | predicate => 'has_pass', | ||
| 90 | clearer => 'clear_pass', | ||
| 91 | default => sub { '' }, | ||
| 92 | ); | ||
| 93 | |||
| 94 | has port => ( | ||
| 95 | lazy => 1, | ||
| 96 | is => 'ro', | ||
| 97 | isa => Num, | ||
| 98 | writer => 'set_port', | ||
| 99 | predicate => 'has_port', | ||
| 100 | default => sub { 6667 }, | ||
| 101 | ); | ||
| 102 | |||
| 103 | has realname => ( | ||
| 104 | lazy => 1, | ||
| 105 | is => 'ro', | ||
| 106 | isa => Str, | ||
| 107 | writer => 'set_realname', | ||
| 108 | predicate => 'has_realname', | ||
| 109 | default => sub { __PACKAGE__ }, | ||
| 110 | ); | ||
| 111 | |||
| 112 | has reconnect => ( | ||
| 113 | lazy => 1, | ||
| 114 | is => 'ro', | ||
| 115 | isa => Num, | ||
| 116 | writer => 'set_reconnect', | ||
| 117 | default => sub { 120 }, | ||
| 118 | ); | ||
| 119 | |||
| 120 | has username => ( | ||
| 121 | lazy => 1, | ||
| 122 | is => 'ro', | ||
| 123 | isa => Str, | ||
| 124 | writer => 'set_username', | ||
| 125 | predicate => 'has_username', | ||
| 126 | default => sub { 'ircplug' }, | ||
| 127 | ); | ||
| 128 | |||
| 129 | ### Typically internal: | ||
| 130 | has backend => ( | ||
| 131 | lazy => 1, | ||
| 132 | is => 'ro', | ||
| 133 | isa => InstanceOf['POEx::IRC::Backend'], | ||
| 134 | builder => sub { | ||
| 135 | my $filter = POE::Filter::IRCv3->new(colonify => 0); | ||
| 136 | POEx::IRC::Backend->new(filter_irc => $filter) | ||
| 137 | } | ||
| 138 | |||
| 139 | ); | ||
| 140 | |||
| 141 | has conn => ( | ||
| 142 | lazy => 1, | ||
| 143 | weak_ref => 1, | ||
| 144 | is => 'ro', | ||
| 145 | isa => Object, | ||
| 146 | writer => '_set_conn', | ||
| 147 | predicate => '_has_conn', | ||
| 148 | clearer => '_clear_conn', | ||
| 149 | ); | ||
| 150 | |||
| 151 | |||
| 152 | sub BUILD { | ||
| 153 | my ($self) = @_; | ||
| 154 | |||
| 155 | $self->set_object_states( | ||
| 156 | [ | ||
| 157 | $self => [ qw/ | ||
| 158 | ircsock_input | ||
| 159 | ircsock_connector_open | ||
| 160 | ircsock_connector_failure | ||
| 161 | ircsock_disconnect | ||
| 162 | / ], | ||
| 163 | $self => { | ||
| 164 | emitter_started => '_emitter_started', | ||
| 165 | connect => '_connect', | ||
| 166 | disconnect => '_disconnect', | ||
| 167 | send => '_send', | ||
| 168 | privmsg => '_privmsg', | ||
| 169 | ctcp => '_ctcp', | ||
| 170 | notice => '_notice', | ||
| 171 | mode => '_mode', | ||
| 172 | join => '_join', | ||
| 173 | part => '_part', | ||
| 174 | }, | ||
| 175 | ( | ||
| 176 | $self->has_object_states ? @{ $self->object_states } : () | ||
| 177 | ), | ||
| 178 | ], | ||
| 179 | ); | ||
| 180 | |||
| 181 | $self->_start_emitter; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _emitter_started { | ||
| 185 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 186 | $kernel->post( $self->backend->spawn->session_id => 'register' ); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub stop { | ||
| 190 | my ($self) = @_; | ||
| 191 | $poe_kernel->post( $self->backend->session_id => 'shutdown' ); | ||
| 192 | $self->_shutdown_emitter; | ||
| 193 | } | ||
| 194 | |||
| 195 | ### ircsock_* | ||
| 196 | |||
| 197 | sub ircsock_connector_open { | ||
| 198 | my (undef, $self, $conn) = @_[KERNEL, OBJECT, ARG0]; | ||
| 199 | |||
| 200 | $self->_set_conn( $conn ); | ||
| 201 | |||
| 202 | if ($self->process( preregister => $conn ) == EAT_ALL) { | ||
| 203 | $self->_clear_conn; | ||
| 204 | $self->emit( irc_connector_killed => $conn ); | ||
| 205 | return | ||
| 206 | } | ||
| 207 | |||
| 208 | my @pre; | ||
| 209 | if ($self->has_pass && (my $pass = $self->pass)) { | ||
| 210 | push @pre, ircmsg( | ||
| 211 | command => 'pass', | ||
| 212 | params => [ | ||
| 213 | $pass | ||
| 214 | ], | ||
| 215 | ) | ||
| 216 | } | ||
| 217 | $self->send( | ||
| 218 | @pre, | ||
| 219 | ircmsg( | ||
| 220 | command => 'user', | ||
| 221 | params => [ | ||
| 222 | $self->username, | ||
| 223 | '*', '*', | ||
| 224 | $self->realname | ||
| 225 | ], | ||
| 226 | ), | ||
| 227 | ircmsg( | ||
| 228 | command => 'nick', | ||
| 229 | params => [ $self->nick ], | ||
| 230 | ), | ||
| 231 | ); | ||
| 232 | |||
| 233 | $self->emit( irc_connected => $conn ); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub ircsock_connector_failure { | ||
| 237 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 238 | #my $connector = $_[ARG0]; | ||
| 239 | #my ($op, $errno, $errstr) = @_[ARG1 .. ARG3]; | ||
| 240 | |||
| 241 | $self->_clear_conn if $self->_has_conn; | ||
| 242 | |||
| 243 | $self->emit( irc_connector_failed => @_[ARG0 .. $#_] ); | ||
| 244 | |||
| 245 | $self->timer( $self->reconnect => 'connect') if $self->reconnect; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub ircsock_disconnect { | ||
| 249 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 250 | my ($conn, $str) = @_[ARG0, ARG1]; | ||
| 251 | |||
| 252 | $self->_clear_conn if $self->_has_conn; | ||
| 253 | |||
| 254 | $self->emit( irc_disconnected => $str, $conn ); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub ircsock_input { | ||
| 258 | my (undef, $self, $ircev) = @_[KERNEL, OBJECT, ARG1]; | ||
| 259 | |||
| 260 | return unless $ircev->command; | ||
| 261 | $self->emit( 'irc_'.lc($ircev->command) => $ircev) | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | ### Our IRC-related handlers. | ||
| 266 | |||
| 267 | sub N_irc_433 { | ||
| 268 | ## Nickname in use. | ||
| 269 | my (undef, $self) = splice @_, 0, 2; | ||
| 270 | my $ircev = ${ $_[0] }; | ||
| 271 | |||
| 272 | my $taken = $ircev->params->[1] || $self->nick; | ||
| 273 | |||
| 274 | $self->send( | ||
| 275 | ircmsg( | ||
| 276 | command => 'nick', | ||
| 277 | params => [ $taken . '_' ], | ||
| 278 | ) | ||
| 279 | ); | ||
| 280 | |||
| 281 | EAT_NONE | ||
| 282 | } | ||
| 283 | |||
| 284 | sub N_irc_ping { | ||
| 285 | my (undef, $self) = splice @_, 0, 2; | ||
| 286 | my $ircev = ${ $_[0] }; | ||
| 287 | |||
| 288 | $self->send( | ||
| 289 | ircmsg( | ||
| 290 | command => 'pong', | ||
| 291 | params => [ @{ $ircev->params } ], | ||
| 292 | ) | ||
| 293 | ); | ||
| 294 | |||
| 295 | EAT_NONE | ||
| 296 | } | ||
| 297 | |||
| 298 | sub N_irc_privmsg { | ||
| 299 | my (undef, $self) = splice @_, 0, 2; | ||
| 300 | my $ircev = ${ $_[0] }; | ||
| 301 | |||
| 302 | if (my $ctcp_ev = ctcp_extract($ircev)) { | ||
| 303 | $self->emit_now( 'irc_'.$ctcp_ev->command => $ctcp_ev ); | ||
| 304 | return EAT_ALL | ||
| 305 | } | ||
| 306 | |||
| 307 | if ($ircev->has_tags && $ircev->get_tag('intent') eq 'ACTION') { | ||
| 308 | $self->emit_now( irc_ctcp_action => $ircev ); | ||
| 309 | return EAT_ALL | ||
| 310 | } | ||
| 311 | |||
| 312 | my $prefix = substr $ircev->params->[0], 0, 1; | ||
| 313 | ## FIXME | ||
| 314 | ## parse isupports as we get them, attempt to find chan prefixes | ||
| 315 | ## attrib defaulting to following: | ||
| 316 | if (grep {; $_ eq $prefix } ('#', '&', '+') ) { | ||
| 317 | $self->emit_now( irc_public_msg => $ircev ) | ||
| 318 | } else { | ||
| 319 | $self->emit_now( irc_private_msg => $ircev ) | ||
| 320 | } | ||
| 321 | |||
| 322 | EAT_ALL | ||
| 323 | } | ||
| 324 | |||
| 325 | sub N_irc_notice { | ||
| 326 | my (undef, $self) = splice @_, 0, 2; | ||
| 327 | my $ircev = ${ $_[0] }; | ||
| 328 | |||
| 329 | if (my $ctcp_ev = ctcp_extract($ircev)) { | ||
| 330 | $self->emit_now( 'irc_'.$ctcp_ev->command => $ctcp_ev ); | ||
| 331 | return EAT_ALL | ||
| 332 | } | ||
| 333 | |||
| 334 | EAT_NONE | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | ### Public | ||
| 340 | |||
| 341 | ## Since the retval of yield() is $self, many of these can be chained: | ||
| 342 | ## $client->connect->join(@channels)->privmsg( | ||
| 343 | ## join(',', @channels), 'hello!' | ||
| 344 | ## ); | ||
| 345 | |||
| 346 | sub connect { | ||
| 347 | my $self = shift; | ||
| 348 | $self->yield( connect => @_ ) | ||
| 349 | } | ||
| 350 | |||
| 351 | sub _connect { | ||
| 352 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 353 | |||
| 354 | $self->backend->create_connector( | ||
| 355 | remoteaddr => $self->server, | ||
| 356 | remoteport => $self->port, | ||
| 357 | ssl => $self->ssl, | ||
| 358 | ( | ||
| 359 | $self->has_ipv6 ? (ipv6 => $self->ipv6) : () | ||
| 360 | ), | ||
| 361 | ( | ||
| 362 | $self->has_bindaddr ? (bindaddr => $self->bindaddr) : () | ||
| 363 | ), | ||
| 364 | ); | ||
| 365 | } | ||
| 366 | |||
| 367 | sub disconnect { | ||
| 368 | my $self = shift; | ||
| 369 | $self->yield( disconnect => @_ ) | ||
| 370 | } | ||
| 371 | |||
| 372 | sub _disconnect { | ||
| 373 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 374 | my $message = $_[ARG0] // 'Leaving'; | ||
| 375 | |||
| 376 | $self->backend->send( | ||
| 377 | ircmsg( | ||
| 378 | command => 'quit', | ||
| 379 | params => [ $message ], | ||
| 380 | ), | ||
| 381 | $self->conn->wheel_id | ||
| 382 | ); | ||
| 383 | |||
| 384 | $kernel->alarm('connect'); | ||
| 385 | |||
| 386 | $self->backend->disconnect( $self->conn->wheel->ID ) | ||
| 387 | if $self->_has_conn and $self->conn->has_wheel; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub send_raw_line { | ||
| 391 | my ($self, $line) = @_; | ||
| 392 | confess "Expected a raw line" unless defined $line; | ||
| 393 | $self->send( ircmsg(raw_line => $line) ); | ||
| 394 | } | ||
| 395 | |||
| 396 | sub send { | ||
| 397 | my $self = shift; | ||
| 398 | $self->yield( send => @_ ) | ||
| 399 | } | ||
| 400 | |||
| 401 | sub _send { | ||
| 402 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 403 | for my $outev (@_[ARG0 .. $#_]) { | ||
| 404 | if ($self->process( outgoing => $outev ) == EAT_ALL) { | ||
| 405 | next | ||
| 406 | } | ||
| 407 | $self->backend->send( $outev, $self->conn->wheel_id ) | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | ## Sugar, and POE-dispatchable counterparts. | ||
| 412 | sub notice { | ||
| 413 | my $self = shift; | ||
| 414 | $self->yield( notice => @_ ) | ||
| 415 | } | ||
| 416 | |||
| 417 | sub _notice { | ||
| 418 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 419 | my ($target, @data) = @_[ARG0 .. $#_]; | ||
| 420 | $self->send( | ||
| 421 | ircmsg( | ||
| 422 | command => 'notice', | ||
| 423 | params => [ $target, join ' ', @data ] | ||
| 424 | ) | ||
| 425 | ) | ||
| 426 | } | ||
| 427 | |||
| 428 | sub privmsg { | ||
| 429 | my $self = shift; | ||
| 430 | $self->yield( privmsg => @_ ) | ||
| 431 | } | ||
| 432 | |||
| 433 | sub _privmsg { | ||
| 434 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 435 | my ($target, @data) = @_[ARG0 .. $#_]; | ||
| 436 | $self->send( | ||
| 437 | ircmsg( | ||
| 438 | command => 'privmsg', | ||
| 439 | params => [ $target, join ' ', @data ] | ||
| 440 | ) | ||
| 441 | ) | ||
| 442 | } | ||
| 443 | |||
| 444 | sub ctcp { | ||
| 445 | my $self = shift; | ||
| 446 | $self->yield( ctcp => @_ ) | ||
| 447 | } | ||
| 448 | |||
| 449 | sub _ctcp { | ||
| 450 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 451 | my ($type, $target, @data) = @_[ARG0 .. $#_]; | ||
| 452 | my $line = join ' ', uc($type), @data; | ||
| 453 | my $quoted = ctcp_quote($line); | ||
| 454 | $self->send( | ||
| 455 | ircmsg( | ||
| 456 | command => 'privmsg', | ||
| 457 | params => [ $target, $quoted ] | ||
| 458 | ) | ||
| 459 | ) | ||
| 460 | } | ||
| 461 | |||
| 462 | sub mode { | ||
| 463 | my $self = shift; | ||
| 464 | $self->yield( mode => @_ ) | ||
| 465 | } | ||
| 466 | |||
| 467 | sub _mode { | ||
| 468 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 469 | my ($target, $mode) = @_[ARG0, ARG1]; | ||
| 470 | |||
| 471 | if (blessed $mode && $mode->isa('IRC::Mode::Set')) { | ||
| 472 | ## FIXME tests for same | ||
| 473 | ## FIXME accept an opt to allow passing in MODES= ? | ||
| 474 | ## don't really want to parse/store isupport here | ||
| 475 | ## (stateful subclasses should worry about it) | ||
| 476 | for my $set ($mode->split_mode_set(4)) { | ||
| 477 | $self->mode( $target, $set->mode_string ) | ||
| 478 | } | ||
| 479 | return $self | ||
| 480 | } | ||
| 481 | |||
| 482 | $self->send( | ||
| 483 | ircmsg( | ||
| 484 | command => 'mode', | ||
| 485 | params => [ $target, $mode ], | ||
| 486 | ) | ||
| 487 | ) | ||
| 488 | } | ||
| 489 | |||
| 490 | sub join { | ||
| 491 | my $self = shift; | ||
| 492 | $self->yield( join => @_ ) | ||
| 493 | } | ||
| 494 | |||
| 495 | sub _join { | ||
| 496 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 497 | my $join_to = CORE::join ',', @_[ARG0 .. $#_]; | ||
| 498 | $self->send( | ||
| 499 | ircmsg( | ||
| 500 | command => 'join', | ||
| 501 | params => [ $join_to ], | ||
| 502 | ) | ||
| 503 | ) | ||
| 504 | } | ||
| 505 | |||
| 506 | sub part { | ||
| 507 | my $self = shift; | ||
| 508 | $self->yield( part => @_ ) | ||
| 509 | } | ||
| 510 | |||
| 511 | sub _part { | ||
| 512 | my (undef, $self) = @_[KERNEL, OBJECT]; | ||
| 513 | my ($channel, $msg) = @_[ARG0, ARG1]; | ||
| 514 | $self->send( | ||
| 515 | ircmsg( | ||
| 516 | command => 'part', | ||
| 517 | params => [ $channel, $msg ], | ||
| 518 | ) | ||
| 519 | ); | ||
| 520 | } | ||
| 521 | |||
| 522 | 1; | ||
| 523 | |||
| 524 | =pod | ||
| 525 | |||
| 526 | =head1 NAME | ||
| 527 | |||
| 528 | POEx::IRC::Client::Lite - Minimalist POE IRC interface | ||
| 529 | |||
| 530 | =head1 SYNOPSIS | ||
| 531 | |||
| 532 | package MyClient; | ||
| 533 | use POE; | ||
| 534 | use POEx::IRC::Client::Lite; | ||
| 535 | use IRC::Toolkit; | ||
| 536 | |||
| 537 | our @channels = ( '#otw', '#eris' ); | ||
| 538 | |||
| 539 | POE::Session->create( | ||
| 540 | package_states => [ | ||
| 541 | MyClient => [ qw/ | ||
| 542 | _start | ||
| 543 | recv_irc_001 | ||
| 544 | recv_irc_public_msg | ||
| 545 | recv_irc_ctcp_version | ||
| 546 | / ], | ||
| 547 | ], | ||
| 548 | ); | ||
| 549 | |||
| 550 | sub _start { | ||
| 551 | my ($kern, $heap) = @_[KERNEL, HEAP]; | ||
| 552 | |||
| 553 | $heap->{irc} = POEx::IRC::Client::Lite->new( | ||
| 554 | event_prefix => 'recv_', | ||
| 555 | server => "irc.perl.org", | ||
| 556 | nick => "MyNick", | ||
| 557 | username => "myuser", | ||
| 558 | ); | ||
| 559 | |||
| 560 | $heap->{irc}->connect; | ||
| 561 | } | ||
| 562 | |||
| 563 | sub recv_irc_001 { | ||
| 564 | my ($kern, $heap) = @_[KERNEL, HEAP]; | ||
| 565 | |||
| 566 | $heap->{irc}->join(@channels)->privmsg( | ||
| 567 | join(',', @channels), "hello!" | ||
| 568 | ); | ||
| 569 | } | ||
| 570 | |||
| 571 | sub recv_irc_public_msg { | ||
| 572 | my ($kern, $heap) = @_[KERNEL, HEAP]; | ||
| 573 | my $event = $_[ARG0]; | ||
| 574 | |||
| 575 | my ($target, $string) = @{ $event->params }; | ||
| 576 | my $from = parse_user( $event->prefix ); | ||
| 577 | |||
| 578 | if (lc($string||'') eq 'hello') { | ||
| 579 | $heap->{irc}->privmsg($target, "hello there, $from") | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | sub recv_irc_ctcp_version { | ||
| 584 | my ($kern, $heap) = @_[KERNEL, HEAP]; | ||
| 585 | my $event = $_[ARG0]; | ||
| 586 | |||
| 587 | my $from = parse_user( $event->prefix ); | ||
| 588 | |||
| 589 | $heap->{irc}->notice( $from => | ||
| 590 | ctcp_quote("VERSION a silly Client::Lite example") | ||
| 591 | ); | ||
| 592 | } | ||
| 593 | |||
| 594 | =head1 DESCRIPTION | ||
| 595 | |||
| 596 | A very thin (but pluggable / extensible) IRC client library using | ||
| 597 | L<POEx::IRC::Backend> and L<IRC::Toolkit> on top of | ||
| 598 | L<MooX::Role::POE::Emitter> and L<MooX::Role::Pluggable>. | ||
| 599 | |||
| 600 | No state is maintained; POEx::IRC::Client::Lite provides a | ||
| 601 | minimalist interface to IRC and serves as a base class for stateful clients. | ||
| 602 | |||
| 603 | This is early development software pulled out of a much larger in-progress | ||
| 604 | project. | ||
| 605 | B<< See L<POE::Component::IRC> for a more mature POE IRC client library. >> | ||
| 606 | |||
| 607 | =head2 new | ||
| 608 | |||
| 609 | my $irc = POEx::IRC::Client::Lite->new( | ||
| 610 | event_prefix => $prefix, | ||
| 611 | server => $server, | ||
| 612 | nick => $nickname, | ||
| 613 | username => $username, | ||
| 614 | ); | ||
| 615 | |||
| 616 | Create a new Client::Lite instance. Optional arguments are: | ||
| 617 | |||
| 618 | =over | ||
| 619 | |||
| 620 | =item bindaddr | ||
| 621 | |||
| 622 | Local address to bind to. | ||
| 623 | |||
| 624 | =item ipv6 | ||
| 625 | |||
| 626 | Boolean value indicating whether to prefer IPv6. | ||
| 627 | |||
| 628 | =item port | ||
| 629 | |||
| 630 | Remote port to use (defaults to 6667). | ||
| 631 | |||
| 632 | =item ssl | ||
| 633 | |||
| 634 | Boolean value indicating whether to (attempt to) connect via SSL. | ||
| 635 | |||
| 636 | Requires L<POE::Component::SSLify>. | ||
| 637 | |||
| 638 | =item reconnect | ||
| 639 | |||
| 640 | Reconnection attempt delay, in seconds. | ||
| 641 | |||
| 642 | B<< Automatic reconnection is only triggered when an outgoing connector fails! | ||
| 643 | >> | ||
| 644 | |||
| 645 | You can trigger a reconnection in your own code by handling | ||
| 646 | L</irc_disconnected> events. For example: | ||
| 647 | |||
| 648 | sub irc_disconnected { | ||
| 649 | # Immediate reconnect; you may want to use a timer (to avoid being banned) | ||
| 650 | # Assuming our IRC component's object lives in our session's HEAP: | ||
| 651 | $_[HEAP]->{irc}->connect | ||
| 652 | } | ||
| 653 | |||
| 654 | =back | ||
| 655 | |||
| 656 | =head2 stop | ||
| 657 | |||
| 658 | $irc->stop; | ||
| 659 | |||
| 660 | Disconnect, stop the Emitter, and purge the plugin pipeline. | ||
| 661 | |||
| 662 | =head2 IRC Methods | ||
| 663 | |||
| 664 | IRC-related methods can be called via normal method dispatch or sent as a POE | ||
| 665 | event: | ||
| 666 | |||
| 667 | ## These are equivalent: | ||
| 668 | $irc->send( $ircevent ); | ||
| 669 | $irc->yield( 'send', $ircevent ); | ||
| 670 | $poe_kernel->post( $irc->session_id, 'send', $ircevent ); | ||
| 671 | |||
| 672 | Methods that dispatch to IRC return C<$self>, so they can be chained: | ||
| 673 | |||
| 674 | $irc->connect->join(@channels)->privmsg( | ||
| 675 | join(',', @channels), | ||
| 676 | 'hello there!' | ||
| 677 | ); | ||
| 678 | |||
| 679 | =head3 connect | ||
| 680 | |||
| 681 | $irc->connect; | ||
| 682 | |||
| 683 | Attempt an outgoing connection. | ||
| 684 | |||
| 685 | =head3 disconnect | ||
| 686 | |||
| 687 | $irc->disconnect($message); | ||
| 688 | |||
| 689 | Quit IRC and shut down the wheel. | ||
| 690 | |||
| 691 | =head3 send | ||
| 692 | |||
| 693 | use IRC::Message::Object 'ircmsg'; | ||
| 694 | $irc->send( | ||
| 695 | ircmsg( | ||
| 696 | command => 'oper', | ||
| 697 | params => [ $user, $passwd ], | ||
| 698 | ) | ||
| 699 | ); | ||
| 700 | |||
| 701 | ## ... or a raw HASH: | ||
| 702 | $irc->send( | ||
| 703 | { | ||
| 704 | command => 'oper', | ||
| 705 | params => [ $user, $passwd ], | ||
| 706 | } | ||
| 707 | ) | ||
| 708 | |||
| 709 | ## ... or a raw line: | ||
| 710 | $irc->send_raw_line('PRIVMSG avenj :some things'); | ||
| 711 | |||
| 712 | Use C<send()> to send an L<IRC::Message::Object> or a compatible | ||
| 713 | HASH; this method will also take a list of events in either of those formats. | ||
| 714 | |||
| 715 | =head3 send_raw_line | ||
| 716 | |||
| 717 | Use C<send_raw_line()> to send a single raw IRC line. This is rarely a good | ||
| 718 | idea; L<POEx::IRC::Backend> provides an IRCv3-capable filter. | ||
| 719 | |||
| 720 | =head3 set_nick | ||
| 721 | |||
| 722 | $irc->set_nick( $new_nick ); | ||
| 723 | |||
| 724 | Attempt to change the current nickname. | ||
| 725 | |||
| 726 | =head3 privmsg | ||
| 727 | |||
| 728 | $irc->privmsg( $target, $string ); | ||
| 729 | |||
| 730 | Sends a PRIVMSG to the specified target. | ||
| 731 | |||
| 732 | =head3 notice | ||
| 733 | |||
| 734 | $irc->notice( $target, $string ); | ||
| 735 | |||
| 736 | Sends a NOTICE to the specified target. | ||
| 737 | |||
| 738 | =head3 ctcp | ||
| 739 | |||
| 740 | $irc->ctcp( $target, $type, @params ); | ||
| 741 | |||
| 742 | Encodes and sends a CTCP B<request> to the target. | ||
| 743 | (To send a CTCP B<reply>, send a L</notice> that has been quoted via | ||
| 744 | L<IRC::Toolkit::CTCP/"ctcp_quote">.) | ||
| 745 | |||
| 746 | =head3 mode | ||
| 747 | |||
| 748 | $irc->mode( $channel, $modestring ); | ||
| 749 | |||
| 750 | Sends a MODE for the specified target. | ||
| 751 | |||
| 752 | Takes a channel name as a string and a mode change as either a string or an | ||
| 753 | L<IRC::Mode::Set>. | ||
| 754 | |||
| 755 | =head3 join | ||
| 756 | |||
| 757 | $irc->join( $channel ); | ||
| 758 | |||
| 759 | Attempts to join the specified channel. | ||
| 760 | |||
| 761 | =head3 part | ||
| 762 | |||
| 763 | $irc->part( $channel, $message ); | ||
| 764 | |||
| 765 | Attempts to leave the specified channel with an optional PART message. | ||
| 766 | |||
| 767 | =head2 Attributes | ||
| 768 | |||
| 769 | =head3 conn | ||
| 770 | |||
| 771 | The L<POEx::IRC::Backend::Connect> instance for our connection. | ||
| 772 | |||
| 773 | =head3 nick | ||
| 774 | |||
| 775 | The nickname we were spawned with. | ||
| 776 | |||
| 777 | This class doesn't track nick changes; if our nick is changed later, ->nick() | ||
| 778 | is not updated. | ||
| 779 | |||
| 780 | =head3 server | ||
| 781 | |||
| 782 | The server we were instructed to connect to. | ||
| 783 | |||
| 784 | =head1 Emitted Events | ||
| 785 | |||
| 786 | =head2 IRC events | ||
| 787 | |||
| 788 | All IRC events are emitted as 'irc_$cmd' e.g. 'irc_005' (ISUPPORT) or | ||
| 789 | 'irc_mode' with a few notable exceptions, detailed below. | ||
| 790 | |||
| 791 | C<$_[ARG0]> is the L<IRC::Message::Object>. | ||
| 792 | |||
| 793 | =head2 Special events | ||
| 794 | |||
| 795 | =head3 irc_connected | ||
| 796 | |||
| 797 | Emitted when a connection has been successfully opened. | ||
| 798 | |||
| 799 | This does not indicate successful server registration, only that the | ||
| 800 | connection has been opened and registration details have been sent. | ||
| 801 | |||
| 802 | C<$_[ARG0]> is the L<POEx::IRC::Backend::Connect> object. | ||
| 803 | |||
| 804 | =head3 irc_connector_failed | ||
| 805 | |||
| 806 | Emitted if an outgoing connection could not be established. | ||
| 807 | |||
| 808 | C<< @_[ARG0 .. ARG3] >> are the operation, errno, and error string passed in | ||
| 809 | by L<POEx::IRC::Backend>; see L<POEx::IRC::Backend/ircsock_connector_failure>. | ||
| 810 | |||
| 811 | =head3 irc_connector_killed | ||
| 812 | |||
| 813 | Emitted if a connection is terminated during L</preregister>. | ||
| 814 | |||
| 815 | C<$_[ARG0]> is the L<POEx::IRC::Backend::Connect> object. | ||
| 816 | |||
| 817 | =head3 irc_private_message | ||
| 818 | |||
| 819 | Emitted for PRIVMSG-type messages not covered by L</irc_public_message>. | ||
| 820 | |||
| 821 | =head3 irc_public_message | ||
| 822 | |||
| 823 | Emitted for PRIVMSG-type messages that appear to be destined for a channel | ||
| 824 | target. | ||
| 825 | |||
| 826 | =head3 irc_ctcp_TYPE | ||
| 827 | |||
| 828 | Emitted for incoming CTCP requests. TYPE is the request type, such as | ||
| 829 | 'version' | ||
| 830 | |||
| 831 | C<$_[ARG0]> is the L<IRC::Message::Object> produced by | ||
| 832 | L<IRC::Toolkit::CTCP/ctcp_extract>. | ||
| 833 | |||
| 834 | An example of sending a CTCP reply lives in L</SYNOPSIS>. | ||
| 835 | See L<IRC::Toolkit::CTCP> for CTCP-related helpers. | ||
| 836 | |||
| 837 | =head3 irc_ctcpreply_TYPE | ||
| 838 | |||
| 839 | Emitted for incoming CTCP replies. | ||
| 840 | |||
| 841 | Mirrors the behavior of L</irc_ctcp_TYPE> | ||
| 842 | |||
| 843 | =head3 irc_disconnected | ||
| 844 | |||
| 845 | Emitted when an IRC connection has been disconnected at the backend. | ||
| 846 | |||
| 847 | C<$_[ARG0]> is the disconnect string from L<POEx::IRC::Backend>. | ||
| 848 | |||
| 849 | C<$_[ARG1]> is the L<POEx::IRC::Backend::Connect> that was disconnected. | ||
| 850 | |||
| 851 | =head1 Pluggable Events | ||
| 852 | |||
| 853 | These are events explicitly dispatched to plugins | ||
| 854 | via L<MooX::Role::POE::Emitter/process>; | ||
| 855 | see L<MooX::Role::POE::Emitter> and L<MooX::Role::Pluggable> for more on | ||
| 856 | making use of plugins. | ||
| 857 | |||
| 858 | =head2 preregister | ||
| 859 | |||
| 860 | Dispatched to plugins when an outgoing connection has been established, | ||
| 861 | but prior to registration. | ||
| 862 | |||
| 863 | The first argument is the L<POEx::IRC::Backend::Connect> object. | ||
| 864 | |||
| 865 | Returning EAT_ALL (see L<MooX::Role::Pluggable::Constants>) to Client::Lite | ||
| 866 | will terminate the connection without registering. | ||
| 867 | |||
| 868 | =head2 outgoing | ||
| 869 | |||
| 870 | Dispatched to plugins prior to sending output. | ||
| 871 | |||
| 872 | The first argument is the item being sent. Note that no sanity checks are | ||
| 873 | performed on the item(s) at this stage (this is done after items are passed to | ||
| 874 | the L<POEx::IRC::Backend> instance) -- your plugin's handler could receive a | ||
| 875 | HASH, an L<IRC::Message::Object>, a raw line, or something invalid. | ||
| 876 | |||
| 877 | Returning EAT_ALL will skip sending the item. | ||
| 878 | |||
| 879 | =head1 SEE ALSO | ||
| 880 | |||
| 881 | L<POE::Component::IRC>, a fully-featured POE IRC client library | ||
| 882 | |||
| 883 | L<IRC::Toolkit> | ||
| 884 | |||
| 885 | L<POEx::IRC::Backend> | ||
| 886 | |||
| 887 | L<POE::Filter::IRCv3> | ||
| 888 | |||
| 889 | L<MooX::Role::POE::Emitter> | ||
| 890 | |||
| 891 | L<MooX::Role::Pluggable> | ||
| 892 | |||
| 893 | =head1 AUTHOR | ||
| 894 | |||
| 895 | Jon Portnoy <avenj@cobaltirc.org> | ||
| 896 | |||
| 897 | =begin Pod::Coverage | ||
| 898 | |||
| 899 | BUILD | ||
| 900 | N_(?i:[A-Z0-9_])+ | ||
| 901 | ircsock_(?i:[A-Z_])+ | ||
| 902 | |||
| 903 | =end Pod::Coverage | ||
| 904 | |||
| 905 | =cut |