[Mailman-Developers] A 2.0.x patch to edit pending message headers and bodies

tneff@bigfoot.com tneff@bigfoot.com
Wed, 10 Oct 2001 18:47:05 -0400


I will leave the ethical debates to others, but if you need to clean up 
messages before approving them through, this patch lets you do it.  Works 
for me on 2.0.6 which means I can finally upgrade from my patched 1.1 on 
lists that need it. :)

*** Mailman/Cgi/admindb.py.orig Wed Oct 10 13:31:46 2001
--- Mailman/Cgi/admindb.py      Wed Oct 10 18:30:25 2001
***************
*** 270,275 ****
--- 270,277 ----
              continue
          # get the action comment and reasons if present
          commentkey = 'comment-%d' % request_id
+         headerskey = 'headers-%d' % request_id
+         contentskey = 'fulltext-%d' % request_id
          preservekey = 'preserve-%d' % request_id
          forwardkey = 'forward-%d' % request_id
          forwardaddrkey = 'forward-addr-%d' % request_id
***************
*** 278,285 ****
--- 280,293 ----
          preserve = 0
          forward = 0
          forwardaddr = ''
+         headers = ''
+         contents = ''
          if cgidata.has_key(commentkey):
              comment = cgidata[commentkey].value
+         if cgidata.has_key(headerskey):
+             headers = cgidata[headerskey].value
+         if cgidata.has_key(contentskey):
+             contents = cgidata[contentskey].value
          if cgidata.has_key(preservekey):
              preserve = cgidata[preservekey].value
          if cgidata.has_key(forwardkey):
***************
*** 290,296 ****
          # handle the request id
          try:
              mlist.HandleRequest(request_id, v, comment,
!                                 preserve, forward, forwardaddr)
          except (KeyError, Errors.LostHeldMessage):
              # that's okay, it just means someone else has already updated 
the
              # database, so just ignore this id
--- 298,304 ----
          # handle the request id
          try:
              mlist.HandleRequest(request_id, v, comment,
!                                 preserve, forward, forwardaddr, headers, 
contents)
          except (KeyError, Errors.LostHeldMessage):
              # that's okay, it just means someone else has already updated 
the
              # database, so just ignore this id
*** Mailman/ListAdmin.py.orig   Wed Oct 10 13:31:46 2001
--- Mailman/ListAdmin.py        Wed Oct 10 18:40:27 2001
***************
*** 122,133 ****
          return type

      def HandleRequest(self, id, value, comment=None, preserve=None,
!                       forward=None, addr=None):
          self.__opendb()
          rtype, data = self.__db[id]
          if rtype == HELDMSG:
              status = self.__handlepost(data, value, comment, preserve,
!                                        forward, addr)
          else:
              assert rtype == SUBSCRIPTION
              status = self.__handlesubscription(data, value, comment)
--- 122,133 ----
          return type

      def HandleRequest(self, id, value, comment=None, preserve=None,
!                       forward=None, addr=None, headers=None, 
contents=None):
          self.__opendb()
          rtype, data = self.__db[id]
          if rtype == HELDMSG:
              status = self.__handlepost(data, value, comment, preserve,
!                                        forward, addr, headers, contents)
          else:
              assert rtype == SUBSCRIPTION
              status = self.__handlesubscription(data, value, comment)
***************
*** 172,178 ****
          data = time.time(), sender, msgsubject, reason, filename, msgdata
          self.__db[id] = (HELDMSG, data)

!     def __handlepost(self, record, value, comment, preserve, forward, 
addr):
          # For backwards compatibility with pre 2.0beta3
          if len(record) == 5:
              ptime, sender, subject, reason, filename = record
--- 172,178 ----
          data = time.time(), sender, msgsubject, reason, filename, msgdata
          self.__db[id] = (HELDMSG, data)

!     def __handlepost(self, record, value, comment, preserve, forward, 
addr, headers, contents):
          # For backwards compatibility with pre 2.0beta3
          if len(record) == 5:
              ptime, sender, subject, reason, filename = record
***************
*** 181,186 ****
--- 181,202 ----
              # New format of record
              ptime, sender, subject, reason, filename, msgdata = record
          path = os.path.join(mm_cfg.DATA_DIR, filename)
+         # Handle editing
+         if len(headers)+len(contents):
+            fp = open(path)
+            unixfrom = fp.readline()
+            rest = fp.read()
+            # Parse headers and body
+            parts = string.split(rest,'\n\n')
+            if len(headers) == 0:
+                headers = parts[0]
+            if len(contents) == 0:
+                contents = parts[1]
+            fp.close
+            # Now write the changed result
+            fp = open(path,'w')
+            fp.write(unixfrom + headers + '\n\n' + contents)
+            fp.close
          # Handle message preservation
          if preserve:
              parts = string.split(os.path.split(path)[1], '-')