#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# tool designed to re-write emails into pages
#
# $Id: email2page 316 2008-01-03 20:21:19Z keescook $
#
# Copyright (C) 2000-2004 Kees Cook
# kees@outflux.net, 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 <kees@outflux.net>

=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;
