new images
[exim-website.git] / config.samples / C028
1 Date: Fri, 5 Nov 1999 02:30:38 +0200 (SAST)
2 From: Paul Sheer <psheer@icon.co.za>
3
4 there is an FAQ question on this, but no examples.
5
6 works with python 1.5.1
7
8 enjoy
9
10 -paul
11
12
13 #!/usr/bin/python
14
15 # Usage:
16 #       exim-filter-mime.py <mime-type> ...
17 #
18 # Example:
19 #       exim-filter-mime.py audio image/jpg video
20 #
21 # This script reads from stdin and writes to stdout.
22 # It strips all the mime attachments from a mail message
23 # that are one of the mime types listed on the command line
24 #
25 # Exim can use it in its configuration file, for example, as follows:
26 #
27 #       remote_smtp:
28 #         driver = smtp
29 #         .
30 #         .
31 #         .
32 #         transport_filter = /etc/exim-filter-mime.py audio video image
33 #
34
35 # The attachment is replaced with the following:
36 def cheeky_response (part):
37     print "[File `%s' of type `%s' is meant to go here]" % (part.getheader('Content-Description'), part.gettype ())
38     print
39     print "This host is restricted from transmitting %s files and" % (part.getmaintype (),)
40     print "hence this attachment was stripped from the mail message."
41     print
42
43 import sys
44 import mimetools
45
46 message = mimetools.Message (sys.stdin, 0)
47
48 # Print out the header:
49 for l in message.headers:
50     sys.stdout.write (l)
51 print
52
53 # Not a multipart message, so just dump the whole thing:
54 if message.getmaintype () != "multipart":
55     l = " "
56     while l:
57         l = message.fp.readline ()
58         sys.stdout.write (l)
59     sys.exit (0)
60
61 # Mime boundaries:
62 boundary = "--" + message.getparam ("boundary") + "\n"
63 lastboundary = "--" + message.getparam ("boundary") + "--\n"
64
65 l = " "
66 while l and l != boundary and l != lastboundary:
67     l = message.fp.readline ()
68     sys.stdout.write (l)
69
70 while l and l != lastboundary:
71     part = mimetools.Message (message.fp, 0)
72     if part.getmaintype () in sys.argv[1:] or part.gettype () in sys.argv[1:]:
73 # If it is of a type on the command-line, do not write the header or body,
74 # just skip over till the next boundary...
75         l = " "
76         while l and l != boundary and l != lastboundary:
77             l = part.fp.readline ()
78 # ... and then give a cheeky replacement:
79         print "Content-Type: TEXT/plain; charset=us-ascii"
80         print
81         cheeky_response (part)
82 # write the boundary:
83         sys.stdout.write (l)
84     else:
85 # if it is anything else (like text/plain, application/octet-stream etc.
86 # then write the header...
87         p = ""
88         for p in part.headers:
89             sys.stdout.write (p)
90         print
91 # ... and then write the body
92         l = " "
93         while l and l != boundary and l != lastboundary:
94             l = part.fp.readline ()
95             sys.stdout.write (l)
96
97 l = " "
98 while l:
99     l = message.fp.readline ()
100     sys.stdout.write (l)
101
102