| 1 |
#! /usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Getopt::Long; |
| 5 |
use lib '../../Mail'; |
| 6 |
use Mail::Box::Manager; |
| 7 |
|
| 8 |
my $VERSION = '2.019'; |
| 9 |
|
| 10 |
#----------- |
| 11 |
# prototypes |
| 12 |
#----------- |
| 13 |
sub open_mailboxes(); |
| 14 |
sub create_outboxes(); |
| 15 |
sub parse_mailboxes(); |
| 16 |
sub compile_regex(); |
| 17 |
sub configure_sigs(); |
| 18 |
sub get_options(); |
| 19 |
sub surpress_werr(); |
| 20 |
sub trace($); |
| 21 |
sub usage($); |
| 22 |
|
| 23 |
my @Mailboxes; |
| 24 |
my $Outbox; |
| 25 |
my %option = ( verbose => 0, |
| 26 |
werr => 0, |
| 27 |
); |
| 28 |
get_options; |
| 29 |
|
| 30 |
|
| 31 |
usage 2 if not @ARGV; |
| 32 |
usage 0 if $option{help}; |
| 33 |
|
| 34 |
surpress_werr if not $option{werr}; |
| 35 |
compile_regex; |
| 36 |
|
| 37 |
my $Manager = Mail::Box::Manager->new; |
| 38 |
configure_sigs; |
| 39 |
$Outbox = create_outboxes; |
| 40 |
open_mailboxes; |
| 41 |
parse_mailboxes; |
| 42 |
$Manager->closeAllFolders; |
| 43 |
|
| 44 |
#----- |
| 45 |
# subs |
| 46 |
#----- |
| 47 |
|
| 48 |
sub open_mailboxes() { |
| 49 |
|
| 50 |
for my $item (@ARGV) { |
| 51 |
|
| 52 |
# $item is a directory |
| 53 |
if (-d $item) { |
| 54 |
opendir DIR, $item or die "Error: Could not open $item: $!"; |
| 55 |
my @mboxes = readdir DIR; |
| 56 |
for my $mb (@mboxes) { |
| 57 |
next if $mb =~ /^\.\.?$/; |
| 58 |
trace "Opening folder $mb. "; |
| 59 |
|
| 60 |
if(my $mbox = $Manager->open( |
| 61 |
folder => "$item/$mb", access => 'r', extract => 'LAZY', |
| 62 |
trace => 'NONE')) |
| 63 |
{ trace "Success.\n"; |
| 64 |
push @Mailboxes, $mbox; |
| 65 |
} |
| 66 |
else { trace "Failed! $item/$mb\n" } |
| 67 |
} |
| 68 |
closedir DIR; |
| 69 |
} |
| 70 |
|
| 71 |
# $item is a file |
| 72 |
if (-f $item) { |
| 73 |
trace "Opening folder $item. "; |
| 74 |
my $mbox = $Manager->open( folder => $item, |
| 75 |
access => 'r', |
| 76 |
extract => 'LAZY', |
| 77 |
trace => 'NONE'); |
| 78 |
if ($mbox) { |
| 79 |
trace "Success.\n"; |
| 80 |
push @Mailboxes, $mbox; |
| 81 |
} |
| 82 |
else { trace "Failed!\n" } |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
sub create_outboxes() { |
| 88 |
my $outbox; |
| 89 |
if ($option{outbox}) { |
| 90 |
trace "Creating $option{outbox}. "; |
| 91 |
$outbox = $Manager->open( folder => $option{outbox}, |
| 92 |
access => 'w', |
| 93 |
create => 1 ); |
| 94 |
|
| 95 |
if($outbox) { trace "Success.\n" } |
| 96 |
else { trace "Failed!\n" } |
| 97 |
} |
| 98 |
return $outbox; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
sub parse_mailboxes() { |
| 103 |
for my $mbox (@Mailboxes) { |
| 104 |
MESSAGE: |
| 105 |
for my $msg ($mbox->messages) { |
| 106 |
|
| 107 |
for my $h (keys %{$option{header}}) { |
| 108 |
my $hd = $msg->head->get($h); |
| 109 |
my $pat = $option{header}{$h}; |
| 110 |
next MESSAGE unless defined $hd && $hd =~ $pat; |
| 111 |
} |
| 112 |
|
| 113 |
for my $h (keys %{$option{nheader}}) { |
| 114 |
my $hd = $msg->head->get($h); |
| 115 |
my $pat = $option{nheader}{$h}; |
| 116 |
last if not $hd; |
| 117 |
next MESSAGE if $hd =~ $pat; |
| 118 |
} |
| 119 |
|
| 120 |
if($Outbox) { $Manager->copyMessage($Outbox, $msg) } |
| 121 |
else { $msg->write } |
| 122 |
|
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
sub compile_regex() { |
| 128 |
for my $h (keys %{$option{header}}) { |
| 129 |
my $pat = $option{header}{$h}; |
| 130 |
$option{header}{$h} = qr($pat); |
| 131 |
} |
| 132 |
for my $h (keys %{$option{nheader}}) { |
| 133 |
my $pat = $option{nheader}{$h}; |
| 134 |
$option{nheader}{$h} = qr($pat); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
sub configure_sigs() { |
| 139 |
$SIG{INT} = sub { |
| 140 |
print "Received sigint\n"; |
| 141 |
$Manager->closeAllFolders; |
| 142 |
exit; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
sub get_options() { |
| 147 |
use Getopt::Long; |
| 148 |
my $res = GetOptions(\%option, |
| 149 |
'outdir=s', |
| 150 |
'outbox=s', |
| 151 |
'header=s%', |
| 152 |
'nheader=s%', |
| 153 |
'werr', |
| 154 |
'verbose', |
| 155 |
'help|?'); |
| 156 |
} |
| 157 |
|
| 158 |
sub surpress_werr() { |
| 159 |
$SIG{__WARN__} = 0; |
| 160 |
} |
| 161 |
|
| 162 |
sub trace($) { |
| 163 |
print STDERR shift if $option{verbose}; |
| 164 |
} |
| 165 |
|
| 166 |
sub usage($) { |
| 167 |
my $ec = shift; |
| 168 |
|
| 169 |
warn <<USAGE; |
| 170 |
Usage: $0 [options] mailbox/mailbox-dir |
| 171 |
options: |
| 172 |
--outdir <dir> create new mailboxes in <dir> |
| 173 |
--outbox <mbox> output to <mbox> (defaults to stdout) |
| 174 |
--header <field>=<regex> capture mails applying to <regexp> |
| 175 |
in header-<field> |
| 176 |
--nheader <field>=<regex> capture mails not applying to <regexp> |
| 177 |
in header-<field> |
| 178 |
--verbose print what is done |
| 179 |
--werr print warnings and errors as well |
| 180 |
--help print this help |
| 181 |
USAGE |
| 182 |
|
| 183 |
exit $ec; |
| 184 |
} |
| 185 |
|
| 186 |
__END__ |
| 187 |
|
| 188 |
=head1 NAME |
| 189 |
|
| 190 |
takemail - walk through mailboxes and grep for something |
| 191 |
|
| 192 |
=head1 SYNOPSIS |
| 193 |
|
| 194 |
takemail [--outbox][--outdir][--header][--nheader] |
| 195 |
[--verbose][--werr][--help] mailbox/mailbox-dir |
| 196 |
|
| 197 |
=head1 DESCRIPTION |
| 198 |
|
| 199 |
Dump mails applying to regular expressions either to stdout or into a |
| 200 |
newly created mailbox. |
| 201 |
|
| 202 |
Options: |
| 203 |
|
| 204 |
=over 4 |
| 205 |
|
| 206 |
=item --outbox FILE |
| 207 |
|
| 208 |
(or C<-c>) Create a new mailbox FILE and write the found messages into it. |
| 209 |
If omitted, output goes to stdout. |
| 210 |
|
| 211 |
=item --outdir DIR |
| 212 |
|
| 213 |
Nothing yet. |
| 214 |
|
| 215 |
=item --header HEADER-FIELD=REGEX |
| 216 |
|
| 217 |
Only find messages whose HEADER-FIELD(s) conform to REGEX. REGEX is a |
| 218 |
standard Perl regular expression, without the leading and trailing slash |
| 219 |
'/'. Multiple key=value pairs can be given by separating them with |
| 220 |
whitespace. Example: |
| 221 |
|
| 222 |
takemail --header subject=[Hh]ello from=peter\|john ~/Mail |
| 223 |
|
| 224 |
Care must be taken when specifying patterns with special shell characters, |
| 225 |
especially those used for piping. This means that '|' etc. will probably |
| 226 |
need to be escaped with a backslash '\'. |
| 227 |
|
| 228 |
=item --nheader HEADER-FIELD=REGEX |
| 229 |
|
| 230 |
Only find messages whose HEADER-FIELD(s) do not conform to REGEX. Same |
| 231 |
usage as --header. |
| 232 |
|
| 233 |
=item --verbose |
| 234 |
|
| 235 |
(or C<-v>) In addition to normal output, print a log of what is being done |
| 236 |
to stderr. |
| 237 |
|
| 238 |
=item --werr |
| 239 |
|
| 240 |
Nothing yet. |
| 241 |
|
| 242 |
=item --help |
| 243 |
|
| 244 |
(or C<-?>) Print a short summary of options. |
| 245 |
|
| 246 |
=back |
| 247 |
|
| 248 |
=head1 AUTHOR |
| 249 |
|
| 250 |
Tassilo v. Parseval (F<[email protected]>). |
| 251 |
|
| 252 |
All rights reserved. This program is free software; you can redistribute |
| 253 |
it and/or modify it under the same terms as Perl itself. |
| 254 |
|
| 255 |
=head1 VERSION |
| 256 |
|
| 257 |
This code is beta, version 2.019 |