PDA

View Full Version : Custom Filter to delete everything UNLESS containing specific pattern in body


cindik
03-18-2008, 05:33 PM
I have been trying to cook up a filter that will dump all e-mail except those messages containing a specific string in the body. grep -v won't work because it will get any e-mail that has any line that doesn't contain the string. !grep doesn't seem to work either.

I tried

if grep -iq "string" then exit 0; else exit 99; fi

but that also went nowhere.

Any ideas?

kitchin
03-19-2008, 01:48 PM
If you are filtering the *body* of the message, you will need a script I think. A very easy script, maybe even a perl one-liner command. I can post later if this sounds right.

cindik
03-19-2008, 02:53 PM
If you are filtering the *body* of the message, you will need a script I think. A very easy script, maybe even a perl one-liner command. I can post later if this sounds right.

I'm not filtering the body. I am filtering messages based on the contents of the body - specifically, only passing messages containing a specific pattern.

HTH

kitchin
03-19-2008, 07:33 PM
It's only 4 lines, but I like to debug this stuff (all commented out; the first line is not a comment but I guess you know that).
I'm filtering out to delete all emails that do not contain the word "bearcat" in the message somewhere, header or body.

#!/usr/local/bin/perl
##############################################
## Experimental, use at your owk risk.
## This file is /big/dom/xACCOUNT/filter/filter.pl
## Use a simple filter in the CNC:
# /usr/local/bin/perl /big/dom/xACCOUNT/filter/filter.pl
## (You can leave out the first part if you chmod 755 this script.)
##############################################
## For debugging, use absolute path, create, chmod 0664:
# $LOG= '>>/big/dom/xACCOUNT/filter/filter.log';

$/= undef; # slurp mode
$_= <STDIN>; # get all text

# This is the test.
# Simpler than a regex, "index($_, 'pattern')==-1" is a negative string match on 'pattern'.
$result= index($_, 'bearcat')==-1 ? 99 : 0;

## For debugging:
# if ($LOG && open(LOG)) {
# ($subject)= /^subject: (.{0,100})/mi;
# $subject=~ s/\W/_/g; # safe
# print LOG "
# ============" . localtime() . "======
# result= [$result]
# subject= [$subject]
# ============done=====================\n";
# close LOG;
# }

exit $result;

## Attempt at a one-liner, needs better quoting:
# /usr/bin/perl -0777 -ne "exit (index($_, 'bearcat')==-1 ? 99 : 0));"

Bruce
03-19-2008, 07:52 PM
I have been trying to cook up a filter that will dump all e-mail except those messages containing a specific string in the body.The following filter works for me:if ! grep -iq 'string'; then exit 99; fiBe very careful with the placement of the semicolons. The original grep recipe posted here was missing one before the "then".

kitchin
03-19-2008, 07:57 PM
Much easier!

cindik
03-20-2008, 12:28 PM
The following filter works for me:if ! grep -iq 'string'; then exit 99; fiBe very careful with the placement of the semicolons. The original grep recipe posted here was missing one before the "then".

Weird, not working for me. Not even if I just use it as posted, to delete e-mails that don't contain the literal 'string'.

cindik
03-20-2008, 03:18 PM
I am an idiot. :(

cindik
03-22-2008, 05:05 PM
An explanation of the idiocy:

I probably had 4 or more filters that worked right. However, I was sending the test messages to the wrong address... in fact, the wrong domain. So there was no chance they would come in contact with the filter.

sheila
03-26-2008, 01:35 AM
I salute you for clarifying that. ;)

kitchin
03-27-2008, 03:54 PM
At least you didn't post an entire script for something that can be done in one line in the CNC! :) Enough hints from Bruce et. al. and I will know bash.

manfred
07-22-2008, 06:19 AM
The following filter works for me:if ! grep -iq 'string'; then exit 99; fiBe very careful with the placement of the semicolons. The original grep recipe posted here was missing one before the "then".

Is it possible to add a custom error-message? For example :"Invalid Mailcode, please follow... bla bla"

Thank you!
Manfred

sheila
07-23-2008, 12:20 AM
Custom error messages in a script like this are simply a matter of printing something to standard output before exiting the script. I'm not someone who does bash scripting, though. I'd think some sort of
echo 'My custom error message'
type of thing is what you would want before the exit 99;

manfred
07-23-2008, 03:51 AM
After some internetsearch and tons of tests, it looks like that I have found a solution.

if ! grep -iq 'mailcode'; then exit 100 | bouncesaying "Invalid Mailcode CUSTOM TEXT"; fi

Sending a mail without "mailcode" results in bouncing the mail with the custom error-text. I know bouncing isn't a good solution but 95% of the spammails are already killed by EFM, SA,... The remainig 5% are kicked out with the custom filter ;)

songdog
08-11-2008, 07:55 PM
The following filter works for me:if ! grep -iq 'string'; then exit 99; fiIn this example, is the email just dropped (i.e. blackholed) when the exit code is 99?

kitchin
08-11-2008, 08:37 PM
Yes. Use condredirect if you want to put it in another mailbox.