2020-07-05 21:35:15 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
|
|
|
import email.parser
|
|
|
|
import sys
|
|
|
|
import re
|
2020-07-06 19:41:22 +00:00
|
|
|
import requests
|
2020-07-05 21:35:15 +00:00
|
|
|
from logging import getLogger
|
|
|
|
from kibicara.platforms.email.model import Email
|
2020-07-06 19:53:48 +00:00
|
|
|
from kibicara.model import Hood
|
2020-07-06 20:15:38 +00:00
|
|
|
import argparse
|
2020-07-05 21:35:15 +00:00
|
|
|
|
|
|
|
|
2020-07-05 21:42:07 +00:00
|
|
|
def main():
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2020-07-06 20:15:38 +00:00
|
|
|
# the MDA passes the recipient address as command line argument
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("recipient_address")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-07-05 21:42:07 +00:00
|
|
|
# read mail from STDIN
|
|
|
|
mailbytes = bytes(sys.stdin.read())
|
|
|
|
|
|
|
|
# parse plaintext to email.EmailMessage object
|
|
|
|
myparser = email.parser.BytesParser()
|
|
|
|
mail = myparser.parsebytes(mailbytes)
|
|
|
|
|
|
|
|
# extract relevant data from mail
|
|
|
|
for part in mail.walk():
|
|
|
|
try:
|
|
|
|
text = part.get_body(('plain',))
|
|
|
|
if not text:
|
|
|
|
text = re.sub(r'<[^>]*>', '', part.get_body(('html',)))
|
|
|
|
if not text:
|
|
|
|
logger.error('No suitable message body')
|
|
|
|
exit(0)
|
|
|
|
except Exception:
|
|
|
|
logger.info("No Body in this message part", exc_info=True)
|
2020-07-05 21:35:15 +00:00
|
|
|
exit(0)
|
2020-07-06 20:15:38 +00:00
|
|
|
# extract hood name from the envelope recipient address
|
|
|
|
hood_name = args.recipient_address.split('@')[0]
|
2020-07-06 19:53:48 +00:00
|
|
|
hood = await Hood.objects.get(name=hood_name)
|
2020-07-05 21:42:07 +00:00
|
|
|
body = {
|
|
|
|
'text': text,
|
2020-07-06 15:21:10 +00:00
|
|
|
'author': mail.get_unixfrom(),
|
2020-07-06 13:16:28 +00:00
|
|
|
'secret': Email.secret,
|
2020-07-05 21:42:07 +00:00
|
|
|
}
|
2020-07-06 19:53:48 +00:00
|
|
|
requests.post('http://localhost/api/' + hood.id + '/email/messages/', data=body)
|