foreach my $imap (@ready) {
my $x = $imap->_getline();
- $imap->_resp($x, sub($) {
- if ($stopwhen->($imap, shift)) {
+ $imap->_resp($x, sub($;$$) {
+ if ($stopwhen->(@_)) {
$aborted = 1;
$timeout = 0; # keep reading the handles while there is pending data
}
}, 'slurp');
}
}
+ return $aborted;
}
# after the $timeout) and false otherwise.
sub idle($$$) {
my ($self, $timeout, $stopwhen) = @_;
+ my $tag = $self->idle_start($timeout);
+ my $r = slurp([$self], $timeout // 1740, $stopwhen); # 29 mins
+ $r += $self->idle_stop($tag, $stopwhen);
+ return $r;
+}
+# $self->idle_start()
+# Enter IDLE (RFC 2177).
+# Return the command tag.
+sub idle_start($) {
+ my $self = shift;
$self->fail("Server did not advertise IDLE (RFC 2177) capability.")
unless $self->_capable('IDLE');
my $tag = $self->_cmd_init('IDLE');
$self->_cmd_flush();
- my $r = slurp([$self], $timeout // 1740, $stopwhen); # 29 mins
+ return $tag;
+}
+
+# $self->idle_stop($tag, $callback)
+# Stop a running IDLE (RFC 2177) command $tag.
+# Returns the number of untagged responses received between the DONE
+# the tagged response that are satisfying $callback.
+sub idle_stop($$$) {
+ my ($self, $tag, $callback) = @_;
+ my $r = 0;
# done idling
$self->_cmd_extend('DONE');
$self->_cmd_flush();
# run the callback again to update the return value if we received
# untagged responses between the DONE and the tagged response
- $self->_recv($tag, sub($) { $r = 1 if $stopwhen->($self, shift) }, 'slurp');
+ $self->_recv($tag, sub($;$$) { $r++ if $callback->($self, @_) }, 'slurp');
return $r;
}
$self->{_NEW} += $1 - $cache->{EXISTS} if $1 > $cache->{EXISTS}; # new mails
}
$cache->{EXISTS} = $1;
- $callback->($self->{_SELECTED} // $self->panic()) if defined $callback and $cmd eq 'slurp';
+ $callback->($self->{_SELECTED} // $self->panic(), EXISTS => $1) if defined $callback and $cmd eq 'slurp';
}
elsif (/\A([0-9]+) EXPUNGE\z/) {
$self->panic() unless defined $cache->{EXISTS}; # sanity check
if ($cmd eq 'FETCH' or $cmd eq 'STORE') {
$callback->(\%mail) if defined $uid and in_set($uid, $set);
} elsif ($cmd eq 'slurp') {
- $callback->($self->{_SELECTED} // $self->panic())
+ $callback->($self->{_SELECTED} // $self->panic(), FETCH => $seq)
}
}
}