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);
19 use GotMArc qw(parse san urlencode initpage endpage thread_header);
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 unveil(".", "r") or die "unveil .: $!";
29 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 ($fh, $p, $n, $mid, $tid) = @_;
66 return if !@prev && !@next;
71 my $encmid = $mail->{mid};
72 say $fh "<a href='/mail/$encmid.html'>Previous</a>";
74 say $fh "<span>Previous</span>";
77 if (defined($mid) && defined($tid)) {
78 my $encmid = urlencode $mid;
79 my $enctid = urlencode $tid;
80 say $fh "<a href='/text/$encmid.txt'>Raw body</a>";
81 say $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
86 my $encmid = $mail->{mid};
87 say $fh "<a href='/mail/$encmid.html'>Next</a>";
89 say $fh "<span>Next</span>";
96 my ($mail, $prev, $next) = @_;
97 my $dest = "$outdir/mail/$mail->{mid}.html";
99 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
101 initpage $fh, $mail->{subj};
103 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
104 or die "can't exec mshow: $!";
106 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
107 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
113 # drop the (1 day ago) string
114 s/ \(.*\)// if /^Date:/;
120 thread_header $fh, $mail->{tid}, $mail->{mid}, \@hdrs;
121 thrnav $fh, $prev, $next;
130 # generate the listing for the exported parts
131 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
132 or die "can't exec mshow: $!";
136 my ($n, $mime, $size, $name) =
137 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
139 next if $mime =~ m(application/application/pgp-signature);
140 next if $mime =~ m(audio/*);
141 next if $mime =~ m(video/*);
144 if ($mime =~ m(image/*)) {
145 if ($mime eq "image/gif") {
147 } elsif ($mime eq "image/jpeg") {
149 } elsif ($mime eq "image/png") {
152 # skip other image types for now.
157 # text/* is bundled in the body by mshow(1).
159 say $fh "<ul class='parts'>" if $partno == 0;
162 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
163 open my $p, '>', $path
164 or die "can't open $mail->{fname}: $!";
165 export_part($p, $n, $mail->{fname});
168 $path =~ s,^.*/parts/,/parts/,;
170 my $url = san($path);
171 my $hs = humanize $size;
172 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
174 say $fh "</ul>" if $partno > 0;
176 thrnav $fh, $prev, $next, $mail->{mid}, $mail->{tid};
191 for (my $i = 0; $i < @thread; $i++) {
193 @prev = @thread[max($i-3, 0)..$i-1] if $i > 0;
194 @next = @thread[$i+1..min($i+3, @thread - 1)]
196 export_one $thread[$i], \@prev, \@next;
205 if ($mail->{level} == 0 && @thread) {
210 $tid = $mail->{mid} if $mail->{level} == 0;
211 die "unknown tid" unless defined $tid;
214 # export_one $mail, $tid
218 export @thread if @thread;