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)";
13 use List::Util qw(max min);
15 use SMArc qw(parse san urlencode initpage endpage thread_header
16 threntry thrslice thrnav);
18 my $outdir = $ENV{'OUTDIR'};
19 die 'Set $OUTDIR' unless defined $outdir;
21 if (`uname` =~ "OpenBSD") {
25 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
26 unveil($outdir, "rwc") or die "unveil $outdir: $!";
28 pledge("stdio rpath wpath cpath proc exec") or die "pledge: $!";
32 my ($fh, $n, $fname) = @_;
35 die "fork: $!" unless defined $pid;
37 open \*STDOUT, '>&', $fh
38 or die "can't redirect stdout: $!";
39 exec 'mshow', '-F', '-O', $fname, $n
40 or die "can't exec mshow: $!";
44 die "mshow exited with $? ($n, $fname)" if $?;
47 # like libutil' fmt_scaled
50 my @units = qw( G M K B);
51 my @scale = (1024*1024*1024, 1024*1024, 1024, 1);
53 for (my $i = 0; $i < @scale; $i++) {
54 if ($scale[$i] < $number) {
55 my $r = $number / $scale[$i];
56 return sprintf "%.0f%s", $r, $units[$i];
62 my ($mail, $prev, $next) = @_;
63 my $dest = "$outdir/mail/$mail->{mid}.html";
65 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
67 initpage $fh, $mail->{subj};
69 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
70 or die "can't exec mshow: $!";
72 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
73 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
79 # drop the (1 day ago) string
80 s/ \(.*\)// if /^Date:/;
86 thread_header $fh, \@hdrs, $mail, $prev, $next;
95 # generate the listing for the exported parts
96 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
97 or die "can't exec mshow: $!";
101 my ($n, $mime, $size, $name) =
102 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
104 next if $mime =~ m(application/pgp-signature);
105 next if $mime =~ m(audio/*);
106 next if $mime =~ m(video/*);
109 if ($mime =~ m(image/*)) {
110 if ($mime eq "image/gif") {
112 } elsif ($mime eq "image/jpeg") {
114 } elsif ($mime eq "image/png") {
117 # skip other image types for now.
122 # text/* is bundled in the body by mshow(1).
124 say $fh "<ul class='parts'>" if $partno == 0;
127 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
128 open my $p, '>', $path
129 or die "can't open $mail->{fname}: $!";
130 export_part($p, $n, $mail->{fname});
133 $path =~ s,^.*/parts/,/parts/,;
135 my $url = san($path);
136 my $hs = humanize $size;
137 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
139 say $fh "</ul>" if $partno > 0;
141 thrnav $fh, $prev, $next;
142 thrslice $fh, $mail, $prev, $next;
157 for (my $i = 0; $i < @thread; $i++) {
159 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
160 @next = @thread[$i+1..min($i+2, @thread - 1)]
162 export_one $thread[$i], \@prev, \@next;
171 if ($mail->{level} == 0 && @thread) {
176 $tid = $mail->{mid} if $mail->{level} == 0;
177 die "unknown tid" unless defined $tid;
180 # export_one $mail, $tid
184 export @thread if @thread;