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 SMArc qw(parse san urlencode initpage endpage thread_header
16 threntry thrslice thrnav);
18 my $outdir = $ENV{'OUTDIR'};
19 die 'Set $OUTDIR' unless defined $outdir;
21 if (`uname` =~ "OpenBSD") {
22 use OpenBSD::Pledge;
23 use OpenBSD::Unveil;
25 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
26 unveil($outdir, "rwc") or die "unveil $outdir: $!";
28 pledge("stdio rpath wpath cpath proc exec") or die "pledge: $!";
29 }
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 export_one {
62 my ($mail, $prev, $next) = @_;
63 my $dest = "$outdir/mail/$mail->{mid}.html";
65 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
67 initpage $fh, $mail->{subj};
69 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
70 or die "can't exec mshow: $!";
72 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
73 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
75 my @hdrs;
76 while (<$mshow>) {
77 last if /^$/;
79 # drop the (1 day ago) string
80 s/ \(.*\)// if /^Date:/;
81 print $text $_;
82 push @hdrs, san($_);
83 }
84 say $text "";
86 thread_header $fh, \@hdrs, $mail, $prev, $next;
88 print $fh "<pre>";
89 while (<$mshow>) {
90 print $text $_;
91 print $fh san($_);
92 }
93 print $fh "</pre>";
95 # generate the listing for the exported parts
96 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
97 or die "can't exec mshow: $!";
99 my $partno = 0;
100 while (<$parts>) {
101 my ($n, $mime, $size, $name) =
102 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
104 next if $mime =~ m(application/pgp-signature);
105 next if $mime =~ m(audio/*);
106 next if $mime =~ m(video/*);
108 my $ext = "bin";
109 if ($mime =~ m(image/*)) {
110 if ($mime eq "image/gif") {
111 $ext = "gif";
112 } elsif ($mime eq "image/jpeg") {
113 $ext = "jpg";
114 } elsif ($mime eq "image/png") {
115 $ext = "png";
116 } else {
117 # skip other image types for now.
118 next;
122 # text/* is bundled in the body by mshow(1).
124 say $fh "<ul class='parts'>" if $partno == 0;
125 $partno++;
127 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
128 open my $p, '>', $path
129 or die "can't open $mail->{fname}: $!";
130 export_part($p, $n, $mail->{fname});
131 close($p);
133 $path =~ s,^.*/parts/,/parts/,;
135 my $url = san($path);
136 my $hs = humanize $size;
137 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
139 say $fh "</ul>" if $partno > 0;
141 thrnav $fh, $prev, $next;
142 thrslice $fh, $mail, $prev, $next;
144 endpage $fh;
146 close($text);
147 close($mshow);
148 close($parts);
149 close($fh);
151 unlink $parts;
154 sub export {
155 my @thread = @_;
157 for (my $i = 0; $i < @thread; $i++) {
158 my (@prev, @next);
159 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
160 @next = @thread[$i+1..min($i+2, @thread - 1)]
161 if $i + 1 < @thread;
162 export_one $thread[$i], \@prev, \@next;
166 my $tid;
167 my @thread;
168 while (<>) {
169 my $mail = parse $_;
171 if ($mail->{level} == 0 && @thread) {
172 export @thread;
173 @thread = ();
176 $tid = $mail->{mid} if $mail->{level} == 0;
177 die "unknown tid" unless defined $tid;
178 $mail->{tid} = $tid;
180 # export_one $mail, $tid
181 push @thread, $mail;
184 export @thread if @thread;