This commit is contained in:
j3d1 2024-04-25 23:45:57 +02:00
parent b356a85628
commit 4aca7e41fd
4 changed files with 63 additions and 7 deletions

1
README.md Normal file
View file

@ -0,0 +1 @@
ansible-playbook deploy/ansible/playbooks/deploy-c3lf-sys3.yml --inventory=deploy/ansible/inventory.yml

View file

@ -0,0 +1,23 @@
from channels.layers import get_channel_layer
class NotificationDispatcher:
channel_layer = None
def __init__(self):
self.channel_layer = get_channel_layer()
async def dispatch(self, message):
print("Dispatching message:", message)
async def run_forever(self):
# Infinite loop to continuously listen for messages
while True:
# Blocking receive to get the message from the channel layer
message = await self.channel_layer.receive()
if message:
# Process the received message
print("Received message:", message)
await self.dispatch(message)

View file

@ -1,10 +1,9 @@
import logging
import aiosmtplib
from asgiref.sync import sync_to_async
from channels.layers import get_channel_layer
from channels.db import database_sync_to_async
from django.core.files.base import ContentFile
from mail.models import Email, EventAddress, EmailAttachment
from notify_sessions.models import SystemEvent
from tickets.models import IssueThread
@ -82,6 +81,23 @@ def make_reply(reply_email, references=None, event=None):
return reply
def make_notification(message, to, event=None): # TODO where should replies to this go
from email.message import EmailMessage
from core.settings import MAIL_DOMAIN
event = event or "mail"
notification = EmailMessage()
notification["From"] = "notifications@%s" % MAIL_DOMAIN
notification["To"] = to
notification["Subject"] = f"System3 Notification"
# 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, 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)
@ -233,7 +249,13 @@ 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
notifcation = None
if new:
notifcation = f"New issue {active_issue_thread.short_uuid()} created by {sender}"
else:
notifcation = f"Reply to issue {active_issue_thread.short_uuid()} by {sender}"
return email, new, reply, notifcation
class LMTPHandler:
@ -255,16 +277,19 @@ class LMTPHandler:
content = None
try:
content = envelope.content
email, new, reply = await sync_to_async(receive_email)(envelope, log)
email, new, reply, notifcation = await database_sync_to_async(receive_email)(envelope, log)
log.info(f"Created email {email.id}")
systemevent = await sync_to_async(SystemEvent.objects.create)(type='email received', reference=email.id)
systemevent = await database_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"}
)
"message": "email received"})
log.info(f"Sent message to frontend")
if notifcation:
await channel_layer.group_send(
'general', {"type": "generic.event", "name": "push_notification", "event_id": systemevent.id,
"message": notifcation})
if new and reply:
await send_smtp(reply, log)
log.info("Sent auto reply")

View file

@ -12,6 +12,7 @@ django.setup()
from helper import init_loop
from mail.protocol import LMTPHandler
from mail.socket import UnixSocketLMTPController
from mail.notifications import NotificationDispatcher
class UvicornServer(uvicorn.Server):
@ -54,6 +55,11 @@ async def lmtp(loop):
log.info("LMTP done")
async def notifications(loop):
dispatcher = NotificationDispatcher()
await dispatcher.run_forever()
def main():
import sdnotify
import setproctitle
@ -67,6 +73,7 @@ def main():
loop.create_task(web(loop))
# loop.create_task(tcp(loop))
loop.create_task(lmtp(loop))
loop.create_task(notifications(loop))
n = sdnotify.SystemdNotifier()
n.notify("READY=1")
log.info("Server ready")