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);
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: $!";
31 sub export_part {
32 my ($fh, $n, $fname) = @_;
34 my $pid = fork;
35 die "fork: $!" unless defined $pid;
36 if ($pid == 0) {
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: $!";
41 }
43 waitpid $pid, 0;
44 die "mshow exited with $? ($n, $fname)" if $?;
45 }
47 # like libutil' fmt_scaled
48 sub humanize {
49 my $number = shift;
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];
57 }
58 }
59 }
61 sub thrnav {
62 my ($fh, $p, $n, $mid, $tid) = @_;
63 my @prev = @{$p};
64 my @next = @{$n};
66 return if !@prev && !@next;
67 print $fh "<nav>";
69 if (@prev) {
70 my $mail = $prev[-1];
71 my $encmid = $mail->{mid};
72 say $fh "<a href='/mail/$encmid.html'>Previous</a>";
73 } else {
74 say $fh "<span>Previous</span>";
75 }
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>";
82 }
84 if (@next) {
85 my $mail = $next[0];
86 my $encmid = $mail->{mid};
87 say $fh "<a href='/mail/$encmid.html'>Next</a>";
88 } else {
89 say $fh "<span>Next</span>";
90 }
92 print $fh "</nav>";
93 }
95 sub export_one {
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: $!";
109 my @hdrs;
110 while (<$mshow>) {
111 last if /^$/;
113 # drop the (1 day ago) string
114 s/ \(.*\)// if /^Date:/;
115 print $text $_;
116 push @hdrs, san($_);
118 say $text "";
120 thread_header $fh, $mail->{tid}, $mail->{mid}, \@hdrs;
121 thrnav $fh, $prev, $next;
123 print $fh "<pre>";
124 while (<$mshow>) {
125 print $text $_;
126 print $fh san($_);
128 print $fh "</pre>";
130 # generate the listing for the exported parts
131 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
132 or die "can't exec mshow: $!";
134 my $partno = 0;
135 while (<$parts>) {
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/*);
143 my $ext = "bin";
144 if ($mime =~ m(image/*)) {
145 if ($mime eq "image/gif") {
146 $ext = "gif";
147 } elsif ($mime eq "image/jpeg") {
148 $ext = "jpg";
149 } elsif ($mime eq "image/png") {
150 $ext = "png";
151 } else {
152 # skip other image types for now.
153 next;
157 # text/* is bundled in the body by mshow(1).
159 say $fh "<ul class='parts'>" if $partno == 0;
160 $partno++;
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});
166 close($p);
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};
178 endpage $fh;
180 close($text);
181 close($mshow);
182 close($parts);
183 close($fh);
185 unlink $parts;
188 sub export {
189 my @thread = @_;
191 for (my $i = 0; $i < @thread; $i++) {
192 my (@prev, @next);
193 @prev = @thread[max($i-3, 0)..$i-1] if $i > 0;
194 @next = @thread[$i+1..min($i+3, @thread - 1)]
195 if $i + 1 < @thread;
196 export_one $thread[$i], \@prev, \@next;
200 my $tid;
201 my @thread;
202 while (<>) {
203 my $mail = parse $_;
205 if ($mail->{level} == 0 && @thread) {
206 export @thread;
207 @thread = ();
210 $tid = $mail->{mid} if $mail->{level} == 0;
211 die "unknown tid" unless defined $tid;
212 $mail->{tid} = $tid;
214 # export_one $mail, $tid
215 push @thread, $mail;
218 export @thread if @thread;