Component/IRC/Plugin/DCC.pm

filter: Statementstmt 80.5% 236 / 293 Branchbran 67.2% 78 / 116 Conditioncond 53.3% 16 / 30 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 96.8% 31 / 32 Podpod 50.0% 4 / 8 totaltotal 74.1% 369 / 498 CC 73 SCAR 23.6
CC
73
Cov
75%
_dcc_failed
29.0
_U_dcc
23.5
_U_dcc_chat
11.6
CRAP 10.6
« Component/IRC/Plugin/Proxy.pm ↑ Summary Component/IRC/Plugin/AutoJoin.pm »
1 package POE::Component::IRC::Plugin::DCC;
2 79 $POE::Component::IRC::Plugin::DCC::VERSION = '6.95';
3 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 79 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 79 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 79 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 79 ▶use File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 79 ▶use File::Spec::Functions 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 79 ▶use POE qw(Driver::SysRW Filter::Line Filter::Stream
Subroutine BEGIN: called CC=1 SCAR=0.0
10 79 Wheel::ReadWrite Wheel::SocketFactory);
11 79 ▶use POE::Component::IRC::Plugin qw(:ALL);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 79 ▶use Socket qw(INADDR_ANY unpack_sockaddr_in inet_aton inet_ntoa);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 79 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 79 OUT_BLOCKSIZE => 1024, # Send DCC data in 1k chunks
16 IN_BLOCKSIZE => 10_240, # 10k per DCC socket read
17 LISTEN_TIMEOUT => 300, # Five minutes for listening DCCs
18 79 };
19  
20 116 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
21 116 my ($package) = shift;
22 116 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 116 116
23 116 my %self = @_;
24 116 return bless \%self, $package;
25 }
26  
27 116 ▶sub PCI_register {
Subroutine PCI_register: called CC=1 SCAR=0.0
Pod undocumented
28 116 my ($self, $irc) = @_;
29  
30 116 $self->{irc} = $irc;
31  
32 116 POE::Session->create(
33 object_states => [
34 $self => [qw(
35 _start
36 _dcc_read
37 _dcc_failed
38 _dcc_timeout
39 _dcc_up
40 _U_dcc
41 _U_dcc_accept
42 _U_dcc_chat
43 _U_dcc_close
44 _U_dcc_resume
45 _cancel_timeout
46 )],
47 ],
48 );
49  
50 116 $irc->plugin_register($self, 'SERVER', qw(disconnected dcc_request));
51 116 $irc->plugin_register($self, 'USER', qw(dcc dcc_accept dcc_chat dcc_close dcc_resume));
52  
53 116 return 1;
54 }
55  
56 116 ▶sub PCI_unregister {
Subroutine PCI_unregister: called CC=2 SCAR=6.9
Pod undocumented
57 116 my ($self) = @_;
58 116 delete $self->{irc};
59 116 delete $self->{$_} for qw(wheelmap dcc);
60 116 $poe_kernel->refcount_decrement($self->{session_id}, __PACKAGE__);
61 116 return 1;
62 }
63  
64 116 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
65 116 my ($kernel, $self) = @_[KERNEL, OBJECT];
66 116 $self->{session_id} = $_[SESSION]->ID();
67 116 $kernel->refcount_increment($self->{session_id}, __PACKAGE__);
68 116 return;
69 }
70  
71 # set the dcc ports
72 0 ▶sub dccports {
Subroutine dccports: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($self, $value) = @_;
74 0 $self->{dccports} = $value;
75 0 return;
76 }
77  
78 # set the NAT address
79 2 ▶sub nataddr {
Subroutine nataddr: called CC=1 SCAR=0.0
Pod documented
80 2 my ($self, $value) = @_;
81 2 $self->{nataddr} = $value;
82 2 return;
83 }
84  
85 # returns information about a connection
86 2 ▶sub dcc_info {
Subroutine dcc_info: called CC=2 SCAR=7.5
Pod documented
87 2 my ($self, $id) = @_;
88  
89 2 ▶ if (!$self->{dcc}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dcc"}{$id}) 0 2 2
90 0 warn "dcc_info: Unknown wheel ID: $id\n";
91 0 return;
92 }
93  
94 2 my %info;
95 @info{qw(nick type port file size done peeraddr)}
96 2 = @{ $self->{dcc}->{$id} }{qw(
97 nick type port file size done peeraddr
98 )};
99 2 return \%info;
100 }
101  
102 10 ▶sub _quote_file {
Subroutine _quote_file: called CC=2 SCAR=6.9
103 10 my ($file) = @_;
104  
105 10 ▶ if ($file =~ /[\s"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /[\s"]/) 1 9 10
106 1 $file =~ s|"|\\"|g;
107 1 $file = qq{"$file"};
108 }
109 10 return $file;
110 }
111  
112 89 ▶sub S_disconnected {
Subroutine S_disconnected: called CC=1 SCAR=0.0
Pod undocumented
113 89 my ($self) = $_;
114 # clean up old cookies for any ignored RESUME requests
115 89 delete $self->{resuming};
116 89 return PCI_EAT_NONE;
117 }
118  
119 10 ▶sub S_dcc_request {
Subroutine S_dcc_request: called CC=8 SCAR=20.9
Pod undocumented
120 10 my ($self, $irc) = splice @_, 0, 2;
121 10 ▶ my ($user, $type, $port, $cookie, $file, $size) = map { ref =~ /REF|SCALAR/ && ${ $_ } } @_;
Condition100%3/3
ref($_) =~ /REF|SCALAR/ && ${$_;}
!ll&&!rl&&r
10563
MC/DC100%2/2
ref($_) =~ /REF|SCALAR/ && ${$_;}
ref($_) =~ /REF|SCALAR/ ${$_;}
Truth table100%3/3
A && B
A= ref($_) =~ /REF|SCALAR/B= ${$_;}
ABresult
0X0
100
111
122 10 my $nick = (split /!/, $user)[0];
123  
124 10 ▶ if ($type eq 'ACCEPT' && $self->{resuming}->{"$port+$nick"}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'ACCEPT' and $self->{'resuming'}{"$port+$nick"}) { } 1 9 10
elsif ($type eq 'RESUME') { } 1 8 9
Condition66%2/3
$type eq "ACCEPT" and $self->{"resuming"}{"$port+$nick"}
!ll&&!rl&&r
901
MC/DC50%1/2
$type eq "ACCEPT" and $self->{"resuming"}{"$port+$nick"}
$type eq "ACCEPT" $self->{"resuming"}{"$port+$nick"}
Truth table66%2/3
A and B
A= $type eq "ACCEPT"B= $self->{"resuming"}{"$port+$nick"}
ABresult
0X0
100
111
125 # the old cookie has the peer's address
126 1 my $old_cookie = delete $self->{resuming}->{"$port+$nick"};
127 1 $irc->yield(dcc_accept => $old_cookie);
128 }
129 elsif ($type eq 'RESUME') {
130 1 for my $cookie (values %{ $self->{dcc} }) {
131 1 ▶ next if $cookie->{nick} ne $nick;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->{"nick"} ne $nick 0 1 1
132 1 ▶ next if $cookie->{port} ne $port;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->{"port"} ne $port 0 1 1
133 1 $file = _quote_file($file);
134 1 $cookie->{done} = $size;
135 1 $irc->yield(ctcp => $nick => "DCC ACCEPT $file $port $size");
136 1 last;
137 }
138 }
139  
140 10 return PCI_EAT_NONE;
141 }
142  
143 # this is a stub handler for all U_dcc* events which redispatches them as
144 # events to our own POE session so that we can do stuff related to it,
145 # namely create wheels and set alarms/delays
146 26 ▶sub _default {
Subroutine _default: called CC=2 SCAR=7.0
147 26 my ($self, $irc, $event) = splice @_, 0, 3;
148 26 ▶ return PCI_EAT_NONE if $event !~ /^U_dcc(?:_accept|_chat|_close|_resume)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $event =~ /^U_dcc(?:_accept|_chat|_close|_resume)?$/ 0 26 26
149 26 $event =~ s/^U_/_U_/;
150 26 pop @_;
151 26 my @args = map { $$_ } @_;
152 26 $poe_kernel->call($self->{session_id}, $event, @args);
153 26 return PCI_EAT_NONE;
154 }
155  
156 # Attempt to initiate a DCC SEND or CHAT connection with another person.
157 8 ▶sub _U_dcc {
Subroutine _U_dcc: called CC=14 SCAR=31.6
158 8 my ($kernel, $self, $nick, $type, $file, $blocksize, $timeout)
159 = @_[KERNEL, OBJECT, ARG0..$#_];
160  
161 8 ▶ if (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 8 8
162 0 warn "The 'dcc' command requires at least two arguments\n";
163 0 return;
164 }
165  
166 8 my $irc = $self->{irc};
167 8 my ($bindport, $bindaddr, $factory, $port, $addr, $size);
168  
169 8 $type = uc $type;
170 8 ▶ if ($type eq 'CHAT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'CHAT') { } 5 3 8
elsif ($type eq 'SEND') { } 3 0 3
171 5 $file = 'chat'; # As per the semi-specification
172 }
173 elsif ($type eq 'SEND') {
174 3 ▶ if (!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 3 3
175 0 warn "The 'dcc' command requires three arguments for a SEND\n";
176 0 return;
177 }
178 3 $file = rel2abs(bsd_glob($file));
179 3 $size = (stat $file)[7];
180 3 ▶ if (!defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 3 3
181 0 $irc->send_event(
182 'irc_dcc_error',
183 undef,
184 "Couldn't get ${file}'s size: $!",
185 $nick,
186 $type,
187 undef,
188 $file,
189 );
190 0 return;
191 }
192 }
193  
194 8 $bindaddr = $irc->localaddr();
195  
196 8 ▶ if ($self->{dccports}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dccports"}) 0 8 8
197 0 $bindport = shift @{ $self->{dccports} };
198 0 ▶ if (!defined $bindport) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bindport) 0 0 0
199 0 warn "dcc: Can't allocate listen port for DCC $type\n";
200 0 return;
201 }
202 }
203  
204 8 ▶ $factory = POE::Wheel::SocketFactory->new(
Condition50%1/2
$bindaddr || "\000\000\000\000"
l!l
80
MC/DC0%0/1
$bindaddr || "\000\000\000\000"
$bindaddr
Truth table50%1/2
A
A= $bindaddr
Aresult
11
00
205 BindAddress => $bindaddr || INADDR_ANY,
206 BindPort => $bindport,
207 SuccessEvent => '_dcc_up',
208 FailureEvent => '_dcc_failed',
209 Reuse => 'yes',
210 );
211  
212 8 ($port, $addr) = unpack_sockaddr_in($factory->getsockname());
213 8 ▶ $addr = inet_aton($self->{nataddr}) if $self->{nataddr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nataddr"} 1 7 8
214  
215 8 ▶ if (!defined $addr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $addr) 0 8 8
216 0 warn "dcc: Can't determine our IP address! ($!)\n";
217 0 return;
218 }
219 8 $addr = unpack 'N', $addr;
220  
221 8 my $basename = fileparse($file);
222 8 $basename = _quote_file($basename);
223  
224 # Tell the other end that we're waiting for them to connect.
225 8 ▶ $irc->yield(ctcp => $nick => "DCC $type $basename $addr $port" . ($size ? " $size" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$size ? : 3 5 8
226  
227 8 ▶ my $alarm_id = $kernel->delay_set(
Condition50%1/2
$timeout || 300
l!l
80
MC/DC0%0/1
$timeout || 300
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
228 '_dcc_timeout', ($timeout || LISTEN_TIMEOUT), $factory->ID,
229 );
230  
231 # Store the state for this connection.
232 8 ▶ $self->{dcc}->{ $factory->ID } = {
Condition100%2/2
$blocksize || 1024
l!l
26
MC/DC100%1/1
$blocksize || 1024
$blocksize
Truth table100%2/2
A
A= $blocksize
Aresult
11
00
233 open => 0,
234 nick => $nick,
235 type => $type,
236 file => $file,
237 size => $size,
238 port => $port,
239 addr => $addr,
240 done => 0,
241 blocksize => ($blocksize || OUT_BLOCKSIZE),
242 listener => 1,
243 factory => $factory,
244 alarm_id => $alarm_id,
245 };
246  
247 8 return;
248 }
249  
250 # Accepts a proposed DCC connection to another client. See '_dcc_up' for
251 # the rest of the logic for this.
252 6 ▶sub _U_dcc_accept {
Subroutine _U_dcc_accept: called CC=4 SCAR=14.1
253 6 my ($self, $cookie, $myfile) = @_[OBJECT, ARG0, ARG1];
254  
255 6 ▶ if (!defined $cookie) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cookie) 0 6 6
256 0 warn "The 'dcc_accept' command requires at least one argument\n";
257 0 return;
258 }
259  
260 6 ▶ if ($cookie->{type} eq 'SEND') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie->{"type"} eq "SEND") 3 3 6
261 3 $cookie->{type} = 'GET';
262 3 ▶ $cookie->{file} = $myfile if defined $myfile; # filename override
Branch: 1 branch
BranchTrueFalseTotal
if defined $myfile 2 1 3
263 }
264  
265 my $factory = POE::Wheel::SocketFactory->new(
266 RemoteAddress => sprintf("%vd", pack("N", $cookie->{addr})),
267 RemotePort => $cookie->{port},
268 6 SuccessEvent => '_dcc_up',
269 FailureEvent => '_dcc_failed',
270 );
271  
272 6 $self->{dcc}->{$factory->ID} = $cookie;
273 6 $self->{dcc}->{$factory->ID}->{factory} = $factory;
274  
275 6 return;
276 }
277  
278 # Send data over a DCC CHAT connection.
279 7 ▶sub _U_dcc_chat {
Subroutine _U_dcc_chat: called CC=6 SCAR=24.5
280 7 my ($self, $id, @data) = @_[OBJECT, ARG0..$#_];
281  
282 7 ▶ if (!defined $id || !@data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id and @data) 0 7 7
Condition33%1/3
defined $id and @data
!ll&&!rl&&r
007
MC/DC0%0/2
defined $id and @data
defined $id @data
Truth table33%1/3
A and B
A= defined $idB= @data
ABresult
0X0
100
111
283 0 warn "The 'dcc_chat' command requires at least two arguments\n";
284 0 return;
285 }
286  
287 7 ▶ if (!exists $self->{dcc}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dcc"}{$id}) 0 7 7
288 0 warn "dcc_chat: Unknown wheel ID: $id\n";
289 0 return;
290 }
291  
292 7 ▶ if (!exists $self->{dcc}->{$id}->{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dcc"}{$id}{"wheel"}) 0 7 7
293 0 warn "dcc_chat: No DCC wheel for id $id!\n";
294 0 return;
295 }
296  
297 7 ▶ if ($self->{dcc}->{$id}->{type} ne 'CHAT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"type"} ne "CHAT") 0 7 7
298 0 warn "dcc_chat: id $id isn't associated with a DCC CHAT connection!\n";
299 0 return;
300 }
301  
302 7 $self->{dcc}->{$id}->{wheel}->put(join "\n", @data);
303 7 return;
304 }
305  
306 # Terminate a DCC connection manually.
307 5 ▶sub _U_dcc_close {
Subroutine _U_dcc_close: called CC=7 SCAR=22.8
308 5 my ($kernel, $self, $id) = @_[KERNEL, OBJECT, ARG0];
309 5 my $irc = $self->{irc};
310  
311 5 ▶ if (!defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 5 5
312 0 warn "The 'dcc_close' command requires an id argument\n";
313 0 return;
314 }
315  
316 5 ▶ if (!exists $self->{dcc}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dcc"}{$id}) 0 5 5
317 0 warn "dcc_close: Unknown wheel ID: $id\n";
318 0 return;
319 }
320  
321 5 ▶ if (!exists $self->{dcc}->{$id}->{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dcc"}{$id}{"wheel"}) 0 5 5
322 0 warn "dcc_close: No DCC wheel for id $id!\n";
323 0 return;
324 }
325  
326 # pending data, wait till it has been flushed
327 5 ▶ if ($self->{dcc}->{$id}->{wheel}->get_driver_out_octets()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"wheel"}->get_driver_out_octets) 1 4 5
328 1 $kernel->delay_set(_U_dcc_close => 2, $id);
329 1 return;
330 }
331  
332 $irc->send_event(
333 'irc_dcc_done',
334 $id,
335 4 @{ $self->{dcc}->{$id} }{qw(
336 nick type port file size done peeraddr
337 )},
338 );
339  
340 # Reclaim our port if necessary.
341 4 ▶ if ($self->{dcc}->{$id}->{listener} && $self->{dccports}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}) 0 4 4
Condition66%2/3
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
!ll&&!rl&&r
220
MC/DC0%0/2
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
$self->{"dcc"}{$id}{"listener"} $self->{"dccports"}
Truth table66%2/3
A and B
A= $self->{"dcc"}{$id}{"listener"}B= $self->{"dccports"}
ABresult
0X0
100
111
342 0 push ( @{ $self->{dccports} }, $self->{dcc}->{$id}->{port} );
343 }
344  
345 4 $self->_remove_dcc($id);
346 4 return;
347 }
348  
349 ## no critic (InputOutput::RequireBriefOpen)
350 1 ▶sub _U_dcc_resume {
Subroutine _U_dcc_resume: called CC=3 SCAR=11.4
351 1 my ($self, $cookie, $myfile) = @_[OBJECT, ARG0, ARG1];
352 1 my $irc = $self->{irc};
353  
354 1 my $sender_file = _quote_file($cookie->{file});
355 1 ▶ $cookie->{file} = $myfile if defined $myfile;
Branch: 1 branch
BranchTrueFalseTotal
if defined $myfile 1 0 1
356 1 $cookie->{done} = -s $cookie->{file};
357 1 $cookie->{resuming} = 1;
358  
359 1 ▶ if (open(my $handle, '>>', $cookie->{file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $handle, '>>', $cookie->{'file'}) { } 1 0 1
360 1 $irc->yield(ctcp => $cookie->{nick} => "DCC RESUME $sender_file $cookie->{port} $cookie->{done}");
361 1 $self->{resuming}->{"$cookie->{port}+$cookie->{nick}"} = $cookie;
362 }
363 else {
364 0 warn "dcc_resume: Can't append to file '$cookie->{file}'\n";
365 0 return;
366 }
367  
368 1 return;
369 }
370  
371 # Accept incoming data on a DCC socket.
372 269 ▶sub _dcc_read {
Subroutine _dcc_read: called CC=7 SCAR=19.6
373 269 my ($kernel, $self, $data, $id) = @_[KERNEL, OBJECT, ARG0, ARG1];
374 269 my $irc = $self->{irc};
375  
376 269 $id = $self->{wheelmap}->{$id};
377 269 ▶ if ($self->{dcc}{$id}{alarm_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"alarm_id"}) 5 264 269
378 5 $kernel->call($self->{session_id}, '_cancel_timeout', $id);
379 }
380  
381 269 ▶ if ($self->{dcc}->{$id}->{type} eq 'GET') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'dcc'}{$id}{'type'} eq 'GET') { } 131 138 269
elsif ($self->{'dcc'}{$id}{'type'} eq 'SEND') { } 131 7 138
382 # Acknowledge the received data.
383 131 print {$self->{dcc}->{$id}->{fh}} $data;
384 131 $self->{dcc}->{$id}->{done} += length $data;
385 $self->{dcc}->{$id}->{wheel}->put(
386 pack 'N', $self->{dcc}->{$id}->{done}
387 131 );
388  
389 # Send an event to let people know about the newly arrived data.
390 $irc->send_event(
391 'irc_dcc_get',
392 $id,
393 131 @{ $self->{dcc}->{$id} }{qw(
394 nick port file size done peeraddr
395 )},
396 );
397 }
398 elsif ($self->{dcc}->{$id}->{type} eq 'SEND') {
399 # Record the client's download progress.
400 131 $self->{dcc}->{$id}->{done} = unpack 'N', substr( $data, -4 );
401  
402 $irc->send_event(
403 'irc_dcc_send',
404 $id,
405 131 @{ $self->{dcc}->{$id} }{qw(
406 nick port file size done peeraddr
407 )},
408 );
409  
410 # Are we done yet?
411 131 ▶ if ($self->{dcc}->{$id}->{done} >= $self->{dcc}->{$id}->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"done"} >= $self->{"dcc"}{$id}{"size"}) 3 128 131
412 # Reclaim our port if necessary.
413 3 ▶ if ( $self->{dcc}->{$id}->{listener} && $self->{dccports}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}) 0 3 3
Condition33%1/3
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
!ll&&!rl&&r
030
MC/DC0%0/2
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
$self->{"dcc"}{$id}{"listener"} $self->{"dccports"}
Truth table33%1/3
A and B
A= $self->{"dcc"}{$id}{"listener"}B= $self->{"dccports"}
ABresult
0X0
100
111
414 0 push @{ $self->{dccports} }, $self->{dcc}->{$id}->{port};
415 }
416  
417 $irc->send_event(
418 'irc_dcc_done',
419 $id,
420 3 @{ $self->{dcc}->{$id} }{qw(
421 nick type port file size done peeraddr
422 )},
423 );
424  
425 3 $self->_remove_dcc($id);
426 3 return;
427 }
428  
429 # Send the next 'blocksize'-sized packet.
430 read $self->{dcc}->{$id}->{fh}, $data,
431 128 $self->{dcc}->{$id}->{blocksize};
432 128 $self->{dcc}->{$id}->{wheel}->put( $data );
433 }
434 else {
435 $irc->send_event(
436 'irc_dcc_' . lc $self->{dcc}->{$id}->{type},
437 $id,
438 7 @{ $self->{dcc}->{$id} }{qw(nick port)},
439 $data,
440 $self->{dcc}->{$id}->{peeraddr},
441 7 );
442 }
443  
444 266 return;
445 }
446  
447 # What happens when an attempted DCC connection fails.
448 7 ▶sub _dcc_failed {
Subroutine _dcc_failed: called CC=12 SCAR=33.7
449 7 my ($self, $operation, $errnum, $errstr, $id) = @_[OBJECT, ARG0 .. ARG3];
450 7 my $irc = $self->{irc};
451  
452 7 ▶ if (!exists $self->{dcc}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dcc"}{$id}) 5 2 7
453 5 ▶ if (exists $self->{wheelmap}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'wheelmap'}{$id}) { } 5 0 5
454 5 $id = $self->{wheelmap}->{$id};
455 }
456 else {
457 0 warn "_dcc_failed: Unknown wheel ID: $id\n";
458 0 return;
459 }
460 }
461  
462 # Reclaim our port if necessary.
463 7 ▶ if ( $self->{dcc}->{$id}->{listener} && $self->{dccports}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}) 0 7 7
Condition66%2/3
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
!ll&&!rl&&r
430
MC/DC0%0/2
$self->{"dcc"}{$id}{"listener"} and $self->{"dccports"}
$self->{"dcc"}{$id}{"listener"} $self->{"dccports"}
Truth table66%2/3
A and B
A= $self->{"dcc"}{$id}{"listener"}B= $self->{"dccports"}
ABresult
0X0
100
111
464 0 push ( @{ $self->{dccports} }, $self->{dcc}->{$id}->{port} );
465 }
466  
467 DCC: {
468 7 ▶ last DCC if $errnum != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $errnum != 0 0 7 7
469  
470 # Did the peer of a DCC GET connection close the socket after the file
471 # transfer finished? If so, it's not really an error.
472 7 ▶ if ($self->{dcc}->{$id}->{type} eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"type"} eq "GET") 3 4 7
473 3 ▶ if ($self->{dcc}->{$id}->{done} < $self->{dcc}->{$id}->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"done"} < $self->{"dcc"}{$id}{"size"}) 0 3 3
474 0 last DCC;
475 }
476 }
477  
478 7 ▶ if ($self->{dcc}->{$id}->{type} =~ /^(GET|CHAT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dcc"}{$id}{"type"} =~ /^(GET|CHAT)$/) 7 0 7
479 $irc->send_event(
480 'irc_dcc_done',
481 $id,
482 7 @{ $self->{dcc}->{$id} }{qw(
483 nick type port file size done peeraddr
484 )},
485 );
486  
487 7 $self->_remove_dcc($id);
488 }
489  
490 7 return;
491 }
492  
493 # something went wrong
494 0 ▶ if ($errnum == 0 && $self->{dcc}->{$id}->{type} eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == 0 and $self->{'dcc'}{$id}{'type'} eq 'GET') { } 0 0 0
Condition0%0/3
$errnum == 0 and $self->{"dcc"}{$id}{"type"} eq "GET"
!ll&&!rl&&r
000
MC/DC0%0/2
$errnum == 0 and $self->{"dcc"}{$id}{"type"} eq "GET"
$errnum == 0 $self->{"dcc"}{$id}{"type"} eq "GET"
Truth table0%0/3
A and B
A= $errnum == 0B= $self->{"dcc"}{$id}{"type"} eq "GET"
ABresult
0X0
100
111
495 0 $errstr = 'Aborted by sender';
496 }
497 else {
498 0 ▶ $errstr = $errstr
Branch: 1 branch
BranchTrueFalseTotal
$errstr ? : 0 0 0
499 ? $errstr = "$operation error $errnum: $errstr"
500 : $errstr = "$operation error $errnum"
501 ;
502 }
503  
504 $irc->send_event(
505 'irc_dcc_error',
506 $id,
507 $errstr,
508 0 @{ $self->{dcc}->{$id} }{qw(
509 nick type port file size done peeraddr
510 )},
511 );
512  
513 0 $self->_remove_dcc($id);
514 0 return;
515 }
516  
517 # What happens when a DCC connection sits waiting for the other end to
518 # pick up the phone for too long.
519 2 ▶sub _dcc_timeout {
Subroutine _dcc_timeout: called CC=3 SCAR=12.0
520 2 my ($kernel, $self, $id) = @_[KERNEL, OBJECT, ARG0];
521  
522 2 ▶ if (exists $self->{dcc}->{$id} && !$self->{dcc}->{$id}->{open}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dcc"}{$id} and not $self->{"dcc"}{$id}{"open"}) 2 0 2
Condition33%1/3
exists $self->{"dcc"}{$id} and not $self->{"dcc"}{$id}{"open"}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $self->{"dcc"}{$id} and not $self->{"dcc"}{$id}{"open"}
exists $self->{"dcc"}{$id} not $self->{"dcc"}{$id}{"open"}
Truth table33%1/3
A and not(B)
A= exists $self->{"dcc"}{$id}B= not $self->{"dcc"}{$id}{"open"}
ABresult
0X0
100
111
523 2 $kernel->yield(
524 '_dcc_failed',
525 'connection',
526 0,
527 'DCC connection timed out',
528 $id,
529 );
530 }
531 2 return;
532 }
533  
534 # This event occurs when a DCC connection is established.
535 ## no critic (InputOutput::RequireBriefOpen)
536 12 ▶sub _dcc_up {
Subroutine _dcc_up: called CC=8 SCAR=21.1
537 12 my ($kernel, $self, $sock, $peeraddr, $id) =
538 @_[KERNEL, OBJECT, ARG0, ARG1, ARG3];
539 12 my $irc = $self->{irc};
540  
541 # Delete the listening socket and monitor the accepted socket
542 # for incoming data
543 12 delete $self->{dcc}->{$id}->{factory};
544 12 $self->{dcc}->{$id}->{open} = 1;
545 12 $self->{dcc}->{$id}->{peeraddr} = inet_ntoa($peeraddr);
546  
547 $self->{dcc}->{$id}->{wheel} = POE::Wheel::ReadWrite->new(
548 Handle => $sock,
549 Driver => ($self->{dcc}->{$id}->{type} eq 'GET'
550 ? POE::Driver::SysRW->new( BlockSize => IN_BLOCKSIZE )
551 : POE::Driver::SysRW->new()
552 ),
553 12 ▶ Filter => ($self->{dcc}->{$id}->{type} eq 'CHAT'
Branch: 2 branches
BranchTrueFalseTotal
$self->{'dcc'}{$id}{'type'} eq 'GET' ? : 3 9 12
$self->{'dcc'}{$id}{'type'} eq 'CHAT' ? : 6 6 12
554 ? POE::Filter::Line->new( Literal => "\012" )
555 : POE::Filter::Stream->new()
556 ),
557 InputEvent => '_dcc_read',
558 ErrorEvent => '_dcc_failed',
559 );
560  
561 12 $self->{wheelmap}->{ $self->{dcc}->{$id}->{wheel}->ID } = $id;
562  
563 12 my $handle;
564 12 ▶ if ($self->{dcc}->{$id}->{type} eq 'GET') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'dcc'}{$id}{'type'} eq 'GET') { } 3 9 12
elsif ($self->{'dcc'}{$id}{'type'} eq 'SEND') { } 3 6 9
565 # check if we're resuming
566 3 ▶ my $mode = $self->{dcc}->{$id}->{resuming} ? '>>' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dcc'}{$id}{'resuming'} ? : 1 2 3
567  
568 3 ▶ if ( !open $handle, $mode, $self->{dcc}->{$id}->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $handle, $mode, $self->{"dcc"}{$id}{"file"}) 0 3 3
569 0 $kernel->yield(_dcc_failed => 'open file', $! + 0, $!, $id);
570 0 return;
571 }
572  
573 3 binmode $handle;
574 3 $self->{dcc}->{$id}->{fh} = $handle;
575 }
576 elsif ($self->{dcc}->{$id}->{type} eq 'SEND') {
577 3 ▶ if (!open $handle, '<', $self->{dcc}->{$id}->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $handle, "<", $self->{"dcc"}{$id}{"file"}) 0 3 3
578 0 $kernel->yield(_dcc_failed => 'open file', $! + 0, $!, $id);
579 0 return;
580 }
581  
582 3 binmode $handle;
583 3 seek $handle, $self->{dcc}{$id}{done}, 0;
584 # Send the first packet to get the ball rolling.
585 3 read $handle, my $buffer, $self->{dcc}->{$id}->{blocksize};
586 3 $self->{dcc}->{$id}->{wheel}->put($buffer);
587 3 $self->{dcc}->{$id}->{fh} = $handle;
588 }
589  
590 # Tell any listening sessions that the connection is up.
591 $irc->send_event(
592 'irc_dcc_start',
593 $id,
594 12 @{ $self->{dcc}->{$id} }{qw(
595 nick type port file size peeraddr
596 )},
597 );
598  
599 12 return;
600 }
601  
602 8 ▶sub _cancel_timeout {
Subroutine _cancel_timeout: called CC=1 SCAR=0.0
603 8 my ($kernel, $self, $id) = @_[KERNEL, OBJECT, ARG0];
604 8 my $alarm_id = delete $self->{dcc}{$id}{alarm_id};
605 8 $kernel->alarm_remove($alarm_id);
606 8 return;
607 }
608  
609 14 ▶sub _remove_dcc {
Subroutine _remove_dcc: called CC=6 SCAR=18.0
610 14 my ($self, $id) = @_;
611  
612 14 ▶ if (exists $self->{dcc}{$id}{alarm_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dcc"}{$id}{"alarm_id"}) 3 11 14
613 3 $poe_kernel->call($self->{session_id}, '_cancel_timeout', $id);
614 }
615  
616 14 ▶ if (exists $self->{dcc}{$id}{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dcc"}{$id}{"wheel"}) 12 2 14
617 12 delete $self->{wheelmap}{ $self->{dcc}{$id}{wheel}->ID };
618 12 ▶ if ($^O =~ /cygwin|MSWin/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /cygwin|MSWin/) 0 12 12
619 0 $self->{dcc}{$id}{wheel}->$_ for qw(shutdown_input shutdown_output);
620 }
621 }
622  
623 # flush the filehandle
624 14 ▶ close $self->{dcc}{$id}{fh} if $self->{dcc}{$id}{type} eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dcc"}{$id}{"type"} eq "GET" 3 11 14
625  
626 14 delete $self->{dcc}{$id};
627 14 return;
628 }
629  
630 79 1;
631  
632 =encoding utf8
633  
634 =head1 NAME
635  
636 POE::Component::IRC::Plugin::DCC - A PoCo-IRC plugin providing support for
637 DCC transfers
638  
639 =head1 SYNOPSIS
640  
641 # send a file
642 my $file = '/home/user/secret.pdf';
643 my $recipient = 'that_guy';
644 $irc->yield(dcc => $recipient => SEND => $file);
645  
646 # receive a file
647 sub irc_dcc_request {
648 my ($user, $type, $port, $cookie, $file, $size, $addr) = @_[ARG0..$#_];
649 return if $type ne 'SEND';
650  
651 my $irc = $_[SENDER]->get_heap();
652 my $nick = (split /!/, $user)[0];
653  
654 print "$nick wants to send me '$file' ($size bytes) from $addr:$port\n");
655 $irc->yield(dcc_accept => $cookie);
656 }
657  
658 =head1 DESCRIPTION
659  
660 This plugin provides the IRC commands needed to make use of DCC. It is used
661 internally by L<POE::Component::IRC|POE::Component::IRC> so there's no
662 need to add it manually.
663  
664 =head1 METHODS
665  
666 =head2 C<new>
667  
668 Takes no arguments.
669  
670 Returns a plugin object suitable for feeding to
671 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
672  
673 =head2 C<dccports>
674  
675 Sets the TCP ports that can be used for DCC sends. Takes one argument,
676 an arrayref containing the port numbers.
677  
678 =head2 C<nataddr>
679  
680 Sets the public NAT address to be used for DCC sends.
681  
682 =head2 C<dcc_info>
683  
684 Takes one argument, a DCC connection id (see below). Returns a hash of
685 information about the connection. The keys are: B<'nick'>, B<'type'>,
686 B<'port'>, B<'file'>, B<'size'>, B<'done,'>, and B<'peeraddr'>.
687  
688 =head1 COMMANDS
689  
690 The plugin responds to the following
691 L<POE::Component::IRC|POE::Component::IRC> commands.
692  
693 =head2 C<dcc>
694  
695 Send a DCC SEND or CHAT request to another person. Takes at least two
696 arguments: the nickname of the person to send the request to and the type
697 of DCC request (SEND or CHAT). For SEND requests, be sure to add a third
698 argument for the filename you want to send. Optionally, you can add a fourth
699 argument for the DCC transfer blocksize, but the default of 1024 should
700 usually be fine. The fifth (and optional) argument is the request timeout
701 value in seconds (default: 300).
702  
703 Incidentally, you can send other weird nonstandard kinds of DCCs too;
704 just put something besides 'SEND' or 'CHAT' (say, 'FOO') in the type
705 field, and you'll get back C<irc_dcc_foo> events (with the same arguments as
706 L<C<irc_dcc_chat>|/irc_dcc_chat>) when data arrives on its DCC connection.
707  
708 If you are behind a firewall or Network Address Translation, you may want to
709 consult L<POE::Component::IRC|POE::Component::IRC>'s
710 L<C<connect>|POE::Component::IRC/spawn> for some parameters that are
711 useful with this command.
712  
713 =head2 C<dcc_accept>
714  
715 Accepts an incoming DCC connection from another host. First argument:
716 the magic cookie from an L<C<irc_dcc_request>|/irc_dcc_request> event.
717 In the case of a DCC GET, the second argument can optionally specify a
718 new name for the destination file of the DCC transfer, instead of using
719 the sender's name for it. (See the L<C<irc_dcc_request>|/irc_dcc_request>
720 section below for more details.)
721  
722 =head2 C<dcc_resume>
723  
724 Resumes a DCC SEND file transfer. First argument: the magic cookie from an
725 L<C<irc_dcc_request>|/irc_dcc_request> event. An optional second argument
726 provides the name of the file to which you want to write.
727  
728 =head2 C<dcc_chat>
729  
730 Sends lines of data to the person on the other side of a DCC CHAT connection.
731 The first argument should be the wheel id of the connection which you got
732 from an L<C<irc_dcc_start>|/irc_dcc_start> event, followed by all the data
733 you wish to send (it'll be separated with newlines for you).
734  
735 =head2 C<dcc_close>
736  
737 Terminates a DCC SEND or GET connection prematurely, and causes DCC CHAT
738 connections to close gracefully. Takes one argument: the wheel id of the
739 connection which you got from an L<C<irc_dcc_start>|/irc_dcc_start>
740 (or similar) event.
741  
742 =head1 OUTPUT EVENTS
743  
744 =head2 C<irc_dcc_request>
745  
746 B<Note:> This event is actually emitted by
747 L<POE::Filter::IRC::Compat|POE::Filter::IRC::Compat>, but documented here
748 to keep all the DCC documentation in one place. In case you were wondering.
749  
750 You receive this event when another IRC client sends you a DCC
751 (e.g. SEND or CHAT) request out of the blue. You can examine the request
752 and decide whether or not to accept it (with L<C<dcc_accept>|/dcc_accept>)
753 here. In the case of DCC SENDs, you can also request to resume the file with
754 L<C<dcc_resume>|/dcc_resume>.
755  
756 B<Note:> DCC doesn't provide a way to explicitly reject requests, so if you
757 don't intend to accept one, just ignore it or send a
758 L<NOTICE|POE::Component::IRC/notice> or L<PRIVMSG|POE::Component::IRC/privmsg>
759 to the peer explaining why you're not going to accept.
760  
761 =over 4
762  
763 =item * C<ARG0>: the peer's nick!user@host
764  
765 =item * C<ARG1>: the DCC type (e.g. 'CHAT' or 'SEND')
766  
767 =item * C<ARG2>: the port which the peer is listening on
768  
769 =item * C<ARG3>: this connection's "magic cookie"
770  
771 =item * C<ARG4>: the file name (SEND only)
772  
773 =item * C<ARG5>: the file size (SEND only)
774  
775 =item * C<ARG6>: the IP address which the peer is listening on
776  
777 =back
778  
779 =head2 C<irc_dcc_start>
780  
781 This event notifies you that a DCC connection has been successfully
782 established.
783  
784 =over 4
785  
786 =item * C<ARG0>: the connection's wheel id
787  
788 =item * C<ARG1>: the peer's nickname
789  
790 =item * C<ARG2>: the DCC type
791  
792 =item * C<ARG3>: the port number
793  
794 =item * C<ARG4>: the file name (SEND/GET only)
795  
796 =item * C<ARG5>: the file size (SEND/GET only)
797  
798 =item * C<ARG6>: the peer's IP address
799  
800 =back
801  
802 =head2 C<irc_dcc_chat>
803  
804 Notifies you that one line of text has been received from the
805 client on the other end of a DCC CHAT connection.
806  
807 =over 4
808  
809 =item * C<ARG0>: the connection's wheel id
810  
811 =item * C<ARG1>: the peer's nickname
812  
813 =item * C<ARG2>: the port number
814  
815 =item * C<ARG3>: the text they sent
816  
817 =item * C<ARG4>: the peer's IP address
818  
819 =back
820  
821 =head2 C<irc_dcc_get>
822  
823 Notifies you that another block of data has been successfully
824 transferred from the client on the other end of your DCC GET connection.
825  
826 =over 4
827  
828 =item * C<ARG0>: the connection's wheel id
829  
830 =item * C<ARG1>: the peer's nickname
831  
832 =item * C<ARG2>: the port number
833  
834 =item * C<ARG3>: the file name
835  
836 =item * C<ARG4>: the file size
837  
838 =item * C<ARG5>: transferred file size
839  
840 =item * C<ARG6>: the peer's IP address
841  
842 =back
843  
844 =head2 C<irc_dcc_send>
845  
846 Notifies you that another block of data has been successfully
847 transferred from you to the client on the other end of a DCC SEND
848 connection.
849  
850 =over 4
851  
852 =item * C<ARG0>: the connection's wheel id
853  
854 =item * C<ARG1>: the peer's nickname
855  
856 =item * C<ARG2>: the port number
857  
858 =item * C<ARG3>: the file name
859  
860 =item * C<ARG4>: the file size
861  
862 =item * C<ARG5>: transferred file size
863  
864 =item * C<ARG6>: the peer's IP address
865  
866 =back
867  
868 =head2 C<irc_dcc_done>
869  
870 You receive this event when a DCC connection terminates normally.
871 Abnormal terminations are reported by L<C<irc_dcc_error>|/irc_dcc_error>.
872  
873 =over 4
874  
875 =item * C<ARG0>: the connection's wheel id
876  
877 =item * C<ARG1>: the peer's nickname
878  
879 =item * C<ARG2>: the DCC type
880  
881 =item * C<ARG3>: the port number
882  
883 =item * C<ARG4>: the filename (SEND/GET only)
884  
885 =item * C<ARG5>: file size (SEND/GET only)
886  
887 =item * C<ARG6>: transferred file size (SEND/GET only)
888  
889 =item * C<ARG7>: the peer's IP address
890  
891 =back
892  
893 =head2 C<irc_dcc_error>
894  
895 You get this event whenever a DCC connection or connection attempt
896 terminates unexpectedly or suffers some fatal error. Some of the
897 following values might be undefined depending the stage at which
898 the connection/attempt failed.
899  
900 =over 4
901  
902 =item * C<ARG0>: the connection's wheel id
903  
904 =item * C<ARG1>: the error string
905  
906 =item * C<ARG2>: the peer's nickname
907  
908 =item * C<ARG3>: the DCC type
909  
910 =item * C<ARG4>: the port number
911  
912 =item * C<ARG5>: the file name
913  
914 =item * C<ARG6>: file size in bytes
915  
916 =item * C<ARG7>: transferred file size in bytes
917  
918 =item * C<ARG8>: the peer's IP address
919  
920 =back
921  
922 =head1 AUTHOR
923  
924 Dennis 'C<fimmtiu>' Taylor and Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
925  
926 =cut
« Component/IRC/Plugin/Proxy.pm ↑ Summary Component/IRC/Plugin/AutoJoin.pm »