2023-11-27 00:38:43 +00:00
|
|
|
import logging
|
2023-12-29 20:50:58 +00:00
|
|
|
|
2023-11-27 00:38:43 +00:00
|
|
|
import aiosmtplib
|
2024-01-07 20:18:42 +00:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from channels.layers import get_channel_layer
|
2023-11-27 00:38:43 +00:00
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
from mail.models import Email, EventAddress
|
|
|
|
from notify_sessions.models import SystemEvent
|
|
|
|
from tickets.models import IssueThread, StateChange
|
2023-11-27 00:38:43 +00:00
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
|
2023-12-29 20:50:58 +00:00
|
|
|
def find_quoted_printable(s, marker):
|
|
|
|
positions = [i for i in range(len(s)) if s.lower().startswith('=?utf-8?' + marker + '?', i)]
|
|
|
|
for pos in positions:
|
|
|
|
end = s.find('?=', pos + 10)
|
|
|
|
if end == -1:
|
|
|
|
continue
|
|
|
|
yield pos, end + 3
|
|
|
|
|
|
|
|
|
|
|
|
def unescape_and_decode_quoted_printable(s):
|
|
|
|
import quopri
|
|
|
|
decoded = ''
|
|
|
|
last_end = 0
|
|
|
|
for start, end in find_quoted_printable(s, 'q'):
|
|
|
|
decoded += s[last_end:start]
|
|
|
|
decoded += quopri.decodestring(s[start + 10:end - 3]).decode('utf-8')
|
|
|
|
last_end = end
|
|
|
|
decoded += s[last_end:]
|
|
|
|
return decoded
|
|
|
|
|
|
|
|
|
|
|
|
def unescape_and_decode_base64(s):
|
|
|
|
import base64
|
|
|
|
decoded = ''
|
|
|
|
last_end = 0
|
|
|
|
for start, end in find_quoted_printable(s, 'b'):
|
|
|
|
decoded += s[last_end:start]
|
|
|
|
decoded += base64.b64decode(s[start + 10:end - 3]).decode('utf-8')
|
|
|
|
last_end = end
|
|
|
|
decoded += s[last_end:]
|
|
|
|
return decoded
|
|
|
|
|
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
def collect_references(issue_thread):
|
|
|
|
mails = issue_thread.emails.order_by('timestamp')
|
|
|
|
references = []
|
|
|
|
for mail in mails:
|
|
|
|
if mail.reference:
|
|
|
|
references.append(mail.reference)
|
|
|
|
return references
|
|
|
|
|
|
|
|
|
|
|
|
def make_reply(reply_email, references=None, event=None, issue_thread=None):
|
2023-11-27 00:38:43 +00:00
|
|
|
from email.message import EmailMessage
|
|
|
|
from core.settings import MAIL_DOMAIN
|
2024-01-07 20:18:42 +00:00
|
|
|
event = event or "noreply"
|
2023-11-27 00:38:43 +00:00
|
|
|
reply = EmailMessage()
|
2024-01-07 20:18:42 +00:00
|
|
|
reply["From"] = reply_email.sender
|
|
|
|
reply["To"] = reply_email.recipient
|
|
|
|
reply["Subject"] = reply_email.subject
|
|
|
|
reply["Reply-To"] = f"{event}+{issue_thread}@{MAIL_DOMAIN}"
|
|
|
|
if reply_email.in_reply_to:
|
|
|
|
reply["In-Reply-To"] = reply_email.in_reply_to
|
|
|
|
if reply_email.reference:
|
|
|
|
reply["Message-ID"] = reply_email.reference
|
|
|
|
else:
|
|
|
|
reply["Message-ID"] = reply_email.id + "@" + MAIL_DOMAIN
|
|
|
|
reply_email.reference = reply["Message-ID"]
|
|
|
|
reply_email.save()
|
|
|
|
if references:
|
|
|
|
reply["References"] = " ".join(references)
|
|
|
|
|
|
|
|
reply.set_content(reply_email.body)
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
return reply
|
|
|
|
|
|
|
|
|
|
|
|
async def send_smtp(message, log):
|
|
|
|
log.info('Sending message to %s' % message['To'])
|
|
|
|
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
|
|
|
|
|
|
|
|
2024-01-07 20:18:42 +00:00
|
|
|
def find_active_issue_thread(in_reply_to, subject=None):
|
|
|
|
reply_to = Email.objects.filter(reference=in_reply_to)
|
|
|
|
if reply_to.exists():
|
|
|
|
return reply_to.first().issue_thread, False
|
|
|
|
else:
|
|
|
|
issue = IssueThread.objects.create(name=subject)
|
|
|
|
return issue, True
|
|
|
|
|
|
|
|
|
2023-12-30 17:33:45 +00:00
|
|
|
def find_target_event(address):
|
|
|
|
try:
|
|
|
|
address_map = EventAddress.objects.get(address=address)
|
|
|
|
if address_map.event:
|
|
|
|
return address_map.event
|
|
|
|
except EventAddress.DoesNotExist:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2023-11-27 00:38:43 +00:00
|
|
|
class LMTPHandler:
|
|
|
|
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
|
|
|
|
from core.settings import MAIL_DOMAIN
|
2023-12-30 00:46:49 +00:00
|
|
|
address = address.lower()
|
2023-11-27 00:38:43 +00:00
|
|
|
if not address.endswith('@' + MAIL_DOMAIN):
|
|
|
|
return '550 not relaying to that domain'
|
|
|
|
envelope.rcpt_tos.append(address)
|
|
|
|
return '250 OK'
|
|
|
|
|
|
|
|
async def handle_DATA(self, server, session, envelope):
|
|
|
|
import email
|
|
|
|
log = logging.getLogger('mail.log')
|
2024-01-07 20:18:42 +00:00
|
|
|
log.setLevel(logging.DEBUG)
|
2023-11-27 00:38:43 +00:00
|
|
|
log.info('Message from %s' % envelope.mail_from)
|
|
|
|
log.info('Message for %s' % envelope.rcpt_tos)
|
|
|
|
log.info('Message data:\n')
|
|
|
|
|
|
|
|
try:
|
|
|
|
parsed = email.message_from_bytes(envelope.content)
|
|
|
|
body = ""
|
|
|
|
if parsed.is_multipart():
|
|
|
|
for part in parsed.walk():
|
|
|
|
ctype = part.get_content_type()
|
|
|
|
cdispo = str(part.get('Content-Disposition'))
|
|
|
|
|
|
|
|
# skip any text/plain (txt) attachments
|
|
|
|
if ctype == 'text/plain' and 'attachment' not in cdispo:
|
|
|
|
body = part.get_payload(decode=True)
|
|
|
|
else:
|
|
|
|
log.info("Attachment", ctype, cdispo)
|
|
|
|
else:
|
|
|
|
body = parsed.get_payload(decode=True)
|
|
|
|
log.info(body)
|
|
|
|
|
|
|
|
header_from = parsed.get('From')
|
|
|
|
header_to = parsed.get('To')
|
2024-01-07 20:18:42 +00:00
|
|
|
header_in_reply_to = parsed.get('In-Reply-To')
|
|
|
|
header_message_id = parsed.get('Message-ID')
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
if header_from != envelope.mail_from:
|
|
|
|
log.warning("Header from does not match envelope from")
|
2024-01-07 20:18:42 +00:00
|
|
|
log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
if header_to != envelope.rcpt_tos[0]:
|
|
|
|
log.warning("Header to does not match envelope to")
|
2024-01-07 20:18:42 +00:00
|
|
|
log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
|
|
|
|
|
2023-12-30 00:46:49 +00:00
|
|
|
recipient = envelope.rcpt_tos[0].lower()
|
2024-01-07 20:18:42 +00:00
|
|
|
sender = envelope.mail_from
|
|
|
|
subject = parsed.get('Subject')
|
2023-12-29 20:50:58 +00:00
|
|
|
subject = unescape_and_decode_quoted_printable(subject)
|
|
|
|
subject = unescape_and_decode_base64(subject)
|
2023-12-30 17:33:45 +00:00
|
|
|
target_event = await sync_to_async(find_target_event)(recipient)
|
2024-01-07 20:18:42 +00:00
|
|
|
|
|
|
|
active_issue_thread, new = await sync_to_async(find_active_issue_thread)(header_in_reply_to, subject)
|
2023-12-29 20:50:58 +00:00
|
|
|
body_decoded = body.decode('utf-8')
|
|
|
|
body_decoded = unescape_and_decode_quoted_printable(body_decoded)
|
|
|
|
body_decoded = unescape_and_decode_base64(body_decoded)
|
|
|
|
|
|
|
|
email = await sync_to_async(Email.objects.create)(
|
|
|
|
sender=sender, recipient=recipient, body=body_decoded, subject=subject, reference=header_message_id,
|
|
|
|
in_reply_to=header_in_reply_to, raw=envelope.content.decode('utf-8'), event=target_event,
|
|
|
|
issue_thread=active_issue_thread)
|
2024-01-07 20:18:42 +00:00
|
|
|
log.info(f"Created email {email.id}")
|
|
|
|
systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id)
|
|
|
|
log.info(f"Created system event {systemevent.id}")
|
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
await channel_layer.group_send(
|
|
|
|
'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id,
|
|
|
|
"message": "email received"}
|
|
|
|
)
|
|
|
|
log.info(f"Sent message to frontend")
|
|
|
|
if new:
|
|
|
|
references = await sync_to_async(collect_references)(active_issue_thread)
|
|
|
|
|
2023-12-29 20:50:58 +00:00
|
|
|
reply_email = await sync_to_async(Email.objects.create)(
|
|
|
|
sender=recipient, recipient=sender, body="Thank you for your message.", subject="Message received",
|
|
|
|
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
|
2024-01-07 20:18:42 +00:00
|
|
|
await send_smtp(make_reply(reply_email, references), log)
|
|
|
|
log.info("Sent auto reply")
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
return '250 Message accepted for delivery'
|
|
|
|
except Exception as e:
|
|
|
|
log.error(e)
|
|
|
|
return '550 Message rejected'
|