Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / mail / examples / emailserver.tac
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 # You can run this module directly with:
5 #    twistd -ny emailserver.tac
6
7 """
8 A toy email server.
9 """
10
11 from zope.interface import implements
12
13 from twisted.internet import defer
14 from twisted.mail import smtp
15 from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
16
17 from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
18 from twisted.cred.portal import IRealm
19 from twisted.cred.portal import Portal
20
21
22
23 class ConsoleMessageDelivery:
24     implements(smtp.IMessageDelivery)
25     
26     def receivedHeader(self, helo, origin, recipients):
27         return "Received: ConsoleMessageDelivery"
28
29     
30     def validateFrom(self, helo, origin):
31         # All addresses are accepted
32         return origin
33
34     
35     def validateTo(self, user):
36         # Only messages directed to the "console" user are accepted.
37         if user.dest.local == "console":
38             return lambda: ConsoleMessage()
39         raise smtp.SMTPBadRcpt(user)
40
41
42
43 class ConsoleMessage:
44     implements(smtp.IMessage)
45     
46     def __init__(self):
47         self.lines = []
48
49     
50     def lineReceived(self, line):
51         self.lines.append(line)
52
53     
54     def eomReceived(self):
55         print "New message received:"
56         print "\n".join(self.lines)
57         self.lines = None
58         return defer.succeed(None)
59
60     
61     def connectionLost(self):
62         # There was an error, throw away the stored lines
63         self.lines = None
64
65
66
67 class ConsoleSMTPFactory(smtp.SMTPFactory):
68     protocol = smtp.ESMTP
69
70     def __init__(self, *a, **kw):
71         smtp.SMTPFactory.__init__(self, *a, **kw)
72         self.delivery = ConsoleMessageDelivery()
73     
74
75     def buildProtocol(self, addr):
76         p = smtp.SMTPFactory.buildProtocol(self, addr)
77         p.delivery = self.delivery
78         p.challengers = {"LOGIN": LOGINCredentials, "PLAIN": PLAINCredentials}
79         return p
80
81
82
83 class SimpleRealm:
84     implements(IRealm)
85
86     def requestAvatar(self, avatarId, mind, *interfaces):
87         if smtp.IMessageDelivery in interfaces:
88             return smtp.IMessageDelivery, ConsoleMessageDelivery(), lambda: None
89         raise NotImplementedError()
90
91
92
93 def main():
94     from twisted.application import internet
95     from twisted.application import service    
96     
97     portal = Portal(SimpleRealm())
98     checker = InMemoryUsernamePasswordDatabaseDontUse()
99     checker.addUser("guest", "password")
100     portal.registerChecker(checker)
101     
102     a = service.Application("Console SMTP Server")
103     internet.TCPServer(2500, ConsoleSMTPFactory(portal)).setServiceParent(a)
104     
105     return a
106
107 application = main()