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;
9 use OpenBSD::Pledge;
10 use OpenBSD::Unveil;
12 use lib ".";
13 use GotMArc qw(parse san $logo initpage endpage);
15 my $outdir = $ENV{'OUTDIR'};
16 die 'Set $OUTDIR' unless defined $outdir;
18 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
19 unveil($outdir, "rwc") or die "unveil $outdir: $!";
20 unveil(".", "rwc") or die "unveil .: $!";
21 pledge("stdio rpath wpath cpath proc exec") or die "pledge: $!";
23 my $tid;
24 while (<>) {
25 my ($level, $fname, $mid, $date, $from, $subj) = parse;
27 $tid = $mid if $level == 0;
28 die "unknown tid" unless defined $tid;
30 my $dest = "$outdir/mail/$mid.html";
31 next if -f $dest;
33 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
35 initpage $fh, $subj;
37 # prepare the parts listing file
38 $ENV{'MESSAGE_ID'} = $mid;
39 open(my $parts, '+>', "parts.html")
40 or die "can't create parts.html: $!";
42 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
43 or die "can't exec mshow: $!";
45 open(my $text, '>', "$outdir/text/$mid.txt")
46 or die "can't open $outdir/text/$mid.txt: $!";
48 print $fh "<header class='mail-header'>";
49 print $fh "<p>";
50 print $fh $logo;
51 print $fh "<a href='/'>Index</a>";
52 print $fh " | ";
53 print $fh "<a href='/thread/$tid.html#$mid'>Thread</a>";
54 print $fh "</p>";
55 print $fh "<dl>";
56 while (<$mshow>) {
57 chomp;
58 say $text $_;
59 last if /^$/;
60 my ($h, $v) = m/^([-A-Za-z]+): (.*)/;
61 die "bogus line? $fname : $_" unless (defined $h and defined $v);
63 # drop the (1 day ago) string
64 $v =~ s/\(.*\)//g if ($h eq "Date");
66 print $fh "<dt>", san($h), ":</dt>";
67 print $fh "<dd>", san($v), "</dd>";
68 }
69 print $fh "</dl>";
70 print $fh "<p>Download raw <a href='/text/$mid.txt'>body</a>.</p>";
71 print $fh "</header>";
73 print $fh "<pre>";
74 while (<$mshow>) {
75 print $text $_;
76 print $fh san($_);
77 }
78 print $fh "</pre>";
80 # generate the listing for the exported parts
81 my $part_seen = 0;
82 while (<$parts>) {
83 if (!$part_seen) {
84 $part_seen = 1;
85 say $fh "<ul class='parts'>";
86 }
87 print $fh $_;
88 }
89 say $fh "</ul>" if $part_seen;
91 endpage $fh;
93 close($text);
94 close($mshow);
95 close($parts);
96 close($fh);
97 }
99 unlink "parts.html";