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 GotMArc qw(parse san urlencode initpage endpage thread_header
19 threntry thrslice thrnav);
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 export_one {
60 my ($mail, $prev, $next) = @_;
61 my $dest = "$outdir/mail/$mail->{mid}.html";
63 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
65 initpage $fh, $mail->{subj};
67 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $mail->{fname})
68 or die "can't exec mshow: $!";
70 open(my $text, '>', "$outdir/text/$mail->{mid}.txt")
71 or die "can't open $outdir/text/$mail->{mid}.txt: $!";
73 my @hdrs;
74 while (<$mshow>) {
75 last if /^$/;
77 # drop the (1 day ago) string
78 s/ \(.*\)// if /^Date:/;
79 print $text $_;
80 push @hdrs, san($_);
81 }
82 say $text "";
84 thread_header $fh, \@hdrs, $mail, $prev, $next;
86 print $fh "<pre>";
87 while (<$mshow>) {
88 print $text $_;
89 print $fh san($_);
90 }
91 print $fh "</pre>";
93 # generate the listing for the exported parts
94 open(my $parts, '-|', 'mshow', '-t', $mail->{fname})
95 or die "can't exec mshow: $!";
97 my $partno = 0;
98 while (<$parts>) {
99 my ($n, $mime, $size, $name) =
100 m/(\d+): ([^ ]+) size=(\d+) name="(.*)"/ or next;
102 next if $mime =~ m(application/pgp-signature);
103 next if $mime =~ m(audio/*);
104 next if $mime =~ m(video/*);
106 my $ext = "bin";
107 if ($mime =~ m(image/*)) {
108 if ($mime eq "image/gif") {
109 $ext = "gif";
110 } elsif ($mime eq "image/jpeg") {
111 $ext = "jpg";
112 } elsif ($mime eq "image/png") {
113 $ext = "png";
114 } else {
115 # skip other image types for now.
116 next;
120 # text/* is bundled in the body by mshow(1).
122 say $fh "<ul class='parts'>" if $partno == 0;
123 $partno++;
125 my $path = "$outdir/parts/$mail->{mid}.$partno.$ext";
126 open my $p, '>', $path
127 or die "can't open $mail->{fname}: $!";
128 export_part($p, $n, $mail->{fname});
129 close($p);
131 $path =~ s,^.*/parts/,/parts/,;
133 my $url = san($path);
134 my $hs = humanize $size;
135 say $fh "<li><a href='$url'>$name ($hs)</a></li>";
137 say $fh "</ul>" if $partno > 0;
139 thrnav $fh, $prev, $next;
140 thrslice $fh, $mail, $prev, $next;
142 endpage $fh;
144 close($text);
145 close($mshow);
146 close($parts);
147 close($fh);
149 unlink $parts;
152 sub export {
153 my @thread = @_;
155 for (my $i = 0; $i < @thread; $i++) {
156 my (@prev, @next);
157 @prev = @thread[max($i-2, 0)..$i-1] if $i > 0;
158 @next = @thread[$i+1..min($i+2, @thread - 1)]
159 if $i + 1 < @thread;
160 export_one $thread[$i], \@prev, \@next;
164 my $tid;
165 my @thread;
166 while (<>) {
167 my $mail = parse $_;
169 if ($mail->{level} == 0 && @thread) {
170 export @thread;
171 @thread = ();
174 $tid = $mail->{mid} if $mail->{level} == 0;
175 die "unknown tid" unless defined $tid;
176 $mail->{tid} = $tid;
178 # export_one $mail, $tid
179 push @thread, $mail;
182 export @thread if @thread;