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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#! /usr/bin/perl -w
use strict;
use Getopt::Long;
use lib '../../Mail';
use Mail::Box::Manager;
my $VERSION = '2.019';
#-----------
# prototypes
#-----------
sub open_mailboxes();
sub create_outboxes();
sub parse_mailboxes();
sub compile_regex();
sub configure_sigs();
sub get_options();
sub surpress_werr();
sub trace($);
sub usage($);
my @Mailboxes;
my $Outbox;
my %option = ( verbose => 0,
werr => 0,
);
get_options;
usage 2 if not @ARGV;
usage 0 if $option{help};
surpress_werr if not $option{werr};
compile_regex;
my $Manager = Mail::Box::Manager->new;
configure_sigs;
$Outbox = create_outboxes;
open_mailboxes;
parse_mailboxes;
$Manager->closeAllFolders;
#-----
# subs
#-----
sub open_mailboxes() {
for my $item (@ARGV) {
# $item is a directory
if (-d $item) {
opendir DIR, $item or die "Error: Could not open $item: $!";
my @mboxes = readdir DIR;
for my $mb (@mboxes) {
next if $mb =~ /^\.\.?$/;
trace "Opening folder $mb. ";
if(my $mbox = $Manager->open(
folder => "$item/$mb", access => 'r', extract => 'LAZY',
trace => 'NONE'))
{ trace "Success.\n";
push @Mailboxes, $mbox;
}
else { trace "Failed! $item/$mb\n" }
}
closedir DIR;
}
# $item is a file
if (-f $item) {
trace "Opening folder $item. ";
my $mbox = $Manager->open( folder => $item,
access => 'r',
extract => 'LAZY',
trace => 'NONE');
if ($mbox) {
trace "Success.\n";
push @Mailboxes, $mbox;
}
else { trace "Failed!\n" }
}
}
}
sub create_outboxes() {
my $outbox;
if ($option{outbox}) {
trace "Creating $option{outbox}. ";
$outbox = $Manager->open( folder => $option{outbox},
access => 'w',
create => 1 );
if($outbox) { trace "Success.\n" }
else { trace "Failed!\n" }
}
return $outbox;
}
sub parse_mailboxes() {
for my $mbox (@Mailboxes) {
MESSAGE:
for my $msg ($mbox->messages) {
for my $h (keys %{$option{header}}) {
my $hd = $msg->head->get($h);
my $pat = $option{header}{$h};
next MESSAGE unless defined $hd && $hd =~ $pat;
}
for my $h (keys %{$option{nheader}}) {
my $hd = $msg->head->get($h);
my $pat = $option{nheader}{$h};
last if not $hd;
next MESSAGE if $hd =~ $pat;
}
if($Outbox) { $Manager->copyMessage($Outbox, $msg) }
else { $msg->write }
}
}
}
sub compile_regex() {
for my $h (keys %{$option{header}}) {
my $pat = $option{header}{$h};
$option{header}{$h} = qr($pat);
}
for my $h (keys %{$option{nheader}}) {
my $pat = $option{nheader}{$h};
$option{nheader}{$h} = qr($pat);
}
}
sub configure_sigs() {
$SIG{INT} = sub {
print "Received sigint\n";
$Manager->closeAllFolders;
exit;
}
}
sub get_options() {
use Getopt::Long;
my $res = GetOptions(\%option,
'outdir=s',
'outbox=s',
'header=s%',
'nheader=s%',
'werr',
'verbose',
'help|?');
}
sub surpress_werr() {
$SIG{__WARN__} = 0;
}
sub trace($) {
print STDERR shift if $option{verbose};
}
sub usage($) {
my $ec = shift;
warn <<USAGE;
Usage: $0 [options] mailbox/mailbox-dir
options:
--outdir <dir> create new mailboxes in <dir>
--outbox <mbox> output to <mbox> (defaults to stdout)
--header <field>=<regex> capture mails applying to <regexp>
in header-<field>
--nheader <field>=<regex> capture mails not applying to <regexp>
in header-<field>
--verbose print what is done
--werr print warnings and errors as well
--help print this help
USAGE
exit $ec;
}
__END__
=head1 NAME
takemail - walk through mailboxes and grep for something
=head1 SYNOPSIS
takemail [--outbox][--outdir][--header][--nheader]
[--verbose][--werr][--help] mailbox/mailbox-dir
=head1 DESCRIPTION
Dump mails applying to regular expressions either to stdout or into a
newly created mailbox.
Options:
=over 4
=item --outbox FILE
(or C<-c>) Create a new mailbox FILE and write the found messages into it.
If omitted, output goes to stdout.
=item --outdir DIR
Nothing yet.
=item --header HEADER-FIELD=REGEX
Only find messages whose HEADER-FIELD(s) conform to REGEX. REGEX is a
standard Perl regular expression, without the leading and trailing slash
'/'. Multiple key=value pairs can be given by separating them with
whitespace. Example:
takemail --header subject=[Hh]ello from=peter\|john ~/Mail
Care must be taken when specifying patterns with special shell characters,
especially those used for piping. This means that '|' etc. will probably
need to be escaped with a backslash '\'.
=item --nheader HEADER-FIELD=REGEX
Only find messages whose HEADER-FIELD(s) do not conform to REGEX. Same
usage as --header.
=item --verbose
(or C<-v>) In addition to normal output, print a log of what is being done
to stderr.
=item --werr
Nothing yet.
=item --help
(or C<-?>) Print a short summary of options.
=back
=head1 AUTHOR
Tassilo v. Parseval (F<tassilo.parseval@post.rwth-aachen.de>).
All rights reserved. This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=head1 VERSION
This code is beta, version 2.019
|