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
7 use open ":std", ":encoding(UTF-8)";
12 use File::Temp qw(tempfile);
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: $!";
34 my ($fh, $n, $fname) = @_;
37 die "fork: $!" unless defined $pid;
39 open \*STDOUT, '>&', $fh
40 or die "can't redirect stdout: $!";
41 exec 'mshow', '-F', '-O', $fname, $n
42 or die "can't exec mshow: $!";
46 die "mshow exited with $? ($n, $fname)" if $?;
49 # like libutil' fmt_scaled
52 my @units = qw( G M K B);
53 my @scale = (1024*1024*1024, 1024*1024, 1024, 1);
55 for (my $i = 0; $i < @scale; $i++) {
56 if ($scale[$i] < $number) {
57 my $r = $number / $scale[$i];
58 return sprintf "%.0f%s", $r, $units[$i];
65 my ($level, $fname, $mid, $date, $from, $subj) = parse;
67 $tid = $mid if $level == 0;
68 die "unknown tid" unless defined $tid;
70 my $dest = "$outdir/mail/$mid.html";
72 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
76 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
77 or die "can't exec mshow: $!";
79 open(my $text, '>', "$outdir/text/$mid.txt")
80 or die "can't open $outdir/text/$mid.txt: $!";
86 # drop the (1 day ago) string
87 s/ \(.*\)// if /^Date:/;
93 thread_header $fh, $tid, $mid, \@hdrs;
102 # generate the listing for the exported parts
103 open(my $parts, '-|', 'mshow', '-t', $fname)
104 or die "can't exec mshow: $!";
108 my ($n, $mime, $size, $name) =
109 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
111 next if $mime =~ m(application/application/pgp-signature);
112 next if $mime =~ m(audio/*);
113 next if $mime =~ m(video/*);
116 if ($mime =~ m(image/*)) {
117 if ($mime eq "image/gif") {
119 } elsif ($mime eq "image/jpeg") {
121 } elsif ($mime eq "image/png") {
124 # skip other image types for now.
129 # text/* is bundled in the body by mshow(1).
133 say $fh "<ul class='parts'>";
136 my ($p, $path) = tempfile "$outdir/parts/$mid.XXXXXXXXXX";
137 export_part($p, $n, $fname);
139 rename $path, "$path.$ext"
140 or die "can' rename $path as $path.ext";
141 chmod 0644, "$path.$ext";
143 $path =~ s,^.*/parts/,/parts/,;
145 my $url = san("$path.$ext");
146 my $hs = humanize $size;
147 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
149 say $fh "</ul>" if $part_seen;
151 my $encmid = urlencode $mid;
152 my $enctid = urlencode $tid;
155 print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
156 print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
157 print $fh "</nav>\n";