[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r22664: {projects} Parse the To: field inside requests in a saner way. (Fixes m (projects/gettor/lib/gettor)



Author: kaner
Date: 2010-07-19 15:13:49 +0000 (Mon, 19 Jul 2010)
New Revision: 22664

Modified:
   projects/gettor/lib/gettor/config.py
   projects/gettor/lib/gettor/requests.py
Log:
Parse the To: field inside requests in a saner way. (Fixes multiple To: fields)


Modified: projects/gettor/lib/gettor/config.py
===================================================================
--- projects/gettor/lib/gettor/config.py	2010-07-19 14:55:54 UTC (rev 22663)
+++ projects/gettor/lib/gettor/config.py	2010-07-19 15:13:49 UTC (rev 22664)
@@ -28,6 +28,7 @@
      delayAlert = True
      cmdPassFile = /var/lib/gettor/pass
      dumpFile = /var/lib/gettor/dump
+     defaultFrom = gettor@xxxxxxxxxxxxxx
 
  Note that you can set from none to any of these values in your config file.
  Values you dont provide will be taken from the defaults in 'useConf'.
@@ -54,6 +55,8 @@
                 that a package was selected and will be sent.
  cmdPassFile:   Where our forward command password resides
  dumpFile:      Where failed mails get stored
+ defaultFrom:   Use this email address in the From: field, if all else fails to
+                make sense
 
  If no valid config file is provided to __init__, gettorConf will try to use
  '~/.gettorrc' as default config file. If that fails, the default values from
@@ -101,7 +104,8 @@
                         "localeDir":    ("/usr/share/locale",       "global"),
                         "cmdPassFile":  ("/var/lib/gettor/cmdpass", "global"),
                         "dumpFile":     ("/var/lib/gettor/dump",    "global"),
-                        "delayAlert":   (True,                    "global")}
+                        "delayAlert":   (True,                      "global"),
+                        "defaultFrom":  ("gettor@xxxxxxxxxxxxxx",   "global")}
 
         # One ConfigParser instance to read the actual values from config
         self.config = ConfigParser.ConfigParser()
@@ -186,6 +190,9 @@
     def getDumpFile(self):
         return self.useConf["dumpFile"][0]
 
+    def getDefaultFrom(self):
+        return self.useConf["defaultFrom"][0]
+
 if __name__ == "__main__" :
     c = Config()
     print "# This is a suitable default configuration. Tune to fit your needs."

Modified: projects/gettor/lib/gettor/requests.py
===================================================================
--- projects/gettor/lib/gettor/requests.py	2010-07-19 14:55:54 UTC (rev 22663)
+++ projects/gettor/lib/gettor/requests.py	2010-07-19 15:13:49 UTC (rev 22664)
@@ -34,7 +34,6 @@
         self.cmdAddr = cmdAddr
     
 class requestMail:
-
     defaultLang = "en"
     # XXX Move this to the config file
     #                  LANG: ALIASE
@@ -68,16 +67,16 @@
         self.replyLocale = self.defaultLang
         self.replytoAddress = self.parsedMessage["Return-Path"]
         self.bounce = False
+        self.defaultFrom = self.config.getDefaultFrom()
         
         # Filter rough edges
         self.doEarlyFilter()
+        # We want to parse, log and act on the "To" field
+        self.sanitizeAndAssignToField(self.parsedMessage["to"])
 
-        # We want to parse, log and act on the "To" field
-        self.toAddress = self.parsedMessage["to"]
         log.info("User %s made request to %s" % \
                 (self.replytoAddress, self.toAddress))
         self.gotPlusReq = self.matchPlusAddress()
-
         packager = gettor.packages.Packages(config)
         self.packages = packager.getPackageList()
         assert len(self.packages) > 0, "Empty package list"
@@ -92,6 +91,17 @@
         #       except:
         #           pass
 
+    def sanitizeAndAssignToField(self, toField):
+        """Do basic santization of the To: field of the mail header
+        """
+        regexGettorMail = '.*(<)?(gettor.*@torproject.org)+(?(1)>).*'
+        match = re.match(regexGettorMail, toField)
+        if match:
+            self.toAddress= match.group(2)
+        else:
+            # Fall back to default From: address
+            self.toAddress = self.defaultFrom
+
     def parseMail(self):
         if self.parsedMessage.is_multipart():
             for part in self.parsedMessage.walk():
@@ -104,10 +114,13 @@
         if self.returnPackage is None:
             log.info("User didn't select any packages")
 
-        return RequestVal(self.toAddress, self.replytoAddress, \
-                self.replyLocale, \
-                self.returnPackage, \
-                self.splitDelivery, self.signature, self.commandAddress)
+        return RequestVal(self.toAddress, \
+                          self.replytoAddress, \
+                          self.replyLocale, \
+                          self.returnPackage, \
+                          self.splitDelivery, \
+                          self.signature, \
+                          self.commandAddress)
 
     def parseTextPart(self, text):
         text = self.stripTags(text)