2020-07-05 21:35:15 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
|
|
|
import email.parser
|
2020-07-13 01:58:14 +00:00
|
|
|
from email.policy import default
|
|
|
|
import email.message
|
2020-07-05 21:35:15 +00:00
|
|
|
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
|
2020-07-06 19:53:48 +00:00
|
|
|
from kibicara.model import Hood
|
2020-07-12 17:53:22 +00:00
|
|
|
from kibicara.platforms.email.model import Email
|
2020-07-12 19:19:58 +00:00
|
|
|
import argparse
|
2020-07-13 01:58:14 +00:00
|
|
|
from asyncio import run
|
|
|
|
from fastapi import status
|
|
|
|
from ormantic import NoMatch
|
2020-07-05 21:35:15 +00:00
|
|
|
|
|
|
|
|
2020-07-13 01:58:14 +00:00
|
|
|
async def async_main(mail=None, hood_name=None):
|
2020-07-05 21:42:07 +00:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2020-07-12 19:19:58 +00:00
|
|
|
# the MDA passes the recipient address as command line argument
|
|
|
|
parser = argparse.ArgumentParser()
|
2020-07-13 01:58:14 +00:00
|
|
|
if hood_name is None:
|
|
|
|
parser.add_argument("recipient_address")
|
|
|
|
args = parser.parse_args()
|
|
|
|
# extract hood name from the envelope recipient address
|
|
|
|
hood_name = args.recipient_address.split('@')[0].lower()
|
2020-07-12 19:19:58 +00:00
|
|
|
|
2020-07-13 01:58:14 +00:00
|
|
|
if mail is None:
|
|
|
|
# read mail from STDIN
|
|
|
|
mail = bytes(sys.stdin.read(), encoding='ascii')
|
|
|
|
# parse plaintext to email.EmailMessage object
|
|
|
|
mail = email.parser.BytesParser(policy=default).parsebytes(mail)
|
|
|
|
else:
|
|
|
|
mail = email.parser.Parser(policy=default).parsestr(mail)
|
2020-07-05 21:42:07 +00:00
|
|
|
|
2020-07-13 01:58:14 +00:00
|
|
|
assert type(mail) == email.message.EmailMessage
|
2020-07-05 21:42:07 +00:00
|
|
|
|
|
|
|
# extract relevant data from mail
|
2020-07-13 01:58:14 +00:00
|
|
|
body = mail.get_body(preferencelist=('plain', 'html'))
|
|
|
|
if body['content-type'].subtype == 'plain':
|
|
|
|
text = str(body.get_content())
|
|
|
|
elif body['content-type'].subtype == 'html':
|
|
|
|
text = re.sub(r'<[^>]*>', '', body.get_content())
|
|
|
|
|
|
|
|
try:
|
|
|
|
text = str(text)
|
|
|
|
except UnboundLocalError:
|
|
|
|
print('No suitable message body')
|
|
|
|
exit(1)
|
|
|
|
try:
|
|
|
|
hood = await Hood.objects.get(name=hood_name)
|
|
|
|
except NoMatch:
|
|
|
|
print('No hood with this name')
|
2020-07-12 16:57:06 +00:00
|
|
|
exit(1)
|
2020-07-12 17:53:22 +00:00
|
|
|
email_row = await Email.objects.get(hood=hood)
|
2020-07-13 02:19:04 +00:00
|
|
|
author = mail.get_unixfrom()
|
|
|
|
if author is None:
|
|
|
|
author = mail['From']
|
2020-07-05 21:42:07 +00:00
|
|
|
body = {
|
|
|
|
'text': text,
|
2020-07-13 02:19:04 +00:00
|
|
|
'author': author,
|
2020-07-09 09:39:51 +00:00
|
|
|
'secret': email_row.secret,
|
2020-07-05 21:42:07 +00:00
|
|
|
}
|
2020-07-13 01:58:14 +00:00
|
|
|
response = requests.post(
|
|
|
|
'http://localhost:8000/api/hoods/%d/email/messages/' % hood.id, json=body
|
2020-07-11 02:36:12 +00:00
|
|
|
)
|
2020-07-13 01:58:14 +00:00
|
|
|
if response.status_code == status.HTTP_201_CREATED:
|
|
|
|
exit(0)
|
|
|
|
elif response.status_code == status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS:
|
|
|
|
print("Message was't accepted: " + text)
|
|
|
|
elif response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY:
|
|
|
|
print("Malformed request: " + str(response.json()))
|
|
|
|
elif response.status_code == status.HTTP_401_UNAUTHORIZED:
|
|
|
|
logger.error('Wrong API secret. kibicara_mda seems to be misconfigured')
|
|
|
|
else:
|
|
|
|
print(str(response.status_code))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run(async_main())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__.endswith('kibicara_mda'):
|
|
|
|
mail = """From test@example.com Tue Jun 16 15:33:19 2020
|
|
|
|
Return-path: <test@example.com>
|
|
|
|
Envelope-to: hood@localhost
|
|
|
|
Delivery-date: Tue, 16 Jun 2020 15:33:19 +0200
|
|
|
|
Received: from [23.143.35.123] (helo=example.com)
|
|
|
|
by example.com with smtp (Exim 4.89)
|
|
|
|
(envelope-from <test@example.com>)
|
|
|
|
id 1jlC1e-0005ro-PL
|
|
|
|
for hood@localhost; Tue, 16 Jun 2020 15:33:19 +0200
|
|
|
|
Message-ID: <B5F50812.F55DFD8B@example.com>
|
|
|
|
Date: Tue, 16 Jun 2020 06:53:19 -0700
|
|
|
|
Reply-To: "Test" <test@example.com>
|
|
|
|
From: "Test" <test@example.com>
|
|
|
|
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.17) Gecko/20080914 Thunderbird/2.0.0.17
|
|
|
|
MIME-Version: 1.0
|
|
|
|
To: <hood@localhost>
|
|
|
|
Subject: Chat: test
|
|
|
|
Content-Type: multipart/mixed; boundary="AqNPlAX243a8sip3B7kXv8UKD8wuti"
|
|
|
|
|
|
|
|
|
|
|
|
--AqNPlAX243a8sip3B7kXv8UKD8wuti
|
|
|
|
Content-Type: text/plain; charset=utf-8
|
|
|
|
|
|
|
|
test
|
|
|
|
|
|
|
|
--AqNPlAX243a8sip3B7kXv8UKD8wuti--
|
|
|
|
"""
|
|
|
|
run(async_main(mail=mail, hood_name='hood'))
|