Update of /cvsroot/tmda/tmda/TMDA
In directory usw-pr-cvs1:/tmp/cvs-serv21645/TMDA

Modified Files:
        ChangeLog Deliver.py MTA.py MessageLogger.py Util.py 
Log Message:
Replace use of the rfc822 module with the email module.

The goal of this first revision is simply to replace rfc822, with no
changes in functionality or behavior.


Index: ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/ChangeLog,v
retrieving revision 1.230
retrieving revision 1.231
diff -u -r1.230 -r1.231
--- ChangeLog   27 Sep 2002 04:31:25 -0000      1.230
+++ ChangeLog   30 Sep 2002 23:45:54 -0000      1.231
@@ -1,3 +1,21 @@
+2002-09-30  Jason R. Mastaler  <[EMAIL PROTECTED]>
+
+       * Util.py (sendmail): Accept a whole message string rather than
+       header and body strings as two separate arguments.
+
+       (body_as_raw_string): New function.
+       (headers_as_list): Ditto.
+       (headers_as_raw_string): Ditto.
+       (headers_as_string): Ditto.
+       (rename_headers): Ditto.
+       
+       * Deliver.py: Use an email.Message object rather than an
+       rfc822.Message object and message body.
+
+       * MTA.py: Ditto.
+
+       * MessageLogger.py: Ditto.
+
 2002-09-26  Tim Legant  <[EMAIL PROTECTED]>
 
        * FilterParser.py (FilterParser.__expandmacros): Fixed bug where

Index: Deliver.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Deliver.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Deliver.py  28 Feb 2002 20:38:10 -0000      1.5
+++ Deliver.py  30 Sep 2002 23:45:54 -0000      1.6
@@ -52,18 +52,15 @@
 
 
 class Deliver:
-    def __init__(self, headers, body, delivery_option):
+    def __init__(self, msg, delivery_option):
         """
-        headers is an rfc822.Message instance.
-
-        body is the message content from that instance.
+        msg is an email.Message object.
 
         deliver_option is a delivery action option string returned
         from the TMDA.FilterParser instance.
         """
-        self.headers = headers
-        self.body = body
-        self.message = str(headers) + '\n' + body
+        self.msg = msg
+        self.message = msg.as_string()
         self.option = delivery_option
         self.env_sender = os.environ.get('SENDER')
         
@@ -115,7 +112,7 @@
         if type == 'program':
             self.__deliver_program(self.message, dest)
         elif type == 'forward':
-            self.__deliver_forward(self.headers, self.body, dest)
+            self.__deliver_forward(self.message, dest)
         elif type == 'mbox':
             # Ensure destination path exists.
             if not os.path.exists(dest):
@@ -140,9 +137,9 @@
         """Deliver message to /bin/sh -c program."""
         Util.pipecmd(program, message)
 
-    def __deliver_forward(self, headers, body, address):
+    def __deliver_forward(self, message, address):
         """Forward message to address, preserving the existing Return-Path."""
-        Util.sendmail(headers, body, address, self.env_sender)
+        Util.sendmail(message, address, self.env_sender)
         
     def __deliver_mbox(self, message, mbox):
         """Reliably deliver a mail message into an mboxrd-format mbox file.

Index: MTA.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/MTA.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- MTA.py      2 May 2002 20:27:00 -0000       1.12
+++ MTA.py      30 Sep 2002 23:45:54 -0000      1.13
@@ -50,10 +50,10 @@
     def stop(self):
         sys.exit(self.EX_OK)
 
-    def deliver(self, headers, body, instruction=None):
+    def deliver(self, msg, instruction=None):
         if instruction is None:
             instruction = Defaults.DELIVERY
-        msg = Deliver.Deliver(headers, body, instruction)
+        msg = Deliver.Deliver(msg, instruction)
         msg.deliver()
         self.stop()
             
@@ -90,13 +90,13 @@
     def stop(self):
         sys.exit(self.EX_STOP)
 
-    def deliver(self, headers, body, instruction=None):
+    def deliver(self, msg, instruction=None):
         if instruction is None:
             instruction = Defaults.DELIVERY
         if instruction == '_qok_':
             sys.exit(self.EX_OK)
         else:
-            msg = Deliver.Deliver(headers, body, instruction)
+            msg = Deliver.Deliver(msg, instruction)
             msg.deliver()
             self.stop()
 

Index: MessageLogger.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/MessageLogger.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MessageLogger.py    8 Aug 2002 16:49:46 -0000       1.1
+++ MessageLogger.py    30 Sep 2002 23:45:54 -0000      1.2
@@ -24,20 +24,22 @@
 """
 
 
+from email.Utils import parseaddr
+
 import Util
 
 
 class MessageLogger:
-    def __init__(self, logfile, headers, **vardict):
+    def __init__(self, logfile, msg, **vardict):
         """
         logfile is the full path to the logfile.
         
-        headers is an rfc822.Message instance.
+        msg in an email.Message object.
 
         vardict is a dictionary containing an indefinite number of
         keyword arguments.
         """
-        self.headers = headers
+        self.msg = msg
         self.vardict = vardict
         self.logfile = logfile
         self.log = open(self.logfile, 'a')
