[Mimedefang] Change subject according to string match in body

Johan Lundberg lundberg at nordu.net
Wed Jan 20 08:44:04 EST 2010

Hash: SHA1

On 20/01/10 09:29, Steffen Kaiser wrote:
> On Tue, 19 Jan 2010, Johan Lundberg wrote:
> Primary, follow the advice of Dave and move your check into filter().
> During this phase the module variables (globally defined varables
> in mimedefang-filter) are shared, hence, you can prepare the check in
> filter_begin(), e.g. open log file, prepare the condition when to search
> for the pattern, do the subject check etc, and end it in filter_end(),
> e.g close the log file.
> If you read INPUTMSG directly, you have to deal with MIME encoding etc,
> hence, I would go the filter() route.
> BTW: because multiple MIMEDefang processes may run simultaneously,
> you might loose log info. You should use Syslog.
>> sub filter_end {
>> *snip* *snip*
>>    # Search for "pattern" in the mail body
>>    # and prepend subject with [See Me].
>>    my $sender = $entity->head->get('From',0);
> Well, there is $Sender, which is the envelope MAIL FROM, which probably
> differs from the header. BTW: There is probably no From header.
>>    if ($sender =~ /example\@example\.com/) {
> I guess, you want to check anchored regex:
>     if ($sender =~ /\A(example\@example\.com|..)\z/) {
>>        my $subject = $entity->head->get('Subject',0);
> Already in $Subject
>>        if ($subject =~ /pattern/i) {
>>            print INFO "Subject hit: Subject changed!\n";
>>            action_change_header('Subject', "[See Me]$subject");
>>        } else {
>>            my $body = $entity->bodyhandle;
> I'm not sure, if this works with multipart MIME messages, at least you
> would need some checks.
>>            my $IO = $body->open("r");
>>            while (defined($_ = $IO->getline)) {
>>                if (/pattern/i) {
>>                    print INFO "Body hit: Subject changed!\n";
>>                    print INFO "Body line: $_\n";
>>                    action_change_header('Subject', "[See Me]$subject");
>>                    last;
>>                }
> BTW: If you use SpamAssassin, you can create your own rule, score it with
> "0.0" - so the SPAM score is not changed - and check the matched rules
> for your one.

Thank you, both Dave and Steffen, for your great suggestions and pointers.

One of the problems with the e-mails I am looking for is that they are
encoded with base64 encoding (gaah!), so no go with the raw message
directly in Mimedefang. Before our mail setup was changed I had a little
python script together with procmail for decoding, matching and
manipulating. So my first idea was to use the raw message or the
action_external_filter() function and my python script but I think this
little snippet is better, don't you agree?

I have revised my code and moved it in to the filter subroutine as
suggested. Everything seems to work as intended. Time to test it on the
production system :)

sub filter {
    if ($Sender =~ /\A(\<example\@example\.com\>)\z/) {
        if ($Subject =~ /pattern/i) {
            action_change_header('Subject', "[See Me]$Subject");
        else {
            my $io;
            if ($io = $entity->open("r")) {
                while (defined($_ = $io->getline)) {
                    if (/pattern/i) {
                        action_change_header('Subject',"[See Me]$Subject");

Best regards,
- -- 
Johan Lundberg
lundberg at nordu.net
+46(0)8 207860
Tulegatan 11
113 53 Stockholm
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/


More information about the MIMEDefang mailing list