/
mailreduce
executable file
·144 lines (119 loc) · 4 KB
/
mailreduce
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
#!/usr/bin/perl -w
# Mit Sep 5 14:50:32 CEST 2007
(my $email='pflanze%gmx,ch')=~ tr/%,/@./;
use strict;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname file [outfile]
(Specialized tool for elvis-mail handling: strip headers from mails
from delivery through elvis-smtp, so that the mail look again as if
they were coming in right at elvis-smtp.)
If outfile is not given, print to stdout. (If given, outfile is
written as tmpfile first.)
Options:
--dir file and outfile are dir and outdir instead, try to convert
all files in dir to outdir. (In case of errors just print the
error but continue.)
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose=0;
our $opt_dir;
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
"directory"=> \$opt_dir,
) or exit 1;
usage unless @ARGV==1 or @ARGV==2;
our ($file,$maybe_outfile)=@ARGV;
if ($opt_dir and not $maybe_outfile) {
usage "requiring outdir argument if --dir option is given"
}
our $oldest_acceptable_mail= 1176240568; # Tue Apr 10 23:29:28 2007,
# this is the first
# wrongly-as-spam-or-maybespam
# tagged mail which contains
# *my* spamassassin marks and
# my elvis-smtp 'signature'
#use Chj::ruse; use Chj::repl;
use Chj::Mail::Striphead;
use Chj::xopen 'xopen_read';
use Chj::xperlfunc 'basename';
use Chj::xtmpfile;
use Chj::xopendir;
use Chj::Mail::Detectoutgoing;
sub Warn {
print STDERR "$myname: @_\n";
}
our $detectoutgoing= new Chj::Mail::Detectoutgoing;
our $striphead= Chj::Mail::Striphead->new_with_hosts
(# the names of the hosts I'm expecting my incoming smtp server to have in it's qmail 'me' file
'elvis-jaeger.mine.nu' => ['LAST', 2],
'ethlife-dev.ethz.ch' => ['SINGLE', 1],
# [for some reasons I've got mails in my INBOX which do not carry
# any of the above but just:
# Delivered-To: christian.jaeger@ethlife.ethz.ch
# and this in e.g. 27 Jun 2007 13:15:10
# (1182950110.14978.ethlife-a:* AH: that was seemingly moved here
# -- ah then it's all ok actually??) bisschen strange aber lass diese also sein.]
);
sub mailreduce {
my ($file,$maybe_outfile)= @_;
local our $bn= basename $file;
if ($bn=~ /^(\d{9,10})\./) {
my $filetime= $1;
if ($filetime >= $oldest_acceptable_mail) {
#ok
} else {
die "$myname: file '$bn' too old\n";
}
}
#^- muss hier/an ort gehen wo filename überhaupt bekannt ist. ok könnte noch deliverydates in headers an schauen.
local our $in= xopen_read $file;
local our $head= Chj::Mail::SimpleHead->new_from_fh ($in);
#if (not $head->headers("received")) {
# die "$myname: file '$bn' is outgoing, or at least not incoming mail\n";
#^- so einfach dass ich keine klass dafür hah. --- doch dann doch nid. hah.
if ($detectoutgoing->head_is_outgoing($head)) {
die "$myname: file '$bn' is not incoming mail\n";
#todo: fix method name head_is_outgoing, since it can't detect outgoing, just if it's not real incoming.right?.
}
#repl;
local our $out= do {
if ($maybe_outfile) {
xtmpfile $maybe_outfile
} else {
bless *STDOUT{IO},"Chj::IO::File";
}
};
$out->xprint($striphead->stripped_head_string($head));
$in->xprintfile_to($out);
$out->xclose;
$in->xclose;
if ($maybe_outfile) {
$out->xputback (0600); #ok?
}
}
if ($opt_dir) {
my $inbase= $file;
my $d= xopendir $inbase;
my $outbase= $maybe_outfile;
while (defined (my $item= $d->xnread)) {
if ($item=~ /^\d/) {
eval {
mailreduce "$inbase/$item", "$outbase/$item"
};
if (ref $@ or $@) {
Warn "file '$item': $@"
}
} else {
Warn "ignoring file '$item'"
}
}
# ^such wrapping should be generic. once again etc usw.
} else {
mailreduce $file, $maybe_outfile
}