View difference between Paste ID: 6nFXiTgK and mbRztuid
SHOW: | | - or go back to the newest paste.
1
from twisted.mail import smtp
2
from zope.interface import implements
3
from twisted.internet import reactor, defer
4
5
class NullMessage(object):
6
    implements(smtp.IMessage)
7
8
    def lineReceived(self, line):
9
        pass
10
11
    def eomReceived(self):
12
        return defer.succeed(None)
13
14
    def connectionLost(self):
15
        pass
16
17
class FileMessage(object):
18
    implements(smtp.IMessage)
19
20
    def __init__(self, fileObj):
21
        self.fileObj = fileObj
22
        self.msg = []
23
        self.envelope_recipients = []
24
25
    def lineReceived(self, line):
26
        self.fileObj.write(line + '\n')
27
        self.msg.append(line)
28
       
29
    def eomReceived(self):
30
        self.fileObj.close()
31
        print "\n".join(self.msg)
32
        print self.envelope_recipients
33
        return defer.succeed(None)
34
       
35
    def connectionLost(self):
36
        self.fileObj.close()
37
        os.remove(self.fileObj.name)
38
39
    def add_envelope_recipient(self, user):
40
        self.envelope_recipients.append(user)
41
42
43
class MUMessageDelivery(object):
44
    implements(smtp.IMessageDelivery)
45
46-
        
46+
47-
        self.fm.add_envelope_recipient(user.dest)
47+
48-
        
48+
49-
        def serve_file_message():
49+
50-
            return self.fm
50+
51-
         
51+
	self.to = []
52-
        return serve_file_message
52+
53
    def receivedHeader(self, helo, origin, recipients):
54
        return ''
55
56
    def validateFrom(self, helo, origin):
57
        return origin
58
59
    def validateTo(self, user):
60-
class MU_ESMTP(smtp.ESMTP):
60+
	self.fm.add_envelope_recipient(user.dest)
61
      	if self.to:
62-
    def _cbToValidate(self, to, user=None, code=250, msg='Recipient address accepted'):
62+
            self.to.append(user)
63-
        if user is None:
63+
            return NullMessage
64-
            user = to
64+
        else:
65-
        if len(self._to) == 0:
65+
            self.to = [user]
66-
            self._to.append((user, to))
66+
   	    return self.fm  
67-
        self.sendCode(code, msg) 
67+
68
class MUMessageDeliveryFactory(object):
69
    implements(smtp.IMessageDeliveryFactory)
70
    def getMessageDelivery(self):
71
        return MUMessageDelivery()
72
73
74
class MU_ESMTPFactory(smtp.SMTPFactory):
75
		
76
        protocol = MU_ESMTP
77
78
        def buildProtocol(self, addr):
79
            proto = smtp.SMTPFactory.buildProtocol(self, addr)
80
            proto.deliveryFactory = MUMessageDeliveryFactory()
81
            return proto
82
83
reactor.listenTCP(8123, MU_ESMTPFactory())
84
reactor.run()