diff --git a/Makefile b/Makefile index e67d7f8..bb332ff 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .POSIX: -.PHONY: test e2etest unittest crontest pre-clean clean restore-keyhome +.PHONY: test e2etest unittest crontest daemontest pre-clean clean restore-keyhome # # On systems where Python 3.x binary has a different name, just @@ -17,7 +17,7 @@ TEST_DB = test/lacre.db # # Main goal to run tests. # -test: e2etest unittest crontest +test: e2etest unittest crontest daemontest # # Run a set of end-to-end tests. @@ -42,6 +42,14 @@ crontest: clean-db $(TEST_DB) $(TEST_DB): $(PYTHON) test/schema.py $(TEST_DB) +# +# Run an e2e test of Advanced Content Filter. +# +daemontest: + $(PYTHON) test/relay.py 2500 + PYTHONPATH=`pwd` $(PYTHON) -m lacre.daemon + $(PYTHON) test/sendmail.py + # Before running the crontest goal we need to make sure that the # database gets regenerated. clean-db: diff --git a/lacre/daemon.py b/lacre/daemon.py index 2b0f3ee..1981caf 100644 --- a/lacre/daemon.py +++ b/lacre/daemon.py @@ -5,6 +5,7 @@ import lacre import lacre.config as conf import sys from aiosmtpd.controller import Controller +import asyncio # Mail status constants. # @@ -30,8 +31,9 @@ class MailEncryptionProxy: # for now, just return an error because we're not ready to handle mail for recipient, operation in gate.delivery_plan(envelope.rcpt_tos): - new_message = operation.perform(envelope.body) - gate.send_msg(new_message, [recipient]) + LOG.debug(f"Sending mail to {recipient} via {operation}") + new_message = operation.perform(envelope.content) + gate.send_msg(new_message, [recipient], envelope.mail_from) return RESULT_NOT_IMPLEMENTED @@ -39,6 +41,7 @@ class MailEncryptionProxy: def _init_controller(): proxy = MailEncryptionProxy() host, port = conf.daemon_params() + LOG.info(f"Initialising a mail Controller at {host}:{port}") return Controller(proxy, hostname=host, port=port) @@ -50,19 +53,32 @@ def _validate_config(): sys.exit(lacre.EX_CONFIG) +async def _sleep(): + while True: + await asyncio.sleep(5) + + def _main(): _validate_config() controller = _init_controller() + LOG.info("Starting the daemon...") # starts the controller in a new thread controller.start() # _this_ thread now continues operation, so it may be used to control key # and certificate cache + try: + asyncio.run(_sleep()) + except KeyboardInterrupt: + LOG.info("Finishing...") + controller.stop() + LOG.info("Done") + if __name__ == '__main__': _main() diff --git a/lacre/mailgate.py b/lacre/mailgate.py index f15e748..70daab2 100644 --- a/lacre/mailgate.py +++ b/lacre/mailgate.py @@ -376,10 +376,13 @@ def _get_first_payload(payloads): return payloads -def send_msg(message, recipients): +def send_msg(message, recipients, fromaddr=None): """Send MESSAGE to RECIPIENTS to the mail relay.""" global from_addr + if fromaddr is not None: + from_addr = fromaddr + recipients = [_f for _f in recipients if _f] if recipients: LOG.info(f"Sending email to: {recipients!r}") diff --git a/test/sendmail.py b/test/sendmail.py new file mode 100644 index 0000000..bc59c6e --- /dev/null +++ b/test/sendmail.py @@ -0,0 +1,22 @@ +import smtplib + + +def _send(host, port, from_addr, recipients, message): + smtp = smtplib.SMTP(host, port) + # smtp.starttls() + # try: + # breakpoint() + smtp.sendmail(from_addr, recipients, message) + # except smtplib.SMTPDataError as e: + # print(f"Couldn't deliver message.\nGot error: {e}\n") + + +message = """\ +From: dave@disposlab +To: alice@disposlab +Subject: Test message + +Lorem ipsum dolor sit amet. +""" + +_send('localhost', 10025, 'dave@disposlab', ['alice@disposlab'], message)