Module kibicara.platforms.email.bot
Expand source code
# Copyright (C) 2020 by Maike <maike@systemli.org>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from kibicara import email
from kibicara.config import config
from kibicara.model import Hood
from kibicara.platformapi import Censor, Spawner
from kibicara.platforms.email.model import Email, EmailSubscribers
from kibicara.webapi.admin import to_token
from logging import getLogger
from smtplib import SMTPException
logger = getLogger(__name__)
class EmailBot(Censor):
def __init__(self, hood):
super().__init__(hood)
self.enabled = hood.email_enabled
@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()
async def run(self):
""" Loop which waits for new messages and sends emails to all subscribers. """
while True:
message = await self.receive()
logger.debug(
'Received message from censor (%s): %s' % (self.hood.name, message.text)
)
for subscriber in await EmailSubscribers.objects.filter(
hood=self.hood
).all():
token = to_token(email=subscriber.email, hood=self.hood.id)
body = (
'%s\n\n--\n'
'If you want to stop receiving these mails,'
'follow this link: %s/hoods/%d/email-unsubscribe?token=%s'
) % (message.text, config['frontend_url'], self.hood.id, token)
try:
logger.debug('Trying to send: \n%s' % body)
email.send_email(
subscriber.email,
"Kibicara " + self.hood.name,
body=body,
)
except (ConnectionRefusedError, SMTPException):
logger.exception("Sending email to subscriber failed.")
spawner = Spawner(Hood, EmailBot)
Classes
class EmailBot (hood)
-
The superclass for a platform bot.
The censor is the superclass for every platform bot. It distributes a message to all other bots from the same hood if it passes the message filter. It provides methods to start and stop the bot and an overwritable stub for a starting routine.
Examples
class XYZPlatform(Censor): def __init__(self, xyz_model): super().__init__(xyz_model.hood) ... async def run(self): await gather(self.poll(), self.push()) ... async def poll(self): while True: # XXX get text message from platform xyz await self.publish(Message(text)) ... async def push(self): while True: message = await self.receive() # XXX send message.text to platform xyz
Args
hood
:Hood
- A Hood Model object
Attributes
hood
:Hood
- A Hood Model object
Expand source code
class EmailBot(Censor): def __init__(self, hood): super().__init__(hood) self.enabled = hood.email_enabled @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() async def run(self): """ Loop which waits for new messages and sends emails to all subscribers. """ while True: message = await self.receive() logger.debug( 'Received message from censor (%s): %s' % (self.hood.name, message.text) ) for subscriber in await EmailSubscribers.objects.filter( hood=self.hood ).all(): token = to_token(email=subscriber.email, hood=self.hood.id) body = ( '%s\n\n--\n' 'If you want to stop receiving these mails,' 'follow this link: %s/hoods/%d/email-unsubscribe?token=%s' ) % (message.text, config['frontend_url'], self.hood.id, token) try: logger.debug('Trying to send: \n%s' % body) email.send_email( subscriber.email, "Kibicara " + self.hood.name, body=body, ) except (ConnectionRefusedError, SMTPException): logger.exception("Sending email to subscriber failed.")
Ancestors
Static methods
async def destroy_hood(hood)
-
Removes all its database entries.
Expand source code
@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()
Methods
async def run(self)
-
Loop which waits for new messages and sends emails to all subscribers.
Expand source code
async def run(self): """ Loop which waits for new messages and sends emails to all subscribers. """ while True: message = await self.receive() logger.debug( 'Received message from censor (%s): %s' % (self.hood.name, message.text) ) for subscriber in await EmailSubscribers.objects.filter( hood=self.hood ).all(): token = to_token(email=subscriber.email, hood=self.hood.id) body = ( '%s\n\n--\n' 'If you want to stop receiving these mails,' 'follow this link: %s/hoods/%d/email-unsubscribe?token=%s' ) % (message.text, config['frontend_url'], self.hood.id, token) try: logger.debug('Trying to send: \n%s' % body) email.send_email( subscriber.email, "Kibicara " + self.hood.name, body=body, ) except (ConnectionRefusedError, SMTPException): logger.exception("Sending email to subscriber failed.")
Inherited members