[Python-checkins] python/dist/src/Lib smtplib.py,1.58,1.59

pierslauder@users.sourceforge.net pierslauder@users.sourceforge.net
Fri, 26 Jul 2002 17:38:32 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv7244/dist/src/Lib

Modified Files:
	smtplib.py 
Log Message:
remove o/s dependancy from test

Index: smtplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/smtplib.py,v
retrieving revision 1.58
retrieving revision 1.59
diff -C2 -d -r1.58 -r1.59
*** smtplib.py	3 Jun 2002 15:58:32 -0000	1.58
--- smtplib.py	27 Jul 2002 00:38:30 -0000	1.59
***************
*** 47,50 ****
--- 47,51 ----
  import base64
  import hmac
+ from email.base64MIME import encode as encode_base64
  
  __all__ = ["SMTPException","SMTPServerDisconnected","SMTPResponseException",
***************
*** 56,59 ****
--- 57,62 ----
  CRLF="\r\n"
  
+ OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I)
+ 
  # Exception classes used by this module.
  class SMTPException(Exception):
***************
*** 400,403 ****
--- 403,419 ----
          del resp[0]
          for each in resp:
+             # To be able to communicate with as many SMTP servers as possible,
+             # we have to take the old-style auth advertisement into account,
+             # because:
+             # 1) Else our SMTP feature parser gets confused.
+             # 2) There are some servers that only advertise the auth methods we
+             #    support using the old style.
+             auth_match = OLDSTYLE_AUTH.match(each)
+             if auth_match:
+                 # This doesn't remove duplicates, but that's no problem
+                 self.esmtp_features["auth"] = self.esmtp_features.get("auth", "") \
+                         + " " + auth_match.groups(0)[0]
+                 continue
+ 
              # RFC 1869 requires a space between ehlo keyword and parameters.
              # It's actually stricter, in that only spaces are allowed between
***************
*** 408,412 ****
                  feature=m.group("feature").lower()
                  params=m.string[m.end("feature"):].strip()
!                 self.esmtp_features[feature]=params
          return (code,msg)
  
--- 424,432 ----
                  feature=m.group("feature").lower()
                  params=m.string[m.end("feature"):].strip()
!                 if feature == "auth":
!                     self.esmtp_features[feature] = self.esmtp_features.get(feature, "") \
!                             + " " + params
!                 else:
!                     self.esmtp_features[feature]=params
          return (code,msg)
  
***************
*** 507,518 ****
              challenge = base64.decodestring(challenge)
              response = user + " " + hmac.HMAC(password, challenge).hexdigest()
!             return base64.encodestring(response)[:-1]
  
          def encode_plain(user, password):
!             return base64.encodestring("%s\0%s\0%s" %
!                                        (user, user, password))[:-1]
  
          AUTH_PLAIN = "PLAIN"
          AUTH_CRAM_MD5 = "CRAM-MD5"
  
          if self.helo_resp is None and self.ehlo_resp is None:
--- 527,539 ----
              challenge = base64.decodestring(challenge)
              response = user + " " + hmac.HMAC(password, challenge).hexdigest()
!             return encode_base64(response, eol="")
  
          def encode_plain(user, password):
!             return encode_base64("%s\0%s\0%s" % (user, user, password), eol="")
!  
  
          AUTH_PLAIN = "PLAIN"
          AUTH_CRAM_MD5 = "CRAM-MD5"
+         AUTH_LOGIN = "LOGIN"
  
          if self.helo_resp is None and self.ehlo_resp is None:
***************
*** 531,536 ****
          # less preferred methods. Except for the purpose of testing the weaker
          # ones, we prefer stronger methods like CRAM-MD5:
!         preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN]
!         #preferred_auths = [AUTH_PLAIN, AUTH_CRAM_MD5]
  
          # Determine the authentication method we'll use
--- 552,556 ----
          # less preferred methods. Except for the purpose of testing the weaker
          # ones, we prefer stronger methods like CRAM-MD5:
!         preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
  
          # Determine the authentication method we'll use
***************
*** 540,544 ****
                  authmethod = method
                  break
-         if self.debuglevel > 0: print "AuthMethod:", authmethod
  
          if authmethod == AUTH_CRAM_MD5:
--- 560,563 ----
***************
*** 551,554 ****
--- 570,579 ----
              (code, resp) = self.docmd("AUTH",
                  AUTH_PLAIN + " " + encode_plain(user, password))
+         elif authmethod == AUTH_LOGIN:
+             (code, resp) = self.docmd("AUTH",
+                 "%s %s" % (AUTH_LOGIN, encode_base64(user, eol="")))
+             if code != 334:
+                 raise SMTPAuthenticationError(code, resp)
+             (code, resp) = self.docmd(encode_base64(user, eol=""))
          elif authmethod is None:
              raise SMTPException("No suitable authentication method found.")