Circle/Net/IRC/Target.pm

filter: Statementstmt 18.0% 32 / 177 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 25.0% 10 / 40 Podpod 3.8% 1 / 26 totaltotal 14.3% 43 / 300 CC 25 SCAR 26.8
CC
25
Cov
14%
on_message_text
56.0
on_message
30.0
msg
30.0
CRAP 14.6
« Circle/Rule/Chain.pm ↑ Summary Circle/Rule/Resultset.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::Net::IRC::Target;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use base qw( Tangence::Object Circle::WindowItem );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.173320';
12  
13 sub new
14 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
15 0 my $class = shift;
16 0 my %args = @_;
17  
18 0 my $self = $class->SUPER::new( @_ );
19  
20 0 $self->{irc} = $args{irc};
21  
22 0 $self->set_prop_name( $args{name} );
23 0 $self->set_prop_tag( $args{name} );
24  
25 0 $self->{root} = $args{root};
26 0 $self->{net} = $args{net};
27  
28 0 return $self;
29 }
30  
31 # Convenience accessor
32 sub name
33 0 ▶{
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $self = shift;
35 0 return $self->get_prop_name;
36 }
37  
38 4 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 use overload
40 # '""' => "STRING",
41 0 ▶ '0+' => sub { refaddr $_[0] },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 4 fallback => 1;
43  
44 4 ▶use constant PREFIX_OVERHEAD => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use constant PRIVMSG_OVERHEAD => length("PRIVMSG :");
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use constant CTCP_ACTION_OVERHEAD => length("PRIVMSG :\x01CTCP ACTION \x01");
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 sub STRING
49 0 ▶{
Subroutine STRING: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $self = shift;
51 0 return ref($self)."[name=".$self->name."]";
52 }
53  
54 sub describe
55 0 ▶{
Subroutine describe: not called CC=1 SCAR=6.9
Pod documented
56 0 my $self = shift;
57 0 return ref($self) . "[" . $self->name . "]";
58 }
59  
60 sub get_prop_tag
61 0 ▶{
Subroutine get_prop_tag: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my $self = shift;
63 0 return $self->name;
64 }
65  
66 sub get_prop_network
67 0 ▶{
Subroutine get_prop_network: not called CC=1 SCAR=6.9
Pod undocumented
68 0 my $self = shift;
69 0 return $self->{net};
70 }
71  
72 sub reify
73 0 ▶{
Subroutine reify: not called CC=2 SCAR=17.9
Pod undocumented
74 0 my $self = shift;
75  
76 0 ▶ return if $self->get_prop_real;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_prop_real 0 0 0
77  
78 0 $self->set_prop_real( 1 );
79  
80 0 my $root = $self->{root};
81 0 $root->broadcast_sessions( "new_item", $self );
82 }
83  
84 sub on_message
85 0 ▶{
Subroutine on_message: not called CC=5 SCAR=34.0
Pod undocumented
86 0 my $self = shift;
87 0 my ( $command, $message, $hints ) = @_;
88  
89 # $command might contain spaces from synthesized events - e.g. "ctcp ACTION"
90 0 ( my $method = "on_message_$command" ) =~ s/ /_/g;
91  
92 0 ▶ return 1 if $self->can( $method ) and $self->$method( $message, $hints );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) and $self->$method($message, $hints) 0 0 0
Condition0%0/3
$self->can($method) and $self->$method($message, $hints)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can($method) and $self->$method($message, $hints)
$self->can($method) $self->$method($message, $hints)
Truth table0%0/3
A and B
A= $self->can($method)B= $self->$method($message, $hints)
ABresult
0X0
100
111
93  
94 0 ▶ if( not $hints->{handled} and not $hints->{synthesized} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hints->{"handled"} or $hints->{"synthesized"}) 0 0 0
Condition0%0/3
$hints->{"handled"} or $hints->{"synthesized"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hints->{"handled"} or $hints->{"synthesized"}
$hints->{"handled"} $hints->{"synthesized"}
Truth table0%0/3
A or B
A= $hints->{"handled"}B= $hints->{"synthesized"}
ABresult
1X1
011
000
95 $self->push_displayevent( "irc.irc", {
96 command => $command,
97 prefix => $message->prefix,
98 0 args => join( " ", map { "'$_'" } $message->args ),
99 } );
100 0 $self->bump_level( 1 );
101 }
102  
103 0 return 1;
104 }
105  
106 sub pick_display_target
107 0 ▶{
Subroutine pick_display_target: not called CC=3 SCAR=24.8
Pod undocumented
108 0 my $self = shift;
109 0 my ( $display ) = @_;
110  
111 0 ▶ return $self if $display eq "self";
Branch: 1 branch
BranchTrueFalseTotal
if $display eq "self" 0 0 0
112 0 ▶ return $self->{net} if $display eq "server";
Branch: 1 branch
BranchTrueFalseTotal
if $display eq "server" 0 0 0
113 }
114  
115 sub default_message_level
116 0 ▶{
Subroutine default_message_level: not called CC=2 SCAR=17.9
Pod undocumented
117 0 my $self = shift;
118 0 my ( $hints ) = @_;
119  
120 0 ▶ return $hints->{is_notice} ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$hints->{'is_notice'} ? : 0 0 0
121 }
122  
123 sub on_message_text
124 0 ▶{
Subroutine on_message_text: not called CC=7 SCAR=40.3
Pod undocumented
125 0 my $self = shift;
126 0 my ( $message, $hints ) = @_;
127  
128 0 my $srcnick = $hints->{prefix_name};
129 0 my $text = $hints->{text};
130  
131 0 my $is_notice = $hints->{is_notice};
132  
133 0 my $net = $self->{net};
134  
135 my $event = {
136 %$hints,
137 text => $net->format_text_tagged( $text ),
138 is_action => 0,
139 level => $self->default_message_level( $hints ),
140 0 ▶ display => ( !defined $hints->{prefix_nick} or $is_notice && !$self->get_prop_real ) ? "server" : "self",
Branch: 1 branch
BranchTrueFalseTotal
!defined($hints->{'prefix_nick'}) || $is_notice && !$self->get_prop_real ? : 0 0 0
Condition0%0/6
not defined $hints->{"prefix_nick"} or $is_notice and not $self->get_prop_real
l!l&&r!l&&!r
000
$is_notice and not $self->get_prop_real
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $hints->{"prefix_nick"} or $is_notice and not $self->get_prop_real
not defined $hints->{"prefix_nick"} $is_notice not $self->get_prop_real
Truth table0%0/4
not(A) or B and not(C)
A= not defined $hints->{"prefix_nick"}B= $is_noticeC= not $self->get_prop_real
ABCresult
1XX1
0111
00X0
0100
141 };
142  
143 0 $net->run_rulechain( "input", $event );
144  
145 0 ▶ my $eventname = $is_notice ? "notice" : "msg";
Branch: 1 branch
BranchTrueFalseTotal
$is_notice ? : 0 0 0
146  
147 0 $self->fire_event( $eventname, $srcnick, $text );
148  
149 0 ▶ if( my $target = $self->pick_display_target( $event->{display} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $target = $self->pick_display_target($event->{"display"})) 0 0 0
150 0 $target->push_displayevent( "irc.$eventname", { target => $self->name, nick => $srcnick, text => $event->{text} } );
151 0 ▶ $target->bump_level( $event->{level} ) if defined $event->{level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $event->{"level"} 0 0 0
152  
153 0 $target->reify;
154 }
155  
156 0 return 1;
157 }
158  
159 sub on_message_ctcp_ACTION
160 0 ▶{
Subroutine on_message_ctcp_ACTION: not called CC=3 SCAR=24.8
Pod undocumented
161 0 my $self = shift;
162 0 my ( $message, $hints ) = @_;
163  
164 0 my $srcnick = $hints->{prefix_name};
165 0 my $text = $hints->{ctcp_args};
166  
167 0 my $net = $self->{net};
168  
169 0 my $event = {
170 %$hints,
171 text => $net->format_text_tagged( $text ),
172 is_action => 1,
173 level => $self->default_message_level( $hints ),
174 display => "self",
175 };
176  
177 0 $net->run_rulechain( "input", $event );
178  
179 0 $self->fire_event( "act", $srcnick, $text );
180  
181 0 ▶ if( my $target = $self->pick_display_target( $event->{display} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $target = $self->pick_display_target($event->{"display"})) 0 0 0
182 0 $target->push_displayevent( "irc.act", { target => $self->name, nick => $srcnick, text => $event->{text} } );
183 0 ▶ $target->bump_level( $event->{level} ) if defined $event->{level};
Branch: 1 branch
BranchTrueFalseTotal
if defined $event->{"level"} 0 0 0
184  
185 0 $target->reify;
186 }
187  
188 0 return 1;
189 }
190  
191 sub on_connected
192 0 ▶{
Subroutine on_connected: not called CC=1 SCAR=6.9
Pod undocumented
193 0 my $self = shift;
194  
195 0 $self->push_displayevent( "status", { text => "Server is connected" } );
196 }
197  
198 sub on_disconnected
199 0 ▶{
Subroutine on_disconnected: not called CC=1 SCAR=6.9
Pod undocumented
200 0 my $self = shift;
201 0 my ( $message ) = @_;
202  
203 0 $self->push_displayevent( "status", { text => $message } );
204 }
205  
206 sub _split_text_chunks
207 0 ▶{
Subroutine _split_text_chunks: not called CC=4 SCAR=30.0
208 0 my ( $text, $maxlen, $on_chunk ) = @_;
209  
210 0 my $head = "<< ";
211 0 my $tail = " >>";
212  
213 0 ▶ while( length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 0 0 0
214 0 ▶ if( $maxlen >= length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxlen >= length $text) 0 0 0
215 0 $on_chunk->( $text );
216 0 return;
217 }
218  
219 0 my $prefix = substr $text, 0, $maxlen - length( $tail );
220 0 ▶ if( $prefix =~ m/\s+\S+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /\s+\S+$/) 0 0 0
221 0 substr( $prefix, $-[0] ) = "";
222 }
223  
224 0 $on_chunk->( $prefix . $tail );
225  
226 0 substr( $text, 0, length $prefix ) = "";
227 0 $text =~ s/^\s+//;
228 0 substr( $text, 0, 0 ) = $head;
229 }
230 }
231  
232 sub msg
233 0 ▶{
Subroutine msg: not called CC=5 SCAR=34.0
Pod undocumented
234 0 my $self = shift;
235 0 my ( $text, %hints ) = @_;
236  
237 0 my $irc = $self->{irc};
238 0 my $net = $self->{net};
239  
240 my $event = {
241 text => Circle::TaggedString->new( $text ),
242 is_action => $hints{action},
243 0 };
244  
245 0 $net->run_rulechain( "output", $event );
246  
247 0 my $is_action = $event->{is_action};
248  
249 my $maxlen = 510 -
250 # To work out the maximum message length size we'd need to know our own
251 # prefix that the server will send. We can't know the host, but we know
252 # everything else. Just pretend it's maximal length, 64
253 0 ( length( $irc->{nick} ) + length( $irc->{user} ) + 64 + PREFIX_OVERHEAD );
254 0 my $target = $self->name;
255  
256 0 foreach my $line ( split m/\n/, $event->{text}->str ) {
257 0 ▶ if( $is_action ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_action) { } 0 0 0
258 0 ▶ _split_text_chunks( $line, $maxlen - length($target) - CTCP_ACTION_OVERHEAD, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 $irc->send_ctcp( undef, $target, "ACTION", $_[0] );
260 0 });
261 }
262 else {
263 0 ▶ _split_text_chunks( $line, $maxlen - length($target) - PRIVMSG_OVERHEAD, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 $irc->send_message( "PRIVMSG", undef, $target, $_[0] );
265 0 });
266 }
267  
268 0 my $line_formatted = $net->format_text( $line );
269  
270 0 ▶ $self->fire_event( $is_action ? "act" : "msg", $irc->nick, $line );
Branch: 1 branch
BranchTrueFalseTotal
$is_action ? : 0 0 0
271 0 ▶ $self->push_displayevent( $is_action ? "irc.act" : "irc.msg", { target => $self->name, nick => $irc->nick, text => $line_formatted } );
Branch: 1 branch
BranchTrueFalseTotal
$is_action ? : 0 0 0
272 }
273 }
274  
275 sub method_msg
276 0 ▶{
Subroutine method_msg: not called CC=1 SCAR=6.9
Pod undocumented
277 0 my $self = shift; my $ctx = shift;
278 0 $self->msg( $_[0], action => 0 );
279 }
280  
281 sub notice
282 0 ▶{
Subroutine notice: not called CC=1 SCAR=6.9
Pod undocumented
283 0 my $self = shift;
284 0 my ( $text ) = @_;
285  
286 0 my $irc = $self->{irc};
287 0 $irc->send_message( "NOTICE", undef, $self->name, $text );
288  
289 0 my $net = $self->{net};
290 0 my $text_formatted = $net->format_text( $text );
291  
292 0 $self->fire_event( "notice", $irc->nick, $text );
293 0 $self->push_displayevent( "irc.notice", { target => $self->name, nick => $irc->nick, text => $text_formatted } );
294 }
295  
296 sub method_notice
297 0 ▶{
Subroutine method_notice: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my $self = shift; my $ctx = shift;
299 0 $self->notice( @_ );
300 }
301  
302 sub method_act
303 0 ▶{
Subroutine method_act: not called CC=1 SCAR=6.9
Pod undocumented
304 0 my $self = shift; my $ctx = shift;
305 0 $self->msg( $_[0], action => 1 );
306 }
307  
308 sub command_say
309 0 ▶ : Command_description("Quote text directly as a PRIVMSG")
Subroutine command_say: not called CC=1 SCAR=6.9
Pod undocumented
310 : Command_arg('text', eatall => 1)
311 {
312 0 my $self = shift;
313 0 my ( $text ) = @_;
314  
315 0 $self->msg( $text, action => 0 );
316  
317 0 return;
318 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
319  
320 sub command_me
321 0 ▶ : Command_description("Send a CTCP ACTION")
Subroutine command_me: not called CC=1 SCAR=6.9
Pod undocumented
322 : Command_arg('text', eatall => 1)
323 {
324 0 my $self = shift;
325 0 my ( $text ) = @_;
326  
327 0 $self->msg( $text, action => 1 );
328  
329 0 return;
330 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
331  
332 sub commandable_parent
333 0 ▶{
Subroutine commandable_parent: not called CC=1 SCAR=6.9
Pod undocumented
334 0 my $self = shift;
335 0 return $self->{net};
336 }
337  
338 sub enumerable_name
339 0 ▶{
Subroutine enumerable_name: not called CC=1 SCAR=6.9
Pod undocumented
340 0 my $self = shift;
341 0 return $self->get_prop_tag;
342 }
343  
344 sub parent
345 0 ▶{
Subroutine parent: not called CC=1 SCAR=6.9
Pod undocumented
346 0 my $self = shift;
347 0 return $self->{net};
348 }
349  
350 sub enter_text
351 0 ▶{
Subroutine enter_text: not called CC=2 SCAR=17.9
Pod undocumented
352 0 my $self = shift;
353 0 my ( $text ) = @_;
354  
355 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
356  
357 0 $self->msg( $text );
358 }
359  
360 sub get_widget_commandentry
361 0 ▶{
Subroutine get_widget_commandentry: not called CC=1 SCAR=6.9
Pod undocumented
362 0 my $self = shift;
363 0 my $widget = $self->SUPER::get_widget_commandentry;
364  
365 0 $self->{net}->add_entry_widget_completegroups( $widget );
366  
367 0 return $widget;
368 }
369  
370 4 0x55AA;
« Circle/Rule/Chain.pm ↑ Summary Circle/Rule/Resultset.pm »