aboutsummaryrefslogtreecommitdiff
path: root/ayatanawebmail/idler.py
diff options
context:
space:
mode:
authorRobert Tari <robert@tari.in>2020-08-17 17:40:59 +0200
committerRobert Tari <robert@tari.in>2020-08-17 17:40:59 +0200
commit711050055339f6a14f0c3da4d3d28f707b97a102 (patch)
tree7c69fbc7809bc1592bab47c811c6d9e4ddf964da /ayatanawebmail/idler.py
downloadayatana-webmail-711050055339f6a14f0c3da4d3d28f707b97a102.tar.gz
ayatana-webmail-711050055339f6a14f0c3da4d3d28f707b97a102.tar.bz2
ayatana-webmail-711050055339f6a14f0c3da4d3d28f707b97a102.zip
Initial port from Unity Mail
Diffstat (limited to 'ayatanawebmail/idler.py')
-rwxr-xr-xayatanawebmail/idler.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/ayatanawebmail/idler.py b/ayatanawebmail/idler.py
new file mode 100755
index 0000000..8f956ea
--- /dev/null
+++ b/ayatanawebmail/idler.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+import time
+from threading import *
+
+class Idler(object):
+
+ def __init__(self, oConnection, fnCallback, oLogger):
+
+ self.oThread = Thread(target=self.idle)
+ self.oConnection = oConnection
+ self.oEvent = Event()
+ self.fnCallback = fnCallback
+ self.bNeedSync = False
+ self.oLogger = oLogger
+ self.bAborted = False
+
+ def start(self):
+
+ self.oThread.start()
+
+ def stop(self):
+
+ self.oEvent.set()
+
+ def join(self):
+
+ self.oThread.join()
+
+ def idle(self):
+
+ while True:
+
+ if self.oEvent.isSet():
+ break
+
+ self.bNeedSync = False
+ self.bAborted = False
+
+ def callback(lstArgs):
+
+ if (lstArgs[2] != None) and (lstArgs[2][0] is self.oConnection.oImap.abort):
+
+ self.oLogger.info('"{0}:{1}" has been closed by the server.'.format(self.oConnection.strLogin, self.oConnection.strFolder))
+ self.bAborted = True
+
+ else:
+
+ self.bNeedSync = True
+
+ # We may need to skip the condition
+ # if not self.oEvent.isSet():
+ self.oEvent.set()
+
+ while not self.oConnection.isOpen():
+
+ self.oLogger.info('"{0}:{1}" IDLE is waiting for a connection.'.format(self.oConnection.strLogin, self.oConnection.strFolder))
+ time.sleep(10)
+
+ self.oConnection.oImap.idle(callback=callback, timeout=600)
+ self.oEvent.wait()
+
+ if self.bNeedSync:
+
+ self.oEvent.clear()
+ self.fnCallback(self.oConnection, False)
+
+ try:
+
+ if self.oConnection.oImap != None:
+ self.oConnection.oImap.noop()
+
+ except:
+
+ pass
+
+ if self.bAborted:
+ self.fnCallback(self.oConnection, True)