Advertisement
Guest User

Untitled

a guest
Nov 6th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     def __init__(self):
  47.         import time
  48.         fileName = 'smtplog.' + str(int(time.time()))
  49.         self.fm = FileMessage(file(fileName, 'w'))
  50.  
  51.     self.to = []
  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.     self.fm.add_envelope_recipient(user.dest)
  61.         if self.to:
  62.             self.to.append(user)
  63.             return NullMessage
  64.         else:
  65.             self.to = [user]
  66.         return self.fm  
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement