Sat Aug 27 22:21:11 2022 UTC
drop trailing space on Date header values
#!/usr/bin/env perl
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 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: $!";
unveil("/tmp", "rwc") or die "unveil /tmp: $!";
unveil(".", "r") or die "unveil .: $!";
# fattr because of File::Temp somehow.
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/";
$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;
print $fh "<nav>";
print $fh "<a href='/text/$mid.txt'>Raw body</a>";
print $fh "<a href='/thread/$tid.html#$mid'>Thread</a>";
print $fh "</nav>\n";
endpage $fh;
unlink $parts;
Omar Polo