better error handling, does not crash any more on loss of connection to mail server
[enigma2-plugins.git] / emailclient / src / protocol.py
1 from twisted.internet import reactor
2 from twisted.internet import protocol
3 from twisted.internet import ssl
4 from twisted.internet import defer
5 from twisted.mail import imap4
6
7 from Tools import Notifications
8 from Screens.MessageBox import MessageBox
9 from twisted.internet.protocol import ReconnectingClientFactory
10
11 import time
12 # from twisted.python import log
13 # log.startLogging(open("/tmp/twisted.log","w"))
14 from . import debug
15 # defer.setDebugging(True)
16
17 class SimpleIMAP4Client(imap4.IMAP4Client):
18         greetDeferred = None
19         def __init__(self,e2session, contextFac = None):
20                 self.e2session = e2session
21                 imap4.IMAP4Client.__init__(self,contextFactory = contextFac)
22                 
23         def serverGreeting(self, caps):
24                 debug("[SimpleIMAP4Client] serverGreeting: %s" %caps)
25                 self.serverCapabilities = caps
26                 if self.greetDeferred is not None:
27                         self.greetDeferred(self)
28
29 class SimpleIMAP4ClientFactory(protocol.ReconnectingClientFactory):
30         
31         protocol = SimpleIMAP4Client
32
33         def __init__(self, e2session, username, factory):
34                 self.maxDelay = 30
35                 self.noisy = True
36                 self.ctx = factory
37                 self.e2session = e2session
38                 self.username = username
39
40         def buildProtocol(self, addr):
41                 debug("[SimpleIMAP4ClientFactory] building protocol: %s" %addr)
42                 p = self.protocol(self.e2session,contextFac = self.ctx)
43                 p.factory = self
44                 p.greetDeferred = self.e2session.onConnect
45                 auth = imap4.CramMD5ClientAuthenticator(self.username)
46                 p.registerAuthenticator(auth)
47                 return p
48
49         def startedConnecting(self, connector):
50                 debug("[SimpleIMAP4ClientFactory] startedConnecting: %s" %time.ctime())
51
52         def clientConnectionFailed(self, connector, reason):
53                 debug("[SimpleIMAP4ClientFactory] clientConnectionFailed: %s" %reason.getErrorMessage())
54                 self.e2session.onConnectFailed(reason)
55                 ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
56
57         def clientConnectionLost(self, connector, reason):
58                 debug("[SimpleIMAP4ClientFactory] clientConnectionLost: %s" %reason.getErrorMessage())
59                 self.e2session.onConnectFailed(reason)
60                 ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
61
62 def createFactory( e2session,username,hostname, port):
63         debug("createFactory: for %s@%s:%s at %s" %(username,hostname,port, "time: %s" %time.ctime()))
64
65         f2 = ssl.ClientContextFactory()
66         factory = SimpleIMAP4ClientFactory(e2session, username, f2)
67         if port == 993:
68                 c = reactor.connectSSL( hostname, port,factory,f2)
69         else:
70                 c = reactor.connectTCP( hostname, port,factory)
71
72         debug("createFactory: factory started")
73         return factory