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);
18 use GotMArc qw(parse san urlencode initpage endpage thread_header
19 threntry thrslice thrnav);
21 my $outdir = $ENV{'OUTDIR'};
22 die 'Set $OUTDIR' unless defined $outdir;
24 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
25 unveil($outdir, "rwc") or die "unveil $outdir: $!";
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];
60 my ($mail, $prev, $next) = @_;
61 my $dest = "$outdir/mail/$mail->{mid}.html";
63 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
65 initpage $fh, $mail->{subj};
67 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
68 or die "can't exec mshow: $!";
70 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
71 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
77 # drop the (1 day ago) string
78 s/ \(.*\)// if /^Date:/;
84 thread_header $fh, \@hdrs, $mail, $prev, $next;
93 # generate the listing for the exported parts
94 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
95 or die "can't exec mshow: $!";
99 my ($n, $mime, $size, $name) =
100 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
102 next if $mime =~ m(application/pgp-signature);
103 next if $mime =~ m(audio/*);
104 next if $mime =~ m(video/*);
107 if ($mime =~ m(image/*)) {
108 if ($mime eq "image/gif") {
110 } elsif ($mime eq "image/jpeg") {
112 } elsif ($mime eq "image/png") {
115 # skip other image types for now.
120 # text/* is bundled in the body by mshow(1).
122 say $fh "<ul class='parts'>" if $partno == 0;
125 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
126 open my $p, '>', $path
127 or die "can't open $mail->{fname}: $!";
128 export_part($p, $n, $mail->{fname});
131 $path =~ s,^.*/parts/,/parts/,;
133 my $url = san($path);
134 my $hs = humanize $size;
135 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
137 say $fh "</ul>" if $partno > 0;
139 thrnav $fh, $prev, $next;
140 thrslice $fh, $mail, $prev, $next;
155 for (my $i = 0; $i < @thread; $i++) {
157 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
158 @next = @thread[$i+1..min($i+2, @thread - 1)]
160 export_one $thread[$i], \@prev, \@next;
169 if ($mail->{level} == 0 && @thread) {
174 $tid = $mail->{mid} if $mail->{level} == 0;
175 die "unknown tid" unless defined $tid;
178 # export_one $mail, $tid
182 export @thread if @thread;