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";
73 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
77 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
78 or die "can't exec mshow: $!";
80 open(my $text, '>', "$outdir/text/$mid.txt")
81 or die "can't open $outdir/text/$mid.txt: $!";
87 # drop the (1 day ago) string
88 s/ \(.*\)// if /^Date:/;
94 thread_header $fh, $tid, $mid, \@hdrs;
103 # generate the listing for the exported parts
104 open(my $parts, '-|', 'mshow', '-t', $fname)
105 or die "can't exec mshow: $!";
109 my ($n, $mime, $size, $name) =
110 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
112 next if $mime =~ m(application/application/pgp-signature);
113 next if $mime =~ m(audio/*);
114 next if $mime =~ m(video/*);
117 if ($mime =~ m(image/*)) {
118 if ($mime eq "image/gif") {
120 } elsif ($mime eq "image/jpeg") {
122 } elsif ($mime eq "image/png") {
125 # skip other image types for now.
130 # text/* is bundled in the body by mshow(1).
134 say $fh "<ul class='parts'>";
137 my ($p, $path) = tempfile "$outdir/parts/$mid.XXXXXXXXXX";
138 export_part($p, $n, $fname);
140 rename $path, "$path.$ext"
141 or die "can' rename $path as $path.ext";
142 chmod 0644, "$path.$ext";
144 $path =~ s,^.*/parts/,/parts/,;
146 my $url = san("$path.$ext");
147 my $hs = humanize $size;
148 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
150 say $fh "</ul>" if $part_seen;
152 my $encmid = urlencode $mid;
153 my $enctid = urlencode $tid;
156 print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
157 print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
158 print $fh "</nav>\n";