]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: FETCH: more granular CRLF conversion
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index 12123072d1aed9e976b00607d265b3adb9ed575f..3cc68e668720814a9135ace5901ce43af2cf02c3 100644 (file)
@@ -40,20 +40,27 @@ sub UID_BLOCK () { 50_000 }
 # these values area also used for sorting
 sub NEED_SMSG () { 1 }
 sub NEED_BLOB () { NEED_SMSG|2 }
-sub NEED_EML () { NEED_BLOB|4 }
-my $OP_EML_NEW = [ NEED_EML - 1, \&op_eml_new ];
+sub CRLF_BREF () { 4 }
+sub EML_HDR () { 8 }
+sub CRLF_HDR () { 16 }
+sub EML_BDY () { 32 }
+sub CRLF_BDY () { 64 }
+my $OP_EML_NEW = [ EML_HDR - 1, \&op_eml_new ];
+my $OP_CRLF_BREF = [ CRLF_BREF, \&op_crlf_bref ];
+my $OP_CRLF_HDR = [ CRLF_HDR, \&op_crlf_hdr ];
+my $OP_CRLF_BDY = [ CRLF_BDY, \&op_crlf_bdy ];
 
 my %FETCH_NEED = (
-       'BODY[HEADER]' => [ NEED_EML, \&emit_rfc822_header ],
-       'BODY[TEXT]' => [ NEED_EML, \&emit_rfc822_text ],
-       'BODY[]' => [ NEED_BLOB, \&emit_rfc822 ],
-       'RFC822.HEADER' => [ NEED_EML, \&emit_rfc822_header ],
-       'RFC822.TEXT' => [ NEED_EML, \&emit_rfc822_text ],
+       'BODY[HEADER]' => [ NEED_BLOB|EML_HDR|CRLF_HDR, \&emit_rfc822_header ],
+       'BODY[TEXT]' => [ NEED_BLOB|EML_BDY|CRLF_BDY, \&emit_rfc822_text ],
+       'BODY[]' => [ NEED_BLOB|CRLF_BREF, \&emit_rfc822 ],
+       'RFC822.HEADER' => [ NEED_BLOB|EML_HDR|CRLF_HDR, \&emit_rfc822_header ],
+       'RFC822.TEXT' => [ NEED_BLOB|EML_BDY|CRLF_BDY, \&emit_rfc822_text ],
        'RFC822.SIZE' => [ NEED_SMSG, \&emit_rfc822_size ],
-       RFC822 => [ NEED_BLOB, \&emit_rfc822 ],
-       BODY => [ NEED_EML, \&emit_body ],
-       BODYSTRUCTURE => [ NEED_EML, \&emit_bodystructure ],
-       ENVELOPE => [ NEED_EML, \&emit_envelope ],
+       RFC822 => [ NEED_BLOB|CRLF_BREF, \&emit_rfc822 ],
+       BODY => [ NEED_BLOB|EML_HDR|EML_BDY, \&emit_body ],
+       BODYSTRUCTURE => [ NEED_BLOB|EML_HDR|EML_BDY, \&emit_bodystructure ],
+       ENVELOPE => [ NEED_BLOB|EML_HDR, \&emit_envelope ],
        FLAGS => [ 0, \&emit_flags ],
        INTERNALDATE => [ NEED_SMSG, \&emit_internaldate ],
 );
