2020-07-05 21:49:32 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
2020-07-15 21:50:24 +00:00
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
|
|
|
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
|
2020-09-28 22:39:32 +00:00
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2020-07-05 21:49:32 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-10-13 08:35:20 +00:00
|
|
|
from logging import getLogger
|
|
|
|
from smtplib import SMTPException
|
|
|
|
|
2020-07-16 12:29:57 +00:00
|
|
|
from kibicara import email
|
2020-07-06 16:47:35 +00:00
|
|
|
from kibicara.config import config
|
2020-07-15 21:50:24 +00:00
|
|
|
from kibicara.model import Hood
|
|
|
|
from kibicara.platformapi import Censor, Spawner
|
2020-09-06 17:11:18 +00:00
|
|
|
from kibicara.platforms.email.model import Email, EmailSubscribers
|
2020-07-07 13:08:18 +00:00
|
|
|
from kibicara.webapi.admin import to_token
|
2020-07-07 13:28:51 +00:00
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
2020-07-05 21:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmailBot(Censor):
|
2020-07-15 21:50:24 +00:00
|
|
|
def __init__(self, hood):
|
|
|
|
super().__init__(hood)
|
2020-07-18 11:53:11 +00:00
|
|
|
self.enabled = hood.email_enabled
|
2020-07-05 21:49:32 +00:00
|
|
|
|
2020-09-06 17:11:18 +00:00
|
|
|
@classmethod
|
|
|
|
async def destroy_hood(cls, hood):
|
|
|
|
"""Removes all its database entries."""
|
|
|
|
for inbox in await Email.objects.filter(hood=hood).all():
|
|
|
|
await inbox.delete()
|
|
|
|
for subscriber in await EmailSubscribers.objects.filter(hood=hood).all():
|
|
|
|
await subscriber.delete()
|
|
|
|
|
2020-07-05 21:49:32 +00:00
|
|
|
async def run(self):
|
2020-10-13 09:38:33 +00:00
|
|
|
"""Loop which waits for new messages and sends emails to all subscribers."""
|
2020-07-05 21:49:32 +00:00
|
|
|
while True:
|
|
|
|
message = await self.receive()
|
2020-07-15 21:50:24 +00:00
|
|
|
logger.debug(
|
2020-10-12 20:47:06 +00:00
|
|
|
'Received message from censor ({0}): {1}'.format(
|
|
|
|
self.hood.name, message.text
|
|
|
|
)
|
2020-07-15 21:50:24 +00:00
|
|
|
)
|
|
|
|
for subscriber in await EmailSubscribers.objects.filter(
|
|
|
|
hood=self.hood
|
|
|
|
).all():
|
2020-07-07 13:08:18 +00:00
|
|
|
token = to_token(email=subscriber.email, hood=self.hood.id)
|
2020-07-15 21:50:24 +00:00
|
|
|
body = (
|
2020-09-28 22:39:32 +00:00
|
|
|
'{0}\n\n--\n'
|
2020-10-13 01:27:23 +00:00
|
|
|
+ 'If you want to stop receiving these mails,'
|
|
|
|
+ 'follow this link: {1}/hoods/{2}/email-unsubscribe?token={3}'
|
2020-09-28 22:39:32 +00:00
|
|
|
).format(message.text, config['frontend_url'], self.hood.id, token)
|
2020-07-07 13:28:51 +00:00
|
|
|
try:
|
2020-09-28 22:39:32 +00:00
|
|
|
logger.debug('Trying to send: \n{0}'.format(body))
|
2020-07-16 12:29:57 +00:00
|
|
|
email.send_email(
|
2020-09-04 23:39:24 +00:00
|
|
|
subscriber.email,
|
2020-10-13 08:12:35 +00:00
|
|
|
'Kibicara {0}'.format(self.hood.name),
|
2020-09-04 23:39:24 +00:00
|
|
|
body=body,
|
2020-07-07 13:28:51 +00:00
|
|
|
)
|
|
|
|
except (ConnectionRefusedError, SMTPException):
|
2020-10-13 08:12:35 +00:00
|
|
|
logger.exception('Sending email to subscriber failed.')
|
2020-07-05 21:49:32 +00:00
|
|
|
|
|
|
|
|
2020-07-15 21:50:24 +00:00
|
|
|
spawner = Spawner(Hood, EmailBot)
|