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>";
99 $l = $_->{level} if $_->{level} < $l;
105 my ($fh, $base, $last_level, $cur, $mail) = @_;
106 my $level = $mail->{level} - $base;
108 say $fh "</ul></li>" x ($last_level - $level) if $last_level > $level;
109 say $fh "<li><ul>" if $last_level < $level;
111 my $encmid = urlencode $mail->{mid};
113 print $fh "<li id='$encmid' class='mail'>";
114 print $fh "<p class='mail-meta'>";
115 print $fh "<time>$mail->{date}</time> ";
116 print $fh "<span class='from'>$mail->{from}</span>";
117 print $fh "<span class='colon'>:</span>";
119 print $fh "<p class='subject'>";
121 my $subj = $mail->{subj};
122 if ($mail->{mid} ne $cur->{mid}) {
123 print $fh "<a href='/mail/$encmid.html'>$subj</a>";
125 print $fh "<span>$subj</span>";
135 my ($fh, $mail, $p, $n) = @_;
138 my @thread = (@prev, $mail, @next);
139 return unless @thread;
140 my $base = min_level @thread;
142 print $fh "<div class='thread'>";
143 print $fh "<ul class='mails'>";
144 $level = threntry $fh, $base, $level, $mail, $_ for @thread;
145 print $fh "</ul></li>" x $level;
146 print $fh "</ul></div>";
150 my ($mail, $prev, $next) = @_;
151 my $dest = "$outdir/mail/$mail->{mid}.html";
153 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
155 initpage $fh, $mail->{subj};
157 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
158 or die "can't exec mshow: $!";
160 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
161 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
167 # drop the (1 day ago) string
168 s/ \(.*\)// if /^Date:/;
174 thread_header $fh, $mail->{tid}, $mail->{mid}, \@hdrs;
183 # generate the listing for the exported parts
184 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
185 or die "can't exec mshow: $!";
189 my ($n, $mime, $size, $name) =
190 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
192 next if $mime =~ m(application/application/pgp-signature);
193 next if $mime =~ m(audio/*);
194 next if $mime =~ m(video/*);
197 if ($mime =~ m(image/*)) {
198 if ($mime eq "image/gif") {
200 } elsif ($mime eq "image/jpeg") {
202 } elsif ($mime eq "image/png") {
205 # skip other image types for now.
210 # text/* is bundled in the body by mshow(1).
212 say $fh "<ul class='parts'>" if $partno == 0;
215 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
216 open my $p, '>', $path
217 or die "can't open $mail->{fname}: $!";
218 export_part($p, $n, $mail->{fname});
221 $path =~ s,^.*/parts/,/parts/,;
223 my $url = san($path);
224 my $hs = humanize $size;
225 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
227 say $fh "</ul>" if $partno > 0;
229 thrnav $fh, $prev, $next, $mail->{mid}, $mail->{tid};
230 thrslice $fh, $mail, $prev, $next;
245 for (my $i = 0; $i < @thread; $i++) {
247 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
248 @next = @thread[$i+1..min($i+2, @thread - 1)]
250 export_one $thread[$i], \@prev, \@next;
259 if ($mail->{level} == 0 && @thread) {
264 $tid = $mail->{mid} if $mail->{level} == 0;
265 die "unknown tid" unless defined $tid;
268 # export_one $mail, $tid
272 export @thread if @thread;