about summary refs log tree commit homepage
path: root/lib/PublicInbox
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-08-21 01:29:04 +0000
committerEric Wong <e@80x24.org>2015-08-21 10:25:28 +0000
commitf71e9e9b67a6ff23642ccd119390bd6b3cb0d91e (patch)
tree432b1afa027048b608797651719d97ff1beb7480 /lib/PublicInbox
parent6a8f5c169911d1ee9338bb77a3d5302deb86fdb4 (diff)
downloadpublic-inbox-f71e9e9b67a6ff23642ccd119390bd6b3cb0d91e.tar.gz
Some folks may not want to download and install Perl code like
ssoma, so allow downloading an mbox containing the entire
thread.
Diffstat (limited to 'lib/PublicInbox')
-rw-r--r--lib/PublicInbox/Mbox.pm55
-rw-r--r--lib/PublicInbox/WWW.pm11
2 files changed, 66 insertions, 0 deletions
diff --git a/lib/PublicInbox/Mbox.pm b/lib/PublicInbox/Mbox.pm
new file mode 100644
index 00000000..2ec50656
--- /dev/null
+++ b/lib/PublicInbox/Mbox.pm
@@ -0,0 +1,55 @@
+# Copyright (C) 2015, all contributors <meta@public-inbox.org>
+# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+# Streaming interface for formatting messages as an mbox
+package PublicInbox::Mbox;
+use strict;
+use warnings;
+use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
+use Fcntl qw(SEEK_SET);
+
+sub thread_mbox {
+        my ($ctx, $srch) = @_;
+        my $mid = mid_compressed($ctx->{mid});
+        my $res = $srch->get_thread($mid);
+        my $msgs = delete $res->{msgs};
+        require PublicInbox::GitCatFile;
+        require Email::Simple;
+        my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+
+        sub {
+                my ($res) = @_; # Plack callback
+                my $w = $res->([200, [ 'Content-Type' => 'text/plain' ] ]);
+                while (defined(my $smsg = shift @$msgs)) {
+                        my $msg = eval {
+                                my $path = 'HEAD:' . mid2path($smsg->mid);
+                                Email::Simple->new($git->cat_file($path));
+                        };
+                        emit($w, $msg) if $msg;
+                }
+        }
+}
+
+sub emit {
+        my ($fh, $simple) = @_; # Email::Simple object
+
+        # drop potentially confusing headers, ssoma already should've dropped
+        # Lines and Content-Length
+        foreach my $d (qw(Lines Content-Length Status)) {
+                $simple->header_set($d);
+        }
+
+        my $buf = $simple->header_obj->as_string;
+        unless ($buf =~ /\AFrom /) {
+                $fh->write("From a\@a Thu Jan  1 00:00:00 1970\n");
+        }
+        $fh->write($buf .= $simple->crlf);
+
+        $buf = $simple->body;
+        $simple->body_set('');
+        $buf =~ s/^(From )/>$1/gm;
+        $buf .= "\n" unless $buf =~ /\n\z/s;
+
+        $fh->write($buf);
+}
+
+1;
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index b0c13486..cd8a5705 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -53,6 +53,9 @@ sub run {
         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
                 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx, $cgi);
 
+        } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.mbox\z!o) {
+                invalid_list_mid(\%ctx, $1, $2) || get_thread_mbox(\%ctx, $cgi);
+
         } elsif ($path_info =~ m!$LISTNAME_RE/f/\S+\.txt\z!o) {
                 invalid_list_mid(\%ctx, $1, $2) ||
                         redirect_mid_txt(\%ctx, $cgi);
@@ -326,4 +329,12 @@ sub msg_pfx {
         "../f/$href.html";
 }
 
+# /$LISTNAME/t/$MESSAGE_ID.mbox                    -> search results as mbox
+sub get_thread_mbox {
+        my ($ctx, $cgi) = @_;
+        my $srch = searcher($ctx) or return need_search($ctx);
+        require PublicInbox::Mbox;
+        PublicInbox::Mbox::thread_mbox($ctx, $srch);
+}
+
 1;