2019-03-21 10:24:56 +01:00
|
|
|
import re
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Callable
|
|
|
|
from typing import Any
|
2019-03-21 10:24:56 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.text import slugify
|
|
|
|
|
|
|
|
from zerver.models import Stream
|
|
|
|
|
2019-05-26 16:25:23 +02:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def default_option_handler_factory(address_option: str) -> Callable[[dict[str, Any]], None]:
|
|
|
|
def option_setter(options_dict: dict[str, Any]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
options_dict[address_option.replace("-", "_")] = True
|
2020-01-15 14:35:30 +01:00
|
|
|
|
|
|
|
return option_setter
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-01-15 14:35:30 +01:00
|
|
|
optional_address_tokens = {
|
|
|
|
"show-sender": default_option_handler_factory("show-sender"),
|
|
|
|
"include-footer": default_option_handler_factory("include-footer"),
|
|
|
|
"include-quotes": default_option_handler_factory("include-quotes"),
|
2020-01-15 16:28:46 +01:00
|
|
|
"prefer-text": lambda options: options.update(prefer_text=True),
|
|
|
|
"prefer-html": lambda options: options.update(prefer_text=False),
|
2020-01-15 14:35:30 +01:00
|
|
|
}
|
2019-03-21 10:24:56 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-21 11:28:14 +01:00
|
|
|
class ZulipEmailForwardError(Exception):
|
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-31 23:46:34 +02:00
|
|
|
class ZulipEmailForwardUserError(ZulipEmailForwardError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-03-21 11:28:14 +01:00
|
|
|
def get_email_gateway_message_string_from_address(address: str) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
pattern_parts = [re.escape(part) for part in settings.EMAIL_GATEWAY_PATTERN.split("%s")]
|
2019-03-21 10:24:56 +01:00
|
|
|
if settings.EMAIL_GATEWAY_EXTRA_PATTERN_HACK:
|
|
|
|
# Accept mails delivered to any Zulip server
|
|
|
|
pattern_parts[-1] = settings.EMAIL_GATEWAY_EXTRA_PATTERN_HACK
|
2024-04-26 20:30:22 +02:00
|
|
|
match_email_re = re.compile(r"(.*?)".join(pattern_parts))
|
2019-03-21 10:24:56 +01:00
|
|
|
match = match_email_re.match(address)
|
|
|
|
|
|
|
|
if not match:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise ZulipEmailForwardError("Address not recognized by gateway.")
|
2019-03-21 10:24:56 +01:00
|
|
|
msg_string = match.group(1)
|
|
|
|
|
|
|
|
return msg_string
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def encode_email_address(stream: Stream, show_sender: bool = False) -> str:
|
2019-09-05 11:34:05 +02:00
|
|
|
return encode_email_address_helper(stream.name, stream.email_token, show_sender)
|
2019-03-21 10:24:56 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def encode_email_address_helper(name: str, email_token: str, show_sender: bool = False) -> str:
|
2019-03-21 10:24:56 +01:00
|
|
|
# Some deployments may not use the email gateway
|
2021-02-12 08:20:45 +01:00
|
|
|
if settings.EMAIL_GATEWAY_PATTERN == "":
|
|
|
|
return ""
|
2019-03-21 10:24:56 +01:00
|
|
|
|
|
|
|
# Given the fact that we have almost no restrictions on stream names and
|
|
|
|
# that what characters are allowed in e-mail addresses is complicated and
|
|
|
|
# dependent on context in the address, we opt for a simple scheme:
|
|
|
|
# 1. Replace all substrings of non-alphanumeric characters with a single hyphen.
|
|
|
|
# 2. Use Django's slugify to convert the resulting name to ascii.
|
|
|
|
# 3. If the resulting name is shorter than the name we got in step 1,
|
|
|
|
# it means some letters can't be reasonably turned to ascii and have to be dropped,
|
|
|
|
# which would mangle the name, so we just skip the name part of the address.
|
2021-02-12 08:20:45 +01:00
|
|
|
name = re.sub(r"\W+", "-", name)
|
2019-03-21 10:24:56 +01:00
|
|
|
slug_name = slugify(name)
|
2021-02-12 08:20:45 +01:00
|
|
|
encoded_name = slug_name if len(slug_name) == len(name) else ""
|
2019-03-21 10:24:56 +01:00
|
|
|
|
|
|
|
# If encoded_name ends up empty, we just skip this part of the address:
|
|
|
|
if encoded_name:
|
2020-06-10 06:41:04 +02:00
|
|
|
encoded_token = f"{encoded_name}.{email_token}"
|
2019-03-21 10:24:56 +01:00
|
|
|
else:
|
|
|
|
encoded_token = email_token
|
|
|
|
|
2019-09-05 11:34:05 +02:00
|
|
|
if show_sender:
|
|
|
|
encoded_token += ".show-sender"
|
|
|
|
|
2019-03-21 10:24:56 +01:00
|
|
|
return settings.EMAIL_GATEWAY_PATTERN % (encoded_token,)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def decode_email_address(email: str) -> tuple[str, dict[str, bool]]:
|
2019-03-21 10:24:56 +01:00
|
|
|
# Perform the reverse of encode_email_address. Returns a tuple of
|
2019-05-26 16:25:23 +02:00
|
|
|
# (email_token, options)
|
2019-03-21 10:24:56 +01:00
|
|
|
msg_string = get_email_gateway_message_string_from_address(email)
|
|
|
|
|
2019-07-14 03:00:36 +02:00
|
|
|
# Support both + and . as separators. For background, the `+` is
|
|
|
|
# more aesthetically pleasing, but because Google groups silently
|
|
|
|
# drops the use of `+` in email addresses, which would completely
|
|
|
|
# break the integration, we now favor `.` as the separator between
|
|
|
|
# tokens in the email addresses we generate.
|
|
|
|
#
|
|
|
|
# We need to keep supporting `+` indefinitely for backwards
|
|
|
|
# compatibility with older versions of Zulip that offered users
|
|
|
|
# email addresses prioritizing using `+` for better aesthetics.
|
2021-02-12 08:20:45 +01:00
|
|
|
msg_string = msg_string.replace(".", "+")
|
2019-07-14 03:00:36 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
parts = msg_string.split("+")
|
2024-07-12 02:30:17 +02:00
|
|
|
options: dict[str, bool] = {}
|
2019-05-26 16:25:23 +02:00
|
|
|
for part in parts:
|
|
|
|
if part in optional_address_tokens:
|
2020-01-15 14:35:30 +01:00
|
|
|
optional_address_tokens[part](options)
|
2019-05-26 16:25:23 +02:00
|
|
|
|
2020-01-15 14:35:30 +01:00
|
|
|
remaining_parts = [part for part in parts if part not in optional_address_tokens]
|
2019-05-26 16:25:23 +02:00
|
|
|
|
2020-01-15 14:35:30 +01:00
|
|
|
# There should be one or two parts left:
|
2019-03-21 10:24:56 +01:00
|
|
|
# [stream_name, email_token] or just [email_token]
|
2020-01-15 14:35:30 +01:00
|
|
|
if len(remaining_parts) == 1:
|
|
|
|
token = remaining_parts[0]
|
2019-03-21 10:24:56 +01:00
|
|
|
else:
|
2020-01-15 14:35:30 +01:00
|
|
|
token = remaining_parts[1]
|
2019-03-21 10:24:56 +01:00
|
|
|
|
2019-05-26 16:25:23 +02:00
|
|
|
return token, options
|