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;
12 use File::Temp qw(tempfile);
14 use OpenBSD::Pledge;
15 use OpenBSD::Unveil;
17 use lib ".";
18 use GotMArc qw(parse san urlencode initpage endpage thread_header);
20 my $outdir = $ENV{'OUTDIR'};
21 die 'Set $OUTDIR' unless defined $outdir;
23 unveil("/usr/local/bin/mshow", "rx") or die "unveil mshow: $!";
24 unveil($outdir, "rwc") or die "unveil $outdir: $!";
26 # can't use tmppath because File::Temp checks whether /tmp exists.
27 unveil("/tmp", "rwc") or die "unveil /tmp: $!";
28 unveil(".", "r") or die "unveil .: $!";
30 # fattr for File::Temp
31 pledge("stdio rpath wpath cpath proc exec fattr") or die "pledge: $!";
33 sub export_part {
34 my ($fh, $n, $fname) = @_;
36 my $pid = fork;
37 die "fork: $!" unless defined $pid;
38 if ($pid == 0) {
39 open \*STDOUT, '>&', $fh
40 or die "can't redirect stdout: $!";
41 exec 'mshow', '-F', '-O', $fname, $n
42 or die "can't exec mshow: $!";
43 }
45 waitpid $pid, 0;
46 die "mshow exited with $? ($n, $fname)" if $?;
47 }
49 # like libutil' fmt_scaled
50 sub humanize {
51 my $number = shift;
52 my @units = qw( G M K B);
53 my @scale = (1024*1024*1024, 1024*1024, 1024, 1);
55 for (my $i = 0; $i < @scale; $i++) {
56 if ($scale[$i] < $number) {
57 my $r = $number / $scale[$i];
58 return sprintf "%.0f%s", $r, $units[$i];
59 }
60 }
61 }
63 my $tid;
64 while (<>) {
65 my ($level, $fname, $mid, $date, $from, $subj) = parse;
67 $tid = $mid if $level == 0;
68 die "unknown tid" unless defined $tid;
70 my $dest = "$outdir/mail/$mid.html";
72 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
74 initpage $fh, $subj;
76 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
77 or die "can't exec mshow: $!";
79 open(my $text, '>', "$outdir/text/$mid.txt")
80 or die "can't open $outdir/text/$mid.txt: $!";
82 my @hdrs;
83 while (<$mshow>) {
84 last if /^$/;
86 # drop the (1 day ago) string
87 s/ \(.*\)// if /^Date:/;
88 print $text $_;
89 push @hdrs, san($_);
90 }
91 say $text "";
93 thread_header $fh, $tid, $mid, \@hdrs;
95 print $fh "<pre>";
96 while (<$mshow>) {
97 print $text $_;
98 print $fh san($_);
99 }
100 print $fh "</pre>";
102 # generate the listing for the exported parts
103 open(my $parts, '-|', 'mshow', '-t', $fname)
104 or die "can't exec mshow: $!";
106 my $part_seen = 0;
107 while (<$parts>) {
108 my ($n, $mime, $size, $name) =
109 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
111 next if $mime =~ m(application/application/pgp-signature);
112 next if $mime =~ m(audio/*);
113 next if $mime =~ m(video/*);
115 my $ext = "bin";
116 if ($mime =~ m(image/*)) {
117 if ($mime eq "image/gif") {
118 $ext = "gif";
119 } elsif ($mime eq "image/jpeg") {
120 $ext = "jpg";
121 } elsif ($mime eq "image/png") {
122 $ext = "png";
123 } else {
124 # skip other image types for now.
125 next;
129 # text/* is bundled in the body by mshow(1).
131 if (!$part_seen) {
132 $part_seen = 1;
133 say $fh "<ul class='parts'>";
136 my ($p, $path) = tempfile "$outdir/parts/$mid.XXXXXXXXXX";
137 export_part($p, $n, $fname);
138 close($p);
139 rename $path, "$path.$ext"
140 or die "can' rename $path as $path.ext";
141 chmod 0644, "$path.$ext";
143 $path =~ s,^.*/parts/,/parts/,;
145 my $url = san("$path.$ext");
146 my $hs = humanize $size;
147 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
149 say $fh "</ul>" if $part_seen;
151 my $encmid = urlencode $mid;
152 my $enctid = urlencode $tid;
154 print $fh "<nav>";
155 print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
156 print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
157 print $fh "</nav>\n";
159 endpage $fh;
161 close($text);
162 close($mshow);
163 close($parts);
164 close($fh);
166 unlink $parts;