Blob


1 #!/usr/bin/env perl
2 #
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
5 # code.
7 use open ":std", ":encoding(UTF-8)";
8 use utf8;
9 use strict;
10 use warnings;
11 use v5.32;
13 use List::Util qw(max min);
15 use OpenBSD::Pledge;
16 use OpenBSD::Unveil;
18 use lib ".";
19 use GotMArc qw(parse san urlencode initpage endpage thread_header threntry);
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: $!";
29 sub export_part {
30 my ($fh, $n, $fname) = @_;
32 my $pid = fork;
33 die "fork: $!" unless defined $pid;
34 if ($pid == 0) {
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: $!";
39 }
41 waitpid $pid, 0;
42 die "mshow exited with $? ($n, $fname)" if $?;
43 }
45 # like libutil' fmt_scaled
46 sub humanize {
47 my $number = shift;
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];
55 }
56 }
57 }
59 sub thrnav {
60 my ($fh, $p, $n, $mid, $tid) = @_;
61 my @prev = @{$p};
62 my @next = @{$n};
64 return if !@prev && !@next;
65 print $fh "<nav>";
67 if (@prev) {
68 my $mail = $prev[-1];
69 my $encmid = $mail->{mid};
70 say $fh "<a href='/mail/$encmid.html'>Previous</a>";
71 } else {
72 say $fh "<span>Previous</span>";
73 }
75 if (defined($mid) && defined($tid)) {
76 my $encmid = urlencode $mid;
77 my $enctid = urlencode $tid;
78 say $fh "<a href='/text/$encmid.txt'>Raw body</a>";
79 say $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
80 }
82 if (@next) {
83 my $mail = $next[0];
84 my $encmid = $mail->{mid};
85 say $fh "<a href='/mail/$encmid.html'>Next</a>";
86 } else {
87 say $fh "<span>Next</span>";
88 }
90 print $fh "</nav>";
91 }
93 sub min_level {
94 my $l = 999;
95 return 0 unless @_;
96 for (@_) {
97 $l = $_->{level} if $_->{level} < $l;
98 }
99 return $l;
102 sub thrslice {
103 my ($fh, $mail, $p, $n) = @_;
104 my @prev = @{$p};
105 my @next = @{$n};
106 my @thread = (@prev, $mail, @next);
107 return unless @thread;
108 my $base = min_level @thread;
109 my $level = 0;
110 print $fh "<div class='thread'>";
111 print $fh "<ul class='mails'>";
112 $level = threntry $fh, "mail", $base, $level, $_, $mail for @thread;
113 print $fh "</ul></li>" x $level;
114 print $fh "</ul></div>";
117 sub export_one {
118 my ($mail, $prev, $next) = @_;
119 my $dest = "$outdir/mail/$mail->{mid}.html";
121 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
123 initpage $fh, $mail->{subj};
125 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
126 or die "can't exec mshow: $!";
128 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
129 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
131 my @hdrs;
132 while (<$mshow>) {
133 last if /^$/;
135 # drop the (1 day ago) string
136 s/ \(.*\)// if /^Date:/;
137 print $text $_;
138 push @hdrs, san($_);
140 say $text "";
142 thread_header $fh, $mail->{tid}, $mail->{mid}, \@hdrs;
144 print $fh "<pre>";
145 while (<$mshow>) {
146 print $text $_;
147 print $fh san($_);
149 print $fh "</pre>";
151 # generate the listing for the exported parts
152 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
153 or die "can't exec mshow: $!";
155 my $partno = 0;
156 while (<$parts>) {
157 my ($n, $mime, $size, $name) =
158 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
160 next if $mime =~ m(application/pgp-signature);
161 next if $mime =~ m(audio/*);
162 next if $mime =~ m(video/*);
164 my $ext = "bin";
165 if ($mime =~ m(image/*)) {
166 if ($mime eq "image/gif") {
167 $ext = "gif";
168 } elsif ($mime eq "image/jpeg") {
169 $ext = "jpg";
170 } elsif ($mime eq "image/png") {
171 $ext = "png";
172 } else {
173 # skip other image types for now.
174 next;
178 # text/* is bundled in the body by mshow(1).
180 say $fh "<ul class='parts'>" if $partno == 0;
181 $partno++;
183 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
184 open my $p, '>', $path
185 or die "can't open $mail->{fname}: $!";
186 export_part($p, $n, $mail->{fname});
187 close($p);
189 $path =~ s,^.*/parts/,/parts/,;
191 my $url = san($path);
192 my $hs = humanize $size;
193 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
195 say $fh "</ul>" if $partno > 0;
197 thrnav $fh, $prev, $next, $mail->{mid}, $mail->{tid};
198 thrslice $fh, $mail, $prev, $next;
200 endpage $fh;
202 close($text);
203 close($mshow);
204 close($parts);
205 close($fh);
207 unlink $parts;
210 sub export {
211 my @thread = @_;
213 for (my $i = 0; $i < @thread; $i++) {
214 my (@prev, @next);
215 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
216 @next = @thread[$i+1..min($i+2, @thread - 1)]
217 if $i + 1 < @thread;
218 export_one $thread[$i], \@prev, \@next;
222 my $tid;
223 my @thread;
224 while (<>) {
225 my $mail = parse $_;
227 if ($mail->{level} == 0 && @thread) {
228 export @thread;
229 @thread = ();
232 $tid = $mail->{mid} if $mail->{level} == 0;
233 die "unknown tid" unless defined $tid;
234 $mail->{tid} = $tid;
236 # export_one $mail, $tid
237 push @thread, $mail;
240 export @thread if @thread;