1 from twisted.internet import reactor, protocol, ssl
2 from twisted.mail import imap4
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
10 class SimpleIMAP4Client(imap4.IMAP4Client):
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)
18 class SimpleIMAP4ClientFactory(protocol.ReconnectingClientFactory):
20 protocol = SimpleIMAP4Client
22 def __init__(self, e2session, username, factory):
26 self.e2session = e2session
27 self.username = username
29 def buildProtocol(self, addr):
30 debug("[SimpleIMAP4ClientFactory] building protocol: %s" %addr)
31 pr = self.protocol(contextFactory = self.ctx)
33 pr.greetDeferred = self.e2session.onConnect
34 auth = imap4.CramMD5ClientAuthenticator(self.username)
35 pr.registerAuthenticator(auth)
38 def startedConnecting(self, connector):
39 debug("[SimpleIMAP4ClientFactory] startedConnecting")
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)
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)
51 def createFactory(e2session, username, hostname, port):
52 debug("createFactory: for %s@%s:%s" %(username, hostname, port))
54 f2 = ssl.ClientContextFactory()
55 factory = SimpleIMAP4ClientFactory(e2session, username, f2)
57 reactor.connectSSL(hostname, port, factory, f2) #@UndefinedVariable # pylint: disable-msg=E1101
59 reactor.connectTCP(hostname, port, factory) #@UndefinedVariable # pylint: disable-msg=E1101
61 debug("createFactory: factory started")