forked from billzhong/inbox.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbox.py
68 lines (45 loc) · 1.86 KB
/
inbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
import argparse
import gevent
import gevent.monkey
from logbook import Logger
gevent.monkey.patch_select()
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):
log.info('Collating message from {0}'.format(mailfrom))
log.debug(dict(to=rcpttos, sender=mailfrom, body=data))
return self._handler(to=rcpttos, sender=mailfrom, 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
log.info('Starting SMTP server at {0}:{1}'.format(address, port))
server = InboxServer(self.collator, (address, port), None)
try:
asyncore.loop()
except KeyboardInterrupt:
log.info('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)