- adding some debugout witch hopefully helps to find errors with loggin in to differe...
[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.internet import stdio
6 from twisted.mail import imap4
7 from twisted.protocols import basic
8
9 #from twisted.python import log
10 #log.startLogging(open("/tmp/twisted.log","w"))
11
12 class SimpleIMAP4Client(imap4.IMAP4Client):
13     greetDeferred = None
14     def __init__(self,e2session, contextFac = None):
15         self.e2session = e2session
16         imap4.IMAP4Client.__init__(self,contextFactory = contextFac)
17         
18     def serverGreeting(self, caps):
19         print "serverGreeting",caps
20         self.serverCapabilities = caps
21         if self.greetDeferred is not None:
22             d, self.greetDeferred = self.greetDeferred, None
23             d.callback(self)
24
25 class SimpleIMAP4ClientFactory(protocol.ClientFactory):
26     
27     protocol = SimpleIMAP4Client
28
29     def __init__(self, e2session, username, onConn,factory):
30         self.ctx = factory
31         self.e2session = e2session
32         self.username = username
33         self.onConn = onConn
34
35     def buildProtocol(self, addr):
36         print "building protocol",addr
37         p = self.protocol(self.e2session,contextFac = self.ctx)
38         p.factory = self
39         p.greetDeferred = self.onConn
40         auth = imap4.CramMD5ClientAuthenticator(self.username)
41         p.registerAuthenticator(auth)
42         return p
43     
44     def clientConnectionFailed(self, connector, reason):
45         d, self.onConn = self.onConn, None
46         d.errback(reason)
47
48 def createFactory( e2session,username,hostname, port):
49     print "creating factory for ",username,hostname,port
50     onConn = defer.Deferred(
51         ).addCallback(e2session.onConnect
52         ).addErrback(e2session.onConnectFailed
53         )
54
55     f2 = ssl.ClientContextFactory()
56     factory = SimpleIMAP4ClientFactory(e2session,username, onConn,f2)
57     if port == 993:
58         reactor.connectSSL( hostname, port,factory,f2)
59     else:
60         reactor.connectTCP( hostname, port,factory)
61
62     print "factory started"