diff --git a/server.py b/server.py index 70156eb..a247384 100644 --- a/server.py +++ b/server.py @@ -12,6 +12,7 @@ class Resource(object): # noinspection PyMethodMayBeStatic def on_get(self, _, resp): response = [] + count = 0 for entry in os.scandir(DATA_PATH): if entry.name == '.gitkeep': continue @@ -21,6 +22,9 @@ class Resource(object): except json.decoder.JSONDecodeError: pass os.remove(entry.path) + count += 1 + if count == 50: + break resp.body = str.encode(json.dumps(response)) resp.status = falcon.HTTP_200 diff --git a/smtpd.py b/smtpd.py index a279d54..caf4e78 100755 --- a/smtpd.py +++ b/smtpd.py @@ -18,13 +18,15 @@ class Handler(object): print('Recieved message...') # noinspection PyBroadException try: - with open(os.path.join('messages', str(uuid.uuid4().hex)), 'w') as open_file: - message = mailparser.parse_from_string(envelope.content.decode('utf-8')) + file_name = os.path.join('messages', str(uuid.uuid4().hex)) + with open(file_name, 'w') as open_file: + content = envelope.original_content.decode('utf-8').replace("\r\r\n", "\r\n") + message = mailparser.parse_from_string(content) json.dump({'mail_from': envelope.mail_from, 'rcpt_tos': envelope.rcpt_tos, 'subject': message.subject, 'body': message.body, - 'data': envelope.content.decode('utf-8'), + 'data': content, 'timestamp': datetime.utcnow().isoformat()}, open_file) return '250 OK' @@ -42,6 +44,14 @@ if __name__ == '__main__': # noinspection PyBroadException try: controller.start() - input("Running smtpd server on {}:{}\n".format(args.host, args.port)) + pid = str(os.getpid()) + pidfile = "/run/postoffice/smtpd.pid" + open(pidfile, 'w').write(pid) + print("Running smtpd server on {}:{}\n".format(args.host, args.port)) + while True: + time.sleep(3) except Exception: + print(e, file=sys.stderr) controller.stop() + finally: + os.unlink(pidfile)