[PATCH v4] Throw OfflineImapError when we try to request an inexistant message

Sebastian Spaeth Sebastian at SSpaeth.de
Fri Jun 24 15:46:24 BST 2011

During a sync run, someone might remove or move IMAP messages. As we
only cache the list of UIDs in the beginning, we might be requesting
UIDs that don't exist anymore. Protect folder.IMAP.getmessage() against
the response that we get when we ask for unknown UIDs.

Also, if the server responds with anything else than "OK", (eg. Gmail
seems to be saying frequently ['NO', 'Dave I can't let you do that now']
:-) so we should also be throwing OfflineImapErrors here rather than

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
 offlineimap/imapserver.py |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index 10492cd..85eb3df 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -285,22 +285,43 @@ class IMAPServer:
+            severity = OfflineImapError.ERROR.REPO
             if type(e) == gaierror:
                 #DNS related errors. Abort Repo sync
-                severity = OfflineImapError.ERROR.REPO
                 #TODO: special error msg for e.errno == 2 "Name or service not known"?
                 reason = "Could not resolve name '%s' for repository "\
                          "'%s'. Make sure you have configured the ser"\
                          "ver name correctly and that you are online."\
                          % (self.hostname, self.reposname)
                 raise OfflineImapError(reason, severity)
+            elif SSLError and isinstance(e, SSLError) and e.errno == 1:
+                # SSL unknown protocol error
+                # happens e.g. when connecting via SSL to a non-SSL service
+                if self.port != 443:
+                    reason = "Could not connect via SSL to host '%s' and non-s"\
+                        "tandard ssl port %d configured. Make sure you connect"\
+                        " to the correct port." % (self.hostname, self.port)
+                else:
+                    reason = "Unknown SSL protocol connecting to host '%s' for"\
+                         "repository '%s'. OpenSSL responded:\n%s"\
+                         % (self.hostname, self.reposname, e)
+                raise OfflineImapError(reason, severity)
+            elif isinstance(e, socket.error) and e.args[0] == errno.ECONNREFUSED:
+                # "Connection refused", can be a non-existing port, or an unauthorized
+                # webproxy (open WLAN?)
+                reason = "Connection to host '%s:%d' for repository '%s' was "\
+                    "refused. Make sure you have the right host and port "\
+                    "configured and that you are actually able to access the "\
+                    "network." % (self.hostname, self.port, self.reposname)
+                raise OfflineImapError(reason, severity)
             # Could not acquire connection to the remote;
             # socket.error(last_error) raised
             if str(e)[:24] == "can't open socket; error":
                 raise OfflineImapError("Could not connect to remote server '%s' "\
                     "for repository '%s'. Remote does not answer."
-                    % (self.hostname, self.reposname),
-                    OfflineImapError.ERROR.REPO)
+                    % (self.hostname, self.reposname), severity)
                 # re-raise all other errors

More information about the OfflineIMAP-project mailing list