Component/IRC/Plugin/DCC.pm

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