Blob
Date:
Tue Aug 30 11:40:44 2022 UTC
Message:
urlencode the mail/thread id reminded by semarie@, thanks!
#!/usr/bin/env perl
#
# mexp was written by Omar Polo <op@openbsd.org> and is placed in the
# public domain. The author hereby disclaims copyright to this source
# code.
use open ":std", ":encoding(UTF-8)";
use utf8;
use strict;
use warnings;
use v5.32;
use File::Temp qw(tempfile);
use OpenBSD::Pledge;
use OpenBSD::Unveil;
use lib ".";
use GotMArc qw(parse san urlencode initpage endpage thread_header);
my $outdir = $ENV{'OUTDIR'};
die 'Set $OUTDIR' unless defined $outdir;
unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
unveil($outdir, "rwc") or die "unveil $outdir: $!";
# can't use tmppath because File::Temp checks whether /tmp exists.
unveil("/tmp", "rwc") or die "unveil /tmp: $!";
unveil(".", "r") or die "unveil .: $!";
# fattr for File::Temp
pledge("stdio rpath wpath cpath proc exec fattr") or die "pledge: $!";
my $tid;
while (<>) {
my ($level, $fname, $mid, $date, $from, $subj) = parse;
$tid = $mid if $level == 0;
die "unknown tid" unless defined $tid;
my $dest = "$outdir/mail/$mid.html";
next if -f $dest;
open(my $fh, '>', "$dest") or die "can't open $dest: $!";
initpage $fh, $subj;
my ($pfh, $parts) = tempfile "/tmp/gotmark.parts.XXXXXXXXXX";
$ENV{'PARTS_PATH'} = $parts;
$ENV{'MESSAGE_ID'} = $mid;
open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
or die "can't exec mshow: $!";
open(my $text, '>', "$outdir/text/$mid.txt")
or die "can't open $outdir/text/$mid.txt: $!";
my @hdrs;
while (<$mshow>) {
last if /^$/;
# drop the (1 day ago) string
s/ \(.*\)// if /^Date:/;
print $text $_;
push @hdrs, san($_);
}
say $text "";
thread_header $fh, $tid, $mid, \@hdrs;
print $fh "<pre>";
while (<$mshow>) {
print $text $_;
print $fh san($_);
}
print $fh "</pre>";
# generate the listing for the exported parts
my $part_seen = 0;
while (<$pfh>) {
if (!$part_seen) {
$part_seen = 1;
say $fh "<ul class='parts'>";
}
print $fh $_;
}
say $fh "</ul>" if $part_seen;
my $encmid = urlencode $mid;
my $enctid = urlencode $tid;
print $fh "<nav>";
print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
print $fh "</nav>\n";
endpage $fh;
close($text);
close($mshow);
close($pfh);
close($fh);
unlink $parts;
}
Omar Polo