| 1 | # Copyrights 1995-2024 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||
| 5 | # This code is part of the bundle MailTools. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md for Copyright. | ||
| 7 | # Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package Mail::Filter;{ | ||
| 10 | our $VERSION = '2.22'; | ||
| 11 | } | ||
| 12 | |||
| 13 | |||
| 14 | use strict; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | |||
| 18 | sub new(@) | ||
| 19 | { my $class = shift; | ||
| 20 | bless { filters => [ @_ ] }, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | #------------ | ||
| 24 | |||
| 25 | sub add(@) | ||
| 26 | { my $self = shift; | ||
| 27 | push @{$self->{filters}}, @_; | ||
| 28 | } | ||
| 29 | |||
| 30 | #------------ | ||
| 31 | |||
| 32 | sub _filter($) | ||
| 33 | { my ($self, $mail) = @_; | ||
| 34 | |||
| 35 | foreach my $sub ( @{$self->{filters}} ) | ||
| 36 | { my $mail | ||
| 37 | = ref $sub eq 'CODE' ? $sub->($self,$mail) | ||
| 38 | : !ref $sub ? $self->$sub($mail) | ||
| 39 | : carp "Cannot call filter '$sub', ignored"; | ||
| 40 | |||
| 41 | ref $mail or last; | ||
| 42 | } | ||
| 43 | |||
| 44 | $mail; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub filter | ||
| 48 | { my ($self, $obj) = @_; | ||
| 49 | if($obj->isa('Mail::Folder')) | ||
| 50 | { $self->{folder} = $obj; | ||
| 51 | foreach my $m ($obj->message_list) | ||
| 52 | { my $mail = $obj->get_message($m) or next; | ||
| 53 | $self->{msgnum} = $m; | ||
| 54 | $self->_filter($mail); | ||
| 55 | } | ||
| 56 | delete $self->{folder}; | ||
| 57 | delete $self->{msgnum}; | ||
| 58 | } | ||
| 59 | elsif($obj->isa('Mail::Internet')) | ||
| 60 | { return $self->filter($obj); | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { carp "Cannot process '$obj'"; | ||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub folder() {shift->{folder}} | ||
| 70 | |||
| 71 | |||
| 72 | sub msgnum() {shift->{msgnum}} | ||
| 73 | |||
| 74 | 1; |