From 54e4cab7da9d1463a5823c5129683244425701b1 Mon Sep 17 00:00:00 2001 From: TheAssassin Date: Wed, 1 Jan 2025 18:35:02 +0100 Subject: [PATCH] Set up LMTP server manually The controller is meant for running the controller in a separate thread which is unnecessary in this project. --- newpipe_crash_report_importer/__init__.py | 2 +- newpipe_crash_report_importer/cli.py | 29 ++++++++++++-------- newpipe_crash_report_importer/lmtp_server.py | 11 -------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/newpipe_crash_report_importer/__init__.py b/newpipe_crash_report_importer/__init__.py index 3bf7fd4..bd64461 100644 --- a/newpipe_crash_report_importer/__init__.py +++ b/newpipe_crash_report_importer/__init__.py @@ -7,7 +7,7 @@ from ._logging import make_logger, configure_logging from .database_entry import DatabaseEntry -from .lmtp_server import LmtpController, CrashReportHandler +from .lmtp_server import CrashReportHandler from .message import Message from .storage import ( DirectoryStorage, diff --git a/newpipe_crash_report_importer/cli.py b/newpipe_crash_report_importer/cli.py index f7daaa3..942d699 100644 --- a/newpipe_crash_report_importer/cli.py +++ b/newpipe_crash_report_importer/cli.py @@ -1,4 +1,5 @@ import asyncio +import functools import os from datetime import datetime from smtplib import LMTP @@ -7,13 +8,13 @@ import sentry_sdk from sentry_sdk.integrations.asyncio import AsyncioIntegration +from newpipe_crash_report_importer.lmtp_server import CustomLMTP from . import ( DatabaseEntry, DirectoryStorage, GlitchtipStorage, GlitchtipError, AlreadyStoredError, - LmtpController, CrashReportHandler, Message, make_logger, @@ -100,18 +101,24 @@ async def handle_received_mail(message: Message): except GlitchtipError as e: logger.error("Failed to store error in GlitchTip: %s", e) - loop = asyncio.get_event_loop() - - # set up LMTP server - controller = LmtpController( - CrashReportHandler(handle_received_mail), - enable_SMTPUTF8=True, - hostname=host, - port=port, - loop=loop, + handler = CrashReportHandler(handle_received_mail) + + loop = asyncio.new_event_loop() + + loop.run_until_complete( + loop.create_server( + functools.partial( + CustomLMTP, + handler, + ident="NewPipe crash report importer", + enable_SMTPUTF8=True, + ), + host=host, + port=port, + ) ) - logger.info(f"server listening on {controller.hostname}:{controller.port}") + logger.info(f"server listening on {host}:{port}") loop.run_forever() diff --git a/newpipe_crash_report_importer/lmtp_server.py b/newpipe_crash_report_importer/lmtp_server.py index 1e9d809..d6c3504 100644 --- a/newpipe_crash_report_importer/lmtp_server.py +++ b/newpipe_crash_report_importer/lmtp_server.py @@ -2,7 +2,6 @@ from email.parser import Parser import sentry_sdk -from aiosmtpd.controller import Controller from aiosmtpd.lmtp import LMTP from aiosmtpd.smtp import Envelope @@ -40,16 +39,6 @@ def connection_lost(self, *args, **kwargs): return super().connection_lost(*args, **kwargs) -class LmtpController(Controller): - """ - A custom controller implementation, return LMTP instances instead of SMTP ones. - Inspired by GNU Mailman 3"s LMTPController. - """ - - def factory(self): - return CustomLMTP(self.handler, ident="NewPipe crash report importer") - - class CrashReportHandler: """ Very simple handler which only accepts mail for allowed addresses and stores them into the Sentry database.