blib/lib/Circle/Net/IRC/Target.pm

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