Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (47 sloc) 1.97 KB
# -*- coding: utf-8 -*-
import argparse
from email.parser import Parser
import gevent
import gevent.monkey
from logbook import Logger
import smtpd
import asyncore
log = Logger(__name__)
class InboxServer(smtpd.SMTPServer, object):
"""Logging-enabled SMTPServer instance with handler support."""
def __init__(self, handler, *args, **kwargs):
super(InboxServer, self).__init__(*args, **kwargs)
self._handler = handler
def process_message(self, peer, mailfrom, rcpttos, data):'Collating message from {0}'.format(mailfrom))
subject = Parser().parsestr(data)['subject']
log.debug(dict(to=rcpttos, sender=mailfrom, subject=subject, body=data))
return self._handler(to=rcpttos, sender=mailfrom, subject=subject, body=data)
class Inbox(object):
"""A simple SMTP Inbox."""
def __init__(self, port=None, address=None):
self.port = port
self.address = address
self.collator = None
def collate(self, collator):
"""Function decorator. Used to specify inbox handler."""
self.collator = collator
return collator
def serve(self, port=None, address=None):
"""Serves the SMTP server on the given port and address."""
port = port or self.port
address = address or self.address'Starting SMTP server at {0}:{1}'.format(address, port))
server = InboxServer(self.collator, (address, port), None)
except KeyboardInterrupt:'Cleaning up')
def dispatch(self):
"""Command-line dispatch."""
parser = argparse.ArgumentParser(description='Run an Inbox server.')
parser.add_argument('addr', metavar='addr', type=str, help='addr to bind to')
parser.add_argument('port', metavar='port', type=int, help='port to bind to')
args = parser.parse_args()
self.serve(port=args.port, address=args.addr)