From 31fbfbe8c40f37621c1e021a85a5ba5e0225cb7c Mon Sep 17 00:00:00 2001 From: maike Date: Sun, 5 Jul 2020 23:35:15 +0200 Subject: [PATCH] [email] Add MDA script which passes mails to API --- kibicara/kibicara_mda.py | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 kibicara/kibicara_mda.py diff --git a/kibicara/kibicara_mda.py b/kibicara/kibicara_mda.py new file mode 100644 index 0000000..cbf5e85 --- /dev/null +++ b/kibicara/kibicara_mda.py @@ -0,0 +1,46 @@ +# Copyright (C) 2020 by Maike +# +# SPDX-License-Identifier: 0BSD + +import email.parser +import json +import sys +import re +from logging import getLogger +from kibicara.platforms.email.model import Email + + +logger = getLogger(__name__) + + +# 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) + exit(0) +body = { + 'text': text, + 'to': mail['To'].lower(), + 'author': mail['From'].lower(), + 'secret': Email.secret +} + +# POST request mit API-key und JSON-body an /api/email/messages/: +print("curl " + "-X POST http://localhost/api/email/messages/ " + "-H 'Content-Type: application/json' " + "-d " + json.dumps(body))