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
|
# Mail::Filter.pm
#
# Copyright (c) 1997 Graham Barr <gbarr@pobox.com>. All rights
# reserved. This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package Mail::Filter;
use Carp;
use strict;
use vars qw($VERSION);
$VERSION = "1.00";
sub new {
my $self = shift;
bless {
filters => [ @_ ]
}, $self;
}
sub add {
my $self = shift;
push(@{$self->{'filters'}}, @_);
}
sub _filter {
my $self = shift;
my $mail = shift;
my $sub;
foreach $sub (@{$self->{'filters'}}) {
if(ref($sub) eq "CODE") {
$mail = $sub->($self,$mail);
}
elsif(!ref($sub)) {
$mail = $self->$sub($mail);
}
else {
carp "Cannot call filter '$sub', ignored";
}
last unless ref($mail);
}
}
sub filter {
my $self = shift;
my $obj = shift;
if($obj->isa('Mail::Folder')) {
$self->{'folder'} = $obj;
my $m;
foreach $m ($obj->message_list) {
my $mail = $obj->get_message($m) || next;
$self->{'msgnum'} = $m;
_filter($self,$mail);
}
delete $self->{'folder'};
delete $self->{'msgnum'};
}
elsif($obj->isa('Mail::Internet')) {
return _filter($self,$obj);
}
else {
carp "Cannot process '$obj'";
return undef;
}
}
sub folder {
my $self = shift;
exists $self->{'folder'}
? $self->{'folder'}
: undef;
}
sub msgnum {
my $self = shift;
exists $self->{'msgnum'}
? $self->{'msgnum'}
: undef;
}
1;
__END__
=head1 NAME
Mail::Filter - Filter mail through multiple subroutines
=head1 SYNOPSIS
use Mail::Filter;
$filter = new Mail::Filter( \&filter1, \&filter2 );
$mail = new Mail::Internet( [<>] );
$mail = $filter->filter($mail);
$folder = new Mail::Folder( .... );
$filter->filter($folder);
=head1 DESCRIPTION
C<Mail::Filter> provides an interface to filtering Email through multiple
subroutines.
C<Mail::Filter> filters mail by calling each filter subroutine in turn. Each
filter subroutine is called with two arguments, the fist is the filter
object and the second is the mail being filtered.
The result from each filter sub is passed to the next filter as the mail
object. If a filter subroutine returns undef, then C<Mail::Filter> will abort
and return immediately.
=head1 CONSTRUCTOR
=over 4
=item new ( [ FILTER [, ... ]])
Create a new C<Mail::Filter> object with the given filter subroutines. Each
filter may be either a code reference or the name of a method to call
on the <Mail::Filter> object.
=back
=head1 METHODS
=over 4
=item add ( FILTER [, FILTER ...] )
Add the given filters to the end of the fliter list.
=item filter ( MAIL-OBJECT | MAIL-FOLDER )
If the first argument is a C<Mail::Internet> object then this object will
be passed through the filter list. If the first argument is a C<Mail::Folder>
object, then each message in turn will be passed through the filter list.
=item folder
If the C<filter> method is called with a C<Mail::Folder> object, then the
filter subroutines may call this method to obtain the folder object that is
being processed.
=item msgnum
If the C<filter> method is called with a C<Mail::Folder> object, then the
filter subroutines may call this method to obtain the message number
of the message that is being processed.
=back
=head1 SEE ALSO
L<Mail::Internet>
L<Mail::Folder>
=head1 AUTHOR
Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
=head1 COPYRIGHT
Copyright (c) 1997 Graham Barr. All rights reserved. This program is free
software; you can redistribute it and/or modify it under the same terms
as Perl itself.
=cut
|