@@ -217,7 +224,7 @@ sub cmd_idle ($$) {
        my $ibx = $self->{ibx} or return "$tag BAD no mailbox selected\r\n";
        $self->{-idle_tag} = $tag;
        my $max = $ibx->mm->max // 0;
-       my $uid_end = ($self->{uid_base} // 0) + UID_BLOCK;
+       my $uid_end = $self->{uid_base} + UID_BLOCK;
        my $sock = $self->{sock} or return;
        my $fd = fileno($sock);
        # only do inotify on most recent slice
@@ -270,14 +277,13 @@ sub ensure_ranges_exist ($$$) {
 
 sub inbox_lookup ($$) {
        my ($self, $mailbox) = @_;
-       my ($ibx, $exists, $uidnext);
+       my ($ibx, $exists, $uidnext, $uid_base);
        if ($mailbox =~ /\A(.+)\.([0-9]+)\z/) {
                # old mail: inbox.comp.foo.$uid_block_idx
-               my ($mb_top, $uid_base) = ($1, $2 * UID_BLOCK);
-
+               my $mb_top = $1;
+               $uid_base = $2 * UID_BLOCK;
                $ibx = $self->{imapd}->{mailboxes}->{lc $mailbox} or return;
                $exists = $ibx->mm->max // 0;
-               $self->{uid_base} = $uid_base;
                ensure_ranges_exist($self->{imapd}, $ibx, $exists);
                my $uid_end = $uid_base + UID_BLOCK;
                $exists = $uid_end if $exists > $uid_end;
@@ -285,17 +291,17 @@ sub inbox_lookup ($$) {
                $exists -= $uid_base;
        } else { # check for dummy inboxes
                $ibx = $self->{imapd}->{mailboxes}->{lc $mailbox} or return;
-               delete $self->{uid_base};
-               $exists = 0;
+               $uid_base = $exists = 0;
                $uidnext = 1;
        }
-       ($ibx, $exists, $uidnext);
+       ($ibx, $exists, $uidnext, $uid_base);
 }
 
 sub cmd_examine ($$$) {
        my ($self, $tag, $mailbox) = @_;
-       my ($ibx, $exists, $uidnext) = inbox_lookup($self, $mailbox);
+       my ($ibx, $exists, $uidnext, $base) = inbox_lookup($self, $mailbox);
        return "$tag NO Mailbox doesn't exist: $mailbox\r\n" if !$ibx;
+       $self->{uid_base} = $base;
 
        # XXX: do we need this? RFC 5162/7162
        my $ret = $self->{ibx} ? "* OK [CLOSED] previous closed\r\n" : '';
@@ -495,12 +501,7 @@ sub fetch_blob_cb { # called by git->cat_async via git_async_cat
        } else {
                $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
        }
-       $$bref =~ s/(?<!\r)\n/\r\n/sg; # make strict clients happy
-
-       # fixup old bug from import (pre-a0c07cba0e5d8b6a)
-       $$bref =~ s/\A[\r\n]*From [^\r\n]*\r\n//s;
-       fetch_run_ops($self, $self->{uid_base} // 0,
-                       $smsg, $bref, $ops, $partial);
+       fetch_run_ops($self, $self->{uid_base}, $smsg, $bref, $ops, $partial);
        requeue_once($self);
 }
 
@@ -556,9 +557,21 @@ sub emit_body {
 # set $eml once ($_[4] == $eml, $_[3] == $bref)
 sub op_eml_new { $_[4] = PublicInbox::Eml->new($_[3]) }
 
+# s/From / fixes old bug from import (pre-a0c07cba0e5d8b6a)
+sub to_crlf_full {
+       ${$_[0]} =~ s/(?<!\r)\n/\r\n/sg;
+       ${$_[0]} =~ s/\A[\r\n]*From [^\r\n]*\r\n//s;
+}
+
+sub op_crlf_bref { to_crlf_full($_[3]) }
+
+sub op_crlf_hdr { to_crlf_full($_[4]->{hdr}) }
+
+sub op_crlf_bdy { ${$_[4]->{bdy}} =~ s/(?<!\r)\n/\r\n/sg if $_[4]->{bdy} }
+
 sub uid_clamp ($$$) {
        my ($self, $beg, $end) = @_;
-       my $uid_min = ($self->{uid_base} // 0) + 1;
+       my $uid_min = $self->{uid_base} + 1;
        my $uid_end = $uid_min + UID_BLOCK - 1;
        $$beg = $uid_min if $$beg < $uid_min;
        $$end = $uid_end if $$end > $uid_end;
@@ -625,7 +638,7 @@ sub fetch_smsg { # long_response
                        return;
                }
        }
-       my $uid_base = $self->{uid_base} // 0;
+       my $uid_base = $self->{uid_base};
        fetch_run_ops($self, $uid_base, $_, undef, $ops) for @$msgs;
        @$msgs = ();
        1; # more
@@ -652,7 +665,7 @@ sub fetch_uid { # long_response
                }
                # continue looping
        }
-       my $uid_base = $self->{uid_base} // 0;
+       my $uid_base = $self->{uid_base};
        my ($i, $k);
        for (@$uids) {
                $self->msg_more(fetch_msn_uid($uid_base, $_));
@@ -792,8 +805,8 @@ sub partial_hdr_get {
        join('', ($str =~ m/($hdrs_re)/g), "\r\n");
 }
 
-sub partial_prepare ($$$) {
-       my ($partial, $want, $att) = @_;
+sub partial_prepare ($$$$) {
+       my ($need, $partial, $want, $att) = @_;
 
        # recombine [ "BODY[1.HEADER.FIELDS", "(foo", "bar)]" ]
        # back to: "BODY[1.HEADER.FIELDS (foo bar)]"
@@ -806,6 +819,7 @@ sub partial_prepare ($$$) {
                        (?:\.(HEADER|MIME|TEXT))? # 2 - section_name
                        \](?:<([0-9]+)(?:\.([0-9]+))?>)?\z/sx) { # 3, 4
                $partial->{$att} = [ \&partial_body, $1, $2, $3, $4 ];
+               $$need |= CRLF_BREF|EML_HDR|EML_BDY;
        } elsif ($att =~ /\ABODY\[(?:([0-9]+(?:\.[0-9]+)*)\.)? # 1 - section_idx
                                (?:HEADER\.FIELDS(\.NOT)?)\x20 # 2
                                \(([A-Z0-9\-\x20]+)\) # 3 - hdrs
@@ -814,6 +828,7 @@ sub partial_prepare ($$$) {
                                                : \&partial_hdr_get,
                                                $1, undef, $4, $5 ];
                $tmp->[2] = hdrs_regexp($3);
+               $$need |= CRLF_HDR|EML_HDR;
        } else {
                undef;
        }
@@ -859,16 +874,9 @@ sub fetch_compile ($) {
                        while (my ($k, $fl_cb) = each %$x) {
                                next if $seen{$k}++;
                                $need |= $fl_cb->[0];
-
-                               # insert a special op to convert $bref to $eml
-                               # the first time we need it
-                               if ($need == NEED_EML && !$seen{$need}++) {
-                                       push @op, $OP_EML_NEW;
-                               }
-                               # $fl_cb = [ flags, \&emit_foo ]
-                               push @op, [ @$fl_cb , $k ];
+                               push @op, [ @$fl_cb, $k ];
                        }
-               } elsif (!partial_prepare(\%partial, $want, $att)) {
+               } elsif (!partial_prepare(\$need, \%partial, $want, $att)) {
                        return "BAD param: $att";
                }
        }
@@ -876,11 +884,25 @@ sub fetch_compile ($) {
 
        # stabilize partial order for consistency and ease-of-debugging:
        if (scalar keys %partial) {
-               $need = NEED_EML;
-               push @op, $OP_EML_NEW if !$seen{$need}++;
+               $need |= NEED_BLOB;
                $r[2] = [ map { [ $_, @{$partial{$_}} ] } sort keys %partial ];
        }
 
+       push @op, $OP_EML_NEW if ($need & (EML_HDR|EML_BDY));
+
+       # do we need CRLF conversion?
+       if ($need & CRLF_BREF) {
+               push @op, $OP_CRLF_BREF;
+       } elsif (my $crlf = ($need & (CRLF_HDR|CRLF_BDY))) {
+               if ($crlf == (CRLF_HDR|CRLF_BDY)) {
+                       push @op, $OP_CRLF_BREF;
+               } elsif ($need & CRLF_HDR) {
+                       push @op, $OP_CRLF_HDR;
+               } else {
+                       push @op, $OP_CRLF_BDY;
+               }
+       }
+
        $r[0] = $need & NEED_BLOB ? \&fetch_blob :
                ($need & NEED_SMSG ? \&fetch_smsg : \&fetch_uid);
 
@@ -903,7 +925,7 @@ sub cmd_uid_fetch ($$$$;@) {
 }
 
 sub msn_to_uid_range ($$) {
-       my $uid_base = $_[0]->{uid_base} // 0;
+       my $uid_base = $_[0]->{uid_base};
        $_[1] =~ s/([0-9]+)/$uid_base + $1/sge;
 }