[PATCH] Unbreak getting password from UI

Sebastian Sebastian at SSpaeth.de
Mon Dec 13 17:45:42 UTC 2010


Commit 9239a2d3268e155d13c9 broke getting the password from the UI. This
unbreaks the change and adds some extended documentation and cleanups in
the functino en-passent.

Signed-Off-By: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
This one replaces the patch [PATCH 2/2] Don't fail mysteriously if we cannot get a password
Sorry if it comes through multiple times, I am on very flaky WiFi right now.
 offlineimap/imapserver.py      |    4 ++--
 offlineimap/repository/IMAP.py |   28 +++++++++++++++++++++-------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index 74f1a27..d9dd6ba 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -176,7 +176,7 @@ class IMAPServer:
         challenge = response.strip()
         ui.debug('imap', 'md5handler: got challenge %s' % challenge)
 
-        passwd = self.repos.getpassword()
+        passwd = self.getpassword()
         retval = self.username + ' ' + hmac.new(passwd, challenge).hexdigest()
         ui.debug('imap', 'md5handler: returning %s' % retval)
         return retval
@@ -184,7 +184,7 @@ class IMAPServer:
     def plainauth(self, imapobj):
         UIBase.getglobalui().debug('imap',
                                    'Attempting plain authentication')
-        imapobj.login(self.username, self.repos.getpassword())
+        imapobj.login(self.username, self.getpassword())
 
     def gssauth(self, response):
         data = base64.b64encode(response)
diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py
index 789a64c..52a8e61 100644
--- a/offlineimap/repository/IMAP.py
+++ b/offlineimap/repository/IMAP.py
@@ -152,24 +152,35 @@ class IMAPRepository(BaseRepository):
         return self.getconfboolean('expunge', 1)
 
     def getpassword(self):
-        passwd = None
-        localeval = self.localeval
+        """Return the IMAP password for this repository.
 
-        if self.config.has_option(self.getsection(), 'remotepasseval'):
-            passwd = self.getconf('remotepasseval')
-        if passwd != None:
-            return localeval.eval(passwd)
+        It tries to get passwords in the following order:
+
+        1. evaluate Repository 'remotepasseval'
+        2. read password from Repository 'remotepass'
+        3. read password from file specified in Repository 'remotepassfile'
+        4. read password from ~/.netrc
+        5. read password from /etc/netrc
 
+        On success we return the password.
+        If all strategies fail we return None.
+        """
+        # 1. evaluate Repository 'remotepasseval'
+        passwd = self.getconf('remotepasseval', None)
+        if passwd != None:
+            return self.localeval.eval(passwd)
+        # 2. read password from Repository 'remotepass'
         password = self.getconf('remotepass', None)
         if password != None:
             return password
+        # 3. read password from file specified in Repository 'remotepassfile'
         passfile = self.getconf('remotepassfile', None)
         if passfile != None:
             fd = open(os.path.expanduser(passfile))
             password = fd.readline().strip()
             fd.close()
             return password
-
+        # 4. read password from ~/.netrc
         try:
             netrcentry = netrc.netrc().authenticators(self.gethost())
         except IOError, inst:
@@ -180,6 +191,7 @@ class IMAPRepository(BaseRepository):
                 user = self.getconf('remoteuser')
                 if user == None or user == netrcentry[0]:
                     return netrcentry[2]
+        # 5. read password from /etc/netrc
         try:
             netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost())
         except IOError, inst:
@@ -190,8 +202,10 @@ class IMAPRepository(BaseRepository):
                 user = self.getconf('remoteuser')
                 if user == None or user == netrcentry[0]:
                     return netrcentry[2]
+        # no strategy yielded a password!
         return None
 
+
     def getfolder(self, foldername):
         return self.getfoldertype()(self.imapserver, foldername,
                                     self.nametrans(foldername),
-- 
1.7.1




More information about the OfflineIMAP-project mailing list