Skip to content

Commit

Permalink
Set up LMTP server manually
Browse files Browse the repository at this point in the history
The controller is meant for running the controller in a separate thread which is unnecessary in this project.
  • Loading branch information
TheAssassin committed Jan 1, 2025
1 parent e1c93a9 commit 54e4cab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion newpipe_crash_report_importer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 18 additions & 11 deletions newpipe_crash_report_importer/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import functools
import os
from datetime import datetime
from smtplib import LMTP
Expand All @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
11 changes: 0 additions & 11 deletions newpipe_crash_report_importer/lmtp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 54e4cab

Please sign in to comment.