replaced deprecated ConfigEnableDisable by ConfigOnOff
[enigma2-plugins.git] / emailclient / src / protocol.py
1 from twisted.internet import reactor, protocol, ssl
2 from twisted.mail import imap4
3
4 # from twisted.internet import defer
5 # from twisted.python import log
6 # log.startLogging(open("/tmp/twisted.log","w"))
7 # defer.setDebugging(True)
8 from . import debug #@UnresolvedImport # pylint: disable-msg=F0401
9
10 class SimpleIMAP4Client(imap4.IMAP4Client):
11         greetDeferred = None
12         def serverGreeting(self, caps):
13                 debug("[SimpleIMAP4Client] serverGreeting: %s" %caps)
14                 self.serverCapabilities = caps
15                 if self.greetDeferred is not None:
16                         self.greetDeferred(self)
17
18 class SimpleIMAP4ClientFactory(protocol.ReconnectingClientFactory):
19         
20         protocol = SimpleIMAP4Client
21
22         def __init__(self, e2session, username, factory):
23                 self.maxDelay = 30
24                 self.noisy = True
25                 self.ctx = factory
26                 self.e2session = e2session
27                 self.username = username
28
29         def buildProtocol(self, addr):
30                 debug("[SimpleIMAP4ClientFactory] building protocol: %s" %addr)
31                 pr = self.protocol(contextFactory = self.ctx)
32                 pr.factory = self
33                 pr.greetDeferred = self.e2session.onConnect
34                 auth = imap4.CramMD5ClientAuthenticator(self.username)
35                 pr.registerAuthenticator(auth)
36                 return pr
37
38         def startedConnecting(self, connector):
39                 debug("[SimpleIMAP4ClientFactory] startedConnecting")
40
41         def clientConnectionFailed(self, connector, reason):
42                 # debug("[SimpleIMAP4ClientFactory] clientConnectionFailed: %s" %reason.getErrorMessage())
43                 self.e2session.onConnectionFailed(reason)
44                 protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
45
46         def clientConnectionLost(self, connector, reason):
47                 # debug("[SimpleIMAP4ClientFactory] clientConnectionLost: %s" %reason.getErrorMessage())
48                 self.e2session.onConnectionLost(reason)
49                 protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
50
51 def createFactory(e2session, username, hostname, port):
52         debug("createFactory: for %s@%s:%s" %(username, hostname, port))
53
54         f2 = ssl.ClientContextFactory()
55         factory = SimpleIMAP4ClientFactory(e2session, username, f2)
56         if port == 993:
57                 reactor.connectSSL(hostname, port, factory, f2) #@UndefinedVariable # pylint: disable-msg=E1101
58         else:
59                 reactor.connectTCP(hostname, port, factory) #@UndefinedVariable # pylint: disable-msg=E1101
60
61         debug("createFactory: factory started")
62         return factory