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 14:33:07 +00:00
|
|
|
from kibicara.platforms.email.model import Email, EmailRecipients
|
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-06 15:21:10 +00:00
|
|
|
from kibicara.model import Hood
|
2020-07-06 15:57:24 +00:00
|
|
|
from kibicara.webapi.hoods import get_hood
|
2020-07-05 21:35:56 +00:00
|
|
|
from ormantic.exceptions import NoMatch
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from sqlite3 import IntegrityError
|
2020-07-06 17:59:00 +00:00
|
|
|
from nacl.encoding import URLSafeBase64Encoder
|
|
|
|
from nacl.secret import SecretBox
|
2020-07-06 15:57:24 +00:00
|
|
|
from os import urandom
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BodyMessage(BaseModel):
|
|
|
|
text: str
|
|
|
|
to: str
|
|
|
|
author: str
|
|
|
|
secret: str
|
|
|
|
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
class Recipient(BaseModel):
|
|
|
|
email: str
|
|
|
|
|
|
|
|
|
2020-07-06 15:21:10 +00:00
|
|
|
async def get_email_bot(to):
|
|
|
|
hood_name = to.split('@')[0]
|
|
|
|
hood = await Hood.objects.get(name=hood_name)
|
2020-07-05 21:35:56 +00:00
|
|
|
try:
|
2020-07-06 15:21:10 +00:00
|
|
|
return await Email.objects.get(hood=hood.id)
|
2020-07-05 21:35:56 +00:00
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
hood_router = APIRouter()
|
|
|
|
mailbox_router = APIRouter()
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2020-07-06 14:33:07 +00:00
|
|
|
# get Email row?
|
2020-07-05 21:35:56 +00:00
|
|
|
@router.get('/')
|
|
|
|
async def test_read_all(hood=Depends(get_hood)):
|
|
|
|
return await Email.objects.filter(hood=hood).all()
|
2020-07-06 15:57:24 +00:00
|
|
|
"""
|
|
|
|
|
2020-07-05 21:35:56 +00:00
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
@hood_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-05 21:35:56 +00:00
|
|
|
try:
|
2020-07-06 15:57:24 +00:00
|
|
|
emailbot = await Email.objects.create(hood=hood, secret=urandom(32))
|
|
|
|
spawner.start(emailbot)
|
|
|
|
return emailbot
|
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 17:14:12 +00:00
|
|
|
@hood_router.delete('/', status_code=status.HTTP_200_OK)
|
|
|
|
async def email_delete(hood=Depends(get_hood)):
|
2020-07-06 15:57:24 +00:00
|
|
|
# who calls this function usually?
|
|
|
|
email_bot = await Email.objects.get(hood=hood)
|
|
|
|
spawner.stop(email_bot)
|
|
|
|
await EmailRecipients.objects.delete_many(hood=hood)
|
|
|
|
await email_bot.delete()
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
@hood_router.post('/recipient/')
|
|
|
|
async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)):
|
2020-07-06 17:59:00 +00:00
|
|
|
secretbox = SecretBox(Email.secret)
|
|
|
|
token = secretbox.encrypt({'email': recipient.email,}, encoder=URLSafeBase64Encoder)
|
|
|
|
asciitoken = token.decode('ascii')
|
2020-07-06 17:14:12 +00:00
|
|
|
confirm_link = (
|
2020-07-06 17:59:00 +00:00
|
|
|
config['root_url'] + "api/" + hood.id + "/email/recipient/confirm/" + asciitoken
|
2020-07-06 17:14:12 +00:00
|
|
|
)
|
2020-07-06 15:21:10 +00:00
|
|
|
send_email(
|
|
|
|
recipient.email,
|
2020-07-06 17:14:12 +00:00
|
|
|
"Subscribe to Kibicara " + hood.name,
|
|
|
|
sender=hood.name,
|
2020-07-06 15:21:10 +00:00
|
|
|
body="To confirm your subscription, follow this link: " + confirm_link,
|
|
|
|
)
|
2020-07-06 14:33:07 +00:00
|
|
|
return status.HTTP_200_OK
|
|
|
|
|
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
@hood_router.post('/recipient/confirm/{token}')
|
|
|
|
async def email_recipient_confirm(token, hood=Depends(get_hood)):
|
2020-07-06 17:59:00 +00:00
|
|
|
secretbox = SecretBox(Email.secret)
|
|
|
|
json = secretbox.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder)
|
2020-07-06 14:33:07 +00:00
|
|
|
try:
|
2020-07-06 15:21:10 +00:00
|
|
|
await EmailRecipients.objects.create(hood=hood.id, email=json['email'])
|
2020-07-06 14:33:07 +00:00
|
|
|
return status.HTTP_201_CREATED
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
|
|
|
|
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
@hood_router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK)
|
|
|
|
async def email_recipient_unsubscribe(token, hood=Depends(get_hood)):
|
2020-07-06 17:59:00 +00:00
|
|
|
secretbox = SecretBox(Email.secret)
|
|
|
|
json = secretbox.decrypt(token.encode('ascii'), encoder=URLSafeBase64Encoder)
|
2020-07-06 17:14:12 +00:00
|
|
|
if hood.id is not json['hood']:
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
2020-07-06 16:47:35 +00:00
|
|
|
await EmailRecipients.objects.delete_many(hood=json['hood'], email=json['email'])
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-06 17:14:12 +00:00
|
|
|
@mailbox_router.post('/messages/')
|
2020-07-05 21:35:56 +00:00
|
|
|
async def email_message_create(message: BodyMessage):
|
|
|
|
# get bot via "To:" header
|
|
|
|
email_bot = await get_email_bot(message.to)
|
|
|
|
# check API secret
|
|
|
|
if message.secret is not email_bot.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 13:14:10 +00:00
|
|
|
if await spawner.get(email_bot).publish(Message(message.text)):
|
|
|
|
return status.HTTP_201_CREATED
|
|
|
|
else:
|
2020-07-06 14:33:07 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS)
|