[Pymilter] Emulating another MimeDefang function

Larry G. Wapnitsky LWapnitsky at ph.wrtdesign.com
Mon Nov 21 13:22:41 EST 2011


I'll check it out.  I don't seem to have the bms.py file in my download.




-----Original Message-----
From: Stuart D. Gathman [stuart at bmsi.com]
Received: Monday, 21 Nov 2011, 13:19
To: Larry G. Wapnitsky [LWapnitsky at ph.wrtdesign.com]
CC: pymilter at bmsi.com [pymilter at bmsi.com]
Subject: Re: [Pymilter] Emulating another MimeDefang function


On Mon, 21 Nov 2011, Larry G. Wapnitsky wrote:

> I?m trying to add to the current features by creating a function similar to
> MimeDefang whereby attachments can be saved to a folder and replaced with
> warning text.  The stumbling block I?m currently hitting is getting the
> names of each file attached to a message.  I?ve been working with the code
> for a few days and seem to keep missing something.

Check out the mime.py module provided with pymilter.  Here is a simple
example of usage (extending milter-template).  For a more complex example
see bms.py in the milter package.  You can provide your own 
check_attachment method if desired.  The default removes attachments
with banned extensions, optionally including inside zip files.

...

   def eom(self):
     self.fp.seek(0)
     msg = email.message_from_file(self.fp)
     # pass header changes in top level message to sendmail
     msg.headerchange = self._headerChange

     # filter leaf attachments through _chk_attach
     assert not msg.ismodified()
     # FIXME: copy self.fp to a save directory and save filename in
     # self.savname
     rc = mime.defang(msg,self.savname)
     defanged = msg.ismodified()
     if defanged:
       # Body modified, copy modified message to a temp file
       out = tempfile.TemporaryFile()
       try:
 	msg.dump(out)
 	out.seek(0)
 	# Since we wrote headers with '\n' (no CR),
 	# the following header/body split should always work.
 	msg = out.read().split('\n\n',1)[-1]
 	self.replacebody(msg)     # feed modified message to sendmail

     self.setreply('250','2.5.1','Grokked by pymilter')
     return Milter.ACCEPT

   def _headerChange(self,msg,name,value):
     if value:   # add header
       self.addheader(name,value)
     else:       # delete all headers with name
       h = msg.getheaders(name)
       if h:
         for i in range(len(h),0,-1):
           self.chgheader(name,i-1,'')

--
 	      Stuart D. Gathman <stuart at bmsi.com>
     Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flammis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.





----wrtmail--%3423wrt%----

<!DSPAM:133C75D395E11560410343425>



More information about the Pymilter mailing list