blib/lib/Net/Async/IRC/Protocol.pm

filter: Statementstmt 91.8% 102 / 111 Branchbran 79.4% 27 / 34 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 95.8% 23 / 24 Podpod 90.9% 10 / 11 totaltotal 86.3% 164 / 190 CC 22 SCAR 12.2
CC
22
Cov
87%
configure
8.0
__ANON__
6.0
on_read
4.1
CRAP 3.4
« Build ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2015 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::IRC::Protocol;
7  
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $VERSION = '0.11';
12  
13 7 ▶use base qw( IO::Async::Stream Protocol::IRC );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use Protocol::IRC::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 ▶use Encode qw( find_encoding );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 ▶use IO::Async::Timer::Countdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 C<Net::Async::IRC::Protocol> - send and receive IRC messages
27  
28 =head1 DESCRIPTION
29  
30 This subclass of L<IO::Async::Stream> implements an established IRC
31 connection that has already completed its inital login sequence and is ready
32 to send and receive IRC messages. It handles base message sending and
33 receiving, and implements ping timers. This class provides most of the
34 functionality required for sending and receiving IRC commands and responses
35 by mixing in from L<Protocol::IRC>.
36  
37 Objects of this type would not normally be constructed directly. For IRC
38 clients, see L<Net::Async::IRC> which is a subclass of it. All the events,
39 parameters, and methods documented below are relevant there.
40  
41 =cut
42  
43 =head1 EVENTS
44  
45 The following events are invoked, either using subclass methods or C<CODE>
46 references in parameters:
47  
48 =head2 $handled = on_message
49  
50 =head2 $handled = on_message_MESSAGE
51  
52 Invoked on receipt of a valid IRC message. See C<MESSAGE HANDLING> below.
53  
54 =head2 on_irc_error $err
55  
56 Invoked on receipt of an invalid IRC message if parsing fails. C<$err> is the
57 error message text. If left unhandled, any parse error will result in the
58 connection being immediataely closed, followed by the exception being
59 re-thrown.
60  
61 =head2 on_ping_timeout
62  
63 Invoked if the peer fails to respond to a C<PING> message within the given
64 timeout.
65  
66 =head2 on_pong_reply $lag
67  
68 Invoked when the peer successfully sends a C<PONG> reply response to a C<PING>
69 message. C<$lag> is the response time in (fractional) seconds.
70  
71 =cut
72  
73 =head1 PARAMETERS
74  
75 The following named parameters may be passed to C<new> or C<configure>:
76  
77 =over 8
78  
79 =item on_message => CODE
80  
81 =item on_message_MESSAGE => CODE
82  
83 =item on_irc_error => CODE
84  
85 =item on_ping_timeout => CODE
86  
87 =item on_pong_reply => CODE
88  
89 C<CODE> references for event handlers.
90  
91 =item pingtime => NUM
92  
93 Amount of quiet time, in seconds, after a message is received from the peer,
94 until a C<PING> will be sent to check it is still alive.
95  
96 =item pongtime => NUM
97  
98 Timeout, in seconds, after sending a C<PING> message, to wait for a C<PONG>
99 response.
100  
101 =item encoding => STRING
102  
103 If supplied, sets an encoding to use to encode outgoing messages and decode
104 incoming messages.
105  
106 =back
107  
108 =cut
109  
110 =head1 CONSTRUCTOR
111  
112 =cut
113  
114 =head2 new
115  
116 $irc = Net::Async::IRC::Protocol->new( %args )
117  
118 Returns a new instance of a C<Net::Async::IRC::Protocol> object. This object
119 represents a IRC connection to a peer.
120  
121 =cut
122  
123 sub new
124 {
125 7 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=2.9
Pod documented
126 7 my %args = @_;
127  
128 7 my $on_closed = delete $args{on_closed};
129  
130 return $class->SUPER::new(
131 %args,
132  
133 on_closed => sub {
134 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
135  
136 0 my $loop = $self->get_loop;
137  
138 0 $self->{pingtimer}->stop;
139 0 $self->{pongtimer}->stop;
140  
141 0 ▶ $on_closed->( $self ) if $on_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $on_closed 0 0 0
142  
143 0 undef $self->{connect_f};
144 0 undef $self->{login_f};
145 },
146 7 );
147 }
148  
149 sub _init
150 {
151 7 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
152 7 $self->SUPER::_init( @_ );
153  
154 7 my $pingtime = 60;
155 7 my $pongtime = 10;
156  
157 $self->{pingtimer} = IO::Async::Timer::Countdown->new(
158 delay => $pingtime,
159  
160 on_expire => sub {
161 2 ▶ my $now = time();
Subroutine __ANON__: called CC=1 SCAR=0.0
162  
163 2 $self->send_message( "PING", undef, "$now" );
164  
165 2 $self->{ping_send_time} = $now;
166  
167 2 $self->{pongtimer}->start;
168 },
169 7 );
170 7 $self->add_child( $self->{pingtimer} );
171  
172 $self->{pongtimer} = IO::Async::Timer::Countdown->new(
173 delay => $pongtime,
174  
175 on_expire => sub {
176 1 ▶ $self->{on_ping_timeout}->( $self ) if $self->{on_ping_timeout};
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_ping_timeout"} 1 0 1
177 },
178 7 );
179 7 $self->add_child( $self->{pongtimer} );
180 }
181  
182 # for Protocol::IRC
183 sub encoder
184 {
185 27 ▶ my $self = shift;
Subroutine encoder: called CC=1 SCAR=0.0
Pod documented
186 27 return $self->{encoder};
187 }
188  
189 sub configure
190 {
191 9 ▶ my $self = shift;
Subroutine configure: called CC=8 SCAR=20.8
Pod documented
192 9 my %args = @_;
193  
194 9 $self->{$_} = delete $args{$_} for grep m/^on_message/, keys %args;
195  
196 9 for (qw( on_ping_timeout on_pong_reply on_irc_error )) {
197 27 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 4 23 27
198 }
199  
200 9 ▶ if( exists $args{pingtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"pingtime"}) 1 8 9
201 1 $self->{pingtimer}->configure( delay => delete $args{pingtime} );
202 }
203  
204 9 ▶ if( exists $args{pongtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"pongtime"}) 1 8 9
205 1 $self->{pongtimer}->configure( delay => delete $args{pongtime} );
206 }
207  
208 9 ▶ if( exists $args{encoding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"encoding"}) 1 8 9
209 1 my $encoding = delete $args{encoding};
210 1 my $obj = find_encoding( $encoding );
211 1 ▶ defined $obj or croak "Cannot handle an encoding of '$encoding'";
Branch: 1 branch
BranchTrueFalseTotal
defined $obj or croak("Cannot handle an encoding of '${encoding}'") 0 1 1
212 1 $self->{encoder} = $obj;
213 }
214  
215 9 $self->SUPER::configure( %args );
216 }
217  
218 sub incoming_message
219 {
220 13 ▶ my $self = shift;
Subroutine incoming_message: called CC=3 SCAR=11.0
Pod documented
221 13 my ( $message ) = @_;
222  
223 13 my @shortargs = ( $message->arg( 0 ) );
224 13 ▶ push @shortargs, $message->arg( 1 ) if $message->command =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $message->command =~ /^\d+$/ 6 7 13
225 13 ▶ push @shortargs, "..." if $message->args > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $message->args > 1 8 5 13
226  
227 13 $self->debug_printf( "COMMAND ${\ $message->command } @shortargs" );
228  
229 13 return $self->SUPER::incoming_message( @_ );
230 }
231  
232 =head1 METHODS
233  
234 =cut
235  
236 =head2 is_connected
237  
238 $connect = $irc->is_connected
239  
240 Returns true if a connection to the peer is established. Note that even
241 after a successful connection, the connection may not yet logged in to. See
242 also the C<is_loggedin> method.
243  
244 =cut
245  
246 sub is_connected
247 {
248 6 ▶ my $self = shift;
Subroutine is_connected: called CC=3 SCAR=11.4
Pod documented
249 6 ▶ return 0 unless my $connect_f = $self->{connect_f};
Branch: 1 branch
BranchTrueFalseTotal
unless my $connect_f = $self->{"connect_f"} 1 5 6
250 5 ▶ return $connect_f->is_ready && !$connect_f->failure;
Condition33%1/3
$connect_f->is_ready && !$connect_f->failure
!ll&&!rl&&r
005
MC/DC0%0/2
$connect_f->is_ready && !$connect_f->failure
$connect_f->is_ready !$connect_f->failure
Truth table33%1/3
A && not(B)
A= $connect_f->is_readyB= !$connect_f->failure
ABresult
0X0
100
111
251 }
252  
253 =head2 is_loggedin
254  
255 $loggedin = $irc->is_loggedin
256  
257 Returns true if the full login sequence has been performed on the connection
258 and it is ready to use.
259  
260 =cut
261  
262 sub is_loggedin
263 {
264 2 ▶ my $self = shift;
Subroutine is_loggedin: called CC=3 SCAR=11.4
Pod documented
265 2 ▶ return 0 unless my $login_f = $self->{login_f};
Branch: 1 branch
BranchTrueFalseTotal
unless my $login_f = $self->{"login_f"} 1 1 2
266 1 ▶ return $login_f->is_ready && !$login_f->failure;
Condition33%1/3
$login_f->is_ready && !$login_f->failure
!ll&&!rl&&r
001
MC/DC0%0/2
$login_f->is_ready && !$login_f->failure
$login_f->is_ready !$login_f->failure
Truth table33%1/3
A && not(B)
A= $login_f->is_readyB= !$login_f->failure
ABresult
0X0
100
111
267 }
268  
269 sub on_read
270 {
271 14 ▶ my $self = shift;
Subroutine on_read: called CC=4 SCAR=14.0
Pod documented
272 14 my ( $buffref, $eof ) = @_;
273  
274 14 my $pingtimer = $self->{pingtimer};
275  
276 14 ▶ $pingtimer->is_running ? $pingtimer->reset : $pingtimer->start;
Branch: 1 branch
BranchTrueFalseTotal
$pingtimer->is_running ? : 8 6 14
277  
278 14 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->Protocol::IRC::on_read($$buffref); 1 } } and return 0 13 1 14
279 14 $self->Protocol::IRC::on_read( $$buffref );
280 13 1;
281 } and return 0;
282  
283 1 my $e = "$@"; chomp $e;
284  
285 1 ▶ $self->maybe_invoke_event( on_irc_error => $e )
Branch: 1 branch
BranchTrueFalseTotal
$self->maybe_invoke_event("on_irc_error", $e) and return 0 1 0 1
286 and return 0;
287  
288 0 $self->close_now;
289 0 die "$e\n";
290 }
291  
292 =head2 nick
293  
294 $nick = $irc->nick
295  
296 Returns the current nick in use by the connection.
297  
298 =cut
299  
300 sub _set_nick
301 {
302 9 ▶ my $self = shift;
Subroutine _set_nick: called CC=1 SCAR=0.0
303 9 ( $self->{nick} ) = @_;
304 9 $self->{nick_folded} = $self->casefold_name( $self->{nick} );
305 }
306  
307 sub nick
308 {
309 4 ▶ my $self = shift;
Subroutine nick: called CC=1 SCAR=0.0
Pod documented
310 4 return $self->{nick};
311 }
312  
313 =head2 nick_folded
314  
315 $nick_folded = $irc->nick_folded
316  
317 Returns the current nick in use by the connection, folded by C<casefold_name>
318 for convenience.
319  
320 =cut
321  
322 sub nick_folded
323 {
324 11 ▶ my $self = shift;
Subroutine nick_folded: called CC=1 SCAR=0.0
Pod documented
325 11 return $self->{nick_folded};
326 }
327  
328 =head1 MESSAGE HANDLING
329  
330 Every incoming message causes a sequence of message handling to occur. First,
331 the message is parsed, and a hash of data about it is created; this is called
332 the hints hash. The message and this hash are then passed down a sequence of
333 potential handlers.
334  
335 Each handler indicates by return value, whether it considers the message to
336 have been handled. Processing of the message is not interrupted the first time
337 a handler declares to have handled a message. Instead, the hints hash is
338 marked to say it has been handled. Later handlers can still inspect the
339 message or its hints, using this information to decide if they wish to take
340 further action.
341  
342 A message with a command of C<COMMAND> will try handlers in following places:
343  
344 =over 4
345  
346 =item 1.
347  
348 A CODE ref in a parameter called C<on_message_COMMAND>
349  
350 $on_message_COMMAND->( $irc, $message, \%hints )
351  
352 =item 2.
353  
354 A method called C<on_message_COMMAND>
355  
356 $irc->on_message_COMMAND( $message, \%hints )
357  
358 =item 3.
359  
360 A CODE ref in a parameter called C<on_message>
361  
362 $on_message->( $irc, 'COMMAND', $message, \%hints )
363  
364 =item 4.
365  
366 A method called C<on_message>
367  
368 $irc->on_message( 'COMMAND', $message, \%hints )
369  
370 =back
371  
372 As this message handling ability is provided by C<Protocol::IRC>, more details
373 about how it works and how to use it can be found at
374 L<Protocol::IRC/MESSAGE HANDLING>.
375  
376 Additionally, some types of messages receive further processing by
377 C<Protocol::IRC> and in turn cause new types of events to be invoked. These
378 are further documented by L<Protocol::IRC/INTERNAL MESSAGE HANDLING>.
379  
380 =cut
381  
382 sub invoke
383 {
384 34 ▶ my $self = shift;
Subroutine invoke: called CC=2 SCAR=6.9
Pod documented
385 34 ▶ my $retref = $self->maybe_invoke_event( @_ ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $retref = $self->maybe_invoke_event(@_) or return undef 13 21 34
386 21 return $retref->[0];
387 }
388  
389 sub on_message_PONG
390 {
391 1 ▶ my $self = shift;
Subroutine on_message_PONG: called CC=3 SCAR=11.1
Pod undocumented
392 1 my ( $message, $hints ) = @_;
393  
394 1 ▶ return 1 unless $self->{pongtimer}->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pongtimer"}->is_running 0 1 1
395  
396 1 my $lag = time - $self->{ping_send_time};
397  
398 1 $self->{current_lag} = $lag;
399 1 ▶ $self->{on_pong_reply}->( $self, $lag ) if $self->{on_pong_reply};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_pong_reply"} 1 0 1
400  
401 1 $self->{pongtimer}->stop;
402  
403 1 return 1;
404 }
405  
406 =head1 AUTHOR
407  
408 Paul Evans <leonerd@leonerd.org.uk>
409  
410 =cut
411  
412 7 0x55AA;
« Build ↑ Summary