Add option to not enforce starttls and smtputf8 #6

Merged
balki merged 1 commits from starttls_opt into main 2024-04-16 18:11:36 -04:00
4 changed files with 33 additions and 11 deletions
Showing only changes of commit b8858085ab - Show all commits

View File

@ -56,13 +56,14 @@ class PopCfg(ServerCfg):
class SmtpStartTLSCfg(ServerCfg): class SmtpStartTLSCfg(ServerCfg):
server_type = "smtp_starttls" server_type = "smtp_starttls"
smtputf8 = True # Not used yet require_starttls = True
smtputf8 = True
port = 25 port = 25
class SmtpCfg(ServerCfg): class SmtpCfg(ServerCfg):
server_type = "smtp_starttls" server_type = "smtp"
smtputf8 = True # Not used yet smtputf8 = True
port = 465 port = 465

View File

@ -84,6 +84,8 @@ async def a_main(cfg: config.Config) -> None:
mails_path=Path(cfg.mails_path), mails_path=Path(cfg.mails_path),
mbox_finder=mbox_finder, mbox_finder=mbox_finder,
ssl_context=stls_context, ssl_context=stls_context,
require_starttls=stls.require_starttls,
smtputf8=stls.smtputf8,
) )
servers.append(smtp_server_starttls) servers.append(smtp_server_starttls)
elif scfg.server_type == "smtp": elif scfg.server_type == "smtp":
@ -94,6 +96,7 @@ async def a_main(cfg: config.Config) -> None:
mails_path=Path(cfg.mails_path), mails_path=Path(cfg.mails_path),
mbox_finder=mbox_finder, mbox_finder=mbox_finder,
ssl_context=get_tls_context(smtp.tls), ssl_context=get_tls_context(smtp.tls),
smtputf8=smtp.smtputf8,
) )
servers.append(smtp_server) servers.append(smtp_server)
else: else:

View File

@ -59,16 +59,20 @@ class MyHandler(AsyncMessage):
def protocol_factory_starttls( 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") logger.info("Got smtp client cb starttls")
try: try:
handler = MyHandler(mails_path, mbox_finder) handler = MyHandler(mails_path, mbox_finder)
smtp = SMTP( smtp = SMTP(
handler=handler, handler=handler,
require_starttls=True, require_starttls=require_starttls,
tls_context=context, tls_context=context,
enable_SMTPUTF8=True, enable_SMTPUTF8=smtputf8,
) )
except: except:
logger.exception("Something went wrong") logger.exception("Something went wrong")
@ -76,11 +80,13 @@ def protocol_factory_starttls(
return smtp 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") logger.info("Got smtp client cb")
try: try:
handler = MyHandler(mails_path, mbox_finder) handler = MyHandler(mails_path, mbox_finder)
smtp = SMTP(handler=handler, enable_SMTPUTF8=True) smtp = SMTP(handler=handler, enable_SMTPUTF8=smtputf8)
except: except:
logger.exception("Something went wrong") logger.exception("Something went wrong")
raise raise
@ -93,13 +99,22 @@ async def create_smtp_server_starttls(
mails_path: Path, mails_path: Path,
mbox_finder: Callable[[str], list[str]], mbox_finder: Callable[[str], list[str]],
ssl_context: ssl.SSLContext, ssl_context: ssl.SSLContext,
require_starttls: bool,
smtputf8: bool,
) -> asyncio.Server: ) -> asyncio.Server:
logging.info( logging.info(
f"Starting SMTP STARTTLS server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}" f"Starting SMTP STARTTLS server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}"
) )
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
return await loop.create_server( 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, host=host,
port=port, port=port,
start_serving=False, start_serving=False,
@ -111,14 +126,15 @@ async def create_smtp_server(
port: int, port: int,
mails_path: Path, mails_path: Path,
mbox_finder: Callable[[str], list[str]], mbox_finder: Callable[[str], list[str]],
ssl_context: Optional[ssl.SSLContext] = None, ssl_context: Optional[ssl.SSLContext],
smtputf8: bool,
) -> asyncio.Server: ) -> asyncio.Server:
logging.info( logging.info(
f"Starting SMTP server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}" f"Starting SMTP server {host=}, {port=}, {mails_path=!s}, {ssl_context != None=}"
) )
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
return await loop.create_server( return await loop.create_server(
partial(protocol_factory, mails_path, mbox_finder), partial(protocol_factory, mails_path, mbox_finder, smtputf8),
host=host, host=host,
port=port, port=port,
ssl=ssl_context, ssl=ssl_context,

View File

@ -33,6 +33,8 @@ class TestSMTP(unittest.IsolatedAsyncioTestCase):
port=7996, port=7996,
mails_path=MAILS_PATH, mails_path=MAILS_PATH,
mbox_finder=lambda addr: [TEST_MBOX], mbox_finder=lambda addr: [TEST_MBOX],
ssl_context=None,
smtputf8=True,
) )
self.task = asyncio.create_task(smtp_server.serve_forever()) self.task = asyncio.create_task(smtp_server.serve_forever())