adding emailclient
[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 class SimpleIMAP4Client(imap4.IMAP4Client):
10     greetDeferred = None
11     def __init__(self,e2session, contextFactory = None):
12         self.e2session = e2session
13         imap4.IMAP4Client.__init__(self,contextFactory = contextFactory)
14         
15     def serverGreeting(self, caps):
16         self.serverCapabilities = caps
17         if self.greetDeferred is not None:
18             d, self.greetDeferred = self.greetDeferred, None
19             d.callback(self)
20
21 class SimpleIMAP4ClientFactory(protocol.ClientFactory):
22     
23     protocol = SimpleIMAP4Client
24
25     def __init__(self, e2session, username, onConn):
26         self.ctx = ssl.ClientContextFactory()
27         self.e2session = e2session
28         self.username = username
29         self.onConn = onConn
30
31     def buildProtocol(self, addr):
32         p = self.protocol(self.e2session,self.ctx)
33         p.factory = self
34         p.greetDeferred = self.onConn
35         auth = imap4.CramMD5ClientAuthenticator(self.username)
36         p.registerAuthenticator(auth)
37         return p
38     
39     def clientConnectionFailed(self, connector, reason):
40         d, self.onConn = self.onConn, None
41         d.errback(reason)
42
43 def createFactory( e2session,username,hostname, port):
44     print "creating factory for ",username,hostname,port
45     onConn = defer.Deferred(
46         ).addCallback(e2session.onConnect
47         ).addErrback(e2session.onConnectFailed
48         )
49
50     factory = SimpleIMAP4ClientFactory(e2session,username, onConn)
51     reactor.connectTCP(hostname, port, factory)
52