[PATCH 06/13] Recognize configuration for idlefolders

Ethan Glasser-Camp ethan at betacantrips.com
Tue Mar 8 15:05:19 GMT 2011


This code was originally by James Bunton <jamesbunton at fastmail.fm>.

Signed-off-by: Ethan Glasser-Camp <ethan at betacantrips.com>
---
 offlineimap.conf               |   13 +++++++++++++
 offlineimap/imapserver.py      |    6 +++++-
 offlineimap/repository/IMAP.py |   12 +++++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/offlineimap.conf b/offlineimap.conf
index d3be8be..8204bd0 100644
--- a/offlineimap.conf
+++ b/offlineimap.conf
@@ -334,6 +334,19 @@ remoteuser = username
 #
 # reference = Mail
 
+# In between synchronisations, OfflineIMAP can monitor mailboxes for new
+# messages using the IDLE command. If you want to enable this, specify here
+# the folders you wish to monitor. Note that the IMAP protocol requires a
+# separate connection for each folder monitored in this way, so setting
+# this option will force settings for:
+#     maxconnections - to be at least the number of folders you give
+#     holdconnectionopen - to be true
+#     keepalive - to be 29 minutes unless you specify otherwise
+# This option should return a Python list. For example
+#
+# idlefolders = ['INBOX', 'INBOX.Alerts']
+#
+
 # OfflineIMAP can use multiple connections to the server in order
 # to perform multiple synchronization actions simultaneously.
 # This may place a higher burden on the server.  In most cases,
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index a5c1a65..d255b3e 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -104,7 +104,7 @@ class IMAPServer:
                  username = None, password = None, hostname = None,
                  port = None, ssl = 1, maxconnections = 1, tunnel = None,
                  reference = '""', sslclientcert = None, sslclientkey = None,
-                 sslcacertfile= None):
+                 sslcacertfile = None, idlefolders = []):
         self.ui = getglobalui()
         self.reposname = reposname
         self.config = config
@@ -133,6 +133,7 @@ class IMAPServer:
         self.semaphore = BoundedSemaphore(self.maxconnections)
         self.connectionlock = Lock()
         self.reference = reference
+        self.idlefolders = idlefolders
         self.gss_step = self.GSS_STATE_STEP
         self.gss_vc = None
         self.gssapi = False
@@ -419,6 +420,7 @@ class ConfigedIMAPServer(IMAPServer):
             sslclientkey = self.repos.getsslclientkey()
             sslcacertfile = self.repos.getsslcacertfile()
         reference = self.repos.getreference()
+        idlefolders = self.repos.getidlefolders()
         server = None
         password = None
         
@@ -430,6 +432,7 @@ class ConfigedIMAPServer(IMAPServer):
             IMAPServer.__init__(self, self.config, self.repos.getname(),
                                 tunnel = usetunnel,
                                 reference = reference,
+                                idlefolders = idlefolders,
                                 maxconnections = self.repos.getmaxconnections())
         else:
             if not password:
@@ -438,6 +441,7 @@ class ConfigedIMAPServer(IMAPServer):
                                 user, password, host, port, ssl,
                                 self.repos.getmaxconnections(),
                                 reference = reference,
+                                idlefolders = idlefolders,
                                 sslclientcert = sslclientcert,
                                 sslclientkey = sslclientkey,
                                 sslcacertfile = sslcacertfile)
diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py
index 3bfa5db..cc8dd57 100644
--- a/offlineimap/repository/IMAP.py
+++ b/offlineimap/repository/IMAP.py
@@ -74,9 +74,13 @@ class IMAPRepository(BaseRepository):
         self.imapserver.close()
 
     def getholdconnectionopen(self):
+        if self.getidlefolders():
+            return 1
         return self.getconfboolean("holdconnectionopen", 0)
 
     def getkeepalive(self):
+        if self.getidlefolders():
+            return 29*60
         return self.getconfint("keepalive", 0)
 
     def getsep(self):
@@ -148,8 +152,14 @@ class IMAPRepository(BaseRepository):
     def getreference(self):
         return self.getconf('reference', '""')
 
+    def getidlefolders(self):
+        localeval = self.localeval
+        return localeval.eval(self.getconf('idlefolders', '[]'))
+
     def getmaxconnections(self):
-        return self.getconfint('maxconnections', 1)
+        num1 = len(self.getidlefolders())
+        num2 = self.getconfint('maxconnections', 1)
+        return max(num1, num2)
 
     def getexpunge(self):
         return self.getconfboolean('expunge', 1)
-- 
1.7.1





More information about the OfflineIMAP-project mailing list