extract-message-from-mbox [plain text]
use strict;
use bytes;
use vars qw {
$opt_f $opt_h $opt_m $opt_H
};
use Getopt::Std;
getopts("f:hmH");
sub usage {
die "extract-message-from-mbox [-f=file] [-m] [-H] offset
Extracts the message starting at offset from file (or stdin). Very
useful in combination with mass-check logs and mboxes. If the -m
option is used, the input should be in \"mass-check\" format (as
output by mass-check). Use the -H option to just output headers.
";
}
usage() if($opt_h || (!defined($ARGV[0]) && !$opt_m));
my $offset = $ARGV[0];
if($opt_m) {
masscheck();
} else {
$opt_f ||= '&STDIN';
extract($opt_f, $offset);
}
sub extract {
my $file = shift;
my $offset = shift;
my $found = 0;
open (IN, "<$file") || die "Could not open $file: $!\n";
while(<IN>) {
if(!$found) {
$offset -= length;
$found = $offset <= 0;
} else {
$found++ if(/^From /);
last if($found == 3);
print;
last if ($opt_H && /^$/) }
}
}
sub masscheck {
while (<STDIN>) {
my $mail = (split(/\s+/, $_))[2];
$mail =~ tr/_/ /;
if ($mail =~ /^(.*)\.(\d+)$/) {
extract($1, $2);
} else { chomp $mail;
extract ($mail, 0);
}
}
}