Blob


1 #!/usr/bin/env perl
2 #
3 # mexp was written by Omar Polo <op@openbsd.org> and is placed in the
4 # public domain. The author hereby disclaims copyright to this source
5 # code.
7 use open ":std", ":encoding(UTF-8)";
8 use utf8;
9 use strict;
10 use warnings;
11 use v5.32;
12 use File::Temp qw(tempfile);
14 use OpenBSD::Pledge;
15 use OpenBSD::Unveil;
17 use lib ".";
18 use GotMArc qw(parse san urlencode initpage endpage thread_header);
20 my $outdir = $ENV{'OUTDIR'};
21 die 'Set $OUTDIR' unless defined $outdir;
23 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
24 unveil($outdir, "rwc") or die "unveil $outdir: $!";
26 # can't use tmppath because File::Temp checks whether /tmp exists.
27 unveil("/tmp", "rwc") or die "unveil /tmp: $!";
28 unveil(".", "r") or die "unveil .: $!";
30 # fattr for File::Temp
31 pledge("stdio rpath wpath cpath proc exec fattr") or die "pledge: $!";
33 my $tid;
34 while (<>) {
35 my ($level, $fname, $mid, $date, $from, $subj) = parse;
37 $tid = $mid if $level == 0;
38 die "unknown tid" unless defined $tid;
40 my $dest = "$outdir/mail/$mid.html";
41 next if -f $dest;
43 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
45 initpage $fh, $subj;
47 my ($pfh, $parts) = tempfile "/tmp/gotmark.parts.XXXXXXXXXX";
49 $ENV{'PARTS_PATH'} = $parts;
50 $ENV{'MESSAGE_ID'} = $mid;
51 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
52 or die "can't exec mshow: $!";
54 open(my $text, '>', "$outdir/text/$mid.txt")
55 or die "can't open $outdir/text/$mid.txt: $!";
57 my @hdrs;
58 while (<$mshow>) {
59 last if /^$/;
61 # drop the (1 day ago) string
62 s/ \(.*\)// if /^Date:/;
63 print $text $_;
64 push @hdrs, san($_);
65 }
66 say $text "";
68 thread_header $fh, $tid, $mid, \@hdrs;
70 print $fh "<pre>";
71 while (<$mshow>) {
72 print $text $_;
73 print $fh san($_);
74 }
75 print $fh "</pre>";
77 # generate the listing for the exported parts
78 my $part_seen = 0;
79 while (<$pfh>) {
80 if (!$part_seen) {
81 $part_seen = 1;
82 say $fh "<ul class='parts'>";
83 }
84 print $fh $_;
85 }
86 say $fh "</ul>" if $part_seen;
88 my $encmid = urlencode $mid;
89 my $enctid = urlencode $tid;
91 print $fh "<nav>";
92 print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
93 print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
94 print $fh "</nav>\n";
96 endpage $fh;
98 close($text);
99 close($mshow);
100 close($pfh);
101 close($fh);
103 unlink $parts;