[email] Limit messages to subscribers

This commit is contained in:
Cathy Hu 2020-09-11 22:03:02 +02:00
parent 2475080bfd
commit 4edaeb706a

View file

@ -7,9 +7,10 @@
from asyncio import run as asyncio_run from asyncio import run as asyncio_run
from email.parser import BytesParser from email.parser import BytesParser
from email.policy import default from email.policy import default
from email.utils import parseaddr
from fastapi import status from fastapi import status
from kibicara.config import args, config from kibicara.config import args, config
from kibicara.platforms.email.model import Email from kibicara.platforms.email.model import Email, EmailSubscribers
from logging import getLogger from logging import getLogger
from ormantic import NoMatch from ormantic import NoMatch
from re import sub from re import sub
@ -36,6 +37,25 @@ class Main:
# read mail from STDIN and parse to EmailMessage object # read mail from STDIN and parse to EmailMessage object
message = BytesParser(policy=default).parsebytes(stdin.buffer.read()) message = BytesParser(policy=default).parsebytes(stdin.buffer.read())
sender = ''
if message.get('sender'):
sender = message.get('sender')
elif message.get('from'):
sender = message.get('from')
else:
logger.error('No Sender of From header')
exit(1)
sender = parseaddr(sender)[1]
if not sender:
logger.error('Could not parse sender')
exit(1)
maybe_subscriber = await EmailSubscribers.objects.filter(email=sender).all()
if len(maybe_subscriber) != 1 or maybe_subscriber[0].hood.id != email.id:
logger.error('Not a subscriber')
exit(1)
# extract relevant data from mail # extract relevant data from mail
text = sub( text = sub(
r'<[^>]*>', r'<[^>]*>',