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
|
# 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-2021 -- leonerd@leonerd.org.uk
package Net::Async::IRC::Protocol 0.12;
use v5.14;
use warnings;
use base qw( IO::Async::Stream Protocol::IRC );
use Carp;
use Protocol::IRC::Message;
use Encode qw( find_encoding );
use Time::HiRes qw( time );
use IO::Async::Timer::Countdown;
=head1 NAME
C<Net::Async::IRC::Protocol> - send and receive IRC messages
=head1 DESCRIPTION
This subclass of L<IO::Async::Stream> implements an established IRC
connection that has already completed its inital login sequence and is ready
to send and receive IRC messages. It handles base message sending and
receiving, and implements ping timers. This class provides most of the
functionality required for sending and receiving IRC commands and responses
by mixing in from L<Protocol::IRC>.
Objects of this type would not normally be constructed directly. For IRC
clients, see L<Net::Async::IRC> which is a subclass of it. All the events,
parameters, and methods documented below are relevant there.
=cut
=head1 EVENTS
The following events are invoked, either using subclass methods or C<CODE>
references in parameters:
=head2 $handled = on_message
=head2 $handled = on_message_MESSAGE
Invoked on receipt of a valid IRC message. See C<MESSAGE HANDLING> below.
=head2 on_irc_error $err
Invoked on receipt of an invalid IRC message if parsing fails. C<$err> is the
error message text. If left unhandled, any parse error will result in the
connection being immediataely closed, followed by the exception being
re-thrown.
=head2 on_ping_timeout
Invoked if the peer fails to respond to a C<PING> message within the given
timeout.
=head2 on_pong_reply $lag
Invoked when the peer successfully sends a C<PONG> reply response to a C<PING>
message. C<$lag> is the response time in (fractional) seconds.
=cut
=head1 PARAMETERS
The following named parameters may be passed to C<new> or C<configure>:
=over 8
=item on_message => CODE
=item on_message_MESSAGE => CODE
=item on_irc_error => CODE
=item on_ping_timeout => CODE
=item on_pong_reply => CODE
C<CODE> references for event handlers.
=item pingtime => NUM
Amount of quiet time, in seconds, after a message is received from the peer,
until a C<PING> will be sent to check it is still alive.
=item pongtime => NUM
Timeout, in seconds, after sending a C<PING> message, to wait for a C<PONG>
response.
=item encoding => STRING
If supplied, sets an encoding to use to encode outgoing messages and decode
incoming messages.
=back
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$irc = Net::Async::IRC::Protocol->new( %args );
Returns a new instance of a C<Net::Async::IRC::Protocol> object. This object
represents a IRC connection to a peer.
=cut
sub new
{
my $class = shift;
my %args = @_;
my $on_closed = delete $args{on_closed};
return $class->SUPER::new(
%args,
on_closed => sub {
my $self = shift;
my $loop = $self->get_loop;
$self->{pingtimer}->stop;
$self->{pongtimer}->stop;
$on_closed->( $self ) if $on_closed;
undef $self->{connect_f};
undef $self->{login_f};
},
);
}
sub _init
{
my $self = shift;
$self->SUPER::_init( @_ );
my $pingtime = 60;
my $pongtime = 10;
$self->{pingtimer} = IO::Async::Timer::Countdown->new(
delay => $pingtime,
on_expire => sub {
my $now = time();
$self->send_message( "PING", undef, "$now" );
$self->{ping_send_time} = $now;
$self->{pongtimer}->start;
},
);
$self->add_child( $self->{pingtimer} );
$self->{pongtimer} = IO::Async::Timer::Countdown->new(
delay => $pongtime,
on_expire => sub {
$self->{on_ping_timeout}->( $self ) if $self->{on_ping_timeout};
},
);
$self->add_child( $self->{pongtimer} );
}
# for Protocol::IRC
sub encoder
{
my $self = shift;
return $self->{encoder};
}
sub configure
{
my $self = shift;
my %args = @_;
$self->{$_} = delete $args{$_} for grep m/^on_message/, keys %args;
for (qw( on_ping_timeout on_pong_reply on_irc_error )) {
$self->{$_} = delete $args{$_} if exists $args{$_};
}
if( exists $args{pingtime} ) {
$self->{pingtimer}->configure( delay => delete $args{pingtime} );
}
if( exists $args{pongtime} ) {
$self->{pongtimer}->configure( delay => delete $args{pongtime} );
}
if( exists $args{encoding} ) {
my $encoding = delete $args{encoding};
my $obj = find_encoding( $encoding );
defined $obj or croak "Cannot handle an encoding of '$encoding'";
$self->{encoder} = $obj;
}
$self->SUPER::configure( %args );
}
sub incoming_message
{
my $self = shift;
my ( $message ) = @_;
my @shortargs = ( $message->arg( 0 ) );
push @shortargs, $message->arg( 1 ) if $message->command =~ m/^\d+$/;
push @shortargs, "..." if $message->args > 1;
$self->debug_printf( "COMMAND ${\ $message->command } @shortargs" );
return $self->SUPER::incoming_message( @_ );
}
=head1 METHODS
=cut
=head2 is_connected
$connect = $irc->is_connected;
Returns true if a connection to the peer is established. Note that even
after a successful connection, the connection may not yet logged in to. See
also the C<is_loggedin> method.
=cut
sub is_connected
{
my $self = shift;
return 0 unless my $connect_f = $self->{connect_f};
return $connect_f->is_ready && !$connect_f->failure;
}
=head2 is_loggedin
$loggedin = $irc->is_loggedin;
Returns true if the full login sequence has been performed on the connection
and it is ready to use.
=cut
sub is_loggedin
{
my $self = shift;
return 0 unless my $login_f = $self->{login_f};
return $login_f->is_ready && !$login_f->failure;
}
sub on_read
{
my $self = shift;
my ( $buffref, $eof ) = @_;
my $pingtimer = $self->{pingtimer};
$pingtimer->is_running ? $pingtimer->reset : $pingtimer->start;
eval {
$self->Protocol::IRC::on_read( $$buffref );
1;
} and return 0;
my $e = "$@"; chomp $e;
$self->maybe_invoke_event( on_irc_error => $e )
and return 0;
$self->close_now;
die "$e\n";
}
=head2 nick
$nick = $irc->nick;
Returns the current nick in use by the connection.
=cut
sub _set_nick
{
my $self = shift;
( $self->{nick} ) = @_;
$self->{nick_folded} = $self->casefold_name( $self->{nick} );
}
sub nick
{
my $self = shift;
return $self->{nick};
}
=head2 nick_folded
$nick_folded = $irc->nick_folded;
Returns the current nick in use by the connection, folded by C<casefold_name>
for convenience.
=cut
sub nick_folded
{
my $self = shift;
return $self->{nick_folded};
}
=head1 MESSAGE HANDLING
Every incoming message causes a sequence of message handling to occur. First,
the message is parsed, and a hash of data about it is created; this is called
the hints hash. The message and this hash are then passed down a sequence of
potential handlers.
Each handler indicates by return value, whether it considers the message to
have been handled. Processing of the message is not interrupted the first time
a handler declares to have handled a message. Instead, the hints hash is
marked to say it has been handled. Later handlers can still inspect the
message or its hints, using this information to decide if they wish to take
further action.
A message with a command of C<COMMAND> will try handlers in following places:
=over 4
=item 1.
A CODE ref in a parameter called C<on_message_COMMAND>
$on_message_COMMAND->( $irc, $message, \%hints )
=item 2.
A method called C<on_message_COMMAND>
$irc->on_message_COMMAND( $message, \%hints )
=item 3.
A CODE ref in a parameter called C<on_message>
$on_message->( $irc, 'COMMAND', $message, \%hints )
=item 4.
A method called C<on_message>
$irc->on_message( 'COMMAND', $message, \%hints )
=back
As this message handling ability is provided by C<Protocol::IRC>, more details
about how it works and how to use it can be found at
L<Protocol::IRC/MESSAGE HANDLING>.
Additionally, some types of messages receive further processing by
C<Protocol::IRC> and in turn cause new types of events to be invoked. These
are further documented by L<Protocol::IRC/INTERNAL MESSAGE HANDLING>.
=cut
sub invoke
{
my $self = shift;
my $retref = $self->maybe_invoke_event( @_ ) or return undef;
return $retref->[0];
}
sub on_message_PONG
{
my $self = shift;
my ( $message, $hints ) = @_;
return 1 unless $self->{pongtimer}->is_running;
my $lag = time - $self->{ping_send_time};
$self->{current_lag} = $lag;
$self->{on_pong_reply}->( $self, $lag ) if $self->{on_pong_reply};
$self->{pongtimer}->stop;
return 1;
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|