From f71e9e9b67a6ff23642ccd119390bd6b3cb0d91e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 21 Aug 2015 01:29:04 +0000 Subject: support dumping thread as an mbox Some folks may not want to download and install Perl code like ssoma, so allow downloading an mbox containing the entire thread. --- lib/PublicInbox/Mbox.pm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/PublicInbox/Mbox.pm (limited to 'lib/PublicInbox/Mbox.pm') 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 +# 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; -- cgit v1.2.3-24-ge0c7