-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handlers.py
88 lines (62 loc) · 2.57 KB
/
Handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""" handlers to be used for incoming messages """
import logging
from smtplib import SMTP
from typing import Set
import pyzmail
class Handlers:
"""
This class contains handlers for all possible domains.
It won't initialise clients or fetch messages, this should be done externally (separation of concerns)
"""
def __init__(self, sender: SMTP, this_mail: str, logger: logging.Logger):
"""
:param sender: initialised smtp_client
:param this_mail: mail_address to use as sender
:param logger: global logging object
"""
self._logger: logging.Logger = logger
self._this: str = this_mail
self._sender: SMTP = sender
# TODO invert list? 1 tag : n handlers OR 1 handler : n tags
self._mail_handlers = {"mailrobot@mail.xing.com": self._format_xing}
def handle(self, source: str, mail_raw: bytes):
"""
Call to let mail be handled
:param source: address of source
:param mail_raw: mail body
"""
self._mail_handlers[source](mail_raw)
def _format_xing(self, mail_raw: bytes):
"""" call with RFC 822 Mail body """
self._logger.info("decoding XING message")
mail = pyzmail.PyzMessage.factory(mail_raw)
mail_TO = mail.get_address("to") # mail adress only
mail_FROM = mail.get_address("from")
mail_SUBJECT = mail.get_subject()
# X-Forward To?
# this is the magic bit, XING shows message in text_part but hides it in (visible) html_part
payload = mail.text_part.get_payload().decode(mail.text_part.charset)
self._logger.info(f"From: {mail_FROM}, mail_TO: {mail_TO}, Subject: {mail_SUBJECT}, Size: {len(payload)}")
self._logger.debug("Adding signature")
payload += "\n-[Decoded using ReMailer from Modisch Fabrications]-"
# TODO Collapse whitespace and reformat
# compare against address, not name
if mail_TO[1] == self._this:
target = mail_FROM
else:
target = mail_TO
source = self._this
subject = "[Decoded] " + mail_SUBJECT
self._logger.info(f"Formatting done, sending back to {target}")
self._sender.sendmail(source, target, f"Subject: {subject}\n{payload}".encode())
self._logger.info("Sent successfully")
def _format_no_ip(self, mail_raw: bytes):
"""" extracts key to press """
# TODO fill with life
@property
def sources(self) -> Set:
"""
:return: list of
"""
return set(self._mail_handlers.keys())
# end