Subject: | New Mail::Audit plugin: Filter |
In procmail it is possible to pipe a message through an external program that modifies the message and then continue filtering the modified message. I use this feature to run my mail through Bogofilter, which in passthrough mode adds an X-Bogosity header that categorizes it as spam or not spam. I then filter on this header to accept() spam into my spamhole.
I know bi-directional pipes are a sticky problem, but I hacked the pipe subroutine to make this filter subroutine that allows you to modify a message using an external program and then continue filtering it. I hope this makes sense, and I hope others find it useful.
I am not a Perl guru, and I do not make any guarantee that this will work on any other system than mine, or any other program besides my particular version of bogofilter.
package Mail::Audit::Filter;
use Mail::Audit;
use vars qw(@VERSION $config);
$VERSION = substr(q$Revision: 1.3 $, 10, -1);
1;
package Mail::Audit;
use strict;
use IPC::Open2;
sub filter {
my $self = shift;
return $self->{_audit_opts}->{filter}->(@_) if exists $self->{_audit_opts}->{filter};
my $local_opts = {}; $local_opts = shift if ref($_[0]) eq "HASH";
my ($file) = $self->nifty_interpolate($local_opts, shift);
_log(1, "Filtering through $file");
my $pid;
my ($cmd,@args) = split(" ", $file);
eval { $pid = open2(*READER, *WRITER, $cmd, @args); };
if ($@) {
_log(0, "Couldn't open filter $file: $!\n$@\n");
$self->accept();
}
$self->print(\*WRITER);
close(WRITER);
my @output = <READER>;
close(READER);
waitpid($pid, 0);
my %newopts = (data => \@output);
my %mergedopts = (%newopts, $self->{_audit_opts});
my $newself = Mail::Audit->new(%mergedopts);
return $newself;
}
1;
__END__
=pod
=head1 NAME
Mail::Audit::Filter - Mail::Audit plugin that modifies the mail using a given external filter
=head1 SYNOPSIS
use Mail::Audit qw(Filter);
my $mail = Mail::Audit->new;
...
$mail = $mail->filter($program);
=head1 DESCRIPTION
This is a Mail::Audit plugin which provides a method for modifying a given
email using an external program, such as a spam detection engine that adds a
special header to the email.
=head1 AUTHOR
Philip Douglass <cpan.org@philipdouglass.com>
=head1 SEE ALSO
L<Mail::Audit>