1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Each instance of this represents an IMAP client connected to
5 # public-inbox-imapd. Much of this was taken from NNTP, but
6 # further refined while experimenting on future ideas to handle
11 # * NNTP article numbers are UIDs, mm->created_at is UIDVALIDITY
13 # * public-inboxes are sliced into mailboxes of 50K messages
14 # to not overload MUAs: $NEWSGROUP_NAME.$SLICE_INDEX
15 # Slices are similar in concept to v2 "epochs". Epochs
16 # are for the limitations of git clients, while slices are
17 # for the limitations of IMAP clients.
19 # * We also take advantage of slices being only 50K to store
20 # "UID offset" to message sequence number (MSN) mapping
21 # as a 50K uint16_t array (via pack("S*", ...)). "UID offset"
22 # is the offset from {uid_base} which determines the start of
26 # imapd: PublicInbox::IMAPD ref
27 # ibx: PublicInbox::Inbox ref
28 # long_cb: long_response private data
29 # uid_base: base UID for mailbox slice (0-based)
30 # -login_tag: IMAP TAG for LOGIN
31 # -idle_tag: IMAP response tag for IDLE
32 # uo2m: UID-to-MSN mapping
33 package PublicInbox::IMAP;
35 use parent qw(PublicInbox::DS);
37 use PublicInbox::EmlContentFoo qw(parse_content_disposition);
38 use PublicInbox::DS qw(now);
39 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
40 use PublicInbox::GitAsyncCat;
41 use Text::ParseWords qw(parse_line);
43 use PublicInbox::IMAPsearchqp;
46 for my $mod (qw(Email::Address::XS Mail::Address)) {
47 eval "require $mod" or next;
48 $Address = $mod and last;
50 die "neither Email::Address::XS nor Mail::Address loaded: $@" if !$Address;
52 sub LINE_MAX () { 8000 } # RFC 2683 3.2.1.5
54 # Changing UID_SLICE will cause grief for clients which cache.
55 # This also needs to be <64K: we pack it into a uint16_t
56 # for long_response UID (offset) => MSN mappings
57 sub UID_SLICE () { 50_000 }
59 # these values area also used for sorting
60 sub NEED_SMSG () { 1 }
61 sub NEED_BLOB () { NEED_SMSG|2 }
62 sub CRLF_BREF () { 4 }
64 sub CRLF_HDR () { 16 }
66 sub CRLF_BDY () { 64 }
67 my $OP_EML_NEW = [ EML_HDR - 1, \&op_eml_new ];
68 my $OP_CRLF_BREF = [ CRLF_BREF, \&op_crlf_bref ];
69 my $OP_CRLF_HDR = [ CRLF_HDR, \&op_crlf_hdr ];
70 my $OP_CRLF_BDY = [ CRLF_BDY, \&op_crlf_bdy ];
73 'BODY[HEADER]' => [ NEED_BLOB|EML_HDR|CRLF_HDR, \&emit_rfc822_header ],
74 'BODY[TEXT]' => [ NEED_BLOB|EML_BDY|CRLF_BDY, \&emit_rfc822_text ],
75 'BODY[]' => [ NEED_BLOB|CRLF_BREF, \&emit_rfc822 ],
76 'RFC822.HEADER' => [ NEED_BLOB|EML_HDR|CRLF_HDR, \&emit_rfc822_header ],
77 'RFC822.TEXT' => [ NEED_BLOB|EML_BDY|CRLF_BDY, \&emit_rfc822_text ],
78 'RFC822.SIZE' => [ NEED_SMSG, \&emit_rfc822_size ],
79 RFC822 => [ NEED_BLOB|CRLF_BREF, \&emit_rfc822 ],
80 BODY => [ NEED_BLOB|EML_HDR|EML_BDY, \&emit_body ],
81 BODYSTRUCTURE => [ NEED_BLOB|EML_HDR|EML_BDY, \&emit_bodystructure ],
82 ENVELOPE => [ NEED_BLOB|EML_HDR, \&emit_envelope ],
83 FLAGS => [ 0, \&emit_flags ],
84 INTERNALDATE => [ NEED_SMSG, \&emit_internaldate ],
86 my %FETCH_ATT = map { $_ => [ $_ ] } keys %FETCH_NEED;
88 # aliases (RFC 3501 section 6.4.5)
89 $FETCH_ATT{FAST} = [ qw(FLAGS INTERNALDATE RFC822.SIZE) ];
90 $FETCH_ATT{ALL} = [ @{$FETCH_ATT{FAST}}, 'ENVELOPE' ];
91 $FETCH_ATT{FULL} = [ @{$FETCH_ATT{ALL}}, 'BODY' ];
93 for my $att (keys %FETCH_ATT) {
94 my %h = map { $_ => $FETCH_NEED{$_} } @{$FETCH_ATT{$att}};
95 $FETCH_ATT{$att} = \%h;
99 my $valid_range = '[0-9]+|[0-9]+:[0-9]+|[0-9]+:\*';
100 $valid_range = qr/\A(?:$valid_range)(?:,(?:$valid_range))*\z/;
104 my $capa = capa($self);
105 $self->write(\"* OK [$capa] public-inbox-imapd ready\r\n");
109 my ($class, $sock, $imapd) = @_;
110 my $self = bless { imapd => $imapd }, 'PublicInbox::IMAP_preauth';
113 if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
114 return CORE::close($sock) if $! != EAGAIN;
115 $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
116 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&greet ];
118 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
120 $self->{wbuf} = $wbuf;
132 # dovecot advertises IDLE pre-login; perhaps because some clients
133 # depend on it, so we'll do the same
134 my $capa = 'CAPABILITY IMAP4rev1 IDLE';
135 if ($self->logged_in) {
136 $capa .= ' COMPRESS=DEFLATE';
138 if (!($self->{sock} // $self)->can('accept_SSL') &&
139 $self->{imapd}->{accept_tls}) {
140 $capa .= ' STARTTLS';
142 $capa .= ' AUTH=ANONYMOUS';
146 sub login_success ($$) {
147 my ($self, $tag) = @_;
148 bless $self, 'PublicInbox::IMAP';
149 my $capa = capa($self);
150 "$tag OK [$capa] Logged in\r\n";
153 sub auth_challenge_ok ($) {
155 my $tag = delete($self->{-login_tag}) or return;
156 login_success($self, $tag);
159 sub cmd_login ($$$$) {
160 my ($self, $tag) = @_; # ignore ($user, $password) = ($_[2], $_[3])
161 login_success($self, $tag);
165 my ($self, $tag) = @_;
166 delete @$self{qw(uid_base uo2m)};
167 delete $self->{ibx} ? "$tag OK Close done\r\n"
168 : "$tag BAD No mailbox\r\n";
171 sub cmd_logout ($$) {
172 my ($self, $tag) = @_;
173 delete $self->{-idle_tag};
174 $self->write(\"* BYE logging out\r\n$tag OK Logout done\r\n");
175 $self->shutdn; # PublicInbox::DS::shutdn
179 sub cmd_authenticate ($$$) {
180 my ($self, $tag) = @_; # $method = $_[2], should be "ANONYMOUS"
181 $self->{-login_tag} = $tag;
185 sub cmd_capability ($$) {
186 my ($self, $tag) = @_;
187 '* '.capa($self)."\r\n$tag OK Capability done\r\n";
190 # uo2m: UID Offset to MSN, this is an arrayref by default,
191 # but uo2m_hibernate can compact and deduplicate it
192 sub uo2m_ary_new ($;$) {
193 my ($self, $exists) = @_;
194 my $ub = $self->{uid_base};
195 my $uids = $self->{ibx}->over(1)->uid_range($ub + 1, $ub + UID_SLICE);
197 # convert UIDs to offsets from {base}
198 my @tmp; # [$UID_OFFSET] => $MSN
201 $tmp[$_ - $ub] = ++$msn for @$uids;
202 $$exists = $msn if $exists;
206 # changes UID-offset-to-MSN mapping into a deduplicated scalar:
207 # uint16_t uo2m[UID_SLICE].
208 # May be swapped out for idle clients if THP is disabled.
209 sub uo2m_hibernate ($) {
211 ref(my $uo2m = $self->{uo2m}) or return;
212 my %dedupe = ( uo2m_pack($uo2m) => undef );
213 $self->{uo2m} = (keys(%dedupe))[0];
217 sub uo2m_last_uid ($) {
219 defined(my $uo2m = $self->{uo2m}) or die 'BUG: uo2m_last_uid w/o {uo2m}';
220 (ref($uo2m) ? @$uo2m : (length($uo2m) >> 1)) + $self->{uid_base};
224 # $_[0] is an arrayref of MSNs, it may have undef gaps if there
225 # are gaps in the corresponding UIDs: [ msn1, msn2, undef, msn3 ]
226 no warnings 'uninitialized';
227 pack('S*', @{$_[0]});
230 # extend {uo2m} to account for new messages which arrived since
231 # {uo2m} was created.
232 sub uo2m_extend ($$;$) {
233 my ($self, $new_uid_max) = @_;
234 defined(my $uo2m = $self->{uo2m}) or
235 return($self->{uo2m} = uo2m_ary_new($self));
236 my $beg = uo2m_last_uid($self); # last UID we've learned
237 return $uo2m if $beg >= $new_uid_max; # fast path
239 # need to extend the current range:
240 my $base = $self->{uid_base};
242 my $uids = $self->{ibx}->over(1)->uid_range($beg, $base + UID_SLICE);
243 return $uo2m if !scalar(@$uids);
244 my @tmp; # [$UID_OFFSET] => $MSN
245 my $write_method = $_[2] // 'msg_more';
247 my $msn = $uo2m->[-1];
248 $tmp[$_ - $beg] = ++$msn for @$uids;
249 $self->$write_method("* $msn EXISTS\r\n");
253 my $msn = unpack('S', substr($uo2m, -2, 2));
254 $tmp[$_ - $beg] = ++$msn for @$uids;
255 $self->$write_method("* $msn EXISTS\r\n");
256 $uo2m .= uo2m_pack(\@tmp);
257 my %dedupe = ($uo2m => undef);
258 $self->{uo2m} = (keys %dedupe)[0];
263 my ($self, $tag) = @_;
264 defined($self->{uid_base}) and
265 uo2m_extend($self, $self->{uid_base} + UID_SLICE);
266 \"$tag OK Noop done\r\n";
269 # the flexible version which works on scalars and array refs.
270 # Must call uo2m_extend before this
272 my ($self, $uid) = @_;
273 my $uo2m = $self->{uo2m};
274 my $off = $uid - $self->{uid_base} - 1;
275 ref($uo2m) ? $uo2m->[$off] : unpack('S', substr($uo2m, $off << 1, 2));
278 # returns an arrayref of UIDs, so MSNs can be translated to UIDs via:
279 # $msn2uid->[$MSN-1] => $UID. The result of this is always ephemeral
280 # and does not live beyond the event loop.
283 my $base = $self->{uid_base};
284 my $uo2m = uo2m_extend($self, $base + UID_SLICE);
285 $uo2m = [ unpack('S*', $uo2m) ] if !ref($uo2m);
289 for my $msn (@$uo2m) {
291 $msn2uid[$msn - 1] = $uo + $base if $msn;
296 # converts a set of message sequence numbers in requests to UIDs:
297 sub msn_to_uid_range ($$) {
299 $_[1] =~ s!([0-9]+)!$msn2uid->[$1 - 1] // ($msn2uid->[-1] // 0 + 1)!sge;
302 # called by PublicInbox::InboxIdle
303 sub on_inbox_unlock {
304 my ($self, $ibx) = @_;
305 my $uid_end = $self->{uid_base} + UID_SLICE;
306 uo2m_extend($self, $uid_end, 'write');
307 my $new = uo2m_last_uid($self);
308 if ($new == $uid_end) { # max exceeded $uid_end
309 # continue idling w/o inotify
310 my $sock = $self->{sock} or return;
311 $ibx->unsubscribe_unlock(fileno($sock));
315 # called every minute or so by PublicInbox::DS::later
316 my $IDLERS; # fileno($obj->{sock}) => PublicInbox::IMAP
320 for my $i (values %$old) {
321 next if ($i->{wbuf} || !exists($i->{-idle_tag}));
322 $IDLERS->{fileno($i->{sock})} = $i;
323 $i->write(\"* OK Still here\r\n");
326 PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
330 my ($self, $tag) = @_;
331 # IDLE seems allowed by dovecot w/o a mailbox selected *shrug*
332 my $ibx = $self->{ibx} or return "$tag BAD no mailbox selected\r\n";
333 my $uid_end = $self->{uid_base} + UID_SLICE;
334 uo2m_extend($self, $uid_end);
335 my $sock = $self->{sock} or return;
336 my $fd = fileno($sock);
337 $self->{-idle_tag} = $tag;
338 # only do inotify on most recent slice
339 if ($ibx->over(1)->max < $uid_end) {
340 $ibx->subscribe_unlock($fd, $self);
341 $self->{imapd}->idler_start;
343 PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
344 $IDLERS->{$fd} = $self;
349 my ($self, $ibx) = @_;
350 my $sock = $self->{sock} or return;
351 my $fd = fileno($sock);
352 delete $IDLERS->{$fd};
353 $ibx->unsubscribe_unlock($fd);
357 my ($self, $tag) = @_; # $tag is "DONE" (case-insensitive)
358 defined(my $idle_tag = delete $self->{-idle_tag}) or
359 return "$tag BAD not idle\r\n";
360 my $ibx = $self->{ibx} or do {
361 warn "BUG: idle_tag set w/o inbox";
362 return "$tag BAD internal bug\r\n";
364 stop_idle($self, $ibx);
365 "$idle_tag OK Idle done\r\n";
368 sub ensure_slices_exist ($$$) {
369 my ($imapd, $ibx, $max) = @_;
370 defined(my $mb_top = $ibx->{newsgroup}) or return;
371 my $mailboxes = $imapd->{mailboxes};
373 for (my $i = int($max/UID_SLICE); $i >= 0; --$i) {
374 my $sub_mailbox = "$mb_top.$i";
375 last if exists $mailboxes->{$sub_mailbox};
376 $mailboxes->{$sub_mailbox} = $ibx;
377 $sub_mailbox =~ s/\Ainbox\./INBOX./i; # more familiar to users
378 push @created, $sub_mailbox;
380 return unless @created;
381 my $l = $imapd->{mailboxlist} or return;
382 push @$l, map { qq[* LIST (\\HasNoChildren) "." $_\r\n] } @created;
385 sub inbox_lookup ($$;$) {
386 my ($self, $mailbox, $examine) = @_;
387 my ($ibx, $exists, $uidmax, $uid_base) = (undef, 0, 0, 0);
388 $mailbox = lc $mailbox;
389 $ibx = $self->{imapd}->{mailboxes}->{$mailbox} or return;
390 my $over = $ibx->over(1);
391 if ($over != $ibx) { # not a dummy
392 $mailbox =~ /\.([0-9]+)\z/ or
393 die "BUG: unexpected dummy mailbox: $mailbox\n";
394 $uid_base = $1 * UID_SLICE;
396 $uidmax = $ibx->mm->num_highwater // 0;
398 $self->{uid_base} = $uid_base;
400 $self->{uo2m} = uo2m_ary_new($self, \$exists);
402 my $uid_end = $uid_base + UID_SLICE;
403 $exists = $over->imap_exists($uid_base, $uid_end);
405 ensure_slices_exist($self->{imapd}, $ibx, $over->max);
408 $self->{uid_base} = $uid_base;
410 delete $self->{uo2m};
412 # if "INBOX.foo.bar" is selected and "INBOX.foo.bar.0",
413 # check for new UID ranges (e.g. "INBOX.foo.bar.1")
414 if (my $z = $self->{imapd}->{mailboxes}->{"$mailbox.0"}) {
415 ensure_slices_exist($self->{imapd}, $z,
419 ($ibx, $exists, $uidmax + 1, $uid_base);
422 sub cmd_examine ($$$) {
423 my ($self, $tag, $mailbox) = @_;
424 # XXX: do we need this? RFC 5162/7162
425 my $ret = $self->{ibx} ? "* OK [CLOSED] previous closed\r\n" : '';
426 my ($ibx, $exists, $uidnext, $base) = inbox_lookup($self, $mailbox, 1);
427 return "$tag NO Mailbox doesn't exist: $mailbox\r\n" if !$ibx;
432 * OK [PERMANENTFLAGS ()] Read-only mailbox\r
433 * OK [UNSEEN $exists]\r
434 * OK [UIDNEXT $uidnext]\r
435 * OK [UIDVALIDITY $ibx->{uidvalidity}]\r
436 $tag OK [READ-ONLY] EXAMINE/SELECT done\r
444 } elsif ($v =~ /[{"\r\n%*\\\[]/) { # literal string
445 '{' . length($v) . "}\r\n" . $v;
446 } else { # quoted string
451 sub addr_envelope ($$;$) {
452 my ($eml, $x, $y) = @_;
453 my $v = $eml->header_raw($x) //
454 ($y ? $eml->header_raw($y) : undef) // return 'NIL';
456 my @x = $Address->parse($v) or return 'NIL';
458 map { '(' . join(' ',
459 _esc($_->name), 'NIL',
460 _esc($_->user), _esc($_->host)
466 sub eml_envelope ($) {
469 _esc($eml->header_raw('Date')),
470 _esc($eml->header_raw('Subject')),
471 addr_envelope($eml, 'From'),
472 addr_envelope($eml, 'Sender', 'From'),
473 addr_envelope($eml, 'Reply-To', 'From'),
474 addr_envelope($eml, 'To'),
475 addr_envelope($eml, 'Cc'),
476 addr_envelope($eml, 'Bcc'),
477 _esc($eml->header_raw('In-Reply-To')),
478 _esc($eml->header_raw('Message-ID')),
484 if ($hash && scalar keys %$hash) {
485 $hash = [ %$hash ]; # flatten hash into 1-dimensional array
486 '(' . join(' ', map { _esc($_) } @$hash) . ')';
492 sub body_disposition ($) {
494 my $cd = $eml->header_raw('Content-Disposition') or return 'NIL';
495 $cd = parse_content_disposition($cd);
496 my $buf = '('._esc($cd->{type});
497 $buf .= ' ' . _esc_hash($cd->{attributes});
501 sub body_leaf ($$;$) {
502 my ($eml, $structure, $hold) = @_;
504 $eml->{is_submsg} and # parent was a message/(rfc822|news|global)
505 $buf .= eml_envelope($eml). ' ';
507 $buf .= '('._esc($ct->{type}).' ';
508 $buf .= _esc($ct->{subtype});
509 $buf .= ' ' . _esc_hash($ct->{attributes});
510 $buf .= ' ' . _esc($eml->header_raw('Content-ID'));
511 $buf .= ' ' . _esc($eml->header_raw('Content-Description'));
512 my $cte = $eml->header_raw('Content-Transfer-Encoding') // '7bit';
513 $buf .= ' ' . _esc($cte);
514 $buf .= ' ' . $eml->{imap_body_len};
515 $buf .= ' '.($eml->body_raw =~ tr/\n/\n/) if lc($ct->{type}) eq 'text';
517 # for message/(rfc822|global|news), $hold[0] should have envelope
518 $buf .= ' ' . (@$hold ? join('', @$hold) : 'NIL') if $hold;
521 $buf .= ' '._esc($eml->header_raw('Content-MD5'));
522 $buf .= ' '. body_disposition($eml);
523 $buf .= ' '._esc($eml->header_raw('Content-Language'));
524 $buf .= ' '._esc($eml->header_raw('Content-Location'));
529 sub body_parent ($$$) {
530 my ($eml, $structure, $hold) = @_;
532 my $type = lc($ct->{type});
533 if ($type eq 'multipart') {
535 $buf .= @$hold ? join('', @$hold) : 'NIL';
536 $buf .= ' '._esc($ct->{subtype});
538 $buf .= ' '._esc_hash($ct->{attributes});
539 $buf .= ' '.body_disposition($eml);
540 $buf .= ' '._esc($eml->header_raw('Content-Language'));
541 $buf .= ' '._esc($eml->header_raw('Content-Location'));
545 } else { # message/(rfc822|global|news)
546 @$hold = (body_leaf($eml, $structure, $hold));
550 # this is gross, but we need to process the parent part AFTER
551 # the child parts are done
552 sub bodystructure_prep {
554 my ($eml, $depth) = @$p; # ignore idx
555 # set length here, as $eml->{bdy} gets deleted for message/rfc822
556 $eml->{imap_body_len} = length($eml->body_raw);
557 push @$q, $eml, $depth;
560 # for FETCH BODY and FETCH BODYSTRUCTURE
561 sub fetch_body ($;$) {
562 my ($eml, $structure) = @_;
564 $eml->each_part(\&bodystructure_prep, \@q, 0, 1);
568 my ($part, $depth) = splice(@q, -2);
569 my $is_mp_parent = $depth == ($cur_depth - 1);
573 body_parent($part, $structure, \@hold);
575 unshift @hold, body_leaf($part, $structure);
581 sub requeue_once ($) {
583 # COMPRESS users all share the same DEFLATE context.
584 # Flush it here to ensure clients don't see
588 # no recursion, schedule another call ASAP,
589 # but only after all pending writes are done.
591 my $new_size = push(@{$self->{wbuf}}, \&long_step);
593 # wbuf may be populated by $cb, no need to rearm if so:
594 $self->requeue if $new_size == 1;
598 my ($self, $smsg, $bref, $ops, $partial) = @_;
599 my $uid = $smsg->{num};
600 $self->msg_more('* '.uid2msn($self, $uid)." FETCH (UID $uid");
602 for (my $i = 0; $i < @$ops;) {
604 $ops->[$i++]->($self, $k, $smsg, $bref, $eml);
606 partial_emit($self, $partial, $eml) if $partial;
607 $self->msg_more(")\r\n");
610 sub fetch_blob_cb { # called by git->cat_async via ibx_async_cat
611 my ($bref, $oid, $type, $size, $fetch_arg) = @_;
612 my ($self, undef, $msgs, $range_info, $ops, $partial) = @$fetch_arg;
613 my $ibx = $self->{ibx} or return $self->close; # client disconnected
614 my $smsg = shift @$msgs or die 'BUG: no smsg';
615 if (!defined($oid)) {
616 # it's possible to have TOCTOU if an admin runs
617 # public-inbox-(edit|purge), just move onto the next message
618 warn "E: $smsg->{blob} missing in $ibx->{inboxdir}\n";
619 return requeue_once($self);
621 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
624 if (!$self->{wbuf} && (my $nxt = $msgs->[0])) {
625 $pre = ibx_async_prefetch($ibx, $nxt->{blob},
626 \&fetch_blob_cb, $fetch_arg);
628 fetch_run_ops($self, $smsg, $bref, $ops, $partial);
629 $pre ? $self->zflush : requeue_once($self);
633 my ($self, $k, undef, $bref) = @_;
634 $self->msg_more(" $k {" . length($$bref)."}\r\n");
635 $self->msg_more($$bref);
638 # Mail::IMAPClient::message_string cares about this by default,
639 # (->Ignoresizeerrors attribute). Admins are encouraged to
640 # --reindex for IMAP support, anyways.
641 sub emit_rfc822_size {
642 my ($self, $k, $smsg) = @_;
643 $self->msg_more(' RFC822.SIZE ' . $smsg->{bytes});
646 sub emit_internaldate {
647 my ($self, undef, $smsg) = @_;
648 $self->msg_more(' INTERNALDATE "'.$smsg->internaldate.'"');
651 sub emit_flags { $_[0]->msg_more(' FLAGS ()') }
654 my ($self, undef, undef, undef, $eml) = @_;
655 $self->msg_more(' ENVELOPE '.eml_envelope($eml));
658 sub emit_rfc822_header {
659 my ($self, $k, undef, undef, $eml) = @_;
660 $self->msg_more(" $k {".length(${$eml->{hdr}})."}\r\n");
661 $self->msg_more(${$eml->{hdr}});
664 # n.b. this is sorted to be after any emit_eml_new ops
665 sub emit_rfc822_text {
666 my ($self, $k, undef, $bref) = @_;
667 $self->msg_more(" $k {".length($$bref)."}\r\n");
668 $self->msg_more($$bref);
671 sub emit_bodystructure {
672 my ($self, undef, undef, undef, $eml) = @_;
673 $self->msg_more(' BODYSTRUCTURE '.fetch_body($eml, 1));
677 my ($self, undef, undef, undef, $eml) = @_;
678 $self->msg_more(' BODY '.fetch_body($eml));
681 # set $eml once ($_[4] == $eml, $_[3] == $bref)
682 sub op_eml_new { $_[4] = PublicInbox::Eml->new($_[3]) }
684 # s/From / fixes old bug from import (pre-a0c07cba0e5d8b6a)
686 ${$_[0]} =~ s/(?<!\r)\n/\r\n/sg;
687 ${$_[0]} =~ s/\A[\r\n]*From [^\r\n]*\r\n//s;
690 sub op_crlf_bref { to_crlf_full($_[3]) }
692 sub op_crlf_hdr { to_crlf_full($_[4]->{hdr}) }
694 sub op_crlf_bdy { ${$_[4]->{bdy}} =~ s/(?<!\r)\n/\r\n/sg if $_[4]->{bdy} }
696 sub uid_clamp ($$$) {
697 my ($self, $beg, $end) = @_;
698 my $uid_min = $self->{uid_base} + 1;
699 my $uid_end = $uid_min + UID_SLICE - 1;
700 $$beg = $uid_min if $$beg < $uid_min;
701 $$end = $uid_end if $$end > $uid_end;
704 sub range_step ($$) {
705 my ($self, $range_csv) = @_;
706 my ($beg, $end, $range);
707 if ($$range_csv =~ s/\A([^,]+),//) {
710 $range = $$range_csv;
713 my $uid_base = $self->{uid_base};
714 my $uid_end = $uid_base + UID_SLICE;
715 if ($range =~ /\A([0-9]+):([0-9]+)\z/) {
716 ($beg, $end) = ($1 + 0, $2 + 0);
717 uid_clamp($self, \$beg, \$end);
718 } elsif ($range =~ /\A([0-9]+):\*\z/) {
720 $end = $self->{ibx}->over(1)->max;
721 $end = $uid_end if $end > $uid_end;
722 $beg = $end if $beg > $end;
723 uid_clamp($self, \$beg, \$end);
724 } elsif ($range =~ /\A[0-9]+\z/) {
725 $beg = $end = $range + 0;
726 # just let the caller do an out-of-range query if a single
727 # UID is out-of-range
728 ++$beg if ($beg <= $uid_base || $end > $uid_end);
730 return 'BAD fetch range';
732 [ $beg, $end, $$range_csv ];
735 sub refill_range ($$$) {
736 my ($self, $msgs, $range_info) = @_;
737 my ($beg, $end, $range_csv) = @$range_info;
738 if (scalar(@$msgs = @{$self->{ibx}->over(1)->query_xover($beg, $end)})){
739 $range_info->[0] = $msgs->[-1]->{num} + 1;
742 return 'OK Fetch done' if !$range_csv;
743 my $next_range = range_step($self, \$range_csv);
744 return $next_range if !ref($next_range); # error
745 @$range_info = @$next_range;
746 undef; # keep looping
749 sub fetch_blob { # long_response
750 my ($self, $tag, $msgs, $range_info, $ops, $partial) = @_;
751 while (!@$msgs) { # rare
752 if (my $end = refill_range($self, $msgs, $range_info)) {
753 $self->write(\"$tag $end\r\n");
757 uo2m_extend($self, $msgs->[-1]->{num});
758 ibx_async_cat($self->{ibx}, $msgs->[0]->{blob},
759 \&fetch_blob_cb, \@_);
762 sub fetch_smsg { # long_response
763 my ($self, $tag, $msgs, $range_info, $ops) = @_;
764 while (!@$msgs) { # rare
765 if (my $end = refill_range($self, $msgs, $range_info)) {
766 $self->write(\"$tag $end\r\n");
770 uo2m_extend($self, $msgs->[-1]->{num});
771 fetch_run_ops($self, $_, undef, $ops) for @$msgs;
776 sub refill_uids ($$$;$) {
777 my ($self, $uids, $range_info, $sql) = @_;
778 my ($beg, $end, $range_csv) = @$range_info;
779 my $over = $self->{ibx}->over(1);
781 if (scalar(@$uids = @{$over->uid_range($beg, $end, $sql)})) {
782 $range_info->[0] = $uids->[-1] + 1; # update $beg
784 } elsif (!$range_csv) {
787 my $next_range = range_step($self, \$range_csv);
788 return $next_range if !ref($next_range); # error
789 ($beg, $end, $range_csv) = @$range_info = @$next_range;
795 sub fetch_uid { # long_response
796 my ($self, $tag, $uids, $range_info, $ops) = @_;
797 if (defined(my $err = refill_uids($self, $uids, $range_info))) {
798 $err ||= 'OK Fetch done';
799 $self->write("$tag $err\r\n");
802 my $adj = $self->{uid_base} + 1;
803 my $uo2m = uo2m_extend($self, $uids->[-1]);
804 $uo2m = [ unpack('S*', $uo2m) ] if !ref($uo2m);
807 $self->msg_more("* $uo2m->[$_ - $adj] FETCH (UID $_");
808 for ($i = 0; $i < @$ops;) {
810 $ops->[$i++]->($self, $k);
812 $self->msg_more(")\r\n");
818 sub cmd_status ($$$;@) {
819 my ($self, $tag, $mailbox, @items) = @_;
820 return "$tag BAD no items\r\n" if !scalar(@items);
821 ($items[0] !~ s/\A\(//s || $items[-1] !~ s/\)\z//s) and
822 return "$tag BAD invalid args\r\n";
823 my ($ibx, $exists, $uidnext) = inbox_lookup($self, $mailbox);
824 return "$tag NO Mailbox doesn't exist: $mailbox\r\n" if !$ibx;
826 for my $it (@items) {
829 if ($it =~ /\A(?:MESSAGES|UNSEEN|RECENT)\z/) {
831 } elsif ($it eq 'UIDNEXT') {
833 } elsif ($it eq 'UIDVALIDITY') {
834 push @it, $ibx->{uidvalidity};
836 return "$tag BAD invalid item\r\n";
839 return "$tag BAD no items\r\n" if !@it;
840 "* STATUS $mailbox (".join(' ', @it).")\r\n" .
841 "$tag OK Status done\r\n";
844 my %patmap = ('*' => '.*', '%' => '[^\.]*');
845 sub cmd_list ($$$$) {
846 my ($self, $tag, $refname, $wildcard) = @_;
847 my $l = $self->{imapd}->{mailboxlist};
848 if ($refname eq '' && $wildcard eq '') {
849 # request for hierarchy delimiter
850 $l = [ qq[* LIST (\\Noselect) "." ""\r\n] ];
851 } elsif ($refname ne '' || $wildcard ne '*') {
852 $wildcard =~ s!([^a-z0-9_])!$patmap{$1} // "\Q$1"!egi;
853 $l = [ grep(/ \Q$refname\E$wildcard\r\n\z/is, @$l) ];
855 \(join('', @$l, "$tag OK List done\r\n"));
858 sub cmd_lsub ($$$$) {
859 my (undef, $tag) = @_; # same args as cmd_list
860 "$tag OK Lsub done\r\n";
863 sub eml_index_offs_i { # PublicInbox::Eml::each_part callback
865 my ($eml, undef, $idx) = @$p;
866 if ($idx && lc($eml->ct->{type}) eq 'multipart') {
867 $eml->{imap_bdy} = $eml->{bdy} // \'';
869 $all->{$idx} = $eml; # $idx => Eml
872 # prepares an index for BODY[$SECTION_IDX] fetches
873 sub eml_body_idx ($$) {
874 my ($eml, $section_idx) = @_;
875 my $idx = $eml->{imap_all_parts} // do {
877 $eml->each_part(\&eml_index_offs_i, $all, 0, 1);
878 # top-level of multipart, BODY[0] not allowed (nz-number)
880 $eml->{imap_all_parts} = $all;
882 $idx->{$section_idx};
885 # BODY[($SECTION_IDX)?(.$SECTION_NAME)?]<$offset.$bytes>
887 my ($eml, $section_idx, $section_name) = @_;
888 if (defined $section_idx) {
889 $eml = eml_body_idx($eml, $section_idx) or return;
891 if (defined $section_name) {
892 if ($section_name eq 'MIME') {
893 # RFC 3501 6.4.5 states:
894 # The MIME part specifier MUST be prefixed
895 # by one or more numeric part specifiers
896 return unless defined $section_idx;
897 return $eml->header_obj->as_string . "\r\n";
899 my $bdy = $eml->{bdy} // $eml->{imap_bdy} // \'';
900 $eml = PublicInbox::Eml->new($$bdy);
901 if ($section_name eq 'TEXT') {
902 return $eml->body_raw;
903 } elsif ($section_name eq 'HEADER') {
904 return $eml->header_obj->as_string . "\r\n";
906 die "BUG: bad section_name=$section_name";
909 ${$eml->{bdy} // $eml->{imap_bdy} // \''};
912 # similar to what's in PublicInbox::Eml::re_memo, but doesn't memoize
913 # to avoid OOM with malicious users
914 sub hdrs_regexp ($) {
916 my $names = join('|', map { "\Q$_" } split(/[ \t]+/, $hdrs));
917 qr/^(?:$names):[ \t]*[^\n]*\r?\n # 1st line
918 # continuation lines:
919 (?:[^:\n]*?[ \t]+[^\n]*\r?\n)*
923 # BODY[($SECTION_IDX.)?HEADER.FIELDS.NOT ($HDRS)]<$offset.$bytes>
924 sub partial_hdr_not {
925 my ($eml, $section_idx, $hdrs_re) = @_;
926 if (defined $section_idx) {
927 $eml = eml_body_idx($eml, $section_idx) or return;
929 my $str = $eml->header_obj->as_string;
930 $str =~ s/$hdrs_re//g;
931 $str =~ s/(?<!\r)\n/\r\n/sg;
935 # BODY[($SECTION_IDX.)?HEADER.FIELDS ($HDRS)]<$offset.$bytes>
936 sub partial_hdr_get {
937 my ($eml, $section_idx, $hdrs_re) = @_;
938 if (defined $section_idx) {
939 $eml = eml_body_idx($eml, $section_idx) or return;
941 my $str = $eml->header_obj->as_string;
942 $str = join('', ($str =~ m/($hdrs_re)/g));
943 $str =~ s/(?<!\r)\n/\r\n/sg;
947 sub partial_prepare ($$$$) {
948 my ($need, $partial, $want, $att) = @_;
950 # recombine [ "BODY[1.HEADER.FIELDS", "(foo", "bar)]" ]
951 # back to: "BODY[1.HEADER.FIELDS (foo bar)]"
952 return unless $att =~ /\ABODY\[/s;
953 until (rindex($att, ']') >= 0) {
954 my $next = shift @$want or return;
955 $att .= ' ' . uc($next);
957 if ($att =~ /\ABODY\[([0-9]+(?:\.[0-9]+)*)? # 1 - section_idx
958 (?:\.(HEADER|MIME|TEXT))? # 2 - section_name
959 \](?:<([0-9]+)(?:\.([0-9]+))?>)?\z/sx) { # 3, 4
960 $partial->{$att} = [ \&partial_body, $1, $2, $3, $4 ];
961 $$need |= CRLF_BREF|EML_HDR|EML_BDY;
962 } elsif ($att =~ /\ABODY\[(?:([0-9]+(?:\.[0-9]+)*)\.)? # 1 - section_idx
963 (?:HEADER\.FIELDS(\.NOT)?)\x20 # 2
964 \(([A-Z0-9\-\x20]+)\) # 3 - hdrs
965 \](?:<([0-9]+)(?:\.([0-9]+))?>)?\z/sx) { # 4 5
966 my $tmp = $partial->{$att} = [ $2 ? \&partial_hdr_not
969 $tmp->[2] = hdrs_regexp($3);
971 # don't emit CRLF_HDR instruction, here, partial_hdr_*
972 # will do CRLF conversion with only the extracted result
973 # and not waste time converting lines we don't care about.
980 sub partial_emit ($$$) {
981 my ($self, $partial, $eml) = @_;
983 my ($k, $cb, @args) = @$_;
984 my ($offset, $len) = splice(@args, -2);
985 # $cb is partial_body|partial_hdr_get|partial_hdr_not
986 my $str = $cb->($eml, @args) // '';
987 if (defined $offset) {
989 $str = substr($str, $offset, $len);
990 $k =~ s/\.$len>\z/>/ or warn
991 "BUG: unable to remove `.$len>' from `$k'";
993 $str = substr($str, $offset);
999 $self->msg_more(" $k {$len}\r\n");
1000 $self->msg_more($str);
1004 sub fetch_compile ($) {
1006 if ($want->[0] =~ s/\A\(//s) {
1007 $want->[-1] =~ s/\)\z//s or return 'BAD no rparen';
1009 my (%partial, %seen, @op);
1011 while (defined(my $att = shift @$want)) {
1013 next if $att eq 'UID'; # always returned
1014 $att =~ s/\ABODY\.PEEK\[/BODY\[/; # we're read-only
1015 my $x = $FETCH_ATT{$att};
1017 while (my ($k, $fl_cb) = each %$x) {
1018 next if $seen{$k}++;
1019 $need |= $fl_cb->[0];
1020 push @op, [ @$fl_cb, $k ];
1022 } elsif (!partial_prepare(\$need, \%partial, $want, $att)) {
1023 return "BAD param: $att";
1028 # stabilize partial order for consistency and ease-of-debugging:
1029 if (scalar keys %partial) {
1031 $r[2] = [ map { [ $_, @{$partial{$_}} ] } sort keys %partial ];
1034 push @op, $OP_EML_NEW if ($need & (EML_HDR|EML_BDY));
1036 # do we need CRLF conversion?
1037 if ($need & CRLF_BREF) {
1038 push @op, $OP_CRLF_BREF;
1039 } elsif (my $crlf = ($need & (CRLF_HDR|CRLF_BDY))) {
1040 if ($crlf == (CRLF_HDR|CRLF_BDY)) {
1041 push @op, $OP_CRLF_BREF;
1042 } elsif ($need & CRLF_HDR) {
1043 push @op, $OP_CRLF_HDR;
1045 push @op, $OP_CRLF_BDY;
1049 $r[0] = $need & NEED_BLOB ? \&fetch_blob :
1050 ($need & NEED_SMSG ? \&fetch_smsg : \&fetch_uid);
1052 # r[1] = [ $key1, $cb1, $key2, $cb2, ... ]
1053 use sort 'stable'; # makes output more consistent
1054 $r[1] = [ map { ($_->[2], $_->[1]) } sort { $a->[0] <=> $b->[0] } @op ];
1058 sub cmd_uid_fetch ($$$$;@) {
1059 my ($self, $tag, $range_csv, @want) = @_;
1060 my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
1061 my ($cb, $ops, $partial) = fetch_compile(\@want);
1062 return "$tag $cb\r\n" unless $ops;
1064 # cb is one of fetch_blob, fetch_smsg, fetch_uid
1065 $range_csv = 'bad' if $range_csv !~ $valid_range;
1066 my $range_info = range_step($self, \$range_csv);
1067 return "$tag $range_info\r\n" if !ref($range_info);
1068 uo2m_hibernate($self) if $cb == \&fetch_blob; # slow, save RAM
1069 long_response($self, $cb, $tag, [], $range_info, $ops, $partial);
1072 sub cmd_fetch ($$$$;@) {
1073 my ($self, $tag, $range_csv, @want) = @_;
1074 my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
1075 my ($cb, $ops, $partial) = fetch_compile(\@want);
1076 return "$tag $cb\r\n" unless $ops;
1078 # cb is one of fetch_blob, fetch_smsg, fetch_uid
1079 $range_csv = 'bad' if $range_csv !~ $valid_range;
1080 msn_to_uid_range(msn2uid($self), $range_csv);
1081 my $range_info = range_step($self, \$range_csv);
1082 return "$tag $range_info\r\n" if !ref($range_info);
1083 uo2m_hibernate($self) if $cb == \&fetch_blob; # slow, save RAM
1084 long_response($self, $cb, $tag, [], $range_info, $ops, $partial);
1087 sub msn_convert ($$) {
1088 my ($self, $uids) = @_;
1089 my $adj = $self->{uid_base} + 1;
1090 my $uo2m = uo2m_extend($self, $uids->[-1]);
1091 $uo2m = [ unpack('S*', $uo2m) ] if !ref($uo2m);
1092 $_ = $uo2m->[$_ - $adj] for @$uids;
1095 sub search_uid_range { # long_response
1096 my ($self, $tag, $sql, $range_info, $want_msn) = @_;
1098 if (defined(my $err = refill_uids($self, $uids, $range_info, $sql))) {
1099 $err ||= 'OK Search done';
1100 $self->write("\r\n$tag $err\r\n");
1103 msn_convert($self, $uids) if $want_msn;
1104 $self->msg_more(join(' ', '', @$uids));
1108 sub parse_imap_query ($$) {
1109 my ($self, $query) = @_;
1110 my $q = PublicInbox::IMAPsearchqp::parse($self, $query);
1112 my $max = $self->{ibx}->over(1)->max;
1114 uid_clamp($self, \$beg, \$max);
1115 $q->{range_info} = [ $beg, $max ];
1121 my ($self, $tag, $query, $want_msn) = @_;
1122 my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
1123 my $q = parse_imap_query($self, $query);
1124 return "$tag $q\r\n" if !ref($q);
1125 my ($sql, $range_info) = delete @$q{qw(sql range_info)};
1126 if (!scalar(keys %$q)) { # overview.sqlite3
1127 $self->msg_more('* SEARCH');
1128 long_response($self, \&search_uid_range,
1129 $tag, $sql, $range_info, $want_msn);
1130 } elsif ($q = $q->{xap}) {
1131 my $srch = $self->{ibx}->isrch or
1132 return "$tag BAD search not available for mailbox\r\n";
1136 uid_range => $range_info
1138 my $mset = $srch->mset($q, $opt);
1139 my $uids = $srch->mset_to_artnums($mset, $opt);
1140 msn_convert($self, $uids) if scalar(@$uids) && $want_msn;
1141 "* SEARCH @$uids\r\n$tag OK Search done\r\n";
1143 "$tag BAD Error\r\n";
1147 sub cmd_uid_search ($$$) {
1148 my ($self, $tag, $query) = @_;
1149 search_common($self, $tag, $query);
1152 sub cmd_search ($$$;) {
1153 my ($self, $tag, $query) = @_;
1154 search_common($self, $tag, $query, 1);
1157 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
1158 sub process_line ($$) {
1159 my ($self, $l) = @_;
1161 # TODO: IMAP allows literals for big requests to upload messages
1162 # (which we don't support) but maybe some big search queries use it.
1163 # RFC 3501 9 (2) doesn't permit TAB or multiple SP
1164 my ($tag, $req, @args) = parse_line('[ \t]+', 0, $l);
1165 pop(@args) if (@args && !defined($args[-1]));
1166 if (@args && uc($req) eq 'UID') {
1167 $req .= "_".(shift @args);
1170 if (defined(my $idle_tag = $self->{-idle_tag})) {
1171 (uc($tag // '') eq 'DONE' && !defined($req)) ?
1172 idle_done($self, $tag) :
1173 "$idle_tag BAD expected DONE\r\n";
1174 } elsif (my $cmd = $self->can('cmd_'.lc($req // ''))) {
1175 if ($cmd == \&cmd_uid_search || $cmd == \&cmd_search) {
1176 # preserve user-supplied quotes for search
1177 (undef, @args) = split(/ search /i, $l, 2);
1179 $cmd->($self, $tag, @args);
1180 } else { # this is weird
1181 auth_challenge_ok($self) //
1183 ' BAD Error in IMAP command '.
1185 ": Unknown command\r\n";
1189 if ($err && $self->{sock}) {
1191 err($self, 'error from: %s (%s)', $l, $err);
1193 $res = "$tag BAD program fault - command not performed\r\n";
1195 return 0 unless defined $res;
1201 # wbuf is unset or empty, here; {long} may add to it
1202 my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
1203 my $more = eval { $cb->($self, @args) };
1204 if ($@ || !$self->{sock}) { # something bad happened...
1205 delete $self->{long_cb};
1206 my $elapsed = now() - $t0;
1209 "%s during long response[$fd] - %0.6f",
1212 out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
1214 } elsif ($more) { # $self->{wbuf}:
1215 # control passed to ibx_async_cat if $more == \undef
1216 requeue_once($self) if !ref($more);
1217 } else { # all done!
1218 delete $self->{long_cb};
1219 my $elapsed = now() - $t0;
1220 my $fd = fileno($self->{sock});
1221 out($self, " deferred[$fd] done - %0.6f", $elapsed);
1222 my $wbuf = $self->{wbuf}; # do NOT autovivify
1224 $self->requeue unless $wbuf && @$wbuf;
1229 my ($self, $fmt, @args) = @_;
1230 printf { $self->{imapd}->{err} } $fmt."\n", @args;
1234 my ($self, $fmt, @args) = @_;
1235 printf { $self->{imapd}->{out} } $fmt."\n", @args;
1238 sub long_response ($$;@) {
1239 my ($self, $cb, @args) = @_; # cb returns true if more, false if done
1241 my $sock = $self->{sock} or return;
1242 # make sure we disable reading during a long response,
1243 # clients should not be sending us stuff and making us do more
1244 # work while we are stream a response to them
1245 $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
1246 long_step($self); # kick off!
1250 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
1254 return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
1256 # only read more requests if we've drained the write buffer,
1257 # otherwise we can be buffering infinitely w/o backpressure
1259 my $rbuf = $self->{rbuf} // \(my $x = '');
1260 my $line = index($$rbuf, "\n");
1262 if (length($$rbuf) >= LINE_MAX) {
1263 $self->write(\"\* BAD request too long\r\n");
1264 return $self->close;
1266 $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or
1267 return uo2m_hibernate($self);
1268 $line = index($$rbuf, "\n");
1270 $line = substr($$rbuf, 0, $line + 1, '');
1271 $line =~ s/\r?\n\z//s;
1272 return $self->close if $line =~ /[[:cntrl:]]/s;
1274 my $fd = fileno($self->{sock});
1275 my $r = eval { process_line($self, $line) };
1276 my $pending = $self->{wbuf} ? ' pending' : '';
1277 out($self, "[$fd] %s - %0.6f$pending - $r", $line, now() - $t0);
1279 return $self->close if $r < 0;
1280 $self->rbuf_idle($rbuf);
1282 # maybe there's more pipelined data, or we'll have
1283 # to register it for socket-readiness notifications
1284 $self->requeue unless $pending;
1287 sub compressed { undef }
1289 sub zflush {} # overridden by IMAPdeflate
1292 sub cmd_compress ($$$) {
1293 my ($self, $tag, $alg) = @_;
1294 return "$tag BAD DEFLATE only\r\n" if uc($alg) ne "DEFLATE";
1295 return "$tag BAD COMPRESS active\r\n" if $self->compressed;
1297 # CRIME made TLS compression obsolete
1298 # return "$tag NO [COMPRESSIONACTIVE]\r\n" if $self->tls_compressed;
1300 PublicInbox::IMAPdeflate->enable($self, $tag);
1305 sub cmd_starttls ($$) {
1306 my ($self, $tag) = @_;
1307 my $sock = $self->{sock} or return;
1308 if ($sock->can('stop_SSL') || $self->compressed) {
1309 return "$tag BAD TLS or compression already enabled\r\n";
1311 my $opt = $self->{imapd}->{accept_tls} or
1312 return "$tag BAD can not initiate TLS negotiation\r\n";
1313 $self->write(\"$tag OK begin TLS negotiation now\r\n");
1314 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
1315 $self->requeue if PublicInbox::DS::accept_tls_step($self);
1319 sub busy { # for graceful shutdown in PublicInbox::Daemon:
1321 if (defined($self->{-idle_tag})) {
1322 $self->write(\"* BYE server shutting down\r\n");
1323 return; # not busy anymore
1325 defined($self->{rbuf}) || defined($self->{wbuf}) ||
1326 !$self->write(\"* BYE server shutting down\r\n");
1331 if (my $ibx = delete $self->{ibx}) {
1332 stop_idle($self, $ibx);
1334 $self->SUPER::close; # PublicInbox::DS::close
1337 # we're read-only, so SELECT and EXAMINE do the same thing
1339 *cmd_select = \&cmd_examine;
1341 package PublicInbox::IMAP_preauth;
1342 our @ISA = qw(PublicInbox::IMAP);