Advertisement
Guest User

Untitled

a guest
Nov 6th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. from twisted.mail import smtp
  2. from zope.interface import implements
  3. from twisted.internet import reactor, defer
  4.  
  5. class FileMessage(object):
  6.     implements(smtp.IMessage)
  7.  
  8.     def __init__(self, fileObj):
  9.         self.fileObj = fileObj
  10.         self.msg = []
  11.         self.envelope_recipients = []
  12.  
  13.     def lineReceived(self, line):
  14.         self.fileObj.write(line + '\n')
  15.         self.msg.append(line)
  16.        
  17.     def eomReceived(self):
  18.         self.fileObj.close()
  19.         print "\n".join(self.msg)
  20.         print self.envelope_recipients
  21.         return defer.succeed(None)
  22.        
  23.     def connectionLost(self):
  24.         self.fileObj.close()
  25.         os.remove(self.fileObj.name)
  26.  
  27.     def add_envelope_recipient(self, user):
  28.         self.envelope_recipients.append(user)
  29.  
  30.  
  31. class MUMessageDelivery(object):
  32.     implements(smtp.IMessageDelivery)
  33.  
  34.     def __init__(self):
  35.         import time
  36.         fileName = 'smtplog.' + str(int(time.time()))
  37.         self.fm = FileMessage(file(fileName, 'w'))
  38.  
  39.     def receivedHeader(self, helo, origin, recipients):
  40.         return ''
  41.  
  42.     def validateFrom(self, helo, origin):
  43.         return origin
  44.  
  45.     def validateTo(self, user):
  46.        
  47.         self.fm.add_envelope_recipient(user.dest)
  48.        
  49.         def serve_file_message():
  50.             return self.fm
  51.          
  52.         return serve_file_message
  53.  
  54.  
  55. class MUMessageDeliveryFactory(object):
  56.     implements(smtp.IMessageDeliveryFactory)
  57.     def getMessageDelivery(self):
  58.         return MUMessageDelivery()
  59.  
  60. class MU_ESMTP(smtp.ESMTP):
  61.  
  62.     def _cbToValidate(self, to, user=None, code=250, msg='Recipient address accepted'):
  63.         if user is None:
  64.             user = to
  65.         if len(self._to) == 0:
  66.             self._to.append((user, to))
  67.         self.sendCode(code, msg)
  68.  
  69. class MU_ESMTPFactory(smtp.SMTPFactory):
  70.        
  71.         protocol = MU_ESMTP
  72.  
  73.         def buildProtocol(self, addr):
  74.             proto = smtp.SMTPFactory.buildProtocol(self, addr)
  75.             proto.deliveryFactory = MUMessageDeliveryFactory()
  76.             return proto
  77.  
  78. reactor.listenTCP(8123, MU_ESMTPFactory())
  79. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement