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

filter: Statementstmt 80.3% 90 / 112 Branchbran 61.7% 21 / 34 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 88.0% 22 / 25 Podpod 75.0% 9 / 12 totaltotal 70.9% 144 / 203 SCAR 36.2
CC
24
Cov
72%
setup_transport
12.0
teardown_transport
12.0
configure
9.4
CRAP 37.3
« blib/lib/Protocol/IRC/Client.pm ↑ Summary Build »
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-2014 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::IRC::Protocol;
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.10';
12  
13 5 ▶use base qw( IO::Async::Stream Protocol::IRC );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶use Protocol::IRC::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use Encode qw( find_encoding );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶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::Protocol::LineStream> implements an established
31 IRC connection that has already completed its inital login sequence and is
32 ready to send and receive IRC messages. It handles base message sending and
33 receiving, and implements ping timers.
34  
35 Objects of this type would not normally be constructed directly. For IRC
36 clients, see L<Net::Async::IRC> which is a subclass of it. All the events,
37 parameters, and methods documented below are relevant there.
38  
39 =cut
40  
41 =head1 EVENTS
42  
43 The following events are invoked, either using subclass methods or C<CODE>
44 references in parameters:
45  
46 =head2 $handled = on_message
47  
48 =head2 $handled = on_message_MESSAGE
49  
50 Invoked on receipt of a valid IRC message. See C<MESSAGE HANDLING> below.
51  
52 =head2 on_irc_error $err
53  
54 Invoked on receipt of an invalid IRC message if parsing fails. C<$err> is the
55 error message text. If left unhandled, any parse error will result in the
56 connection being immediataely closed, followed by the exception being
57 re-thrown.
58  
59 =head2 on_ping_timeout
60  
61 Invoked if the peer fails to respond to a C<PING> message within the given
62 timeout.
63  
64 =head2 on_pong_reply $lag
65  
66 Invoked when the peer successfully sends a C<PONG> reply response to a C<PING>
67 message. C<$lag> is the response time in (fractional) seconds.
68  
69 =cut
70  
71 =head1 PARAMETERS
72  
73 The following named parameters may be passed to C<new> or C<configure>:
74  
75 =over 8
76  
77 =item on_message => CODE
78  
79 =item on_message_MESSAGE => CODE
80  
81 =item on_irc_error => CODE
82  
83 =item on_ping_timeout => CODE
84  
85 =item on_pong_reply => CODE
86  
87 C<CODE> references for event handlers.
88  
89 =item pingtime => NUM
90  
91 Amount of quiet time, in seconds, after a message is received from the peer,
92 until a C<PING> will be sent to check it is still alive.
93  
94 =item pongtime => NUM
95  
96 Timeout, in seconds, after sending a C<PING> message, to wait for a C<PONG>
97 response.
98  
99 =item encoding => STRING
100  
101 If supplied, sets an encoding to use to encode outgoing messages and decode
102 incoming messages.
103  
104 =back
105  
106 =cut
107  
108 =head1 CONSTRUCTOR
109  
110 =cut
111  
112 =head2 $irc = Net::Async::IRC::Protocol->new( %args )
113  
114 Returns a new instance of a C<Net::Async::IRC::Protocol> object. This object
115 represents a IRC connection to a peer. As it is a subclass of
116 C<IO::Async::Protocol::LineStream> its constructor takes any arguments for
117 that class, in addition to the parameters named below.
118  
119 =cut
120  
121 sub new
122 {
123 5 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=2.9
Pod documented
124 5 my %args = @_;
125  
126 5 my $on_closed = delete $args{on_closed};
127  
128 return $class->SUPER::new(
129 %args,
130  
131 on_closed => sub {
132 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
133  
134 0 my $loop = $self->get_loop;
135  
136 0 $self->{pingtimer}->stop;
137 0 $self->{pongtimer}->stop;
138  
139 0 ▶ $on_closed->( $self ) if $on_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $on_closed 0 0 0
140  
141 0 undef $self->{connect_f};
142 0 undef $self->{login_f};
143 },
144 5 );
145 }
146  
147 sub _init
148 {
149 5 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
150 5 $self->SUPER::_init( @_ );
151  
152 5 my $pingtime = 60;
153 5 my $pongtime = 10;
154  
155 $self->{pingtimer} = IO::Async::Timer::Countdown->new(
156 delay => $pingtime,
157  
158 on_expire => sub {
159 2 ▶ my $now = time();
Subroutine __ANON__: called CC=1 SCAR=0.0
160  
161 2 $self->send_message( "PING", undef, "$now" );
162  
163 2 $self->{ping_send_time} = $now;
164  
165 2 $self->{pongtimer}->start;
166 },
167 5 );
168 5 $self->add_child( $self->{pingtimer} );
169  
170 $self->{pongtimer} = IO::Async::Timer::Countdown->new(
171 delay => $pongtime,
172  
173 on_expire => sub {
174 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
175 },
176 5 );
177 5 $self->add_child( $self->{pongtimer} );
178 }
179  
180 # for Protocol::IRC
181 sub encoder
182 {
183 25 ▶ my $self = shift;
Subroutine encoder: called CC=1 SCAR=0.0
Pod documented
184 25 return $self->{encoder};
185 }
186  
187 sub configure
188 {
189 6 ▶ my $self = shift;
Subroutine configure: called CC=8 SCAR=22.4
Pod documented
190 6 my %args = @_;
191  
192 6 $self->{$_} = delete $args{$_} for grep m/^on_message/, keys %args;
193  
194 6 for (qw( on_ping_timeout on_pong_reply on_irc_error )) {
195 18 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 3 15 18
196 }
197  
198 6 ▶ if( exists $args{pingtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"pingtime"}) 1 5 6
199 1 $self->{pingtimer}->configure( delay => delete $args{pingtime} );
200 }
201  
202 6 ▶ if( exists $args{pongtime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"pongtime"}) 1 5 6
203 1 $self->{pongtimer}->configure( delay => delete $args{pongtime} );
204 }
205  
206 6 ▶ if( exists $args{encoding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"encoding"}) 0 6 6
207 0 my $encoding = delete $args{encoding};
208 0 my $obj = find_encoding( $encoding );
209 0 ▶ 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 0 0
210 0 $self->{encoder} = $obj;
211 }
212  
213 6 $self->SUPER::configure( %args );
214 }
215  
216 sub setup_transport
217 {
218 0 ▶ my $self = shift;
Subroutine setup_transport: not called CC=3 SCAR=24.8
Pod undocumented
219 0 $self->SUPER::setup_transport( @_ );
220  
221 0 $self->{connect_f} = Future->new->done( $self->transport->read_handle );
222 0 ▶ $self->{pingtimer}->start if $self->{pingtimer} and $self->get_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pingtimer"} and $self->get_loop 0 0 0
Condition0%0/3
$self->{"pingtimer"} and $self->get_loop
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pingtimer"} and $self->get_loop
$self->{"pingtimer"} $self->get_loop
Truth table0%0/3
A and B
A= $self->{"pingtimer"}B= $self->get_loop
ABresult
0X0
100
111
223 }
224  
225 sub teardown_transport
226 {
227 0 ▶ my $self = shift;
Subroutine teardown_transport: not called CC=3 SCAR=24.8
Pod undocumented
228  
229 0 undef $self->{connect_f};
230 0 undef $self->{login_f};
231 0 ▶ $self->{pingtimer}->stop if $self->{pingtimer} and $self->get_loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pingtimer"} and $self->get_loop 0 0 0
Condition0%0/3
$self->{"pingtimer"} and $self->get_loop
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pingtimer"} and $self->get_loop
$self->{"pingtimer"} $self->get_loop
Truth table0%0/3
A and B
A= $self->{"pingtimer"}B= $self->get_loop
ABresult
0X0
100
111
232  
233 0 $self->SUPER::teardown_transport( @_ );
234 }
235  
236 =head1 METHODS
237  
238 =cut
239  
240 =head2 $connect = $irc->is_connected
241  
242 Returns true if a connection to the peer is established. Note that even
243 after a successful connection, the connection may not yet logged in to. See
244 also the C<is_loggedin> method.
245  
246 =cut
247  
248 sub is_connected
249 {
250 6 ▶ my $self = shift;
Subroutine is_connected: called CC=3 SCAR=11.4
Pod documented
251 6 ▶ return 0 unless my $connect_f = $self->{connect_f};
Branch: 1 branch
BranchTrueFalseTotal
unless my $connect_f = $self->{"connect_f"} 1 5 6
252 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
253 }
254  
255 =head2 $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 = $irc->nick
293  
294 Returns the current nick in use by the connection.
295  
296 =cut
297  
298 sub _set_nick
299 {
300 9 ▶ my $self = shift;
Subroutine _set_nick: called CC=1 SCAR=0.0
301 9 ( $self->{nick} ) = @_;
302 9 $self->{nick_folded} = $self->casefold_name( $self->{nick} );
303 }
304  
305 sub nick
306 {
307 4 ▶ my $self = shift;
Subroutine nick: called CC=1 SCAR=0.0
Pod documented
308 4 return $self->{nick};
309 }
310  
311 =head2 $nick_folded = $irc->nick_folded
312  
313 Returns the current nick in use by the connection, folded by C<casefold_name>
314 for convenience.
315  
316 =cut
317  
318 sub nick_folded
319 {
320 9 ▶ my $self = shift;
Subroutine nick_folded: called CC=1 SCAR=0.0
Pod documented
321 9 return $self->{nick_folded};
322 }
323  
324 =head1 MESSAGE HANDLING
325  
326 A message with a command of C<COMMAND> will try handlers in following places:
327  
328 =over 4
329  
330 =item 1.
331  
332 A CODE ref in a parameter called C<on_message_COMMAND>
333  
334 $on_message_COMMAND->( $irc, $message, \%hints )
335  
336 =item 2.
337  
338 A method called C<on_message_COMMAND>
339  
340 $irc->on_message_COMMAND( $message, \%hints )
341  
342 =item 3.
343  
344 A CODE ref in a parameter called C<on_message>
345  
346 $on_message->( $irc, 'COMMAND', $message, \%hints )
347  
348 =item 4.
349  
350 A method called C<on_message>
351  
352 $irc->on_message( 'COMMAND', $message, \%hints )
353  
354 =back
355  
356 Certain commands are handled internally by methods on the base
357 C<Net::Async::IRC::Protocol> class itself. These may cause other hints hash
358 keys to be created, or to invoke other handler methods. These are documented
359 below.
360  
361 =cut
362  
363 sub invoke
364 {
365 34 ▶ my $self = shift;
Subroutine invoke: called CC=2 SCAR=6.9
Pod documented
366 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
367 21 return $retref->[0];
368 }
369  
370 sub on_message_PONG
371 {
372 1 ▶ my $self = shift;
Subroutine on_message_PONG: called CC=3 SCAR=11.1
Pod undocumented
373 1 my ( $message, $hints ) = @_;
374  
375 1 ▶ return 1 unless $self->{pongtimer}->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pongtimer"}->is_running 0 1 1
376  
377 1 my $lag = time - $self->{ping_send_time};
378  
379 1 $self->{current_lag} = $lag;
380 1 ▶ $self->{on_pong_reply}->( $self, $lag ) if $self->{on_pong_reply};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_pong_reply"} 1 0 1
381  
382 1 $self->{pongtimer}->stop;
383  
384 1 return 1;
385 }
386  
387 =head1 AUTHOR
388  
389 Paul Evans <leonerd@leonerd.org.uk>
390  
391 =cut
392  
393 5 0x55AA;
« blib/lib/Protocol/IRC/Client.pm ↑ Summary Build »