From ed18b0c89c49ff59c17a83dbb200a34f8ac38ec4 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Mon, 1 Apr 2024 11:11:07 -0400 Subject: [PATCH] wip --- mail4one/pop3.py | 9 +++++++-- mail4one/poputils.py | 7 +++++++ tests/test_pop.py | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mail4one/pop3.py b/mail4one/pop3.py index 3214ce9..d3de548 100644 --- a/mail4one/pop3.py +++ b/mail4one/pop3.py @@ -29,7 +29,7 @@ from .poputils import ( end, Request, MailEntry, - get_mail, + get_mail_fp, get_mails_list, MailList, ) @@ -217,7 +217,12 @@ def trans_command_retr(mails: MailList, req: Request) -> None: entry = mails.get(req.arg1) if entry: write(ok("Contents follow")) - write(get_mail(entry)) + with get_mail_fp(entry) as fp: + for line in fp: + if line[0] == b'.': + write(b'.') + write(line) + # write(get_mail(entry)) write(end()) mails.delete(req.arg1) else: diff --git a/mail4one/poputils.py b/mail4one/poputils.py index c325ff1..451a1e2 100644 --- a/mail4one/poputils.py +++ b/mail4one/poputils.py @@ -2,6 +2,7 @@ import os from dataclasses import dataclass from enum import Enum, auto from pathlib import Path +from contextlib import contextmanager class ClientError(Exception): @@ -124,6 +125,12 @@ def set_nid(entries: list[MailEntry]): entry.nid = i +@contextmanager +def get_mail_fp(entry: MailEntry): + with open(entry.path, mode="rb") as fp: + yield fp + + def get_mail(entry: MailEntry) -> bytes: with open(entry.path, mode="rb") as fp: return fp.read() diff --git a/tests/test_pop.py b/tests/test_pop.py index 93fd879..b17dfd1 100644 --- a/tests/test_pop.py +++ b/tests/test_pop.py @@ -21,7 +21,13 @@ AQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF TEST_USER = "foobar" TEST_MBOX = "foobar_mails" -USERS = [User(username=TEST_USER, password_hash=TEST_HASH, mbox=TEST_MBOX)] +TEST_USER2 = "foo2" +TEST_MBOX2 = "foo2mails" + +USERS = [ + User(username=TEST_USER, password_hash=TEST_HASH, mbox=TEST_MBOX), + User(username=TEST_USER2, password_hash=TEST_HASH, mbox=TEST_MBOX2), +] MAILS_PATH: Path