Compare commits

...

4 Commits

Author SHA1 Message Date
c66fe37eb4 black format 2024-05-26 12:49:48 -04:00
1d54e2232f update deps 2024-05-24 22:10:43 -04:00
d4efe91593 add devnotes 2024-05-24 22:09:31 -04:00
92dc1ab713 Add X-SSL header to email
1. Type of listener the client used
2. If starttls was called
2024-05-24 22:00:28 -04:00
6 changed files with 39 additions and 9 deletions

View File

@ -4,6 +4,7 @@ Notes for developers
``` ```
python -m unittest tests.test_pop.TestPop3.test_CAPA python -m unittest tests.test_pop.TestPop3.test_CAPA
python -m unittest tests.test_smtp.TestSMTP
``` ```
## Patch for enable logging in test ## Patch for enable logging in test
@ -14,7 +15,11 @@ Patch generated using below
git diff --patch -U1 tests >> ./DEVNOTES.md git diff --patch -U1 tests >> ./DEVNOTES.md
``` ```
Apply with below Apply with below. Disables smtp test mail dir cleanup.
```
ls -ltd /tmp/m41*
git checkout tests
```
```bash ```bash
git apply - <<PATCH git apply - <<PATCH
@ -27,6 +32,18 @@ index 55c1a91..a825665 100644
- logging.basicConfig(level=logging.CRITICAL) - logging.basicConfig(level=logging.CRITICAL)
+ logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=logging.DEBUG)
td = tempfile.TemporaryDirectory(prefix="m41.pop.") td = tempfile.TemporaryDirectory(prefix="m41.pop.")
diff --git a/tests/test_smtp.py b/tests/test_smtp.py
index 0554d4c..52d147b 100644
--- a/tests/test_smtp.py
+++ b/tests/test_smtp.py
@@ -18,5 +18,5 @@ def setUpModule() -> None:
global MAILS_PATH
- logging.basicConfig(level=logging.CRITICAL)
+ logging.basicConfig(level=logging.DEBUG)
td = tempfile.TemporaryDirectory(prefix="m41.smtp.")
- unittest.addModuleCleanup(td.cleanup)
+ # unittest.addModuleCleanup(td.cleanup)
MAILS_PATH = Path(td.name)
PATCH PATCH
``` ```

6
Pipfile.lock generated
View File

@ -18,12 +18,12 @@
"default": { "default": {
"aiosmtpd": { "aiosmtpd": {
"hashes": [ "hashes": [
"sha256:78d7b14f859ad0e6de252b47f9cf1ca6f1c82a8b0f10a9e39bec7e915a6aa5fe", "sha256:5a811826e1a5a06c25ebc3e6c4a704613eb9a1bcf6b78428fbe865f4f6c9a4b8",
"sha256:a196922f1903e54c4d37c53415b7613056d39e2b1e8249f324b9ee7a439be0f1" "sha256:72c99179ba5aa9ae0abbda6994668239b64a5ce054471955fe75f581d2592475"
], ],
"index": "pypi", "index": "pypi",
"markers": "python_version >= '3.8'", "markers": "python_version >= '3.8'",
"version": "==1.4.5" "version": "==1.4.6"
}, },
"atpublic": { "atpublic": {
"hashes": [ "hashes": [

View File

@ -340,7 +340,9 @@ def make_pop_server_callback(mails_path: Path, users: list[User], timeout_second
async def session_cb(reader: StreamReader, writer: StreamWriter): async def session_cb(reader: StreamReader, writer: StreamWriter):
c_shared_state.set(s_state) c_shared_state.set(s_state)
ip, _ = writer.get_extra_info("peername") ip, _ = writer.get_extra_info("peername")
c_state.set(State(reader=reader, writer=writer, ip=ip, req_id=s_state.next_id())) c_state.set(
State(reader=reader, writer=writer, ip=ip, req_id=s_state.next_id())
)
logger.info("Got pop server callback") logger.info("Got pop server callback")
try: try:
try: try:

View File

@ -20,21 +20,31 @@ logger = logging.getLogger("smtp")
class MyHandler(AsyncMessage): class MyHandler(AsyncMessage):
def __init__(self, mails_path: Path, mbox_finder: Callable[[str], list[str]]): def __init__(
self,
mails_path: Path,
mbox_finder: Callable[[str], list[str]],
listener_type: str,
):
super().__init__() super().__init__()
self.mails_path = mails_path self.mails_path = mails_path
self.mbox_finder = mbox_finder self.mbox_finder = mbox_finder
self.rcpt_tos = [] self.rcpt_tos = []
self.peer = None self.peer = None
self.starttls = False
self.listener_type = listener_type
async def handle_DATA( async def handle_DATA(
self, server: SMTP, session: SMTPSession, envelope: SMTPEnvelope self, server: SMTP, session: SMTPSession, envelope: SMTPEnvelope
) -> str: ) -> str:
self.rcpt_tos = envelope.rcpt_tos self.rcpt_tos = envelope.rcpt_tos
self.peer = session.peer self.peer = session.peer
if session.ssl:
self.starttls = True
return await super().handle_DATA(server, session, envelope) return await super().handle_DATA(server, session, envelope)
async def handle_message(self, message: Message): # type: ignore[override] async def handle_message(self, message: Message): # type: ignore[override]
message["X-SSL"] = f"Type: {self.listener_type}, STARTTLS: {self.starttls}"
all_mboxes: set[str] = set() all_mboxes: set[str] = set()
for addr in self.rcpt_tos: for addr in self.rcpt_tos:
for mbox in self.mbox_finder(addr.lower()): for mbox in self.mbox_finder(addr.lower()):
@ -68,7 +78,7 @@ def protocol_factory_starttls(
): ):
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, "starttls")
smtp = SMTP( smtp = SMTP(
handler=handler, handler=handler,
require_starttls=require_starttls, require_starttls=require_starttls,
@ -86,7 +96,7 @@ def protocol_factory(
): ):
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, "plain")
smtp = SMTP(handler=handler, enable_SMTPUTF8=smtputf8) smtp = SMTP(handler=handler, enable_SMTPUTF8=smtputf8)
except: except:
logger.exception("Something went wrong") logger.exception("Something went wrong")

View File

@ -1,5 +1,5 @@
-i https://pypi.org/simple -i https://pypi.org/simple
aiosmtpd==1.4.5; python_version >= '3.8' aiosmtpd==1.4.6; python_version >= '3.8'
atpublic==4.1.0; python_version >= '3.8' atpublic==4.1.0; python_version >= '3.8'
attrs==23.2.0; python_version >= '3.7' attrs==23.2.0; python_version >= '3.7'
python-jata==1.2; python_version >= '3.8' python-jata==1.2; python_version >= '3.8'

View File

@ -61,6 +61,7 @@ class TestSMTP(unittest.IsolatedAsyncioTestCase):
X-Peer: ('127.0.0.1', {local_port}) X-Peer: ('127.0.0.1', {local_port})
X-MailFrom: foo@sender.com X-MailFrom: foo@sender.com
X-RcptTo: foo@bar.com X-RcptTo: foo@bar.com
X-SSL: Type: plain, STARTTLS: False
Hello world Hello world
Byee Byee