1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2010-2017 -- leonerd@leonerd.org.uk
package Protocol::IRC::Client 0.13;
use v5.14;
use warnings;
use base qw( Protocol::IRC );
use Carp;
=head1 NAME
C<Protocol::IRC::Client> - IRC protocol handling for a client
=head1 DESCRIPTION
This mix-in class provides a layer of IRC message handling logic suitable for
an IRC client. It builds upon L<Protocol::IRC> to provide extra message
processing useful to IRC clients, such as handling inbound server numerics.
It provides some of the methods required by C<Protocol::IRC>:
=over 4
=item * isupport
=back
=cut
=head1 INHERITED METHODS
The following methods, inherited from L<Protocol::IRC>, are notable here as
being particularly useful for a client.
=head2 send_message
$irc->send_message( $message )
$irc->send_message( $command, { %args } )
$irc->send_message( $command, $prefix, @args )
See L<Protocol::IRC/send_message>
=cut
=head1 METHODS
=cut
=head2 isupport
$value = $irc->isupport( $key )
Returns an item of information from the server's C<005 ISUPPORT> lines.
Traditionally IRC servers use all-capital names for keys.
=cut
# A few hardcoded defaults from RFC 2812
my %ISUPPORT = (
channame_re => qr/^[#&]/,
prefixflag_re => qr/^[\@+]/,
chanmodes_list => [qw( b k l imnpst )], # TODO: ov
);
sub isupport
{
my $self = shift;
my ( $field ) = @_;
return $self->{Protocol_IRC_isupport}->{$field} // $ISUPPORT{$field};
}
sub on_message_RPL_ISUPPORT
{
my $self = shift;
my ( $message, $hints ) = @_;
my $isupport = $self->{Protocol_IRC_isupport} ||= {};
foreach my $entry ( @{ $hints->{isupport} } ) {
my ( $name, $value ) = $entry =~ m/^([A-Z]+)(?:=(.*))?$/;
$value = 1 if !defined $value;
$isupport->{$name} = $value;
if( $name eq "PREFIX" ) {
my $prefix = $value;
my ( $prefix_modes, $prefix_flags ) = $prefix =~ m/^\(([a-z]+)\)(.+)$/i
or warn( "Unable to parse PREFIX=$value" ), next;
$isupport->{prefix_modes} = $prefix_modes;
$isupport->{prefix_flags} = $prefix_flags;
$isupport->{prefixflag_re} = qr/[$prefix_flags]/;
my %prefix_map;
$prefix_map{substr $prefix_modes, $_, 1} = substr $prefix_flags, $_, 1 for ( 0 .. length($prefix_modes) - 1 );
$isupport->{prefix_map_m2f} = \%prefix_map;
$isupport->{prefix_map_f2m} = { reverse %prefix_map };
}
elsif( $name eq "CHANMODES" ) {
$isupport->{chanmodes_list} = [ split( m/,/, $value ) ];
}
elsif( $name eq "CASEMAPPING" ) {
# TODO
# $self->{nick_folded} = $self->casefold_name( $self->{nick} );
}
elsif( $name eq "CHANTYPES" ) {
$isupport->{channame_re} = qr/^[$value]/;
}
}
return 0;
}
=head2 server_info
$info = $irc->server_info( $key )
Returns an item of information from the server's C<004> line. C<$key> should
one of
=over 8
=item * host
=item * version
=item * usermodes
=item * channelmodes
=back
=cut
sub server_info
{
my $self = shift;
my ( $key ) = @_;
return $self->{Protocol_IRC_server_info}{$key};
}
sub on_message_RPL_MYINFO
{
my $self = shift;
my ( $message, $hints ) = @_;
@{$self->{Protocol_IRC_server_info}}{qw( host version usermodes channelmodes )} =
@{$hints}{qw( serverhost serverversion usermodes channelmodes )};
return 0;
}
=head1 GATING MESSAGES
If messages with a gating disposition are received, extra processing is
applied. Messages whose gating effect is C<more> are simply collected up by
pushing the hints hash to an array. Added to this hash is the command name
itself, so that in the case of multiple message types (for example C<WHOIS>
replies) the individual messages can still be identified.
When the effect of C<done> or C<fail> is eventually received, this collected
array is passed as C<$data> to a handler in one of the following places:
=over 4
=item 1.
A method called C<on_gate_EFFECT_GATE>
$client->on_gate_EFFECT_GATE( $message, $hints, $data )
=item 2.
A method called C<on_gate_EFFECT>
$client->on_gate_EFFECT( 'GATE', $message, $hints, $data )
=item 3.
A method called C<on_gate>
$client->on_gate( 'EFFECT, 'GATE', $message, $hints, $data )
=item 4.
If the gate effect is C<done>, two more places are tried; looking like regular
event handling on a command whose name is the (lowercase) gate name
$client->on_message_GATE( $message, $hints )
$client->on_message( 'GATE', $message, $hints )
=back
For the following types of gate, the C<$data> is further processed in the
following way to provide extra hints fields.
=cut
sub on_message_gate
{
my $self = shift;
my ( $effect, $gate, $message, $hints ) = @_;
my $target = $hints->{target_name_folded} // "*";
if( $effect eq "more" ) {
push @{ $self->{Protocol_IRC_gate}{$gate}{$target} }, {
%$hints,
command => $message->command_name,
};
return 1;
}
my $data = delete $self->{Protocol_IRC_gate}{$gate}{$target};
my @morehints;
if( $effect eq "done" and my $code = $self->can( "prepare_gatehints_$gate" ) ) {
@morehints = $self->$code( $data );
}
my %hints = (
%$hints,
synthesized => 1,
@morehints,
);
my $futures;
if( $futures = $self->{Protocol_IRC_gate_futures}{$gate}{$target} and @$futures ) {
my $f = shift @$futures;
if( $effect eq "done" ) {
$f->done( $message, \%hints, $data );
}
else {
$f->fail( $hints->{text}, irc_gate => $message, \%hints );
}
}
$self->invoke( "on_gate_${effect}_$gate", $message, \%hints, $data ) and $hints{handled} = 1;
$self->invoke( "on_gate_$effect", $gate, $message, \%hints, $data ) and $hints{handled} = 1;
$self->invoke( "on_gate", $effect, $gate, $message, \%hints, $data ) and $hints{handled} = 1;
if( $effect eq "done" ) {
$self->invoke( "on_message_$gate", $message, \%hints ) and $hints{handled} = 1;
$self->invoke( "on_message", $gate, $message, \%hints ) and $hints{handled} = 1;
}
return $hints{handled};
}
=head2 who
The hints hash will contain an extra key, C<who>, which will be an ARRAY ref
containing the lines of the WHO reply. Each line will be a HASH reference
containing:
=over 8
=item user_ident
=item user_host
=item user_server
=item user_nick
=item user_nick_folded
=item user_flags
=back
=cut
sub prepare_gatehints_who
{
my $self = shift;
my ( $data ) = @_;
my @who = map {
my $b = $_;
+{ map { $_ => $b->{$_} } qw( user_ident user_host user_server user_nick user_nick_folded user_flags ) }
} @$data;
return who => \@who;
}
=head2 names
The hints hash will contain an extra key, C<names>, which will be an ARRAY ref
containing the usernames in the channel. Each will be a HASH reference
containing:
=over 8
=item nick
=item flag
=back
=cut
sub prepare_gatehints_names
{
my $self = shift;
my ( $data ) = @_;
my @names = map { @{ $_->{names} } } @$data;
my $prefixflag_re = $self->isupport( 'prefixflag_re' );
my $re = qr/^($prefixflag_re)?(.*)$/;
my %names;
foreach my $name ( @names ) {
my ( $flag, $nick ) = $name =~ $re or next;
$flag ||= ''; # make sure it's defined
$names{ $self->casefold_name( $nick ) } = { nick => $nick, flag => $flag };
}
return names => \%names;
}
=head2 bans
The hints hash will contain an extra key, C<bans>, which will be an ARRAY ref
containing the ban lines. Each line will be a HASH reference containing:
=over 8
=item mask
User mask of the ban
=item by_nick
=item by_nick_folded
Nickname of the user who set the ban
=item timestamp
UNIX timestamp the ban was created
=back
=cut
sub prepare_gatehints_bans
{
my $self = shift;
my ( $data ) = @_;
my @bans = map {
my $b = $_;
+{ map { $_ => $b->{$_} } qw( mask by_nick by_nick_folded timestamp ) }
} @$data;
return bans => \@bans;
}
=head2 motd
The hints hash will contain an extra key, C<motd>, which will be an ARRAY ref
containing the lines of the MOTD.
=cut
sub prepare_gatehints_motd
{
my $self = shift;
my ( $data ) = @_;
return motd => [ map { $_->{text} } @$data ];
}
=head2 whois
The hints hash will contain an extra key, C<whois>, which will be an ARRAY ref
of entries that mostly relate to the received C<RPL_WHOIS*> numerics.
Each C<RPL_WHOIS*> reply will be stripped of the standard hints hash keys,
leaving whatever remains. Added to this will be a key called C<whois>, whose
value will be the command name, minus the leading C<RPL_WHOIS>, and converted
to lowercase.
=cut
use constant STANDARD_HINTS => qw(
prefix_nick prefix_nick_folded
prefix_name prefix_name_folded
prefix_user
prefix_host
target_name target_name_folded
target_is_me
target_type
handled
);
sub prepare_gatehints_whois
{
my $self = shift;
my ( $data ) = @_;
my @whois;
my $channels;
foreach my $h ( @$data ) {
# Just delete all the standard hints from each one
delete @{$h}{STANDARD_HINTS()};
( $h->{whois} = lc delete $h->{command} ) =~ s/^rpl_whois//;
# Combine all the 'channels' results into one list
if( $h->{whois} eq "channels" ) {
if( $channels ) {
push @{$channels->{channels}}, @{$h->{channels}};
next;
}
$channels = $h;
}
push @whois, $h;
}
return whois => \@whois;
}
=head2 join
No additional keys.
=cut
# TODO: maybe JOIN gate should wait for initial events?
=head2 next_gate_future
$f = $client->next_gate_future( $gate, $target )
As an alternative to using the event handlers above, a client can instead
obtain a L<Future> that will succeed or fail the next time a result on a given
gate is received for a given target. This is often more convenient to use in a
client, as it represents the result of running a command.
If the gate completes successfully, then so will the future, yielding the same
values as would be passed to the C<on_gate_done_GATE> event; namely that
( $message, $hints, $data ) = $f->get
If the gate fails, then so will the future, containing the text message from
the error numeric as its failure message, C<irc_gate> as its category, and the
full message and hints for it as the details.
=cut
sub next_gate_future
{
my $self = shift;
my ( $gate, $target ) = @_;
$target = $self->casefold_name( $target // "*" );
my $futures = $self->{Protocol_IRC_gate_futures}{$gate}{$target} //= [];
my $f = $self->new_future;
push @$futures, $f;
$f->on_cancel( sub {
my ( $f ) = @_;
@$futures = grep { $_ != $f } @$futures
});
return $f;
}
=head1 INTERNAL MESSAGE HANDLING
The following messages are handled internally by C<Protocol::IRC::Client>.
=cut
=head2 CAP
This message takes a sub-verb as its second argument, and a list of capability
names as its third. On receipt of a C<CAP> message, the verb is extracted and
set as the C<verb> hint, and the list capabilities set as the keys of a hash
given as the C<caps> hint. These are then passed to an event called
$irc->on_message_cap_VERB( $message, \%hints )
or
$irc->on_message_cap( 'VERB', $message, \%hints )
=cut
sub on_message_CAP
{
my $self = shift;
my ( $message, $hints ) = @_;
my $verb = $message->arg(1);
my %hints = (
%$hints,
verb => $verb,
caps => { map { $_ => 1 } split m/ /, $message->arg(2) },
);
$self->invoke( "on_message_cap_$verb", $message, \%hints ) and $hints{handled} = 1;
$self->invoke( "on_message_cap", $verb, $message, \%hints ) and $hints{handled} = 1;
return $hints{handled};
}
=head2 MODE (on channels) and 324 (RPL_CHANNELMODEIS)
These messages involve channel modes. The raw list of channel modes is parsed
into an array containing one entry per affected piece of data. Each entry will
contain at least a C<type> key, indicating what sort of mode or mode change
it is:
=over 8
=item list
The mode relates to a list; bans, invites, etc..
=item value
The mode sets a value about the channel
=item bool
The mode is a simple boolean flag about the channel
=item occupant
The mode relates to a user in the channel
=back
Every mode type then provides a C<mode> key, containing the mode character
itself, and a C<sense> key which is an empty string, C<+>, or C<->.
For C<list> and C<value> types, the C<value> key gives the actual list entry
or value being set.
For C<occupant> types, a C<flag> key gives the mode converted into an occupant
flag (by the C<prefix_mode2flag> method), C<nick> and C<nick_folded> store the
user name affected.
C<boolean> types do not create any extra keys.
=cut
sub prepare_hints_channelmode
{
my $self = shift;
my ( $message, $hints ) = @_;
my ( $listmodes, $argmodes, $argsetmodes, $boolmodes ) = @{ $self->isupport( 'chanmodes_list' ) };
my $modechars = $hints->{modechars};
my @modeargs = @{ $hints->{modeargs} };
my @modes; # [] -> { type => $, sense => $, mode => $, arg => $ }
my $sense = 0;
foreach my $modechar ( split( m//, $modechars ) ) {
$sense = 1, next if $modechar eq "+";
$sense = -1, next if $modechar eq "-";
my $hasarg;
my $mode = {
mode => $modechar,
sense => $sense,
};
if( index( $listmodes, $modechar ) > -1 ) {
$mode->{type} = 'list';
$mode->{value} = shift @modeargs if ( $sense != 0 );
}
elsif( index( $argmodes, $modechar ) > -1 ) {
$mode->{type} = 'value';
$mode->{value} = shift @modeargs if ( $sense != 0 );
}
elsif( index( $argsetmodes, $modechar ) > -1 ) {
$mode->{type} = 'value';
$mode->{value} = shift @modeargs if ( $sense > 0 );
}
elsif( index( $boolmodes, $modechar ) > -1 ) {
$mode->{type} = 'bool';
}
elsif( my $flag = $self->prefix_mode2flag( $modechar ) ) {
$mode->{type} = 'occupant';
$mode->{flag} = $flag;
$mode->{nick} = shift @modeargs if ( $sense != 0 );
$mode->{nick_folded} = $self->casefold_name( $mode->{nick} );
}
else {
# TODO: Err... not recognised ... what do I do?
}
# TODO: Consider a per-mode event here...
push @modes, $mode;
}
$hints->{modes} = \@modes;
}
sub prepare_hints_MODE
{
my $self = shift;
my ( $message, $hints ) = @_;
if( $hints->{target_type} eq "channel" ) {
$self->prepare_hints_channelmode( $message, $hints );
}
}
sub prepare_hints_RPL_CHANNELMODEIS
{
my $self = shift;
my ( $message, $hints ) = @_;
$self->prepare_hints_channelmode( $message, $hints );
}
=head1 COMMAND-SENDING METHODS
The following methods actually send IRC commands. Each is named after the
underlying IRC command it sends, using capital letters for methods that simply
send that command. They all take a kvlist of named parameters which is used to
construct the message to send, by calling the
L<Protocol::IRC::Message/new_from_named_args> constructor.
=cut
=head2 do_PRIVMSG
=head2 do_NOTICE
$client->do_PRIVMSG( target => $user_or_channel, text => $message )
$client->do_NOTICE( target => $user_or_channel, text => $message )
Sends a C<PRIVMSG> or C<NOTICE> command.
For convenience, a single C<target> argument may be provided which will be
renamed to C<targets>. If C<targets> is an ARRAY reference, it will be turned
into a comma-separated string.
=cut
sub _do_pmlike
{
my $self = shift;
my $command = shift;
my %args = @_;
my $targets =
( ref $args{targets} eq "ARRAY" ) ? join( ",", @{ $args{targets} } ) :
( defined $args{target} ) ? delete $args{target} :
$args{targets};
$self->send_message( $command => { @_, targets => $targets } );
}
sub do_PRIVMSG { shift->_do_pmlike( PRIVMSG => @_ ) }
sub do_NOTICE { shift->_do_pmlike( NOTICE => @_ ) }
=head1 REQUIRED METHODS
As this class is an abstract base class, a concrete implementation must
provide the following methods to complete it and make it useable.
=cut
=head2 new_future
$f = $client->new_future
Returns a new L<Future> instance or subclass thereof.
=cut
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|