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)";
17 use GotMArc qw(parse san urlencode initpage endpage thread_header);
19 my $outdir = $ENV{'OUTDIR'};
20 die 'Set $OUTDIR' unless defined $outdir;
22 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
23 unveil($outdir, "rwc") or die "unveil $outdir: $!";
25 unveil(".", "r") or die "unveil .: $!";
27 pledge("stdio rpath wpath cpath proc exec") or die "pledge: $!";
30 my ($fh, $n, $fname) = @_;
33 die "fork: $!" unless defined $pid;
35 open \*STDOUT, '>&', $fh
36 or die "can't redirect stdout: $!";
37 exec 'mshow', '-F', '-O', $fname, $n
38 or die "can't exec mshow: $!";
42 die "mshow exited with $? ($n, $fname)" if $?;
45 # like libutil' fmt_scaled
48 my @units = qw( G M K B);
49 my @scale = (1024*1024*1024, 1024*1024, 1024, 1);
51 for (my $i = 0; $i < @scale; $i++) {
52 if ($scale[$i] < $number) {
53 my $r = $number / $scale[$i];
54 return sprintf "%.0f%s", $r, $units[$i];
63 $tid = $mail->{mid} if $mail->{level} == 0;
64 die "unknown tid" unless defined $tid;
66 my $dest = "$outdir/mail/$mail->{mid}.html";
68 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
70 initpage $fh, $mail->{subj};
72 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
73 or die "can't exec mshow: $!";
75 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
76 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
82 # drop the (1 day ago) string
83 s/ \(.*\)// if /^Date:/;
89 thread_header $fh, $tid, $mail->{mid}, \@hdrs;
98 # generate the listing for the exported parts
99 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
100 or die "can't exec mshow: $!";
104 my ($n, $mime, $size, $name) =
105 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
107 next if $mime =~ m(application/application/pgp-signature);
108 next if $mime =~ m(audio/*);
109 next if $mime =~ m(video/*);
112 if ($mime =~ m(image/*)) {
113 if ($mime eq "image/gif") {
115 } elsif ($mime eq "image/jpeg") {
117 } elsif ($mime eq "image/png") {
120 # skip other image types for now.
125 # text/* is bundled in the body by mshow(1).
127 say $fh "<ul class='parts'>" if $partno == 0;
130 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
131 open my $p, '>', $path
132 or die "can't open $mail->{fname}: $!";
133 export_part($p, $n, $mail->{fname});
136 $path =~ s,^.*/parts/,/parts/,;
138 my $url = san($path);
139 my $hs = humanize $size;
140 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
142 say $fh "</ul>" if $partno > 0;
144 my $encmid = urlencode $mail->{mid};
145 my $enctid = urlencode $tid;
148 print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
149 print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
150 print $fh "</nav>\n";