@@ -57,16 +59,16 @@
         self.__writeline('Date', Util.unixdate())
         envsender = self.vardict.get('envsender', None)
         if (envsender
-            and self.headers.getaddr('from')[1] != envsender):
+            and parseaddr(self.msg.get('from'))[1] != envsender):
             self.__writeline('Sndr', envsender)
-        From = self.headers.getheader('from')
+        From = self.msg.get('from')
         if From:
             self.__writeline('From', From)
-        ReplyTo = self.headers.getheader('reply-to')
+        ReplyTo = self.msg.get('reply-to')
         if ReplyTo:
             self.__writeline('Rept', ReplyTo)
         self.__writeline('To', self.vardict.get('envrecip'))
-        self.__writeline('Subj', self.headers.getheader('subject', 'None'))
+        self.__writeline('Subj', self.msg.get('subject'))
         Action = self.vardict.get('action_msg')
         sizestr = '(%s)' % self.vardict.get('msg_size')
         wsbuf = 72 - len(Action) - len(sizestr)

Index: Util.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Util.py,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -r1.69 -r1.70
--- Util.py     21 Sep 2002 00:27:43 -0000      1.69
+++ Util.py     30 Sep 2002 23:45:54 -0000      1.70
@@ -348,15 +348,12 @@
     os.spawnvp(os.P_WAIT, pager_list[0], pager_list)
 
 
-def sendmail(headers, body, envrecip, envsender):
+def sendmail(msgstr, envrecip, envsender):
     """Send e-mail via direct SMTP, or by opening a pipe to the
     sendmail program.
     
-    headers can be either an rfc822.Message instance, or a set of
-    rfc822 compatible message headers as a string.
-
-    body is the message body content as a string.
-
+    msgstr is an rfc2822 message as a string.
+    
     envrecip is the envelope recipient address.
 
     envsender is the envelope sender address.
@@ -365,17 +362,77 @@
     if Defaults.OUTGOINGMAIL == 'smtp':
         import SMTP
         server = SMTP.Connection()
-        server.sendmail(envsender, envrecip, (str(headers) + '\n' + body))
+        server.sendmail(envsender, envrecip, msgstr)
         server.quit()
     elif Defaults.OUTGOINGMAIL == 'sendmail':
         cmd = "%s -f '%s' -- '%s'" % (Defaults.SENDMAIL_PROGRAM,
                                       envsender, envrecip)
-        pipecmd(cmd, str(headers), '\n', body)
+        pipecmd(cmd, msgstr)
     else:
         raise Errors.ConfigError, \
               "Invalid OUTGOINGMAIL method: " + Defaults.OUTGOINGMAIL
 
 
+def decode_header(str):
+    """Accept a possibly encoded message header as a string, and
+    return a decoded string.
+
+    JRM: email.Header has a decode_header method, but it returns a
+    list of decoded pairs, one for each part of the header, which is
+    an awkward interface IMO, especially when the header contains a
+    mix of encoded and non-encoded parts.
+    """
+    from email import Header
+    parts = []
+    pairs = Header.decode_header(str)
+    for pair in pairs:
+        parts.append(pair[0])
+    decoded_string = ' '.join(parts)
+    return decoded_string
+
+
+def headers_as_list(msg):
+    """Return a list containing the entire set of header lines, in the
+    order in which they were read."""
+    return ['%s: %s' % (k, v) for k, v in msg.items()]
+
+
+def headers_as_string(msg):
+    """Return the (decoded) message headers as a string."""
+    return '\n'.join(['%s: %s' %
+                      (k, decode_header(v)) for k, v in msg.items()])
+
+
+def headers_as_raw_string(msg):
+    """Return the headers as a raw (undecoded) string."""
+    msgtext = msg.as_string()
+    idx = msgtext.index('\n\n')
+    return msgtext[:idx+1]
+
+
+def body_as_raw_string(msg):
+    """Return the body as a raw (undecoded) string."""
+    msgtext = msg.as_string()
+    idx = msgtext.index('\n\n')
+    return msgtext[idx+2:]
+
+
+def rename_headers(msg, old, new):
+    """Rename all occurances of a message header in a Message object.
+        
+    msg is an email.Message.Message object.
+
+    old is name of the header to rename.
+
+    new is the new name of the header
+    """
+    if msg.has_key(old):
+        for pair in msg._headers:
+            if pair[0].lower() == old.lower():
+                index = msg._headers.index(pair)
+                msg._headers[index] = (new, '%s' % pair[1])
+
+
 def build_cdb(filename):
     """Build a cdb file from a text file."""
     import cdb
@@ -447,24 +504,6 @@
     object = cPickle.load(fp)
     fp.close()
     return object
-
-
-def decode_header(str):
-    """Accept a possibly encoded message header as a string, and
-    return a decoded string.
-
-    JRM: email.Header has a decode_header method, but it returns a
-    list of decoded pairs, one for each part of the header, which is
-    an awkward interface IMO, especially when the header contains a
-    mix of encoded and non-encoded parts.
-    """
-    from email import Header
-    parts = []
-    pairs = Header.decode_header(str)
-    for pair in pairs:
-        parts.append(pair[0])
-    decoded_string = ' '.join(parts)
-    return decoded_string
 
 
 def findmatch(list, addrs):

_______________________________________
tmda-cvs mailing list
http://tmda.net/lists/listinfo/tmda-cvs

Reply via email to