[Mailman-Developers] attachments to links :-)

Michael Meltzer mjm@michaelmeltzer.com
Wed, 16 Oct 2002 04:23:47 -0400


Had some heartburn trying to code if mm_cfg.attach_filter does not exist leave it alone, never quit got it, but The code is tested
and working.

MJM


in mm_cfg.py
attach_filter = ['image/bmp', 'image/jpeg', 'image/tiff', 'image/gif', 'image/png', 'image/pjpeg', 'image/x-png', 'image/x-wmf']

Index: MimeDel.py
===================================================================
RCS file: /cvsroot/mailman/mailman/Mailman/Handlers/MimeDel.py,v
retrieving revision 2.5
diff -u -r2.5 MimeDel.py
--- MimeDel.py 8 Oct 2002 00:02:00 -0000 2.5
+++ MimeDel.py 16 Oct 2002 08:16:28 -0000
@@ -35,6 +35,8 @@
 from Mailman.Queue.sbcache import get_switchboard
 from Mailman.Logging.Syslog import syslog
 from Mailman.Version import VERSION
+from Mailman.Handlers.Scrubber import save_attachment
+from time import strftime
 from Mailman.i18n import _


@@ -45,6 +47,8 @@
         return
     if msgdata.get('isdigest'):
         return
+
+    make_attachment(mlist, msg)
     # We also don't care about our own digests or plaintext
     ctype = msg.get_content_type()
     mtype = msg.get_content_maintype()
@@ -64,7 +68,7 @@
     if msg.is_multipart():
         # Recursively filter out any subparts that match the filter list
         prelen = len(msg.get_payload())
-        filter_parts(msg, filtertypes, passtypes)
+        filter_parts(mlist, msg, filtertypes, passtypes)
         # If the outer message is now an empty multipart (and it wasn't
         # before!) then, again it gets discarded.
         postlen = len(msg.get_payload())
@@ -121,7 +125,7 @@



-def filter_parts(msg, filtertypes, passtypes):
+def filter_parts(mlist, msg, filtertypes, passtypes):
     # Look at all the message's subparts, and recursively filter
     if not msg.is_multipart():
         return 1
@@ -129,9 +133,12 @@
     prelen = len(payload)
     newpayload = []
     for subpart in payload:
-        keep = filter_parts(subpart, filtertypes, passtypes)
+        keep = filter_parts(mlist, subpart, filtertypes, passtypes)
         if not keep:
             continue
+        if make_attachment(mlist, subpart):
+            newpayload.append(subpart)
+            continue
         ctype = subpart.get_content_type()
         mtype = subpart.get_content_maintype()
         if ctype in filtertypes or mtype in filtertypes:
@@ -218,3 +225,33 @@
         badq.enqueue(msg, msgdata)
     # Most cases also discard the message
     raise Errors.DiscardMessage
+
+
+
+def make_attachment(mlist, subpart):
+     #should be set from mlist, work in progress
+     #BTW this will act real stupid with mulipart, it need the real object not the house keeping
+    if not mm_cfg.attach_filter:
+        return 0
+    ctype = subpart.get_content_type()
+    mtype = subpart.get_content_maintype()
+    if ctype in mm_cfg.attach_filter or mtype in mm_cfg.attach_filter:
+        #size is off, just could not stand to call decode to correct, might just take off 20% and be done
+        size = len(subpart.get_payload())
+        desc = subpart.get('content-description', (_('not available')))
+        filename = subpart.get_filename(_('not available'))
+        url = save_attachment(mlist, subpart, strftime("attch/%Y%m/%d"))
+        del subpart['content-type']
+        del subpart['content-transfer-encoding']
+        del subpart['content-disposition']
+        del subpart['content-description']
+        subpart.add_header('Content-Type', 'text/plain', charset='us-ascii')
+        subpart.add_header('Content-Transfer-Encoding', '7bit')
+        subpart.add_header('Content-disposition', 'inline')
+        subpart.set_payload(_("""\
+Name: %(filename)s Type: %(ctype)s Size: %(size)d bytes Desc: %(desc)s
+Url: %(url)s
+"""))
+        return 1
+    else:
+        return 0

Index: private.py
===================================================================
RCS file: /cvsroot/mailman/mailman/Mailman/Cgi/private.py,v
retrieving revision 2.16
diff -u -r2.16 private.py
--- private.py 22 May 2002 03:00:19 -0000 2.16
+++ private.py 16 Oct 2002 08:18:10 -0000
@@ -20,6 +20,7 @@
 import sys
 import os
 import cgi
+import mimetypes

 from Mailman import mm_cfg
 from Mailman import Utils
@@ -46,6 +47,8 @@
 def content_type(path):
     if path[-3:] == '.gz':
         path = path[:-3]
+    if mm_cfg.attach_filter and mimetypes.guess_type(os.path.splitext(path)[1])[0] in mm_cfg.attach_filter:
+        return mimetypes.guess_type(os.path.splitext(path)[1])[0]
     if path[-4:] == '.txt':
         return 'text/plain'
     return 'text/html'