stash
This commit is contained in:
parent
f9409bb823
commit
5e6030e715
39 changed files with 1048 additions and 64 deletions
|
@ -3,6 +3,7 @@ import random
|
|||
from django.db import models
|
||||
from django_softdelete.models import SoftDeleteModel
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from core.settings import MAIL_DOMAIN
|
||||
from files.models import AbstractFile
|
||||
from inventory.models import Event
|
||||
|
@ -38,3 +39,6 @@ class EventAddress(models.Model):
|
|||
class EmailAttachment(AbstractFile):
|
||||
email = models.ForeignKey(Email, models.CASCADE, related_name='attachments', null=True)
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
from re import match
|
||||
|
||||
import aiosmtplib
|
||||
from channels.layers import get_channel_layer
|
||||
|
@ -6,10 +7,15 @@ from channels.db import database_sync_to_async
|
|||
from django.core.files.base import ContentFile
|
||||
|
||||
from mail.models import Email, EventAddress, EmailAttachment
|
||||
from notifications.templates import render_auto_reply
|
||||
from notify_sessions.models import SystemEvent
|
||||
from tickets.models import IssueThread
|
||||
|
||||
|
||||
class SpecialMailException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
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:
|
||||
|
@ -82,6 +88,22 @@ def make_reply(reply_email, references=None, event=None):
|
|||
return reply
|
||||
|
||||
|
||||
def make_notification(message, to, title): # TODO where should replies to this go
|
||||
from email.message import EmailMessage
|
||||
from core.settings import MAIL_DOMAIN
|
||||
notification = EmailMessage()
|
||||
notification["From"] = "notifications@%s" % MAIL_DOMAIN
|
||||
notification["To"] = to
|
||||
notification["Subject"] = f"[C3LF Notification]%s" % title
|
||||
# notification["Reply-To"] = f"{event}@{MAIL_DOMAIN}"
|
||||
# notification["In-Reply-To"] = email.reference
|
||||
# notification["Message-ID"] = email.id + "@" + MAIL_DOMAIN
|
||||
|
||||
notification.set_content(message)
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
async def send_smtp(message):
|
||||
await aiosmtplib.send(message, hostname="127.0.0.1", port=25, use_tls=False, start_tls=False)
|
||||
|
||||
|
@ -180,13 +202,23 @@ def receive_email(envelope, log=None):
|
|||
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_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]}")
|
||||
|
||||
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]}")
|
||||
# handle undelivered mail header_from : 'Mail Delivery System <MAILER-DAEMON@...'
|
||||
|
||||
if match(r'^([a-zA-Z ]*<)?MAILER-DAEMON@', header_from) and envelope.mail_from.strip("<>") == "":
|
||||
log.warning("Ignoring mailer daemon")
|
||||
raise SpecialMailException("Ignoring mailer daemon")
|
||||
|
||||
if Email.objects.filter(reference=header_message_id).exists(): # break before issue thread is created
|
||||
log.warning("Email already exists")
|
||||
raise Exception("Email already exists")
|
||||
|
||||
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
|
||||
|
@ -213,16 +245,7 @@ def receive_email(envelope, log=None):
|
|||
references = collect_references(active_issue_thread)
|
||||
if not sender.startswith('noreply'):
|
||||
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())
|
||||
body = render_auto_reply(active_issue_thread)
|
||||
reply_email = Email.objects.create(
|
||||
sender=recipient, recipient=sender, body=body, subject=subject,
|
||||
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
|
||||
|
@ -233,7 +256,7 @@ Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid
|
|||
active_issue_thread.state = 'pending_open'
|
||||
active_issue_thread.save()
|
||||
|
||||
return email, new, reply
|
||||
return email, new, reply, active_issue_thread
|
||||
|
||||
|
||||
class LMTPHandler:
|
||||
|
@ -255,7 +278,7 @@ class LMTPHandler:
|
|||
content = None
|
||||
try:
|
||||
content = envelope.content
|
||||
email, new, reply = await receive_email(envelope, log)
|
||||
email, new, reply, thread = await receive_email(envelope, log)
|
||||
log.info(f"Created email {email.id}")
|
||||
systemevent = await database_sync_to_async(SystemEvent.objects.create)(type='email received',
|
||||
reference=email.id)
|
||||
|
@ -263,14 +286,28 @@ class LMTPHandler:
|
|||
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"}
|
||||
)
|
||||
"message": "email received"})
|
||||
log.info(f"Sent message to frontend")
|
||||
|
||||
if new and reply:
|
||||
log.info('Sending message to %s' % reply['To'])
|
||||
await send_smtp(reply)
|
||||
log.info("Sent auto reply")
|
||||
|
||||
if thread:
|
||||
await channel_layer.group_send(
|
||||
'general', {"type": "generic.event", "name": "user_notification", "event_id": systemevent.id,
|
||||
"ticket_id": thread.id, "new": new})
|
||||
else:
|
||||
print("No thread found")
|
||||
|
||||
return '250 Message accepted for delivery'
|
||||
except SpecialMailException as e:
|
||||
import uuid
|
||||
random_filename = 'special-' + str(uuid.uuid4())
|
||||
with open(random_filename, 'wb') as f:
|
||||
f.write(content)
|
||||
log.warning(f"Special mail exception: {e} saved to {random_filename}")
|
||||
return '250 Message accepted for delivery'
|
||||
except Exception as e:
|
||||
from hashlib import sha256
|
||||
|
|
20
core/mail/tests/v2/test_user_notifications.py
Normal file
20
core/mail/tests/v2/test_user_notifications.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
from django.contrib.auth.models import Permission
|
||||
from django.test import TestCase
|
||||
|
||||
from authentication.models import ExtendedUser
|
||||
from notifications.models import UserNotificationChannel
|
||||
|
||||
|
||||
class UserNotificationTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test')
|
||||
self.user.user_permissions.add(*Permission.objects.all())
|
||||
self.user.save()
|
||||
self.channel = UserNotificationChannel.objects.create(user=self.user, channel_type='telegram',
|
||||
channel_target='123456789',
|
||||
event_filter='*', active=True)
|
||||
|
||||
async def test_telegram_notify(self):
|
||||
pass
|
Loading…
Add table
Add a link
Reference in a new issue