2020-06-05 23:35:52 +02:00
|
|
|
import base64
|
2019-01-09 12:27:29 +01:00
|
|
|
import email
|
2020-06-05 23:26:35 +02:00
|
|
|
import email.policy
|
2020-01-14 21:59:46 +01:00
|
|
|
import os
|
2020-06-05 23:26:35 +02:00
|
|
|
from email.message import EmailMessage
|
2020-06-05 23:35:52 +02:00
|
|
|
from typing import Optional
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2019-01-09 12:27:29 +01:00
|
|
|
from django.conf import settings
|
2021-07-16 22:11:10 +02:00
|
|
|
from django.core.management.base import CommandError, CommandParser
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2019-03-16 12:28:18 +01:00
|
|
|
from zerver.lib.email_mirror import mirror_email_message
|
2019-03-21 10:24:56 +01:00
|
|
|
from zerver.lib.email_mirror_helpers import encode_email_address
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.models import Realm, get_realm, get_stream
|
2019-03-16 12:28:18 +01:00
|
|
|
|
2019-01-09 12:27:29 +01:00
|
|
|
# This command loads an email from a specified file and sends it
|
|
|
|
# to the email mirror. Simple emails can be passed in a JSON file,
|
|
|
|
# Look at zerver/tests/fixtures/email/1.json for an example of how
|
|
|
|
# it should look. You can also pass a file which has the raw email,
|
2020-06-05 23:26:35 +02:00
|
|
|
# for example by writing an email.message.EmailMessage type object
|
2019-01-09 12:27:29 +01:00
|
|
|
# to a file using as_string() or as_bytes() methods, or copy-pasting
|
|
|
|
# the content of "Show original" on an email in Gmail.
|
|
|
|
# See zerver/tests/fixtures/email/1.txt for a very simple example,
|
|
|
|
# but anything that the message_from_binary_file function
|
|
|
|
# from the email library can parse should work.
|
2020-03-28 01:25:56 +01:00
|
|
|
# Value of the TO: header doesn't matter, as it is overridden
|
2019-01-09 12:27:29 +01:00
|
|
|
# by the command in order for the email to be sent to the correct stream.
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-01-09 12:27:29 +01:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """
|
|
|
|
Send specified email from a fixture file to the email mirror
|
|
|
|
Example:
|
|
|
|
./manage.py send_to_email_mirror --fixture=zerver/tests/fixtures/emails/filename
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-f",
|
|
|
|
"--fixture",
|
|
|
|
help="The path to the email message you'd like to send "
|
|
|
|
"to the email mirror.\n"
|
|
|
|
"Accepted formats: json or raw email file. "
|
|
|
|
"See zerver/tests/fixtures/email/ for examples",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-s",
|
|
|
|
"--stream",
|
|
|
|
help="The name of the stream to which you'd like to send "
|
|
|
|
"the message. Default: Denmark",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-01-09 12:27:29 +01:00
|
|
|
|
|
|
|
self.add_realm_args(parser, help="Specify which realm to connect to; default is zulip")
|
|
|
|
|
2019-07-29 00:38:14 +02:00
|
|
|
def handle(self, **options: Optional[str]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["fixture"] is None:
|
|
|
|
self.print_help("./manage.py", "send_to_email_mirror")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["stream"] is None:
|
2019-01-09 12:27:29 +01:00
|
|
|
stream = "Denmark"
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
stream = options["stream"]
|
2019-01-09 12:27:29 +01:00
|
|
|
|
|
|
|
realm = self.get_realm(options)
|
|
|
|
if realm is None:
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
full_fixture_path = os.path.join(settings.DEPLOY_ROOT, options["fixture"])
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
# parse the input email into EmailMessage type and prepare to process_message() it
|
2019-01-09 12:27:29 +01:00
|
|
|
message = self._parse_email_fixture(full_fixture_path)
|
|
|
|
self._prepare_message(message, realm, stream)
|
|
|
|
|
2020-06-05 23:35:52 +02:00
|
|
|
mirror_email_message(
|
2021-02-12 08:20:45 +01:00
|
|
|
message["To"].addresses[0].addr_spec,
|
2020-06-05 23:35:52 +02:00
|
|
|
base64.b64encode(message.as_bytes()).decode(),
|
|
|
|
)
|
2019-01-09 12:27:29 +01:00
|
|
|
|
|
|
|
def _does_fixture_path_exist(self, fixture_path: str) -> bool:
|
|
|
|
return os.path.exists(fixture_path)
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
def _parse_email_json_fixture(self, fixture_path: str) -> EmailMessage:
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(fixture_path, "rb") as fp:
|
|
|
|
json_content = orjson.loads(fp.read())[0]
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
message = EmailMessage()
|
2021-02-12 08:20:45 +01:00
|
|
|
message["From"] = json_content["from"]
|
|
|
|
message["Subject"] = json_content["subject"]
|
|
|
|
message.set_content(json_content["body"])
|
2019-01-09 12:27:29 +01:00
|
|
|
return message
|
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
def _parse_email_fixture(self, fixture_path: str) -> EmailMessage:
|
2019-01-09 12:27:29 +01:00
|
|
|
if not self._does_fixture_path_exist(fixture_path):
|
2021-02-12 08:20:45 +01:00
|
|
|
raise CommandError(f"Fixture {fixture_path} does not exist")
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if fixture_path.endswith(".json"):
|
2020-06-05 23:26:35 +02:00
|
|
|
return self._parse_email_json_fixture(fixture_path)
|
2019-01-09 12:27:29 +01:00
|
|
|
else:
|
|
|
|
with open(fixture_path, "rb") as fp:
|
2020-06-05 23:26:35 +02:00
|
|
|
message = email.message_from_binary_file(fp, policy=email.policy.default)
|
2020-09-02 02:50:08 +02:00
|
|
|
# https://github.com/python/typeshed/issues/2417
|
|
|
|
assert isinstance(message, EmailMessage)
|
2020-06-05 23:26:35 +02:00
|
|
|
return message
|
2019-01-09 12:27:29 +01:00
|
|
|
|
2020-06-05 23:26:35 +02:00
|
|
|
def _prepare_message(self, message: EmailMessage, realm: Realm, stream_name: str) -> None:
|
2019-01-09 12:27:29 +01:00
|
|
|
stream = get_stream(stream_name, realm)
|
|
|
|
|
2020-01-14 15:31:53 +01:00
|
|
|
# The block below ensures that the imported email message doesn't have any recipient-like
|
|
|
|
# headers that are inconsistent with the recipient we want (the stream address).
|
2021-02-12 08:19:30 +01:00
|
|
|
recipient_headers = [
|
|
|
|
"X-Gm-Original-To",
|
|
|
|
"Delivered-To",
|
|
|
|
"Envelope-To",
|
|
|
|
"Resent-To",
|
|
|
|
"Resent-CC",
|
|
|
|
"CC",
|
|
|
|
]
|
2019-01-09 12:27:29 +01:00
|
|
|
for header in recipient_headers:
|
|
|
|
if header in message:
|
|
|
|
del message[header]
|
|
|
|
message[header] = encode_email_address(stream)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if "To" in message:
|
|
|
|
del message["To"]
|
|
|
|
message["To"] = encode_email_address(stream)
|