From b8858085abc7051869f3b1afad0b0fd2ec4fc4f2 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Tue, 16 Apr 2024 18:02:21 -0400 Subject: [PATCH] Add option to not enforce starttls and smtputf8 --- mail4one/config.py | 7 ++++--- mail4one/server.py | 3 +++ mail4one/smtp.py | 32 ++++++++++++++++++++++++-------- tests/test_smtp.py | 2 ++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/mail4one/config.py b/mail4one/config.py index 1f97319..e551487 100644 --- a/mail4one/config.py +++ b/mail4one/config.py @@ -56,13 +56,14 @@ class PopCfg(ServerCfg): class SmtpStartTLSCfg(ServerCfg): server_type = "smtp_starttls" - smtputf8 = True # Not used yet + require_starttls = True + smtputf8 = True port = 25 class SmtpCfg(ServerCfg): - server_type = "smtp_starttls" - smtputf8 = True # Not used yet + server_type = "smtp" + smtputf8 = True port = 465 diff --git a/mail4one/server.py b/mail4one/server.py index 541d686..15397b2 100644 --- a/mail4one/server.py +++ b/mail4one/server.py @@ -84,6 +84,8 @@ async def a_main(cfg: config.Config) -> None: mails_path=Path(cfg.mails_path), mbox_finder=mbox_finder, ssl_context=stls_context, + require_starttls=stls.require_starttls, + smtputf8=stls.smtputf8, ) servers.append(smtp_server_starttls) elif scfg.server_type == "smtp": @@ -94,6 +96,7 @@ async def a_main(cfg: config.Config) -> None: mails_path=Path(cfg.mails_path), mbox_finder=mbox_finder, ssl_context=get_tls_context(smtp.tls), + smtputf8=smtp.smtputf8, ) servers.append(smtp_server) else: diff --git a/mail4one/smtp.py b/mail4one/smtp.py index 9c10bff..81367d0 100644 --- a/mail4one/smtp.py +++ b/mail4one/smtp.py @@ -59,16 +59,20 @@ class MyHandler(AsyncMessage): def protocol_factory_starttls( - mails_path: Path, mbox_finder: Callable[[str], list[str]], context: ssl.SSLContext + mails_path: Path, + mbox_finder: Callable[[str], list[str]], + context: ssl.SSLContext, + require_starttls: bool, + smtputf8: bool, ): logger.info("Got smtp client cb starttls") try: handler = MyHandler(mails_path, mbox_finder) smtp = SMTP( handler=handler, - require_starttls=True, + require_starttls=require_starttls, tls_context=context, - enable_SMTPUTF8=True, + enable_SMTPUTF8=smtputf8, ) except: logger.exception("Something went wrong") @@ -76,11 +80,13 @@ def protocol_factory_starttls( return smtp -def protocol_factory(mails_path: Path, mbox_finder: Callable[[str], list[str]]): +def protocol_factory( + mails_path: Path, mbox_finder: Callable[[str], list[str]], smtputf8: bool +): logger.info("Got smtp client cb") try: handler = MyHandler(mails_path, mbox_finder) - smtp = SMTP(handler=handler, enable_SMTPUTF8=True) + smtp = SMTP(handler=handler, enable_SMTPUTF8=smtputf8) except: logger.exception("Something went wrong") raise @@ -93,13 +99,22 @@ async def create_smtp_server_starttls( mails_path: Path, mbox_finder: Callable[[str], list[str]], ssl_context: ssl.SSLContext, + require_starttls: bool, + smtputf8: bool, ) -> asyncio.Server: logging.info( f"Starting SMTP STARTTLS server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}" ) loop = asyncio.get_event_loop() return await loop.create_server( - partial(protocol_factory_starttls, mails_path, mbox_finder, ssl_context), + partial( + protocol_factory_starttls, + mails_path, + mbox_finder, + ssl_context, + require_starttls, + smtputf8, + ), host=host, port=port, start_serving=False, @@ -111,14 +126,15 @@ async def create_smtp_server( port: int, mails_path: Path, mbox_finder: Callable[[str], list[str]], - ssl_context: Optional[ssl.SSLContext] = None, + ssl_context: Optional[ssl.SSLContext], + smtputf8: bool, ) -> asyncio.Server: logging.info( f"Starting SMTP server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}" ) loop = asyncio.get_event_loop() return await loop.create_server( - partial(protocol_factory, mails_path, mbox_finder), + partial(protocol_factory, mails_path, mbox_finder, smtputf8), host=host, port=port, ssl=ssl_context, diff --git a/tests/test_smtp.py b/tests/test_smtp.py index c3cac47..b69b0c5 100644 --- a/tests/test_smtp.py +++ b/tests/test_smtp.py @@ -33,6 +33,8 @@ class TestSMTP(unittest.IsolatedAsyncioTestCase): port=7996, mails_path=MAILS_PATH, mbox_finder=lambda addr: [TEST_MBOX], + ssl_context=None, + smtputf8=True, ) self.task = asyncio.create_task(smtp_server.serve_forever()) -- 2.34.1