Blob


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