blib/lib/Protocol/IRC.pm

filter: Statementstmt 94.5% 140 / 148 Branchbran 77.0% 74 / 96 Conditioncond 70.0% 14 / 20 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 84.0% 21 / 25 Podpod 80.0% 16 / 20 totaltotal 84.4% 272 / 322 SCAR 42.0
CC
58
Cov
86%
on_read
26.6
cmp_prefix_flags
11.7
_on_message_text
11.1
CRAP 66.5
↑ Summary blib/lib/Net/Async/IRC.pm »
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 Protocol::IRC;
7  
8 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 our $VERSION = '0.10';
12  
13 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use Protocol::IRC::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # This should be mixed in MI-style
18  
19 =head1 NAME
20  
21 C<Protocol::IRC> - IRC protocol handling
22  
23 =head1 DESCRIPTION
24  
25 This mix-in class provides a base layer of IRC message handling logic. It
26 allows reading of IRC messages from a string buffer and dispatching them to
27 handler methods on its instance.
28  
29 L<Protocol::IRC::Client> provides an extension to this logic that may be more
30 convenient for IRC client implementations.
31  
32 =head1 MESSAGE HANDLING
33  
34 Every incoming message causes a sequence of message handling to occur. First,
35 the message is parsed, and a hash of data about it is created; this is called
36 the hints hash. The message and this hash are then passed down a sequence of
37 potential handlers.
38  
39 Each handler indicates by return value, whether it considers the message to
40 have been handled. Processing of the message is not interrupted the first time
41 a handler declares to have handled a message. Instead, the hints hash is marked
42 to say it has been handled. Later handlers can still inspect the message or its
43 hints, using this information to decide if they wish to take further action.
44  
45 A message with a command of C<COMMAND> will try handlers in following places:
46  
47 =over 4
48  
49 =item 1.
50  
51 A method called C<on_message_COMMAND>
52  
53 $irc->on_message_COMMAND( $message, \%hints )
54  
55 =item 2.
56  
57 A method called C<on_message>
58  
59 $irc->on_message( 'COMMAND', $message, \%hints )
60  
61 =back
62  
63 For server numeric replies, if the numeric reply has a known name, it will be
64 attempted first at its known name, before falling back to the numeric if it
65 was not handled. Unrecognised numerics will be attempted only at their numeric
66 value.
67  
68 Because of the wide variety of messages in IRC involving various types of data
69 the message handling specific cases for certain types of message, including
70 adding extra hints hash items, or invoking extra message handler stages. These
71 details are noted here.
72  
73 Many of these messages create new events; called synthesized messages. These
74 are messages created by the C<Protocol::IRC> object itself, to better
75 represent some of the details derived from the primary ones from the server.
76 These events all take lower-case command names, rather than capitals, and will
77 have a C<synthesized> key in the hints hash, set to a true value. These are
78 dispatched and handled identically to regular primary events, detailed above.
79  
80 If any handler of the synthesized message returns true, then this marks the
81 primary message handled as well.
82  
83 If a message is received that has a gating disposition, extra processing is
84 applied to it before the processing above. The effect on its gate is given as
85 a string (one of C<more>, C<done>, C<fail>) to handlers in the following
86 places:
87  
88 =over 4
89  
90 =item 1.
91  
92 A method called C<on_message_gate_EFFECT_GATE>
93  
94 $irc->on_message_gate_EFFECT_GATE( $message, \%hints )
95  
96 =item 2.
97  
98 A method called C<on_message_gate_EFFECT>
99  
100 $irc->on_message_gate_EFFECT( 'GATE', $message, \%hints )
101  
102 =item 3.
103  
104 A method called C<on_message_gate>
105  
106 $irc->on_message_gate( 'EFFECT', 'GATE', $message, \%hints )
107  
108 =back
109  
110 =head2 Message Hints
111  
112 When messages arrive they are passed to the appropriate message handling
113 method, which the implementation may define. As well as the message, a hash
114 of extra information derived from or relating to the message is also given.
115  
116 The following keys will be present in any message hint hash:
117  
118 =over 8
119  
120 =item handled => BOOL
121  
122 Initially false. Will be set to true the first time a handler returns a true
123 value.
124  
125 =item prefix_nick => STRING
126  
127 =item prefix_user => STRING
128  
129 =item prefix_host => STRING
130  
131 Values split from the message prefix; see the C<Protocol::IRC::Message>
132 C<prefix_split> method.
133  
134 =item prefix_name => STRING
135  
136 Usually the prefix nick, or the hostname in case the nick isn't defined
137 (usually on server messages).
138  
139 =item prefix_is_me => BOOL
140  
141 True if the nick mentioned in the prefix refers to this connection.
142  
143 =back
144  
145 Added to this set, will be all the values returned by the message's
146 C<named_args> method. Some of these values may cause yet more values to be
147 generated.
148  
149 If the message type defines a C<target_name>:
150  
151 =over 8
152  
153 =item * target_type => STRING
154  
155 Either C<channel> or C<user>, as returned by C<classify_name>.
156  
157 =item * target_is_me => BOOL
158  
159 True if the target name is a user and refers to this connection.
160  
161 =back
162  
163 Finally, any key whose name ends in C<_nick> or C<_name> will have a
164 corresponding key added with C<_folded> suffixed on its name, containing the
165 value casefolded using C<casefold_name>. This is for the convenience of string
166 comparisons, hash keys, etc..
167  
168 =cut
169  
170 =head1 METHODS
171  
172 =cut
173  
174 =head2 $irc->on_read( $buffer )
175  
176 Informs the protocol implementation that more bytes have been read from the
177 peer. This method will modify the C<$buffer> directly, and remove from it the
178 prefix of bytes it has consumed. Any bytes remaining should be stored by the
179 caller for next time.
180  
181 Any messages found in the buffer will be passed, in sequence, to the
182 C<incoming_message> method.
183  
184 =cut
185  
186 sub on_read
187 {
188 63 ▶ my $self = shift;
Subroutine on_read: called CC=26 SCAR=32.8
Pod documented
189 # buffer in $_[0]
190  
191 63 ▶ while( $_[0] =~ s/^(.*)\x0d\x0a// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ s/^(.*)\x0d\x0a//) 64 62 126
192 64 my $line = $1;
193 # Ignore blank lines
194 64 ▶ next if !length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 2 62 64
195  
196 62 my $message = Protocol::IRC::Message->new_from_line( $line );
197  
198 61 my $command = $message->command_name;
199  
200 61 my ( $prefix_nick, $prefix_user, $prefix_host ) = $message->prefix_split;
201  
202 61 ▶ my $hints = {
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix_nick ? : 26 35 61
Condition100%3/3
defined $prefix_nick && $self->is_nick_me($prefix_nick)
!ll&&!rl&&r
35242
MC/DC100%2/2
defined $prefix_nick && $self->is_nick_me($prefix_nick)
defined $prefix_nick $self->is_nick_me($prefix_nick)
Truth table100%3/3
A && B
A= defined $prefix_nickB= $self->is_nick_me($prefix_nick)
ABresult
0X0
100
111
203 handled => 0,
204  
205 prefix_nick => $prefix_nick,
206 prefix_user => $prefix_user,
207 prefix_host => $prefix_host,
208 # Most of the time this will be "nick", except for special messages from the server
209 prefix_name => defined $prefix_nick ? $prefix_nick : $prefix_host,
210 prefix_is_me => defined $prefix_nick && $self->is_nick_me( $prefix_nick ),
211 };
212  
213 61 ▶ if( my $named_args = $message->named_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $named_args = $message->named_args) 53 8 61
214 53 $hints->{$_} = $named_args->{$_} for keys %$named_args;
215 }
216  
217 61 ▶ if( defined $hints->{text} and my $encoder = $self->encoder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hints->{"text"} and my $encoder = $self->encoder) 2 59 61
Condition100%3/3
defined $hints->{"text"} and my $encoder = $self->encoder
!ll&&!rl&&r
35242
MC/DC100%2/2
defined $hints->{"text"} and my $encoder = $self->encoder
defined $hints->{"text"} my $encoder = $self->encoder
Truth table100%3/3
A and B
A= defined $hints->{"text"}B= my $encoder = $self->encoder
ABresult
0X0
100
111
218 2 $hints->{text} = $encoder->decode( $hints->{text} );
219 }
220  
221 61 ▶ if( defined( my $target_name = $hints->{target_name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $target_name = $hints->{"target_name"})) 25 36 61
222 25 $hints->{target_is_me} = $self->is_nick_me( $target_name );
223  
224 25 my $target_type = $self->classify_name( $target_name );
225 25 $hints->{target_type} = $target_type;
226 }
227  
228 61 my $prepare_method = "prepare_hints_$command";
229 61 ▶ $self->$prepare_method( $message, $hints ) if $self->can( $prepare_method );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($prepare_method) 12 49 61
230  
231 61 ▶ foreach my $k ( grep { m/_nick$/ or m/_name$/ } keys %$hints ) {
Branch: 1 branch
BranchTrueFalseTotal
/_nick$/ or /_name$/ 477 68 545
232 154 $hints->{"${k}_folded"} = $self->casefold_name( $hints->{$k} );
233 }
234  
235 61 ▶ if( my $disp = $message->gate_disposition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $disp = $message->gate_disposition) 15 46 61
236 15 my ( $type, $gate ) = $disp =~ m/^([-+!])(.*)$/;
237 15 ▶ my $effect = ( $type eq "-" ? "more" :
Branch: 3 branches
BranchTrueFalseTotal
$type eq '-' ? : 10 5 15
$type eq '+' ? : 5 0 5
$type eq '!' ? : 0 0 0
238 $type eq "+" ? "done" :
239 $type eq "!" ? "fail" : die "TODO" );
240  
241 15 ▶ $self->invoke( "on_message_gate_${effect}_$gate", $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_gate_${effect}_$gate", $message, $hints) and $hints->{"handled"} = 1 0 15 15
242 15 ▶ $self->invoke( "on_message_gate_$effect", $gate, $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_gate_$effect", $gate, $message, $hints) and $hints->{"handled"} = 1 0 15 15
243 15 ▶ $self->invoke( "on_message_gate", $effect, $gate, $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_gate", $effect, $gate, $message, $hints) and $hints->{"handled"} = 1 15 0 15
244 }
245  
246 61 ▶ $self->invoke( "on_message_$command", $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_$command", $message, $hints) and $hints->{"handled"} = 1 15 46 61
247 61 ▶ $self->invoke( "on_message", $command, $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message", $command, $message, $hints) and $hints->{"handled"} = 1 36 25 61
248  
249 61 ▶ if( !$hints->{handled} and $message->command ne $command ) { # numerics
Branch: 1 branch
BranchTrueFalseTotal
if (not $hints->{"handled"} and $message->command ne $command) 4 57 61
Condition100%3/3
not $hints->{"handled"} and $message->command ne $command
!ll&&!rl&&r
5614
MC/DC100%2/2
not $hints->{"handled"} and $message->command ne $command
not $hints->{"handled"} $message->command ne $command
Truth table100%3/3
not(A) and B
A= not $hints->{"handled"}B= $message->command ne $command
ABresult
0X0
100
111
250 4 my $numeric = $message->command;
251 4 ▶ $self->invoke( "on_message_$numeric", $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_$numeric", $message, $hints) and $hints->{"handled"} = 1 0 4 4
252 4 ▶ $self->invoke( "on_message", $numeric, $message, $hints ) and $hints->{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message", $numeric, $message, $hints) and $hints->{"handled"} = 1 0 4 4
253 }
254 }
255 }
256  
257 =head2 $irc->send_message( $message )
258  
259 Sends a message to the peer from the given C<Protocol::IRC::Message>
260 object.
261  
262 =head2 $irc->send_message( $command, $prefix, @args )
263  
264 Sends a message to the peer directly from the given arguments.
265  
266 =cut
267  
268 sub send_message
269 {
270 22 ▶ my $self = shift;
Subroutine send_message: called CC=5 SCAR=16.6
Pod documented
271  
272 22 my $message;
273  
274 22 ▶ if( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 22 22
275 0 $message = shift;
276 }
277 else {
278 22 my ( $command, $prefix, @args ) = @_;
279  
280 22 ▶ if( my $encoder = $self->encoder ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoder = $self->encoder) 1 21 22
281 1 my $argnames = Protocol::IRC::Message->arg_names( $command );
282  
283 1 ▶ if( defined( my $i = $argnames->{text} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = $argnames->{"text"})) 1 0 1
284 1 ▶ $args[$i] = $encoder->encode( $args[$i] ) if defined $args[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $args[$i] 1 0 1
285 }
286 }
287  
288 22 $message = Protocol::IRC::Message->new( $command, $prefix, @args );
289 }
290  
291 22 $self->write( $message->stream_to_line . "\x0d\x0a" );
292 }
293  
294 =head2 $irc->send_ctcp( $prefix, $target, $verb, $argstr )
295  
296 Shortcut to sending a CTCP message. Sends a PRIVMSG to the given target,
297 containing the given verb and argument string.
298  
299 =cut
300  
301 sub send_ctcp
302 {
303 1 ▶ my $self = shift;
Subroutine send_ctcp: called CC=1 SCAR=0.0
Pod documented
304 1 my ( $prefix, $target, $verb, $argstr ) = @_;
305  
306 1 $self->send_message( "PRIVMSG", undef, $target, "\001$verb $argstr\001" );
307 }
308  
309 =head2 $irc->send_ctcprely( $prefix, $target, $verb, $argstr )
310  
311 Shortcut to sending a CTCP reply. As C<send_ctcp> but using a NOTICE instead.
312  
313 =cut
314  
315 sub send_ctcpreply
316 {
317 1 ▶ my $self = shift;
Subroutine send_ctcpreply: called CC=1 SCAR=0.0
Pod undocumented
318 1 my ( $prefix, $target, $verb, $argstr ) = @_;
319  
320 1 $self->send_message( "NOTICE", undef, $target, "\001$verb $argstr\001" );
321 }
322  
323 =head1 ISUPPORT-DRIVEN UTILITIES
324  
325 The following methods are controlled by the server information given in the
326 C<ISUPPORT> settings. They use the C<isupport> required method to query the
327 information required.
328  
329 =cut
330  
331 =head2 $name_folded = $irc->casefold_name( $name )
332  
333 Returns the C<$name>, folded in case according to the server's C<CASEMAPPING>
334 C<ISUPPORT>. Such a folded name will compare using C<eq> according to whether the
335 server would consider it the same name.
336  
337 Useful for use in hash keys or similar.
338  
339 =cut
340  
341 sub casefold_name
342 {
343 306 ▶ my $self = shift;
Subroutine casefold_name: called CC=5 SCAR=16.1
Pod documented
344 306 my ( $nick ) = @_;
345  
346 306 ▶ return undef unless defined $nick;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nick 35 271 306
347  
348 271 ▶ my $mapping = lc( $self->isupport( "CASEMAPPING" ) || "" );
Condition100%2/2
$self->isupport('CASEMAPPING') || ''
l!l
8263
MC/DC100%1/1
$self->isupport('CASEMAPPING') || ''
$self->isupport('CASEMAPPING')
Truth table100%2/2
A
A= $self->isupport('CASEMAPPING')
Aresult
11
00
349  
350 # Squash the 'capital' [\] into lowercase {|}
351 271 ▶ $nick =~ tr/[\\]/{|}/ if $mapping ne "ascii";
Branch: 1 branch
BranchTrueFalseTotal
if $mapping ne "ascii" 268 3 271
352  
353 # Most RFC 1459 implementations also squash ^ to ~, even though the RFC
354 # didn't mention it
355 271 ▶ $nick =~ tr/^/~/ unless $mapping eq "strict-rfc1459";
Branch: 1 branch
BranchTrueFalseTotal
unless $mapping eq "strict-rfc1459" 269 2 271
356  
357 271 return lc $nick;
358 }
359  
360 =head2 $cmp = $irc->cmp_prefix_flags( $lhs, $rhs )
361  
362 Compares two channel occupant prefix flags, and returns a signed integer to
363 indicate which of them has higher priviledge, according to the server's
364 ISUPPORT declaration. Suitable for use in a C<sort()> function or similar.
365  
366 =cut
367  
368 sub cmp_prefix_flags
369 {
370 4 ▶ my $self = shift;
Subroutine cmp_prefix_flags: called CC=9 SCAR=24.6
Pod documented
371 4 my ( $lhs, $rhs ) = @_;
372  
373 4 ▶ return undef unless defined $lhs and defined $rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lhs and defined $rhs 0 4 4
Condition33%1/3
defined $lhs and defined $rhs
!ll&&!rl&&r
004
MC/DC0%0/2
defined $lhs and defined $rhs
defined $lhs defined $rhs
Truth table33%1/3
A and B
A= defined $lhsB= defined $rhs
ABresult
0X0
100
111
374  
375 # As a special case, compare emptystring as being lower than voice
376 4 ▶ return 0 if $lhs eq "" and $rhs eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $lhs eq "" and $rhs eq "" 0 4 4
Condition33%1/3
$lhs eq "" and $rhs eq ""
!ll&&!rl&&r
400
MC/DC0%0/2
$lhs eq "" and $rhs eq ""
$lhs eq "" $rhs eq ""
Truth table33%1/3
A and B
A= $lhs eq ""B= $rhs eq ""
ABresult
0X0
100
111
377 4 ▶ return 1 if $rhs eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $rhs eq "" 0 4 4
378 4 ▶ return -1 if $lhs eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $lhs eq "" 0 4 4
379  
380 4 my $PREFIX_FLAGS = $self->isupport( 'prefix_flags' );
381  
382 4 ▶ ( my $lhs_index = index $PREFIX_FLAGS, $lhs ) > -1 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
(my $lhs_index = index($PREFIX_FLAGS, $lhs)) > -1 or return undef 0 4 4
383 4 ▶ ( my $rhs_index = index $PREFIX_FLAGS, $rhs ) > -1 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
(my $rhs_index = index($PREFIX_FLAGS, $rhs)) > -1 or return undef 1 3 4
384  
385 # IRC puts these in greatest-first, so we need to swap the ordering here
386 3 return $rhs_index <=> $lhs_index;
387 }
388  
389 =head2 $cmp = $irc->cmp_prefix_modes( $lhs, $rhs )
390  
391 Similar to C<cmp_prefix_flags>, but compares channel occupant C<MODE> command
392 flags.
393  
394 =cut
395  
396 sub cmp_prefix_modes
397 {
398 4 ▶ my $self = shift;
Subroutine cmp_prefix_modes: called CC=5 SCAR=16.8
Pod documented
399 4 my ( $lhs, $rhs ) = @_;
400  
401 4 ▶ return undef unless defined $lhs and defined $rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lhs and defined $rhs 0 4 4
Condition33%1/3
defined $lhs and defined $rhs
!ll&&!rl&&r
004
MC/DC0%0/2
defined $lhs and defined $rhs
defined $lhs defined $rhs
Truth table33%1/3
A and B
A= defined $lhsB= defined $rhs
ABresult
0X0
100
111
402  
403 4 my $PREFIX_MODES = $self->isupport( "prefix_modes" );
404  
405 4 ▶ ( my $lhs_index = index $PREFIX_MODES, $lhs ) > -1 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
(my $lhs_index = index($PREFIX_MODES, $lhs)) > -1 or return undef 0 4 4
406 4 ▶ ( my $rhs_index = index $PREFIX_MODES, $rhs ) > -1 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
(my $rhs_index = index($PREFIX_MODES, $rhs)) > -1 or return undef 1 3 4
407  
408 # IRC puts these in greatest-first, so we need to swap the ordering here
409 3 return $rhs_index <=> $lhs_index;
410 }
411  
412 =head2 $flag = $irc->prefix_mode2flag( $mode )
413  
414 Converts a channel occupant C<MODE> flag (such as C<o>) into a name prefix
415 flag (such as C<@>).
416  
417 =cut
418  
419 sub prefix_mode2flag
420 {
421 7 ▶ my $self = shift;
Subroutine prefix_mode2flag: called CC=1 SCAR=0.0
Pod documented
422 7 my ( $mode ) = @_;
423  
424 7 return $self->isupport( 'prefix_map_m2f' )->{$mode};
425 }
426  
427 =head2 $mode = $irc->prefix_flag2mode( $flag )
428  
429 The inverse of C<prefix_mode2flag>.
430  
431 =cut
432  
433 sub prefix_flag2mode
434 {
435 2 ▶ my $self = shift;
Subroutine prefix_flag2mode: called CC=1 SCAR=0.0
Pod documented
436 2 my ( $flag ) = @_;
437  
438 2 return $self->isupport( 'prefix_map_f2m' )->{$flag};
439 }
440  
441 =head2 $classification = $irc->classify_name( $name )
442  
443 Returns C<channel> if the given name matches the pattern of names allowed for
444 channels according to the server's C<CHANTYPES> C<ISUPPORT>. Returns C<user>
445 if not.
446  
447 =cut
448  
449 sub classify_name
450 {
451 37 ▶ my $self = shift;
Subroutine classify_name: called CC=2 SCAR=6.9
Pod documented
452 37 my ( $name ) = @_;
453  
454 37 ▶ return "channel" if $name =~ $self->isupport( "channame_re" );
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ $self->isupport("channame_re") 26 11 37
455 11 return "user"; # TODO: Perhaps we can be a bit stricter - only check for valid nick chars?
456 }
457  
458 =head2 $me = $irc->is_nick_me( $nick )
459  
460 Returns true if the given nick refers to that in use by the connection.
461  
462 =cut
463  
464 sub is_nick_me
465 {
466 64 ▶ my $self = shift;
Subroutine is_nick_me: called CC=1 SCAR=0.0
Pod documented
467 64 my ( $nick ) = @_;
468  
469 64 return $self->casefold_name( $nick ) eq $self->nick_folded;
470 }
471  
472 =head1 INTERNAL MESSAGE HANDLING
473  
474 The following messages are handled internally by C<Protocol::IRC>.
475  
476 =cut
477  
478 =head2 PING
479  
480 C<PING> messages are automatically replied to with C<PONG>.
481  
482 =cut
483  
484 sub on_message_PING
485 {
486 0 ▶ my $self = shift;
Subroutine on_message_PING: not called CC=1 SCAR=6.9
Pod undocumented
487 0 my ( $message, $hints ) = @_;
488  
489 0 $self->send_message( "PONG", undef, $message->named_args->{text} );
490  
491 0 return 1;
492 }
493  
494 =head2 NOTICE and PRIVMSG
495  
496 Because C<NOTICE> and C<PRIVMSG> are so similar, they are handled together by
497 synthesized events called C<text>, C<ctcp> and C<ctcpreply>. Depending on the
498 contents of the text, and whether it was supplied in a C<PRIVMSG> or a
499 C<NOTICE>, one of these three events will be created.
500  
501 In all cases, the hints hash will contain a C<is_notice> key being true or
502 false, depending on whether the original messages was a C<NOTICE> or a
503 C<PRIVMSG>, a C<target_name> key containing the message target name, a
504 case-folded version of the name in a C<target_name_folded> key, and a
505 classification of the target type in a C<target_type> key.
506  
507 For the C<user> target type, it will contain a boolean in C<target_is_me> to
508 indicate if the target of the message is the user represented by this
509 connection.
510  
511 For the C<channel> target type, it will contain a C<restriction> key
512 containing the channel message restriction, if present.
513  
514 For normal C<text> messages, it will contain a key C<text> containing the
515 actual message text.
516  
517 For either CTCP message type, it will contain keys C<ctcp_verb> and
518 C<ctcp_args> with the parsed message. The C<ctcp_verb> will contain the first
519 space-separated token, and C<ctcp_args> will be a string containing the rest
520 of the line, otherwise unmodified. This type of message is also subject to a
521 special stage of handler dispatch, involving the CTCP verb string. For
522 messages with C<VERB> as the verb, the following are tried. C<CTCP> may stand
523 for either C<ctcp> or C<ctcpreply>.
524  
525 =over 4
526  
527 =item 1.
528  
529 A method called C<on_message_CTCP_VERB>
530  
531 $irc->on_message_CTCP_VERB( $message, \%hints )
532  
533 =item 2.
534  
535 A method called C<on_message_CTCP>
536  
537 $irc->on_message_CTCP( 'VERB', $message, \%hintss )
538  
539 =item 3.
540  
541 A method called C<on_message>
542  
543 $irc->on_message( 'CTCP VERB', $message, \%hints )
544  
545 =back
546  
547 =cut
548  
549 sub on_message_NOTICE
550 {
551 3 ▶ my $self = shift;
Subroutine on_message_NOTICE: called CC=1 SCAR=0.0
Pod undocumented
552 3 my ( $message, $hints ) = @_;
553 3 return $self->_on_message_text( $message, $hints, 1 );
554 }
555  
556 sub on_message_PRIVMSG
557 {
558 7 ▶ my $self = shift;
Subroutine on_message_PRIVMSG: called CC=1 SCAR=0.0
Pod undocumented
559 7 my ( $message, $hints ) = @_;
560 7 return $self->_on_message_text( $message, $hints, 0 );
561 }
562  
563 sub _on_message_text
564 {
565 10 ▶ my $self = shift;
Subroutine _on_message_text: called CC=11 SCAR=24.0
566 10 my ( $message, $hints, $is_notice ) = @_;
567  
568 10 my %hints = (
569 %$hints,
570 synthesized => 1,
571 is_notice => $is_notice,
572 );
573  
574 # TODO: In client->server messages this might be a comma-separated list
575 10 my $target = delete $hints{targets};
576  
577 10 my $prefixflag_re = $self->isupport( 'prefixflag_re' );
578  
579 10 my $restriction = "";
580 10 ▶ while( $target =~ m/^$prefixflag_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /^$prefixflag_re/) 1 10 11
581 1 $restriction .= substr( $target, 0, 1, "" );
582 }
583  
584 10 $hints{target_name} = $target;
585 10 $hints{target_name_folded} = $self->casefold_name( $target );
586  
587 10 my $type = $hints{target_type} = $self->classify_name( $target );
588  
589 10 ▶ if( $type eq "channel" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'channel') { } 5 5 10
elsif ($type eq 'user') { } 5 0 5
590 5 $hints{restriction} = $restriction;
591 5 $hints{target_is_me} = '';
592 }
593 elsif( $type eq "user" ) {
594 # TODO: user messages probably can't have restrictions. What to do
595 # if we got one?
596 5 $hints{target_is_me} = $self->is_nick_me( $target );
597 }
598  
599 10 my $text = $hints->{text};
600  
601 10 ▶ if( $text =~ m/^\x01(.*)\x01$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\x01(.*)\x01$/) { } 2 8 10
602 2 ( my $verb, $text ) = split( m/ /, $1, 2 );
603 2 $hints{ctcp_verb} = $verb;
604 2 $hints{ctcp_args} = $text;
605  
606 2 ▶ my $ctcptype = $is_notice ? "ctcpreply" : "ctcp";
Branch: 1 branch
BranchTrueFalseTotal
$is_notice ? : 1 1 2
607  
608 2 ▶ $self->invoke( "on_message_${ctcptype}_$verb", $message, \%hints ) and $hints{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_${ctcptype}_$verb", $message, \%hints) and $hints{"handled"} = 1 0 2 2
609 2 ▶ $self->invoke( "on_message_${ctcptype}", $verb, $message, \%hints ) and $hints{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_$ctcptype", $verb, $message, \%hints) and $hints{"handled"} = 1 0 2 2
610 2 ▶ $self->invoke( "on_message", "$ctcptype $verb", $message, \%hints ) and $hints{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message", "$ctcptype $verb", $message, \%hints) and $hints{"handled"} = 1 2 0 2
611 }
612 else {
613 8 $hints{text} = $text;
614  
615 8 ▶ $self->invoke( "on_message_text", $message, \%hints ) and $hints{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message_text", $message, \%hints) and $hints{"handled"} = 1 1 7 8
616 8 ▶ $self->invoke( "on_message", "text", $message, \%hints ) and $hints{handled} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->invoke("on_message", "text", $message, \%hints) and $hints{"handled"} = 1 4 4 8
617 }
618  
619 10 return $hints{handled};
620 }
621  
622 =head1 REQUIRED METHODS
623  
624 =cut
625  
626 =head2 $irc->write( $string )
627  
628 Requests the byte string to be sent to the peer
629  
630 =cut
631  
632 0 ▶sub write { croak "Attemped to invoke abstract ->write on " . ref $_[0] }
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
633  
634 =head2 $encoder = $irc->encoder
635  
636 Optional. If supplied, returns an L<Encode> object used to encode or decode
637 the bytes appearing in a C<text> field of a message. If set, all text strings
638 will be returned, and should be given, as Unicode strings. They will be
639 encoded or decoded using this object.
640  
641 =cut
642  
643 20 ▶sub encoder { undef }
Subroutine encoder: called CC=1 SCAR=0.0
Pod documented
644  
645 =head2 $result = $irc->invoke( $name, @args )
646  
647 Optional. If provided, invokes the message handling routine called C<$name>
648 with the given arguments. A default implementation is provided which simply
649 attempts to invoke a method of the given name, or return false if no method
650 of that name exists.
651  
652 If an implementation does override this method, care should be taken to ensure
653 that methods are tested for and invoked if present, in addition to any other
654 work the method wishes to perform, as this is the basis by which derived
655 message handling works.
656  
657 =cut
658  
659 sub invoke
660 {
661 194 ▶ my $self = shift;
Subroutine invoke: called CC=2 SCAR=6.9
Pod documented
662 194 my ( $name, @args ) = @_;
663 194 ▶ return unless $self->can( $name );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($name) 94 100 194
664 100 return $self->$name( @args );
665 }
666  
667 =head2 $value = $irc->isupport( $field )
668  
669 Should return the value of the given C<ISUPPORT> field.
670  
671 As well as the all-capitals server-supplied fields, the following fields may
672 be requested. Their names are all lowercase and contain underscores, to
673 distinguish them from server-supplied fields.
674  
675 =over 8
676  
677 =item prefix_modes => STRING
678  
679 The mode characters from C<PREFIX> (e.g. C<ohv>)
680  
681 =item prefix_flags => STRING
682  
683 The flag characters from C<PREFIX> (e.g. C<@%+>)
684  
685 =item prefixflag_re => Regexp
686  
687 A precompiled regexp that matches any of the prefix flags
688  
689 =item prefix_map_m2f => HASH
690  
691 A map from mode characters to flag characters
692  
693 =item prefix_map_f2m => HASH
694  
695 A map from flag characters to mode characters
696  
697 =item chanmodes_list => ARRAY
698  
699 A 4-element array containing the split portions of C<CHANMODES>;
700  
701 [ $listmodes, $argmodes, $argsetmodes, $boolmodes ]
702  
703 =item channame_re => Regexp
704  
705 A precompiled regexp that matches any string beginning with a channel prefix
706 character in C<CHANTYPES>.
707  
708 =back
709  
710 =cut
711  
712 0 ▶sub isupport { croak "Attempted to invoke abstract ->isupport on " . ref $_[0] }
Subroutine isupport: not called CC=1 SCAR=6.9
Pod documented
713  
714 =head2 $nick = $irc->nick
715  
716 Should return the current nick in use by the connection.
717  
718 =head2 $nick_folded = $irc->nick_folded
719  
720 Optional. If supplied, should return the current nick as case-folded by the
721 C<casefold_name> method. If not provided, this will be performed by
722 case-folding the result from C<nick>.
723  
724 =cut
725  
726 0 ▶sub nick { croak "Attempted to invoke abstract ->nick on " . ref $_[0] }
Subroutine nick: not called CC=1 SCAR=6.9
Pod documented
727 55 ▶sub nick_folded { $_[0]->casefold_name( $_[0]->nick ) }
Subroutine nick_folded: called CC=1 SCAR=0.0
Pod documented
728  
729 =head1 AUTHOR
730  
731 Paul Evans <leonerd@leonerd.org.uk>
732  
733 =cut
734  
735 15 0x55AA;
↑ Summary blib/lib/Net/Async/IRC.pm »