use match statement to remove peer port

This commit is contained in:
Balakrishnan Balasubramanian 2023-06-30 17:32:51 -04:00
parent 8fe42e9163
commit 839a58bfb0

View File

@ -25,6 +25,8 @@ logger = logging.getLogger("smtp")
class MyHandler(AsyncMessage): class MyHandler(AsyncMessage):
peer_addr: Optional[str]
def __init__(self, mails_path: Path, mbox_finder: Callable[[str], list[str]]): def __init__(self, mails_path: Path, mbox_finder: Callable[[str], list[str]]):
super().__init__() super().__init__()
self.mails_path = mails_path self.mails_path = mails_path
@ -34,7 +36,11 @@ class MyHandler(AsyncMessage):
self, server: SMTPServer, session: SMTPSession, envelope: SMTPEnvelope self, server: SMTPServer, session: SMTPSession, envelope: SMTPEnvelope
) -> str: ) -> str:
self.rcpt_tos = envelope.rcpt_tos self.rcpt_tos = envelope.rcpt_tos
self.peer = session.peer match session.peer:
case addr, port:
self.peer_addr = addr
case _:
self.peer_addr = session.peer
return await super().handle_DATA(server, session, envelope) return await super().handle_DATA(server, session, envelope)
async def handle_message(self, m: Message): # type: ignore[override] async def handle_message(self, m: Message): # type: ignore[override]
@ -43,7 +49,7 @@ class MyHandler(AsyncMessage):
for mbox in self.mbox_finder(addr.lower()): for mbox in self.mbox_finder(addr.lower()):
all_mboxes.add(mbox) all_mboxes.add(mbox)
if not all_mboxes: if not all_mboxes:
logger.warning(f"dropping message from: {self.peer}") logger.warning(f"dropping message from: {self.peer_addr}")
return return
for mbox in all_mboxes: for mbox in all_mboxes:
for sub in ("new", "tmp", "cur"): for sub in ("new", "tmp", "cur"):
@ -58,7 +64,7 @@ class MyHandler(AsyncMessage):
for mbox in all_mboxes: for mbox in all_mboxes:
shutil.copy(temp_email_path, self.mails_path / mbox / "new") shutil.copy(temp_email_path, self.mails_path / mbox / "new")
logger.info( logger.info(
f"Saved mail at {filename} addrs: {','.join(self.rcpt_tos)}, mboxes: {','.join(all_mboxes)} peer: {self.peer}" f"Saved mail at {filename} addrs: {','.join(self.rcpt_tos)}, mboxes: {','.join(all_mboxes)} peer: {self.peer_addr}"
) )