aboutsummaryrefslogtreecommitdiff
path: root/ayatanawebmail/idler.py
blob: b6b63b43f59c7588bf1da778521a2dd295745d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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):

        if self.oConnection.oImap is not None:
            try:
                # Send a NOOP command to interrupt the IDLE mode and free the blocked thread
                self.oConnection.oImap.noop()
            except:
                pass
        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)