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 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: $!";
25 unveil("/tmp", "rwc") or die "unveil /tmp: $!";
26 unveil(".", "r") or die "unveil .: $!";
28 # fattr because of File::Temp somehow.
29 pledge("stdio rpath wpath cpath proc exec fattr") or die "pledge: $!";
31 my $tid;
32 while (<>) {
33 my ($level, $fname, $mid, $date, $from, $subj) = parse;
35 $tid = $mid if $level == 0;
36 die "unknown tid" unless defined $tid;
38 my $dest = "$outdir/mail/$mid.html";
39 next if -f $dest;
41 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
43 initpage $fh, $subj;
45 my ($pfh, $parts) = tempfile "/tmp/gotmark.parts.XXXXXXXXXX";
47 $ENV{'PARTS_PATH'} = $parts;
48 $ENV{'MESSAGE_ID'} = $mid;
49 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
50 or die "can't exec mshow: $!";
52 open(my $text, '>', "$outdir/text/$mid.txt")
53 or die "can't open $outdir/text/$mid.txt: $!";
55 my @hdrs;
56 while (<$mshow>) {
57 last if /^$/;
59 # drop the (1 day ago) string
60 s/ \(.*\)// if /^Date:/;
61 print $text $_;
62 push @hdrs, san($_);
63 }
64 say $text "";
66 thread_header $fh, $tid, $mid, \@hdrs;
68 print $fh "<pre>";
69 while (<$mshow>) {
70 print $text $_;
71 print $fh san($_);
72 }
73 print $fh "</pre>";
75 # generate the listing for the exported parts
76 my $part_seen = 0;
77 while (<$pfh>) {
78 if (!$part_seen) {
79 $part_seen = 1;
80 say $fh "<ul class='parts'>";
81 }
82 print $fh $_;
83 }
84 say $fh "</ul>" if $part_seen;
86 print $fh "<nav>";
87 print $fh "<a href='/text/$mid.txt'>Raw body</a>";
88 print $fh "<a href='/thread/$tid.html#$mid'>Thread</a>";
89 print $fh "</nav>\n";
91 endpage $fh;
93 close($text);
94 close($mshow);
95 close($pfh);
96 close($fh);
98 unlink $parts;
99 }