2013-12-16 23:32:08 +01:00
|
|
|
from __future__ import absolute_import
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Text
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
|
|
|
from email.header import decode_header
|
2016-06-05 21:16:54 +02:00
|
|
|
import email.message as message
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2015-10-14 17:11:50 +02:00
|
|
|
from zerver.lib.actions import decode_email_address, get_email_gateway_message_string_from_address, \
|
|
|
|
internal_send_message
|
2014-01-24 22:29:17 +01:00
|
|
|
from zerver.lib.notifications import convert_html_to_markdown
|
2017-04-18 17:28:55 +02:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
2014-07-25 10:40:40 +02:00
|
|
|
from zerver.lib.redis_utils import get_redis_client
|
2013-12-16 23:32:08 +01:00
|
|
|
from zerver.lib.upload import upload_message_image
|
2014-07-25 10:40:40 +02:00
|
|
|
from zerver.lib.utils import generate_random_token
|
2016-07-04 17:11:00 +02:00
|
|
|
from zerver.lib.str_utils import force_text
|
2017-06-26 19:43:32 +02:00
|
|
|
from zerver.lib.send_email import FromAddress
|
2017-05-22 23:37:15 +02:00
|
|
|
from zerver.models import Stream, Recipient, \
|
2016-06-05 21:16:54 +02:00
|
|
|
get_user_profile_by_id, get_display_recipient, get_recipient, \
|
2017-05-22 23:37:15 +02:00
|
|
|
Message, Realm, UserProfile, get_system_bot
|
2016-12-21 13:17:53 +01:00
|
|
|
from six import binary_type
|
2015-11-01 17:14:25 +01:00
|
|
|
import six
|
2016-07-12 14:55:02 +02:00
|
|
|
import talon
|
|
|
|
from talon import quotations
|
|
|
|
|
|
|
|
talon.init()
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def redact_stream(error_message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2013-12-16 23:32:08 +01:00
|
|
|
domain = settings.EMAIL_GATEWAY_PATTERN.rsplit('@')[-1]
|
2016-06-05 21:16:54 +02:00
|
|
|
stream_match = re.search(u'\\b(.*?)@' + domain, error_message)
|
2013-12-16 23:32:08 +01:00
|
|
|
if stream_match:
|
|
|
|
stream_name = stream_match.groups()[0]
|
|
|
|
return error_message.replace(stream_name, "X" * len(stream_name))
|
|
|
|
return error_message
|
|
|
|
|
|
|
|
def report_to_zulip(error_message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> None
|
2016-08-23 06:24:20 +02:00
|
|
|
if settings.ERROR_BOT is None:
|
|
|
|
return
|
2017-05-22 23:37:15 +02:00
|
|
|
error_bot = get_system_bot(settings.ERROR_BOT)
|
2016-08-23 06:24:20 +02:00
|
|
|
error_stream = Stream.objects.get(name="errors", realm=error_bot.realm)
|
|
|
|
send_zulip(settings.ERROR_BOT, error_stream, u"email mirror error",
|
2016-06-05 21:16:54 +02:00
|
|
|
u"""~~~\n%s\n~~~""" % (error_message,))
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
def log_and_report(email_message, error_message, debug_info):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (message.Message, Text, Dict[str, Any]) -> None
|
2016-06-05 21:16:54 +02:00
|
|
|
scrubbed_error = u"Sender: %s\n%s" % (email_message.get("From"),
|
2016-07-07 19:02:03 +02:00
|
|
|
redact_stream(error_message))
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
if "to" in debug_info:
|
2016-06-05 21:16:54 +02:00
|
|
|
scrubbed_error = u"Stream: %s\n%s" % (redact_stream(debug_info["to"]),
|
2016-07-07 19:02:03 +02:00
|
|
|
scrubbed_error)
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
if "stream" in debug_info:
|
2017-03-13 17:42:14 +01:00
|
|
|
scrubbed_error = u"Realm: %s\n%s" % (debug_info["stream"].realm.string_id,
|
2016-07-07 19:02:03 +02:00
|
|
|
scrubbed_error)
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
logger.error(scrubbed_error)
|
|
|
|
report_to_zulip(scrubbed_error)
|
|
|
|
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
# Temporary missed message addresses
|
|
|
|
|
|
|
|
redis_client = get_redis_client()
|
|
|
|
|
|
|
|
|
|
|
|
def missed_message_redis_key(token):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2014-07-25 10:40:40 +02:00
|
|
|
return 'missed_message:' + token
|
|
|
|
|
|
|
|
|
|
|
|
def is_missed_message_address(address):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> bool
|
2015-10-14 17:11:50 +02:00
|
|
|
msg_string = get_email_gateway_message_string_from_address(address)
|
2016-09-22 18:11:09 +02:00
|
|
|
return is_mm_32_format(msg_string)
|
2014-07-25 10:40:40 +02:00
|
|
|
|
2016-09-22 18:11:09 +02:00
|
|
|
def is_mm_32_format(msg_string):
|
2017-05-24 23:48:45 +02:00
|
|
|
# type: (Optional[Text]) -> bool
|
2016-09-22 18:11:09 +02:00
|
|
|
'''
|
|
|
|
Missed message strings are formatted with a little "mm" prefix
|
|
|
|
followed by a randomly generated 32-character string.
|
|
|
|
'''
|
2017-05-24 23:48:45 +02:00
|
|
|
return msg_string is not None and msg_string.startswith('mm') and len(msg_string) == 34
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
def get_missed_message_token_from_address(address):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2015-10-14 17:11:50 +02:00
|
|
|
msg_string = get_email_gateway_message_string_from_address(address)
|
|
|
|
|
2016-09-22 18:55:18 +02:00
|
|
|
if msg_string is None:
|
|
|
|
raise ZulipEmailForwardError('Address not recognized by gateway.')
|
|
|
|
|
2016-09-22 18:41:10 +02:00
|
|
|
if not is_mm_32_format(msg_string):
|
2014-07-25 10:40:40 +02:00
|
|
|
raise ZulipEmailForwardError('Could not parse missed message address')
|
|
|
|
|
2015-10-14 17:11:50 +02:00
|
|
|
# strip off the 'mm' before returning the redis key
|
|
|
|
return msg_string[2:]
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
def create_missed_message_address(user_profile, message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (UserProfile, Message) -> Text
|
2016-07-31 16:49:31 +02:00
|
|
|
if settings.EMAIL_GATEWAY_PATTERN == '':
|
|
|
|
logging.warning("EMAIL_GATEWAY_PATTERN is an empty string, using "
|
|
|
|
"NOREPLY_EMAIL_ADDRESS in the 'from' field.")
|
2017-06-26 19:43:32 +02:00
|
|
|
return FromAddress.NOREPLY
|
2016-07-31 16:49:31 +02:00
|
|
|
|
2014-08-11 14:15:16 +02:00
|
|
|
if message.recipient.type == Recipient.PERSONAL:
|
|
|
|
# We need to reply to the sender so look up their personal recipient_id
|
|
|
|
recipient_id = get_recipient(Recipient.PERSONAL, message.sender_id).id
|
|
|
|
else:
|
|
|
|
recipient_id = message.recipient_id
|
|
|
|
|
2014-07-25 10:40:40 +02:00
|
|
|
data = {
|
|
|
|
'user_profile_id': user_profile.id,
|
2014-08-11 14:15:16 +02:00
|
|
|
'recipient_id': recipient_id,
|
2014-07-25 10:40:40 +02:00
|
|
|
'subject': message.subject,
|
|
|
|
}
|
|
|
|
|
|
|
|
while True:
|
|
|
|
token = generate_random_token(32)
|
|
|
|
key = missed_message_redis_key(token)
|
|
|
|
if redis_client.hsetnx(key, 'uses_left', 1):
|
|
|
|
break
|
|
|
|
|
|
|
|
with redis_client.pipeline() as pipeline:
|
|
|
|
pipeline.hmset(key, data)
|
|
|
|
pipeline.expire(key, 60 * 60 * 24 * 5)
|
|
|
|
pipeline.execute()
|
|
|
|
|
2016-06-05 21:16:54 +02:00
|
|
|
address = u'mm' + token
|
2016-05-04 23:16:27 +02:00
|
|
|
return settings.EMAIL_GATEWAY_PATTERN % (address,)
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def mark_missed_message_address_as_used(address):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> None
|
2014-07-25 10:40:40 +02:00
|
|
|
token = get_missed_message_token_from_address(address)
|
|
|
|
key = missed_message_redis_key(token)
|
|
|
|
with redis_client.pipeline() as pipeline:
|
|
|
|
pipeline.hincrby(key, 'uses_left', -1)
|
|
|
|
pipeline.expire(key, 60 * 60 * 24 * 5)
|
|
|
|
new_value = pipeline.execute()[0]
|
2014-08-11 08:06:07 +02:00
|
|
|
if new_value < 0:
|
2014-07-25 10:40:40 +02:00
|
|
|
redis_client.delete(key)
|
|
|
|
raise ZulipEmailForwardError('Missed message address has already been used')
|
|
|
|
|
|
|
|
|
|
|
|
def send_to_missed_message_address(address, message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, message.Message) -> None
|
2014-07-25 10:40:40 +02:00
|
|
|
token = get_missed_message_token_from_address(address)
|
|
|
|
key = missed_message_redis_key(token)
|
2014-08-11 08:06:07 +02:00
|
|
|
result = redis_client.hmget(key, 'user_profile_id', 'recipient_id', 'subject')
|
2014-08-11 14:15:16 +02:00
|
|
|
if not all(val is not None for val in result):
|
2014-07-25 10:40:40 +02:00
|
|
|
raise ZulipEmailForwardError('Missing missed message address data')
|
2014-08-11 08:06:07 +02:00
|
|
|
user_profile_id, recipient_id, subject = result
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
user_profile = get_user_profile_by_id(user_profile_id)
|
|
|
|
recipient = Recipient.objects.get(id=recipient_id)
|
|
|
|
display_recipient = get_display_recipient(recipient)
|
|
|
|
|
2014-08-11 14:15:16 +02:00
|
|
|
# Testing with basestring so we don't depend on the list return type from
|
|
|
|
# get_display_recipient
|
2015-11-01 17:14:25 +01:00
|
|
|
if not isinstance(display_recipient, six.string_types):
|
2017-03-20 05:02:30 +01:00
|
|
|
recipient_str = u','.join([user['email'] for user in display_recipient])
|
2016-06-12 00:47:19 +02:00
|
|
|
else:
|
|
|
|
recipient_str = display_recipient
|
2014-08-11 14:15:16 +02:00
|
|
|
|
2014-07-25 10:40:40 +02:00
|
|
|
body = filter_footer(extract_body(message))
|
|
|
|
body += extract_and_upload_attachments(message, user_profile.realm)
|
|
|
|
if not body:
|
|
|
|
body = '(No email body)'
|
|
|
|
|
2014-08-11 14:15:16 +02:00
|
|
|
if recipient.type == Recipient.STREAM:
|
|
|
|
recipient_type_name = 'stream'
|
|
|
|
else:
|
|
|
|
recipient_type_name = 'private'
|
|
|
|
|
2017-01-22 05:23:36 +01:00
|
|
|
internal_send_message(user_profile.realm, user_profile.email,
|
|
|
|
recipient_type_name, recipient_str, subject, body)
|
2016-08-27 07:47:09 +02:00
|
|
|
logging.info("Successfully processed email from %s to %s" % (
|
|
|
|
user_profile.email, recipient_str))
|
2014-07-25 10:40:40 +02:00
|
|
|
|
2013-12-16 23:32:08 +01:00
|
|
|
## Sending the Zulip ##
|
|
|
|
|
|
|
|
class ZulipEmailForwardError(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-08-23 06:24:20 +02:00
|
|
|
def send_zulip(sender, stream, topic, content):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Stream, Text, Text) -> None
|
2014-02-26 16:37:23 +01:00
|
|
|
internal_send_message(
|
2017-01-24 07:06:13 +01:00
|
|
|
stream.realm,
|
|
|
|
sender,
|
|
|
|
"stream",
|
|
|
|
stream.name,
|
|
|
|
topic[:60],
|
|
|
|
content[:2000])
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
def valid_stream(stream_name, token):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text) -> bool
|
2013-12-16 23:32:08 +01:00
|
|
|
try:
|
|
|
|
stream = Stream.objects.get(email_token=token)
|
|
|
|
return stream.name.lower() == stream_name.lower()
|
|
|
|
except Stream.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_message_part_by_type(message, content_type):
|
2017-03-05 00:18:18 +01:00
|
|
|
# type: (message.Message, Text) -> Optional[Text]
|
2013-12-16 23:32:08 +01:00
|
|
|
charsets = message.get_charsets()
|
|
|
|
|
|
|
|
for idx, part in enumerate(message.walk()):
|
|
|
|
if part.get_content_type() == content_type:
|
|
|
|
content = part.get_payload(decode=True)
|
2016-07-04 17:13:24 +02:00
|
|
|
assert isinstance(content, binary_type)
|
2013-12-16 23:32:08 +01:00
|
|
|
if charsets[idx]:
|
2016-07-04 17:13:24 +02:00
|
|
|
text = content.decode(charsets[idx], errors="ignore")
|
|
|
|
return text
|
2017-03-05 00:18:18 +01:00
|
|
|
return None
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
def extract_body(message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (message.Message) -> Text
|
2013-12-16 23:32:08 +01:00
|
|
|
# If the message contains a plaintext version of the body, use
|
|
|
|
# that.
|
|
|
|
plaintext_content = get_message_part_by_type(message, "text/plain")
|
|
|
|
if plaintext_content:
|
2016-07-12 14:55:02 +02:00
|
|
|
return quotations.extract_from_plain(plaintext_content)
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
# If we only have an HTML version, try to make that look nice.
|
|
|
|
html_content = get_message_part_by_type(message, "text/html")
|
|
|
|
if html_content:
|
2017-05-24 23:48:45 +02:00
|
|
|
return convert_html_to_markdown(quotations.extract_from_html(html_content))
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
raise ZulipEmailForwardError("Unable to find plaintext or HTML message body")
|
|
|
|
|
|
|
|
def filter_footer(text):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Text
|
2013-12-16 23:32:08 +01:00
|
|
|
# Try to filter out obvious footers.
|
2015-11-01 17:14:31 +01:00
|
|
|
possible_footers = [line for line in text.split("\n") if line.strip().startswith("--")]
|
2013-12-16 23:32:08 +01:00
|
|
|
if len(possible_footers) != 1:
|
|
|
|
# Be conservative and don't try to scrub content if there
|
|
|
|
# isn't a trivial footer structure.
|
|
|
|
return text
|
|
|
|
|
|
|
|
return text.partition("--")[0].strip()
|
|
|
|
|
2014-02-07 05:37:23 +01:00
|
|
|
def extract_and_upload_attachments(message, realm):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (message.Message, Realm) -> Text
|
2017-05-22 23:37:15 +02:00
|
|
|
user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT)
|
2013-12-16 23:32:08 +01:00
|
|
|
attachment_links = []
|
|
|
|
|
|
|
|
payload = message.get_payload()
|
|
|
|
if not isinstance(payload, list):
|
|
|
|
# This is not a multipart message, so it can't contain attachments.
|
|
|
|
return ""
|
|
|
|
|
|
|
|
for part in payload:
|
|
|
|
content_type = part.get_content_type()
|
|
|
|
filename = part.get_filename()
|
|
|
|
if filename:
|
2016-07-04 17:13:24 +02:00
|
|
|
attachment = part.get_payload(decode=True)
|
|
|
|
if isinstance(attachment, binary_type):
|
2017-02-26 11:03:45 +01:00
|
|
|
s3_url = upload_message_image(filename, len(attachment), content_type,
|
2016-07-04 17:13:24 +02:00
|
|
|
attachment,
|
|
|
|
user_profile,
|
|
|
|
target_realm=realm)
|
|
|
|
formatted_link = u"[%s](%s)" % (filename, s3_url)
|
|
|
|
attachment_links.append(formatted_link)
|
|
|
|
else:
|
|
|
|
logger.warning("Payload is not bytes (invalid attachment %s in message from %s)." %
|
|
|
|
(filename, message.get("From")))
|
2013-12-16 23:32:08 +01:00
|
|
|
|
2016-06-05 21:16:54 +02:00
|
|
|
return u"\n".join(attachment_links)
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
def extract_and_validate(email):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> Stream
|
2017-05-24 23:48:45 +02:00
|
|
|
temp = decode_email_address(email)
|
|
|
|
if temp is None:
|
2013-12-16 23:32:08 +01:00
|
|
|
raise ZulipEmailForwardError("Malformed email recipient " + email)
|
2017-05-24 23:48:45 +02:00
|
|
|
stream_name, token = temp
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
if not valid_stream(stream_name, token):
|
|
|
|
raise ZulipEmailForwardError("Bad stream token from email recipient " + email)
|
|
|
|
|
|
|
|
return Stream.objects.get(email_token=token)
|
|
|
|
|
|
|
|
def find_emailgateway_recipient(message):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (message.Message) -> Text
|
2013-12-16 23:32:08 +01:00
|
|
|
# We can't use Delivered-To; if there is a X-Gm-Original-To
|
|
|
|
# it is more accurate, so try to find the most-accurate
|
|
|
|
# recipient list in descending priority order
|
|
|
|
recipient_headers = ["X-Gm-Original-To", "Delivered-To", "To"]
|
2017-05-07 17:07:30 +02:00
|
|
|
recipients = [] # type: List[Text]
|
2013-12-16 23:32:08 +01:00
|
|
|
for recipient_header in recipient_headers:
|
|
|
|
r = message.get_all(recipient_header, None)
|
|
|
|
if r:
|
|
|
|
recipients = r
|
|
|
|
break
|
|
|
|
|
|
|
|
pattern_parts = [re.escape(part) for part in settings.EMAIL_GATEWAY_PATTERN.split('%s')]
|
|
|
|
match_email_re = re.compile(".*?".join(pattern_parts))
|
|
|
|
for recipient_email in recipients:
|
|
|
|
if match_email_re.match(recipient_email):
|
|
|
|
return recipient_email
|
|
|
|
|
|
|
|
raise ZulipEmailForwardError("Missing recipient in mirror email")
|
|
|
|
|
2014-07-25 10:40:40 +02:00
|
|
|
def process_stream_message(to, subject, message, debug_info):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text, message.Message, Dict[str, Any]) -> None
|
2014-07-25 10:40:40 +02:00
|
|
|
stream = extract_and_validate(to)
|
|
|
|
body = filter_footer(extract_body(message))
|
|
|
|
body += extract_and_upload_attachments(message, stream.realm)
|
|
|
|
debug_info["stream"] = stream
|
2016-08-23 06:24:20 +02:00
|
|
|
send_zulip(settings.EMAIL_GATEWAY_BOT, stream, subject, body)
|
2016-08-27 07:47:09 +02:00
|
|
|
logging.info("Successfully processed email to %s (%s)" % (
|
2017-03-13 17:42:14 +01:00
|
|
|
stream.name, stream.realm.string_id))
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
def process_missed_message(to, message, pre_checked):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, message.Message, bool) -> None
|
2014-07-25 10:40:40 +02:00
|
|
|
if not pre_checked:
|
|
|
|
mark_missed_message_address_as_used(to)
|
|
|
|
send_to_missed_message_address(to, message)
|
|
|
|
|
|
|
|
def process_message(message, rcpt_to=None, pre_checked=False):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (message.Message, Optional[Text], bool) -> None
|
2016-07-04 17:11:00 +02:00
|
|
|
subject_header = message.get("Subject", "(no subject)")
|
2016-10-17 08:22:00 +02:00
|
|
|
encoded_subject, encoding = decode_header(subject_header)[0]
|
2016-07-04 17:11:00 +02:00
|
|
|
if encoding is None:
|
2017-05-07 17:07:30 +02:00
|
|
|
subject = force_text(encoded_subject) # encoded_subject has type str when encoding is None
|
2016-07-04 17:11:00 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
subject = encoded_subject.decode(encoding)
|
|
|
|
except (UnicodeDecodeError, LookupError):
|
|
|
|
subject = u"(unreadable subject)"
|
2013-12-16 23:32:08 +01:00
|
|
|
|
|
|
|
debug_info = {}
|
|
|
|
|
|
|
|
try:
|
2013-12-17 22:37:29 +01:00
|
|
|
if rcpt_to is not None:
|
|
|
|
to = rcpt_to
|
|
|
|
else:
|
|
|
|
to = find_emailgateway_recipient(message)
|
2013-12-16 23:32:08 +01:00
|
|
|
debug_info["to"] = to
|
2014-07-25 10:40:40 +02:00
|
|
|
|
|
|
|
if is_missed_message_address(to):
|
|
|
|
process_missed_message(to, message, pre_checked)
|
|
|
|
else:
|
|
|
|
process_stream_message(to, subject, message, debug_info)
|
2015-11-01 17:08:33 +01:00
|
|
|
except ZulipEmailForwardError as e:
|
2013-12-16 23:32:08 +01:00
|
|
|
# TODO: notify sender of error, retry if appropriate.
|
2016-07-13 17:09:49 +02:00
|
|
|
log_and_report(message, str(e), debug_info)
|
2017-04-18 17:28:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
def mirror_email_message(data):
|
|
|
|
# type: (Dict[Text, Text]) -> Dict[str, str]
|
|
|
|
rcpt_to = data['recipient']
|
|
|
|
if is_missed_message_address(rcpt_to):
|
|
|
|
try:
|
|
|
|
mark_missed_message_address_as_used(rcpt_to)
|
|
|
|
except ZulipEmailForwardError:
|
|
|
|
return {
|
|
|
|
"status": "error",
|
|
|
|
"msg": "5.1.1 Bad destination mailbox address: "
|
|
|
|
"Bad or expired missed message address."
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
extract_and_validate(rcpt_to)
|
|
|
|
except ZulipEmailForwardError:
|
|
|
|
return {
|
|
|
|
"status": "error",
|
|
|
|
"msg": "5.1.1 Bad destination mailbox address: "
|
|
|
|
"Please use the address specified in your Streams page."
|
|
|
|
}
|
|
|
|
queue_json_publish(
|
|
|
|
"email_mirror",
|
|
|
|
{
|
|
|
|
"message": data['msg_text'],
|
|
|
|
"rcpt_to": rcpt_to
|
|
|
|
},
|
|
|
|
lambda x: None
|
|
|
|
)
|
|
|
|
return {"status": "success"}
|