Permalink
Browse files

Fix DCC RESUME, it was broken

  • Loading branch information...
1 parent 12e737e commit 385f87bb77d6c1868086da21bf76a60cd5f852a3 @hinrik hinrik committed Nov 28, 2011
Showing with 29 additions and 17 deletions.
  1. +1 −0 Changes
  2. +1 −0 dist.ini
  3. +13 −14 lib/POE/Component/IRC/Plugin/DCC.pm
  4. +14 −3 t/04_plugins/17_dcc/05_resume.t
View
@@ -1,6 +1,7 @@
Revision history for Perl extension POE::Component::IRC.
{{$NEXT}}
+ - DCC.pm: Fix DCC RESUME, it was broken
- NickReclaim.pm: Make it more robust and prevent an error from being
raised when we quit from IRC.
View
@@ -34,6 +34,7 @@ IRC::Utils = 0.11
[Prereqs / TestRequires]
Test::More = 0.47
+Test::Differences = 0.61
[Prereqs / RuntimeRecommends]
POE::Component::Client::DNS = 0.99
@@ -130,6 +130,7 @@ sub S_dcc_request {
next if $cookie->{nick} ne $nick;
next if $cookie->{port} ne $port;
$file = _quote_file($file);
+ $cookie->{done} = $size;
$irc->yield(ctcp => $nick => "DCC ACCEPT $file $port $size");
last;
}
@@ -349,6 +350,9 @@ sub _U_dcc_close {
delete $self->{dcc}->{$id}->{wheel};
}
+ # flush the filehandle
+ close $self->{dcc}{$id}{fh} if $self->{dcc}{$id}{type} eq 'GET';
+
delete $self->{dcc}->{$id};
return;
}
@@ -360,23 +364,17 @@ sub _U_dcc_resume {
my $sender_file = _quote_file($cookie->{file});
$cookie->{file} = $myfile if defined $myfile;
- my $size = -s $cookie->{file};
- my $fraction = $size % IN_BLOCKSIZE;
- $size -= $fraction;
+ $cookie->{done} = -s $cookie->{file};
+ $cookie->{resuming} = 1;
- # we need to truncate the whole thing, adjust the size we are
- # requesting to the size we will truncate the file to
if (open(my $handle, '>>', $cookie->{file})) {
- if (!truncate($handle, $size)) {
- warn "dcc_resume: Can't truncate '$cookie->{file}' to size $size\n";
- return;
- }
-
- $irc->yield(ctcp => $cookie->{nick} => "DCC RESUME $sender_file $cookie->{port} $size");
-
- # save the cookie for later
+ $irc->yield(ctcp => $cookie->{nick} => "DCC RESUME $sender_file $cookie->{port} $cookie->{done}");
$self->{resuming}->{"$cookie->{port}+$cookie->{nick}"} = $cookie;
}
+ else {
+ warn "dcc_resume: Can't append to file '$cookie->{file}'\n";
+ return;
+ }
return;
}
@@ -580,7 +578,7 @@ sub _dcc_up {
my $handle;
if ($self->{dcc}->{$id}->{type} eq 'GET') {
# check if we're resuming
- my $mode = -s $self->{dcc}->{$id}->{file} ? '>>' : '>';
+ my $mode = $self->{dcc}->{$id}->{resuming} ? '>>' : '>';
if ( !open $handle, $mode, $self->{dcc}->{$id}->{file} ) {
$kernel->yield(_dcc_failed => 'open file', $! + 0, $!, $id);
@@ -597,6 +595,7 @@ sub _dcc_up {
}
binmode $handle;
+ seek $handle, $self->{dcc}{$id}{done}, 0;
# Send the first packet to get the ball rolling.
read $handle, my $buffer, $self->{dcc}->{$id}->{blocksize};
$self->{dcc}->{$id}->{wheel}->put($buffer);
@@ -5,7 +5,8 @@ use File::Temp qw(tempfile);
use POE;
use POE::Component::IRC;
use POE::Component::Server::IRC;
-use Test::More tests => 13;
+use Test::Differences;
+use Test::More tests => 12;
my ($resume_fh, $resume_file) = tempfile(UNLINK => 1);
@@ -111,9 +112,19 @@ sub irc_dcc_start {
sub irc_dcc_done {
my ($sender, $size1, $size2) = @_[SENDER, ARG5, ARG6];
- pass('Got dcc close');
+ my $irc = $sender->get_heap();
+ return if $irc != $bot2;
+ pass('Got dcc done');
is($size1, $size2, 'Send test results');
- $sender->get_heap()->yield('quit');
+
+ open my $orig, '<', 'Changes' or die $!;
+ open my $resume, '<', $resume_file or die $!;
+ my $orig_changes = do { local $/; <$orig> };
+ my $resume_changes = do { local $/; <$resume> };
+ eq_or_diff($resume_changes, $orig_changes, 'File contents match');
+
+ $bot1->yield('quit');
+ $bot2->yield('quit');
}
sub irc_dcc_error {

0 comments on commit 385f87b

Please sign in to comment.