From 711050055339f6a14f0c3da4d3d28f707b97a102 Mon Sep 17 00:00:00 2001 From: Robert Tari Date: Mon, 17 Aug 2020 17:40:59 +0200 Subject: Initial port from Unity Mail --- ayatanawebmail/idler.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 ayatanawebmail/idler.py (limited to 'ayatanawebmail/idler.py') 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) -- cgit v1.2.3