Implement a bare minimum of advanced filtering

- Forward messages without encryption.

- Include a simple test setup in the Makefile.

- Add a test to send a test message to the daemon.
This commit is contained in:
Piotr F. Mieszkowski 2022-07-04 21:39:45 +02:00 committed by Gitea
parent 6455c1a280
commit 4c844384e3
4 changed files with 54 additions and 5 deletions

View File

@ -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:

View File

@ -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()

View File

@ -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}")

22
test/sendmail.py Normal file
View File

@ -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)