-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail2page
executable file
·197 lines (155 loc) · 4.38 KB
/
email2page
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
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/local/bin/perl
#
# tool designed to re-write emails into pages
#
# $Id$
#
# Copyright (C) 2000-2004 Kees Cook
# [email protected], http://outflux.net/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
=head1 NAME
email2page - converts RFC822 email text into text suitable for paging
=head1 SYNOPSIS
email2page [-C CONF] [-h]
=head1 OPTIONS
=over 4
=item -C CONF
Read the configuration file CONF instead of /etc/email2page.conf for the
rewriting rules.
=item -h
Display a summary of all the available command line options (and there
sure aren't many).
=back
=head1 DESCRIPTION
This tool is used to break down an email into a shortened version, using
a configurable set of rewriting rules, found in /etc/email2page.conf.
email2page reads STDIN, and writes to STDOUT. Any errors will be reported
on STDERR. It was designed to be used with 'sendpage'.
=head1 AUTHOR
Kees Cook <[email protected]>
=head1 BUGS
All the bugs with the program will probably come from the config file, as
several of the items are run with Perl's 'eval' statement. Please see the
documentation in the /etc/email2page.conf file.
=head1 COPYRIGHT
email2page is free software; it can be used under the terms of the GNU
General Public License.
=head1 SEE ALSO
perl(1), sendpage(1), Mail::Internet(3)
=cut
use Getopt::Std;
use Mail::Internet;
use Mail::Header;
use IO::File;
my %opts;
my $VERSION="0.1";
sub Usage {
die "Usage: $0 [OPTIONS]
version $VERSION
Parses an email message based on the values of the conf file. Reads stdin
and produces results to stdout.
-C CONF read CONF instead of /etc/email2page.conf
-h you're reading it already. :)
";
}
my $maxlines=0; # how many lines to process of the body
my $prefix=""; # prefix written to page text
my $suffix=""; # suffix written to page text
my $headerjoin="|"; # how to join header tags in the page text
my $headbodyjoin="\n"; # how to join the header and body section
my @headrules=(); # rules for handling header tags
my @bodyrules=(); # rules for handling body text
# get our options
if (!getopts('hC:',\%opts) || $opts{h}) {
Usage();
}
$opts{C}="/etc/email2page.conf" unless ($opts{C});
$fh = new IO::File $opts{C}, "r";
if (!defined($fh)) {
die "Cannot read '$opts{C}' file: $!\n";
}
$num=0;
foreach $line (<$fh>) {
chomp($line);
$num++;
# skip comments and blanks
next if ($line =~ /^\s*#/ || $line =~ /^\s*$/);
($cmd,$arg)=split(/:/,$line,2);
if ($cmd eq "headerjoin") {
$headerjoin=$arg;
}
elsif ($cmd eq "headbodyjoin") {
$headbodyjoin=$arg;
}
elsif ($cmd eq "header") {
push(@headrules,$arg);
}
elsif ($cmd eq "body") {
push(@bodyrules,$arg);
}
elsif ($cmd eq "prefix") {
$prefix=$arg;
}
elsif ($cmd eq "suffix") {
$suffix=$arg;
}
elsif ($cmd eq "maxlines") {
$maxlines=$arg;
}
else {
warn "unknown command '$cmd' in '$opts{C}', line $num\n";
}
}
undef $fh;
# read in our email message
my $mail=Mail::Internet->new(\*STDIN);
my $head=$mail->head();
my $body=$mail->body();
@body=@$body;
# trim the body down to "maxlines"
if ($maxlines > 0 && $#body > $maxlines) {
splice @body, $maxlines;
}
my @okheads=();
# handle rewriting the headers
foreach $tag ($head->tags()) {
foreach $index (0 .. ($head->count($tag)-1)) {
$matched=0;
$text="$tag: ".$head->get($tag,$index);
chomp($text);
foreach $rule (@headrules) {
if (eval "\$text =~ $rule") {
$matched=1;
}
}
if ($matched) {
push(@okheads,$text);
}
}
}
# handle rewriting the body
foreach $rule (@bodyrules) {
foreach $index (0 .. $#body) {
eval "\$body[$index] =~ $rule";
}
}
$result=eval $prefix;
$result.=eval "join($headerjoin,\@okheads)";
$result.=eval $headbodyjoin;
$result.=join("",@body);
$result.=eval $suffix;
print $result;