With the assistance of a few others, I managed to create this function, I added it to /usr/local/bin/mimedefang.pl therefore I can simply make a call to it like this in my begin_filter function in /etc/mail/mimedefang-filter
if(dirty_word('subject','/etc/mail/really_nasty_phrases')) {
action_discard();
}
if(dirty_word('body','/etc/mail/no_so_bad_phrases')) {
action_quarentine();
}
//whatever...
Here's the function, perhaps something similar to this can makes its way into future revisions of MimeDefang?!?
Calls are as follows:
dirty_word('<sender, subject, or body>','/path to dirt file/');
Dirt file should be formatted as follows:
# Comments are allowed with a pound sign.
# You can use plain words or regular expressions
badword
\([ ]*free.+?sex?[ ]*\)
#One side note, all text is translated to lowercase when the check is made, so write your regexs to account for that.
sub dirty_word {
my($mode, $dirtfile) = @_;
my $DW_Found = 0;
my ($pattern,$comment,@dirtlist,$pat_match,$line);
unless (open(DIRTFILE, "<$dirtfile")) { # get the Dirt File to use
syslog('err', "Could not open the DirtyWord File to scan for bad words: $!");
return 0;
}
while ( $pattern=<DIRTFILE>) {
chop $pattern ; # get rid of newlines
$comment = index($pattern,"#",0); # get the position of the comment mark
if ($comment >= 0) {
$pattern = substr($pattern,0,$comment); # get rid of comment
}
$pattern =~ s/^\s*// ; # trim whitespace at beginning of string
$pattern =~ s/\s*$// ; # trim whitespace at end of string
if ($pattern=~m!\S!) { # if there is anything other than whitespace
@dirtlist = ( @dirtlist, $pattern); # add new dirty word to list
}
}
close(DIRTFILE); # We got what we needed, close the dirt file.
if($mode eq "body") {
unless (open(MESG, "<INPUTMSG")) { # get the message to scan
syslog('err', "Could not open the DirtyWord File to scan for bad words: $!");
return 0;
}
while ($line=<MESG>) { # operate on each line of the message
chop $line;
$line =~ tr/A-Z/a-z/; # convert to lowercase
if ($DW_Found == 0){
foreach $pattern ( @dirtlist) { # do for each pattern in the list
if (($line=~m!$pattern!) && ($DW_Found == 0)) {
$DW_Found = 1; #mark that we found an DW string
$pat_match = $&; #get the matched pattern
syslog('info', "Dirty word found: $pat_match");
}
}
} last if $DW_Found #end the loop early if patterns matched.
}
} elsif($mode eq "subject") {
chop $Subject;
$Subject =~ tr/A-Z/a-z/; # convert to lowercase
if ($DW_Found == 0){
foreach $pattern ( @dirtlist) { # do for each pattern in the list
if (($Subject=~m!$pattern!) && ($DW_Found == 0)) {
$DW_Found = 1; #mark that we found an DW string
$pat_match = $&; #get the matched pattern
syslog('info', "Dirty word found: $pat_match");
}
}
}
} elsif($mode eq "sender") {
chop $Sender;
$Sender =~ tr/A-Z/a-z/; # convert to lowercase
if ($DW_Found == 0){
foreach $pattern ( @dirtlist) { # do for each pattern in the list
if (($Sender=~m!$pattern!) && ($DW_Found == 0)) {
$DW_Found = 1; #mark that we found an DW string
$pat_match = $&; #get the matched pattern
syslog('info', "Dirty word found: $pat_match");
}
}
}
} else {
# They didn't play nice by telling us what we're scanning.
syslog('err', "Scan for Dirty Words failed, you must provide a mode of either: body, subject, or sender");
return 0;
}
return $DW_Found;
}
--__--__--
Message: 3
From: "Rob Dege" <rcd@amherst.com>
To: <mimedefang@lists.roaringpenguin.com>
Date: Wed, 3 Apr 2002 11:53:36 -0500
Subject: [Mimedefang] Subject query
Reply-To: mimedefang@lists.roaringpenguin.com
I noticed that with the default filter example file, that MIMEDefang can be
configured to filter out attachments. Is there a way that I can create a
rule that queries for regex matches in the Subject?
Thanks
-Rob
--__--__--
_______________________________________________
MIMEDefang mailing list
MIMEDefang@lists.roaringpenguin.com
http://lists.roaringpenguin.com/mailman/listinfo/mimedefang
End of MIMEDefang Digest