-
Notifications
You must be signed in to change notification settings - Fork 0
/
mexp
executable file
·185 lines (144 loc) · 3.89 KB
/
mexp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env perl
#
# mexp was written by Omar Polo <op@openbsd.org> and is placed in the
# public domain. The author hereby disclaims copyright to this source
# code.
use open ":std", ":encoding(UTF-8)";
use utf8;
use strict;
use warnings;
use v5.32;
use List::Util qw(max min);
use SMArc qw(parse san urlencode initpage endpage thread_header
threntry thrslice thrnav);
my $outdir = $ENV{'OUTDIR'};
die 'Set $OUTDIR' unless defined $outdir;
if (`uname` =~ "OpenBSD") {
use OpenBSD::Pledge;
use OpenBSD::Unveil;
unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
unveil($outdir, "rwc") or die "unveil $outdir: $!";
pledge("stdio rpath wpath cpath proc exec") or die "pledge: $!";
}
sub export_part {
my ($fh, $n, $fname) = @_;
my $pid = fork;
die "fork: $!" unless defined $pid;
if ($pid == 0) {
open \*STDOUT, '>&', $fh
or die "can't redirect stdout: $!";
exec 'mshow', '-F', '-O', $fname, $n
or die "can't exec mshow: $!";
}
waitpid $pid, 0;
die "mshow exited with $? ($n, $fname)" if $?;
}
# like libutil' fmt_scaled
sub humanize {
my $number = shift;
my @units = qw( G M K B);
my @scale = (1024*1024*1024, 1024*1024, 1024, 1);
for (my $i = 0; $i < @scale; $i++) {
if ($scale[$i] <= $number) {
my $r = $number / $scale[$i];
return sprintf "%.0f%s", $r, $units[$i];
}
}
return "0B"
}
sub export_one {
my ($mail, $prev, $next) = @_;
my $dest = "$outdir/mail/$mail->{mid}.html";
open(my $fh, '>', "$dest") or die "can't open $dest: $!";
initpage $fh, $mail->{subj};
open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
or die "can't exec mshow: $!";
open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
or die "can't open $outdir/text/$mail->{mid}.txt: $!";
my @hdrs;
while (<$mshow>) {
last if /^$/;
# drop the (1 day ago) string
s/ \(.*\)// if /^Date:/;
print $text $_;
push @hdrs, san($_);
}
say $text "";
thread_header $fh, \@hdrs, $mail, $prev, $next;
print $fh "<pre>";
while (<$mshow>) {
print $text $_;
print $fh san($_);
}
print $fh "</pre>";
# generate the listing for the exported parts
open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
or die "can't exec mshow: $!";
my $partno = 0;
while (<$parts>) {
my ($n, $mime, $size, $name) =
m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
next if $mime =~ m(application/pgp-signature);
next if $mime =~ m(audio/*);
next if $mime =~ m(video/*);
my $ext = "bin";
if ($mime =~ m(image/*)) {
if ($mime eq "image/gif") {
$ext = "gif";
} elsif ($mime eq "image/jpeg") {
$ext = "jpg";
} elsif ($mime eq "image/png") {
$ext = "png";
} else {
# skip other image types for now.
next;
}
}
# text/* is bundled in the body by mshow(1).
say $fh "<ul class='parts'>" if $partno == 0;
$partno++;
my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
open my $p, '>', $path
or die "can't open $mail->{fname}: $!";
export_part($p, $n, $mail->{fname});
close($p);
$path =~ s,^.*/parts/,/parts/,;
my $url = san($path);
my $hs = humanize $size;
say $fh "<li><a href='$url'>$name ($hs)</a></li>";
}
say $fh "</ul>" if $partno > 0;
thrnav $fh, $prev, $next;
thrslice $fh, $mail, $prev, $next;
endpage $fh;
close($text);
close($mshow);
close($parts);
close($fh);
unlink $parts;
}
sub export {
my @thread = @_;
for (my $i = 0; $i < @thread; $i++) {
my (@prev, @next);
@prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
@next = @thread[$i+1..min($i+2, @thread - 1)]
if $i + 1 < @thread;
export_one $thread[$i], \@prev, \@next;
}
}
my $tid;
my @thread;
while (<>) {
my $mail = parse $_;
if ($mail->{level} == 0 && @thread) {
export @thread;
@thread = ();
}
$tid = $mail->{mid} if $mail->{level} == 0;
die "unknown tid" unless defined $tid;
$mail->{tid} = $tid;
# export_one $mail, $tid
push @thread, $mail;
}
export @thread if @thread;