2020-07-05 21:35:56 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
2020-07-05 21:35:56 +00:00
|
|
|
from kibicara.platforms.email.bot import spawner
|
2020-07-06 18:51:07 +00:00
|
|
|
from kibicara.platforms.email.model import Email, EmailSubscribers
|
2020-07-05 21:35:56 +00:00
|
|
|
from kibicara.platformapi import Message
|
2020-07-06 15:00:24 +00:00
|
|
|
from kibicara.config import config
|
2020-07-06 14:33:07 +00:00
|
|
|
from kibicara.email import send_email
|
2020-07-09 01:10:37 +00:00
|
|
|
from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
|
2020-07-05 21:35:56 +00:00
|
|
|
from pydantic import BaseModel
|
2020-07-07 13:08:18 +00:00
|
|
|
from ormantic.exceptions import NoMatch
|
2020-07-05 21:35:56 +00:00
|
|
|
from sqlite3 import IntegrityError
|
2020-07-07 13:08:18 +00:00
|
|
|
from kibicara.webapi.admin import from_token, to_token
|
2020-07-06 15:57:24 +00:00
|
|
|
from os import urandom
|
2020-07-07 13:28:51 +00:00
|
|
|
from smtplib import SMTPException
|
2020-07-07 13:08:18 +00:00
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BodyMessage(BaseModel):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" This model shows which values are supplied by the MDA listener script. """
|
2020-07-06 19:26:32 +00:00
|
|
|
|
2020-07-05 21:35:56 +00:00
|
|
|
text: str
|
|
|
|
author: str
|
|
|
|
secret: str
|
|
|
|
|
|
|
|
|
2020-07-06 18:51:07 +00:00
|
|
|
class Subscriber(BaseModel):
|
|
|
|
""" This model holds the email address of a fresh subscriber. """
|
2020-07-06 19:26:32 +00:00
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
email: str
|
|
|
|
|
|
|
|
|
2020-07-07 13:08:18 +00:00
|
|
|
async def get_email(hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
return await Email.objects.get(hood=hood)
|
|
|
|
except NoMatch:
|
2020-07-07 13:28:51 +00:00
|
|
|
return HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
2020-07-07 13:08:18 +00:00
|
|
|
|
|
|
|
|
2020-07-06 19:53:48 +00:00
|
|
|
router = APIRouter()
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-06 19:53:48 +00:00
|
|
|
@router.post('/', status_code=status.HTTP_201_CREATED)
|
2020-07-06 15:57:24 +00:00
|
|
|
async def email_create(hood=Depends(get_hood)):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Create an Email bot. Call this when creating a hood.
|
|
|
|
|
|
|
|
:param hood: Hood.id of the hood the Email bot is supposed to belong to.
|
|
|
|
:return: Email row of the new email bot.
|
|
|
|
"""
|
2020-07-05 21:35:56 +00:00
|
|
|
try:
|
2020-07-07 13:08:18 +00:00
|
|
|
email_row = await Email.objects.create(hood=hood, secret=urandom(32).hex())
|
|
|
|
spawner.start(email_row)
|
|
|
|
return email_row
|
2020-07-05 21:35:56 +00:00
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
2020-07-06 15:57:24 +00:00
|
|
|
|
|
|
|
|
2020-07-06 19:53:48 +00:00
|
|
|
@router.delete('/', status_code=status.HTTP_200_OK)
|
2020-07-06 17:14:12 +00:00
|
|
|
async def email_delete(hood=Depends(get_hood)):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Delete an Email bot. Call this when deleting a hood.
|
|
|
|
Stops and deletes the Email bot as well as all subscribers.
|
|
|
|
|
|
|
|
:param hood: Hood the Email bot belongs to.
|
|
|
|
"""
|
2020-07-07 13:08:18 +00:00
|
|
|
email_row = await get_email(hood=hood)
|
|
|
|
spawner.stop(email_row)
|
2020-07-06 18:51:07 +00:00
|
|
|
await EmailSubscribers.objects.delete_many(hood=hood.id)
|
2020-07-07 13:08:18 +00:00
|
|
|
await email_row.delete()
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-09 01:10:37 +00:00
|
|
|
@router.post('/subscribe/', status_code=status.HTTP_200_OK)
|
|
|
|
async def email_subscribe(subscriber: Subscriber, hood=Depends(get_hood_unauthorized)):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Send a confirmation mail to subscribe to messages via email.
|
|
|
|
|
|
|
|
:param subscriber: Subscriber object, holds the email address.
|
|
|
|
:param hood: Hood the Email bot belongs to.
|
|
|
|
:return: Returns status code 200 after sending confirmation email.
|
|
|
|
"""
|
2020-07-07 13:08:18 +00:00
|
|
|
token = to_token(email=subscriber.email)
|
2020-07-06 17:14:12 +00:00
|
|
|
confirm_link = (
|
2020-07-07 13:08:18 +00:00
|
|
|
config['root_url'] + "api/" + str(hood.id) + "/email/subscribe/confirm/" + token
|
2020-07-06 17:14:12 +00:00
|
|
|
)
|
2020-07-07 13:28:51 +00:00
|
|
|
try:
|
|
|
|
send_email(
|
|
|
|
subscriber.email,
|
|
|
|
"Subscribe to Kibicara " + hood.name,
|
|
|
|
sender=hood.name,
|
|
|
|
body="To confirm your subscription, follow this link: " + confirm_link,
|
|
|
|
)
|
2020-07-09 01:10:37 +00:00
|
|
|
except ConnectionRefusedError:
|
|
|
|
logger.info(token)
|
|
|
|
logger.error("Sending subscription confirmation email failed.", exc_info=True)
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY)
|
|
|
|
except SMTPException:
|
|
|
|
logger.info(token)
|
2020-07-07 13:28:51 +00:00
|
|
|
logger.error("Sending subscription confirmation email failed.", exc_info=True)
|
|
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY)
|
2020-07-06 14:33:07 +00:00
|
|
|
|
|
|
|
|
2020-07-09 01:10:37 +00:00
|
|
|
@router.get('/subscribe/confirm/{token}', status_code=status.HTTP_201_CREATED)
|
|
|
|
async def email_subscribe_confirm(token, hood=Depends(get_hood_unauthorized)):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Confirm a new subscriber and add them to the database.
|
|
|
|
|
|
|
|
:param token: encrypted JSON token, holds the email of the subscriber.
|
|
|
|
:param hood: Hood the Email bot belongs to.
|
|
|
|
:return: Returns status code 200 after adding the subscriber to the database.
|
|
|
|
"""
|
2020-07-07 13:08:18 +00:00
|
|
|
payload = from_token(token)
|
2020-07-06 14:33:07 +00:00
|
|
|
try:
|
2020-07-07 13:08:18 +00:00
|
|
|
await EmailSubscribers.objects.create(hood=hood.id, email=payload['email'])
|
2020-07-06 14:33:07 +00:00
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
|
2020-07-06 19:53:48 +00:00
|
|
|
@router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK)
|
2020-07-09 01:10:37 +00:00
|
|
|
async def email_unsubscribe(token, hood=Depends(get_hood_unauthorized)):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Remove a subscriber from the database when they click on an unsubscribe link.
|
|
|
|
|
|
|
|
:param token: encrypted JSON token, holds subscriber email + hood.id.
|
|
|
|
:param hood: Hood the Email bot belongs to.
|
|
|
|
"""
|
2020-07-09 01:10:37 +00:00
|
|
|
logger.warning("token is: " + token)
|
2020-07-07 13:08:18 +00:00
|
|
|
payload = from_token(token)
|
2020-07-06 18:51:07 +00:00
|
|
|
# If token.hood and url.hood are different, raise an error:
|
2020-07-07 13:08:18 +00:00
|
|
|
if hood.id is not payload['hood']:
|
2020-07-06 17:14:12 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
2020-07-07 13:28:51 +00:00
|
|
|
await EmailSubscribers.objects.delete_many(
|
|
|
|
hood=payload['hood'], email=payload['email']
|
|
|
|
)
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-09 01:10:37 +00:00
|
|
|
@router.post('/messages/', status_code=status.HTTP_201_CREATED)
|
|
|
|
async def email_message_create(
|
|
|
|
message: BodyMessage, hood=Depends(get_hood_unauthorized)
|
|
|
|
):
|
2020-07-06 18:51:07 +00:00
|
|
|
""" Receive a message from the MDA and pass it to the censor.
|
|
|
|
|
|
|
|
:param message: BodyMessage object, holds the message.
|
2020-07-06 19:53:48 +00:00
|
|
|
:param hood: Hood the Email bot belongs to.
|
2020-07-06 18:51:07 +00:00
|
|
|
:return: returns status code 201 if the message is accepted by the censor.
|
|
|
|
"""
|
2020-07-05 21:35:56 +00:00
|
|
|
# get bot via "To:" header
|
2020-07-07 13:08:18 +00:00
|
|
|
email_row = await get_email(hood)
|
2020-07-05 21:35:56 +00:00
|
|
|
# check API secret
|
2020-07-06 19:53:48 +00:00
|
|
|
if message.secret is not email_row.secret:
|
2020-07-06 14:33:07 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
2020-07-05 21:35:56 +00:00
|
|
|
# pass message.text to bot.py
|
2020-07-06 19:53:48 +00:00
|
|
|
if await spawner.get(email_row).publish(Message(message.text)):
|
2020-07-09 01:10:37 +00:00
|
|
|
pass
|
2020-07-06 13:14:10 +00:00
|
|
|
else:
|
2020-07-06 14:33:07 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS)
|