IRC/Client.pm

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