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
|
2024-01-09 21:42:47 +00:00
|
|
|
|
from django.core.files.base import ContentFile
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
2024-01-09 21:42:47 +00:00
|
|
|
|
from mail.models import Email, EventAddress, EmailAttachment
|
2024-01-07 20:18:42 +00:00
|
|
|
|
from notify_sessions.models import SystemEvent
|
2024-01-09 21:42:47 +00:00
|
|
|
|
from tickets.models import IssueThread
|
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-04-24 17:38:52 +00:00
|
|
|
|
def unescape_simplified_quoted_printable(s):
|
|
|
|
|
import quopri
|
|
|
|
|
return quopri.decodestring(s).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2024-01-13 02:31:17 +00:00
|
|
|
|
def make_reply(reply_email, references=None, event=None):
|
2023-11-27 00:38:43 +00:00
|
|
|
|
from email.message import EmailMessage
|
|
|
|
|
from core.settings import MAIL_DOMAIN
|
2024-01-13 02:31:17 +00:00
|
|
|
|
event = event or "mail"
|
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
|
2024-01-13 02:31:17 +00:00
|
|
|
|
reply["Reply-To"] = f"{event}@{MAIL_DOMAIN}"
|
2024-01-07 20:18:42 +00:00
|
|
|
|
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-12 23:00:19 +00:00
|
|
|
|
def find_active_issue_thread(in_reply_to, address, subject):
|
|
|
|
|
from re import match
|
|
|
|
|
uuid_match = match(r'^ticket\+([a-f0-9-]{36})@', address)
|
|
|
|
|
if uuid_match:
|
|
|
|
|
issue = IssueThread.objects.filter(uuid=uuid_match.group(1))
|
|
|
|
|
if issue.exists():
|
|
|
|
|
return issue.first(), False
|
2024-01-07 20:18:42 +00:00
|
|
|
|
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
|
|
|
|
|
|
2024-01-09 21:42:47 +00:00
|
|
|
|
|
|
|
|
|
def parse_email_body(raw, log=None):
|
|
|
|
|
import email
|
|
|
|
|
from hashlib import sha256
|
|
|
|
|
|
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
|
|
parsed = email.message_from_bytes(raw)
|
|
|
|
|
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:
|
2024-04-22 18:05:53 +00:00
|
|
|
|
segment = part.get_payload()
|
2024-01-12 20:19:14 +00:00
|
|
|
|
if not segment:
|
|
|
|
|
continue
|
2024-01-09 21:42:47 +00:00
|
|
|
|
segment = unescape_and_decode_quoted_printable(segment)
|
|
|
|
|
segment = unescape_and_decode_base64(segment)
|
2024-04-24 17:38:52 +00:00
|
|
|
|
if part.get('Content-Transfer-Encoding') == 'quoted-printable':
|
|
|
|
|
segment = unescape_simplified_quoted_printable(segment)
|
2024-01-09 21:42:47 +00:00
|
|
|
|
log.debug(segment)
|
|
|
|
|
body = body + segment
|
|
|
|
|
elif 'attachment' in cdispo or 'inline' in cdispo:
|
|
|
|
|
file = ContentFile(part.get_payload(decode=True))
|
|
|
|
|
chash = sha256(file.read()).hexdigest()
|
|
|
|
|
name = part.get_filename()
|
|
|
|
|
if name is None:
|
|
|
|
|
name = "unnamed"
|
|
|
|
|
attachment, _ = EmailAttachment.objects.get_or_create(
|
|
|
|
|
name=name, mime_type=ctype, file=file, hash=chash)
|
|
|
|
|
attachment.save()
|
|
|
|
|
attachments.append(attachment)
|
|
|
|
|
if 'inline' in cdispo:
|
|
|
|
|
body = body + f'<img src="cid:{attachment.id}">'
|
|
|
|
|
log.info("Image", ctype, attachment.id)
|
|
|
|
|
else:
|
|
|
|
|
log.info("Attachment", ctype, cdispo)
|
|
|
|
|
else:
|
2024-01-15 22:38:03 +00:00
|
|
|
|
if parsed.get_content_type() == 'text/plain':
|
2024-04-22 18:05:53 +00:00
|
|
|
|
body = parsed.get_payload()
|
2024-01-15 22:38:03 +00:00
|
|
|
|
elif parsed.get_content_type() == 'text/html':
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
import re
|
2024-04-22 18:05:53 +00:00
|
|
|
|
body = parsed.get_payload()
|
2024-01-15 22:38:03 +00:00
|
|
|
|
soup = BeautifulSoup(body, 'html.parser')
|
|
|
|
|
body = re.sub(r'([\r\n]+.?)*[\r\n]', r'\n', soup.get_text()).strip('\n')
|
|
|
|
|
else:
|
|
|
|
|
log.warning("Unknown content type", parsed.get_content_type())
|
|
|
|
|
body = "Unknown content type"
|
2024-01-12 20:19:14 +00:00
|
|
|
|
body = unescape_and_decode_quoted_printable(body)
|
|
|
|
|
body = unescape_and_decode_base64(body)
|
2024-04-24 17:38:52 +00:00
|
|
|
|
if parsed.get('Content-Transfer-Encoding') == 'quoted-printable':
|
|
|
|
|
body = unescape_simplified_quoted_printable(body)
|
2024-01-12 20:19:14 +00:00
|
|
|
|
log.debug(body)
|
2024-01-09 21:42:47 +00:00
|
|
|
|
|
|
|
|
|
return parsed, body, attachments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def receive_email(envelope, log=None):
|
|
|
|
|
parsed, body, attachments = parse_email_body(envelope.content, log)
|
|
|
|
|
|
|
|
|
|
header_from = parsed.get('From')
|
|
|
|
|
header_to = parsed.get('To')
|
|
|
|
|
header_in_reply_to = parsed.get('In-Reply-To')
|
|
|
|
|
header_message_id = parsed.get('Message-ID')
|
|
|
|
|
|
|
|
|
|
if header_from != envelope.mail_from:
|
|
|
|
|
log.warning("Header from does not match envelope from")
|
|
|
|
|
log.info(f"Header from: {header_from}, envelope from: {envelope.mail_from}")
|
|
|
|
|
|
|
|
|
|
if header_to != envelope.rcpt_tos[0]:
|
|
|
|
|
log.warning("Header to does not match envelope to")
|
|
|
|
|
log.info(f"Header to: {header_to}, envelope to: {envelope.rcpt_tos[0]}")
|
|
|
|
|
|
2024-01-10 19:03:25 +00:00
|
|
|
|
recipient = envelope.rcpt_tos[0].lower() if envelope.rcpt_tos else header_to.lower()
|
|
|
|
|
sender = envelope.mail_from if envelope.mail_from else header_from
|
2024-01-09 21:42:47 +00:00
|
|
|
|
subject = parsed.get('Subject')
|
2024-01-12 20:19:14 +00:00
|
|
|
|
if not subject:
|
|
|
|
|
subject = "No subject"
|
2024-01-09 21:42:47 +00:00
|
|
|
|
subject = unescape_and_decode_quoted_printable(subject)
|
|
|
|
|
subject = unescape_and_decode_base64(subject)
|
|
|
|
|
target_event = find_target_event(recipient)
|
|
|
|
|
|
2024-01-12 23:00:19 +00:00
|
|
|
|
active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject)
|
2024-01-12 20:19:14 +00:00
|
|
|
|
|
2024-01-09 21:42:47 +00:00
|
|
|
|
email = Email.objects.create(
|
2024-01-12 20:19:14 +00:00
|
|
|
|
sender=sender, recipient=recipient, body=body, subject=subject, reference=header_message_id,
|
2024-04-22 18:05:53 +00:00
|
|
|
|
in_reply_to=header_in_reply_to, raw=envelope.content, event=target_event,
|
2024-01-09 21:42:47 +00:00
|
|
|
|
issue_thread=active_issue_thread)
|
|
|
|
|
for attachment in attachments:
|
|
|
|
|
email.attachments.add(attachment)
|
|
|
|
|
email.save()
|
|
|
|
|
|
|
|
|
|
reply = None
|
|
|
|
|
if new:
|
2024-01-10 18:03:40 +00:00
|
|
|
|
# auto reply if new issue
|
2024-01-09 21:42:47 +00:00
|
|
|
|
references = collect_references(active_issue_thread)
|
2024-01-12 19:58:37 +00:00
|
|
|
|
if not sender.startswith('noreply'):
|
2024-01-12 22:12:18 +00:00
|
|
|
|
subject = f"Re: {subject} [#{active_issue_thread.short_uuid()}]"
|
|
|
|
|
body = '''Your request (#{}) has been received and will be reviewed by our lost&found angels.
|
|
|
|
|
|
|
|
|
|
We are reviewing incoming requests during the event and teardown. Immediately after the event, expect a delay as the \
|
|
|
|
|
workload is high. We will not forget about your request and get back in touch once we have updated information on your \
|
|
|
|
|
request. Requests for devices, wallets, credit cards or similar items will be handled with priority.
|
|
|
|
|
|
|
|
|
|
If you happen to find your lost item or just want to add additional information, please reply to this email. Please \
|
|
|
|
|
do not create a new request.
|
|
|
|
|
|
|
|
|
|
Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid())
|
2024-01-12 19:58:37 +00:00
|
|
|
|
reply_email = Email.objects.create(
|
2024-01-12 22:12:18 +00:00
|
|
|
|
sender=recipient, recipient=sender, body=body, subject=subject,
|
2024-01-12 19:58:37 +00:00
|
|
|
|
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
|
2024-01-13 02:31:17 +00:00
|
|
|
|
reply = make_reply(reply_email, references, event=target_event.slug if target_event else None)
|
2024-01-10 18:03:40 +00:00
|
|
|
|
else:
|
2024-01-10 19:03:25 +00:00
|
|
|
|
# change state if not new
|
2024-01-10 18:03:40 +00:00
|
|
|
|
if active_issue_thread.state != 'pending_new':
|
|
|
|
|
active_issue_thread.state = 'pending_open'
|
|
|
|
|
active_issue_thread.save()
|
2024-01-09 21:42:47 +00:00
|
|
|
|
|
|
|
|
|
return email, new, reply
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
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')
|
|
|
|
|
|
2024-04-22 18:05:53 +00:00
|
|
|
|
content = None
|
2023-11-27 00:38:43 +00:00
|
|
|
|
try:
|
2024-04-22 18:05:53 +00:00
|
|
|
|
content = envelope.content
|
2024-01-09 21:42:47 +00:00
|
|
|
|
email, new, reply = await sync_to_async(receive_email)(envelope, log)
|
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")
|
2024-01-12 19:58:37 +00:00
|
|
|
|
if new and reply:
|
2024-01-09 21:42:47 +00:00
|
|
|
|
await send_smtp(reply, log)
|
2024-01-07 20:18:42 +00:00
|
|
|
|
log.info("Sent auto reply")
|
2023-11-27 00:38:43 +00:00
|
|
|
|
|
|
|
|
|
return '250 Message accepted for delivery'
|
|
|
|
|
except Exception as e:
|
2024-04-22 18:05:53 +00:00
|
|
|
|
import uuid
|
|
|
|
|
random_filename = 'mail-' + str(uuid.uuid4())
|
|
|
|
|
with open(random_filename, 'wb') as f:
|
|
|
|
|
f.write(content)
|
|
|
|
|
log.error(type(e), e, f"Saved email to {random_filename}")
|
2024-01-09 21:42:47 +00:00
|
|
|
|
return '451 Internal server error'
|