[Exim] Contrib script to filter unwanted MIME attachments

Pàgina inicial
Delete this message
Reply to this message
Autor: Paul Sheer
Data:  
A: exim-users
Assumpte: [Exim] Contrib script to filter unwanted MIME attachments
there is an FAQ question on this, but no examples.

works with python 1.5.1

enjoy

-paul


#!/usr/bin/python

# Usage:
#    exim-filter-mime.py <mime-type> ...
#
# Example:
#    exim-filter-mime.py audio image/jpg video
#
# This script reads from stdin and writes to stdout.
# It strips all the mime attachments from a mail message
# that are one of the mime types listed on the command line
#
# Exim can use it in its configuration file, for example, as follows:
#
#    remote_smtp:
#      driver = smtp
#      .
#      .
#      .
#      transport_filter = /etc/exim-filter-mime.py audio video image
#


# The attachment is replaced with the following:
def cheeky_response (part):
    print "[File `%s' of type `%s' is meant to go here]" % (part.getheader('Content-Description'), part.gettype ())
    print
    print "This host is restricted from transmitting %s files and" % (part.getmaintype (),)
    print "hence this attachment was stripped from the mail message."
    print


import sys
import mimetools

message = mimetools.Message (sys.stdin, 0)

# Print out the header:
for l in message.headers:
    sys.stdout.write (l)
print


# Not a multipart message, so just dump the whole thing:
if message.getmaintype () != "multipart":
    l = " "
    while l:
    l = message.fp.readline ()
    sys.stdout.write (l)
    sys.exit (0)


# Mime boundaries:
boundary = "--" + message.getparam ("boundary") + "\n"
lastboundary = "--" + message.getparam ("boundary") + "--\n"

l = " "
while l and l != boundary and l != lastboundary:
    l = message.fp.readline ()
    sys.stdout.write (l)


while l and l != lastboundary:
    part = mimetools.Message (message.fp, 0)
    if part.getmaintype () in sys.argv[1:] or part.gettype () in sys.argv[1:]:
# If it is of a type on the command-line, do not write the header or body,
# just skip over till the next boundary...
    l = " "
    while l and l != boundary and l != lastboundary:
        l = part.fp.readline ()
# ... and then give a cheeky replacement:
    print "Content-Type: TEXT/plain; charset=us-ascii"
    print
    cheeky_response (part)
# write the boundary:
    sys.stdout.write (l)
    else:
# if it is anything else (like text/plain, application/octet-stream etc.
# then write the header...
    p = ""
    for p in part.headers:
        sys.stdout.write (p)
    print
# ... and then write the body
    l = " "
    while l and l != boundary and l != lastboundary:
        l = part.fp.readline ()
        sys.stdout.write (l)


l = " "
while l:
    l = message.fp.readline ()
    sys.stdout.write (l)





--
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.3i

mQCNAzdiRpAAAAEEANPUPC/Lrs4OCJOjWaIWaCYTzTIY1p73uPY+8ZOJH5fc4QNp
IAX+EFQ/yZ3RMOLg8yy++HufzBwDoePO4W0MKwyVFCcNIIjsY6JCXWdbpQXsY1LL
OASlGexQnEQ4mfc7ThOAKWSgPyiMv5vDJ6S0EL8rdIFL7fVv56BAxnN042FRAAUR
tCJQYXVsIFNoZWVyIDxwc2hlZXJAb2JzaWRpYW4uY28uemE+iQCVAwUQN2JGkKBA
xnN042FRAQF5CAP/Y0TaguqCpYiTEBHxZPpT/dliInVRBzryi4gdlgX6CCLDRRGH
ATP4ac8aiATegc4ev4+vcdn4fBwc6fQ2AP6hd25ZI93vShxztM/bQlGWy0zp79Uo
+69uGdJhdvgYpIsTCqppM/yjeXAJEqq5TG2Gy4pqHY235rspmeA/fX7kgIo=
=aX4m
-----END PGP PUBLIC KEY BLOCK-----

Cellular . . . +27 83 604 0615 . . . . Land . . . +27 21 761 7224
Obsidian Systems . . . . Linux installations, support, networking
info@??? . . . . . . . . . . Tel . . +27 11 792 6500
http://www.obsidian.co.za . . . . . . . Fax . . . +27 11 792 6522
L I N U X . . . . . . . . . . . . . The Choice of a GNU Generation