about summary refs log tree commit homepage
path: root/lib/PublicInbox/Mbox.pm
diff options
context:
space:
mode:
authorEric Wong (Contractor, The Linux Foundation) <e@80x24.org>2018-03-23 01:54:16 +0000
committerEric Wong (Contractor, The Linux Foundation) <e@80x24.org>2018-03-23 01:58:40 +0000
commit41654a8cd9372c0640c4ca5339e5881927965e41 (patch)
tree60f475bc00157cb124de54bfe63622e34219cb05 /lib/PublicInbox/Mbox.pm
parentf6285ab9d73a4eae490dda325096e61eadc415cd (diff)
downloadpublic-inbox-41654a8cd9372c0640c4ca5339e5881927965e41.tar.gz
Since v2 supports duplicate messages, we need to support
looking up different messages with the same Message-Id.
Fortunately, our "raw" endpoint has always been mboxrd,
so users won't need to change their parsing tools.
Diffstat (limited to 'lib/PublicInbox/Mbox.pm')
-rw-r--r--lib/PublicInbox/Mbox.pm71
1 files changed, 62 insertions, 9 deletions
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
index 84cc3845..79e09a70 100644
--- a/lib/PublicInbox/Mbox.pm
+++ b/lib/PublicInbox/Mbox.pm
@@ -26,12 +26,68 @@ sub subject_fn ($) {
         $fn eq '' ? 'no-subject' : $fn;
 }
 
-sub emit1 {
-        my ($ctx, $msg) = @_;
-        $msg = Email::Simple->new($msg);
-        my $fn = subject_fn($msg);
+sub smsg_for ($$$) {
+        my ($head, $db, $mid) = @_;
+        my $doc_id = $head->get_docid;
+        my $doc = $db->get_document($doc_id);
+        PublicInbox::SearchMsg->wrap($doc, $mid)->load_expand;
+}
+
+sub mb_stream {
+        my ($more) = @_;
+        bless $more, 'PublicInbox::Mbox';
+}
+
+# called by PSGI server as body response
+sub getline {
+        my ($more) = @_; # self
+        my ($ctx, $head, $tail, $db, $cur) = @$more;
+        if ($cur) {
+                pop @$more;
+                return msg_str($ctx, $cur);
+        }
+        for (; !defined($cur) && $head != $tail; $head++) {
+                my $smsg = smsg_for($head, $db, $ctx->{mid});
+                next if $smsg->type ne 'mail';
+                my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
+                $cur = Email::Simple->new($mref);
+                $cur = msg_str($ctx, $cur);
+        }
+        $more->[1] = $head;
+        $cur;
+}
+
+sub close {} # noop
+
+sub emit_raw {
+        my ($ctx) = @_;
+        my $mid = $ctx->{mid};
+        my $ibx = $ctx->{-inbox};
+        my $first;
+        my $more;
+        my ($head, $tail, $db);
+        my %seen;
+        if (my $srch = $ibx->search) {
+                $srch->retry_reopen(sub {
+                        ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
+                        for (; !defined($first) && $head != $tail; $head++) {
+                                my $smsg = smsg_for($head, $db, $mid);
+                                next if $smsg->type ne 'mail';
+                                my $mref = $ibx->msg_by_smsg($smsg) or next;
+                                $first = Email::Simple->new($mref);
+                        }
+                        if ($head != $tail) {
+                                $more = [ $ctx, $head, $tail, $db, $first ];
+                        }
+                });
+        } else {
+                my $mref = $ibx->msg_by_mid($mid) or return;
+                $first = Email::Simple->new($mref);
+        }
+        return unless defined $first;
+        my $fn = subject_fn($first);
         my @hdr = ('Content-Type');
-        if ($ctx->{-inbox}->{obfuscate}) {
+        if ($ibx->{obfuscate}) {
                 # obfuscation is stupid, but maybe scrapers are, too...
                 push @hdr, 'application/mbox';
                 $fn .= '.mbox';
@@ -40,10 +96,7 @@ sub emit1 {
                 $fn .= '.txt';
         }
         push @hdr, 'Content-Disposition', "inline; filename=$fn";
-
-        # single message should be easily renderable in browsers,
-        # unless obfuscation is enabled :<
-        [ 200, \@hdr, [ msg_str($ctx, $msg) ] ]
+        [ 200, \@hdr, $more ? mb_stream($more) : [ msg_str($ctx, $first) ] ];
 }
 
 sub msg_str {