[Mimedefang] Change subject according to string match in body

Johan Lundberg lundberg at nordu.net
Tue Jan 19 09:53:51 EST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I have some trouble to get my code working with Mimedefang. The goal is
to search through mail bodies from certain senders and change the
subject of the mail to something easily spotted.

This is the code I'm working on, please be gentle as I normally don't
write perl :)

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);

    # DEBUG
    my $file = '/tmp/MIMEDEFANG_TEST';
    open(INFO, ">>$file");
    print INFO "Trying mails from example\@example.com.\n";
    print INFO "$sender";
    print INFO "\n";
    # DEBUG END

    if ($sender =~ /example\@example\.com/) {
        my $subject = $entity->head->get('Subject',0);
        if ($subject =~ /pattern/i) {
            print INFO "Subject hit: Subject changed!\n";
            action_change_header('Subject', "[See Me]$subject");
        } else {
            my $body = $entity->bodyhandle;
            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;
                }
            }
            $IO->close;
        }
    }
    # DEBUG
    close(INFO);
    # DEBUG END
}

I hope I'm not confusing anyone by leaving my debug code in.

The code works as I expect when the pattern is found in the subject of
the mail but when the script tries to look in the body Mimedefang
crashes and I have to reread the filter file to be able get it working
again.

The code works when I try it locally, parsing a mail from file and
running this bit of code, so I guess there is something I don't
understand or I have misunderstood with how Mimedefang handles mails.

Any input would be greatly appreciated.

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

iEYEARECAAYFAktVx38ACgkQkGqFZ2SyTVMrXgCgj/Yo2RFMosAMs8Auab+W6EFL
GwIAnA1aI18WDSPaoB1YUNMOkR6QX7Mu
=U3IG
-----END PGP SIGNATURE-----



More information about the MIMEDefang mailing list