01
2022-08-24
op
#!/usr/bin/env perl
03
2022-08-24
op
use open ":std", ":encoding(UTF-8)";
05
2022-08-24
op
use strict;
06
2022-08-24
op
use warnings;
08
2022-08-27
op
use File::Temp qw(tempfile);
10
2022-08-25
op
use OpenBSD::Pledge;
11
2022-08-25
op
use OpenBSD::Unveil;
13
2022-08-25
op
use lib ".";
14
2022-08-27
op
use GotMArc qw(parse san initpage endpage thread_header);
16
2022-08-24
op
my $outdir = $ENV{'OUTDIR'};
17
2022-08-24
op
die 'Set $OUTDIR' unless defined $outdir;
19
2022-08-25
op
unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
20
2022-08-25
op
unveil($outdir, "rwc") or die "unveil $outdir: $!";
21
2022-08-27
op
unveil("/tmp", "rwc") or die "unveil /tmp: $!";
22
2022-08-27
op
unveil(".", "r") or die "unveil .: $!";
24
2022-08-27
op
# fattr because of File::Temp somehow.
25
2022-08-27
op
pledge("stdio rpath wpath cpath proc exec fattr") or die "pledge: $!";
28
2022-08-24
op
while (<>) {
29
2022-08-26
op
my ($level, $fname, $mid, $date, $from, $subj) = parse;
31
2022-08-24
op
$tid = $mid if $level == 0;
32
2022-08-25
op
die "unknown tid" unless defined $tid;
34
2022-08-25
op
my $dest = "$outdir/mail/$mid.html";
35
2022-08-25
op
next if -f $dest;
37
2022-08-24
op
open(my $fh, '>', "$dest") or die "can't open $dest: $!";
39
2022-08-25
op
initpage $fh, $subj;
41
2022-08-27
op
my ($pfh, $parts) = tempfile "/tmp/gotmark.parts.XXXXXXXXXX";
43
2022-08-27
op
$ENV{'PARTS_PATH'} = $parts;
44
2022-08-27
op
$ENV{'MESSAGE_ID'} = $mid;
45
2022-08-24
op
open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
46
2022-08-24
op
or die "can't exec mshow: $!";
48
2022-08-24
op
open(my $text, '>', "$outdir/text/$mid.txt")
49
2022-08-24
op
or die "can't open $outdir/text/$mid.txt: $!";
52
2022-08-24
op
while (<$mshow>) {
53
2022-08-24
op
last if /^$/;
55
2022-08-24
op
# drop the (1 day ago) string
56
2022-08-27
op
s/ \(.*\)// if /^Date:/;
57
2022-08-27
op
print $text $_;
58
2022-08-27
op
push @hdrs, san($_);
60
2022-08-27
op
say $text "";
62
2022-08-27
op
thread_header $fh, $tid, $mid, \@hdrs;
64
2022-08-24
op
print $fh "<pre>";
65
2022-08-25
op
while (<$mshow>) {
66
2022-08-25
op
print $text $_;
67
2022-08-25
op
print $fh san($_);
69
2022-08-24
op
print $fh "</pre>";
71
2022-08-24
op
# generate the listing for the exported parts
72
2022-08-24
op
my $part_seen = 0;
73
2022-08-27
op
while (<$pfh>) {
74
2022-08-24
op
if (!$part_seen) {
75
2022-08-24
op
$part_seen = 1;
76
2022-08-24
op
say $fh "<ul class='parts'>";
78
2022-08-24
op
print $fh $_;
80
2022-08-24
op
say $fh "</ul>" if $part_seen;
82
2022-08-27
op
print $fh "<nav>";
83
2022-08-27
op
print $fh "<a href='/text/$mid.txt'>Raw body</a>";
84
2022-08-27
op
print $fh "<a href='/thread/$tid.html#$mid'>Thread</a>";
85
2022-08-27
op
print $fh "</nav>\n";
87
2022-08-25
op
endpage $fh;
89
2022-08-24
op
close($text);
90
2022-08-24
op
close($mshow);
91
2022-08-27
op
close($pfh);
92
2022-08-24
op
close($fh);
94
2022-08-27
op
unlink $parts;