2022-06-28 00:43:57 +02:00
|
|
|
import sys
|
2022-07-27 23:33:49 +02:00
|
|
|
from email.headerregistry import Address
|
2020-06-22 23:25:37 +02:00
|
|
|
from typing import Iterable, Optional, Sequence, Union, cast
|
|
|
|
|
|
|
|
from dateutil.parser import parse as dateparser
|
|
|
|
from django.core import validators
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2022-04-14 23:50:10 +02:00
|
|
|
from zerver.actions.message_send import (
|
2020-06-22 23:25:37 +02:00
|
|
|
check_send_message,
|
|
|
|
compute_irc_user_fullname,
|
|
|
|
compute_jabber_user_fullname,
|
|
|
|
create_mirror_user_if_needed,
|
|
|
|
extract_private_recipients,
|
|
|
|
extract_stream_indicator,
|
|
|
|
)
|
2023-04-17 15:02:07 +02:00
|
|
|
from zerver.actions.scheduled_messages import check_schedule_message
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2020-06-22 23:25:37 +02:00
|
|
|
from zerver.lib.message import render_markdown
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.request import REQ, RequestNotes, has_request_variables
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.response import json_success
|
2020-06-22 23:25:37 +02:00
|
|
|
from zerver.lib.timestamp import convert_to_UTC
|
|
|
|
from zerver.lib.topic import REQ_topic
|
2023-04-17 17:02:07 +02:00
|
|
|
from zerver.lib.validator import check_int, check_string_in, to_float
|
2020-06-22 23:25:37 +02:00
|
|
|
from zerver.lib.zcommand import process_zcommands
|
|
|
|
from zerver.lib.zephyr import compute_mit_user_fullname
|
|
|
|
from zerver.models import (
|
|
|
|
Client,
|
|
|
|
Message,
|
|
|
|
Realm,
|
|
|
|
RealmDomain,
|
|
|
|
UserProfile,
|
|
|
|
get_user_including_cross_realm,
|
|
|
|
)
|
|
|
|
|
2022-06-28 00:43:57 +02:00
|
|
|
if sys.version_info < (3, 9): # nocoverage
|
|
|
|
from backports import zoneinfo
|
|
|
|
else: # nocoverage
|
|
|
|
import zoneinfo
|
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
class InvalidMirrorInputError(Exception):
|
2020-06-22 23:25:37 +02:00
|
|
|
pass
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def create_mirrored_message_users(
|
2022-03-30 20:17:53 +02:00
|
|
|
client: Client,
|
|
|
|
user_profile: UserProfile,
|
|
|
|
recipients: Iterable[str],
|
|
|
|
sender: str,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name: str,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> UserProfile:
|
2022-03-30 20:17:53 +02:00
|
|
|
sender_email = sender.strip().lower()
|
2020-06-22 23:25:37 +02:00
|
|
|
referenced_users = {sender_email}
|
2023-04-18 17:23:58 +02:00
|
|
|
if recipient_type_name == "private":
|
2020-06-22 23:25:37 +02:00
|
|
|
for email in recipients:
|
|
|
|
referenced_users.add(email.lower())
|
|
|
|
|
2021-07-09 18:10:51 +02:00
|
|
|
if client.name == "zephyr_mirror":
|
2020-06-22 23:25:37 +02:00
|
|
|
user_check = same_realm_zephyr_user
|
|
|
|
fullname_function = compute_mit_user_fullname
|
2021-07-09 18:10:51 +02:00
|
|
|
elif client.name == "irc_mirror":
|
2020-06-22 23:25:37 +02:00
|
|
|
user_check = same_realm_irc_user
|
|
|
|
fullname_function = compute_irc_user_fullname
|
2021-07-09 18:10:51 +02:00
|
|
|
elif client.name in ("jabber_mirror", "JabberMirror"):
|
2020-06-22 23:25:37 +02:00
|
|
|
user_check = same_realm_jabber_user
|
|
|
|
fullname_function = compute_jabber_user_fullname
|
|
|
|
else:
|
2022-11-17 09:30:48 +01:00
|
|
|
raise InvalidMirrorInputError("Unrecognized mirroring client")
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
for email in referenced_users:
|
|
|
|
# Check that all referenced users are in our realm:
|
|
|
|
if not user_check(user_profile, email):
|
2022-11-17 09:30:48 +01:00
|
|
|
raise InvalidMirrorInputError("At least one user cannot be mirrored")
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# Create users for the referenced users, if needed.
|
|
|
|
for email in referenced_users:
|
|
|
|
create_mirror_user_if_needed(user_profile.realm, email, fullname_function)
|
|
|
|
|
2022-06-15 04:24:28 +02:00
|
|
|
sender_user_profile = get_user_including_cross_realm(sender_email, user_profile.realm)
|
|
|
|
return sender_user_profile
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
def same_realm_zephyr_user(user_profile: UserProfile, email: str) -> bool:
|
|
|
|
#
|
|
|
|
# Are the sender and recipient both addresses in the same Zephyr
|
|
|
|
# mirroring realm? We have to handle this specially, inferring
|
|
|
|
# the domain from the e-mail address, because the recipient may
|
|
|
|
# not existing in Zulip and we may need to make a stub Zephyr
|
|
|
|
# mirroring user on the fly.
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
return False
|
|
|
|
|
2022-10-20 02:01:24 +02:00
|
|
|
domain = Address(addr_spec=email).domain.lower()
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# Assumes allow_subdomains=False for all RealmDomain's corresponding to
|
|
|
|
# these realms.
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
|
|
|
user_profile.realm.is_zephyr_mirror_realm
|
|
|
|
and RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists()
|
|
|
|
)
|
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
def same_realm_irc_user(user_profile: UserProfile, email: str) -> bool:
|
|
|
|
# Check whether the target email address is an IRC user in the
|
|
|
|
# same realm as user_profile, i.e. if the domain were example.com,
|
|
|
|
# the IRC user would need to be username@irc.example.com
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
return False
|
|
|
|
|
2022-10-20 02:01:24 +02:00
|
|
|
domain = Address(addr_spec=email).domain.lower()
|
2022-07-27 23:33:49 +02:00
|
|
|
if domain.startswith("irc."):
|
|
|
|
domain = domain[len("irc.") :]
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# Assumes allow_subdomains=False for all RealmDomain's corresponding to
|
|
|
|
# these realms.
|
|
|
|
return RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
def same_realm_jabber_user(user_profile: UserProfile, email: str) -> bool:
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If your Jabber users have a different email domain than the
|
|
|
|
# Zulip users, this is where you would do any translation.
|
2022-10-20 02:01:24 +02:00
|
|
|
domain = Address(addr_spec=email).domain.lower()
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# Assumes allow_subdomains=False for all RealmDomain's corresponding to
|
|
|
|
# these realms.
|
|
|
|
return RealmDomain.objects.filter(realm=user_profile.realm, domain=domain).exists()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def handle_deferred_message(
|
|
|
|
sender: UserProfile,
|
|
|
|
client: Client,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name: str,
|
2021-02-12 08:19:30 +01:00
|
|
|
message_to: Union[Sequence[str], Sequence[int]],
|
|
|
|
topic_name: Optional[str],
|
|
|
|
message_content: str,
|
2023-04-14 21:17:26 +02:00
|
|
|
scheduled_message_id: Optional[int],
|
2021-02-12 08:19:30 +01:00
|
|
|
delivery_type: str,
|
|
|
|
defer_until: str,
|
|
|
|
tz_guess: Optional[str],
|
|
|
|
forwarder_user_profile: UserProfile,
|
|
|
|
realm: Optional[Realm],
|
2022-01-31 14:09:23 +01:00
|
|
|
) -> str:
|
2020-06-22 23:25:37 +02:00
|
|
|
deliver_at = None
|
2021-02-12 08:20:45 +01:00
|
|
|
local_tz = "UTC"
|
2020-06-22 23:25:37 +02:00
|
|
|
if tz_guess:
|
|
|
|
local_tz = tz_guess
|
|
|
|
elif sender.timezone:
|
|
|
|
local_tz = sender.timezone
|
|
|
|
try:
|
|
|
|
deliver_at = dateparser(defer_until)
|
|
|
|
except ValueError:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid time format"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
deliver_at_usertz = deliver_at
|
|
|
|
if deliver_at_usertz.tzinfo is None:
|
2022-06-28 00:43:57 +02:00
|
|
|
user_tz = zoneinfo.ZoneInfo(local_tz)
|
|
|
|
deliver_at_usertz = deliver_at.replace(tzinfo=user_tz)
|
2020-06-22 23:25:37 +02:00
|
|
|
deliver_at = convert_to_UTC(deliver_at_usertz)
|
|
|
|
|
|
|
|
if deliver_at <= timezone_now():
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Time must be in the future."))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
check_schedule_message(
|
|
|
|
sender,
|
|
|
|
client,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name,
|
2021-02-12 08:19:30 +01:00
|
|
|
message_to,
|
|
|
|
topic_name,
|
|
|
|
message_content,
|
2023-04-14 21:17:26 +02:00
|
|
|
scheduled_message_id,
|
2021-02-12 08:19:30 +01:00
|
|
|
delivery_type,
|
|
|
|
deliver_at,
|
|
|
|
realm=realm,
|
|
|
|
forwarder_user_profile=forwarder_user_profile,
|
|
|
|
)
|
2022-01-31 14:09:23 +01:00
|
|
|
return str(deliver_at_usertz)
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def send_message_backend(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-04-17 17:02:07 +02:00
|
|
|
req_type: str = REQ("type", str_validator=check_string_in(Message.API_RECIPIENT_TYPES)),
|
2021-02-12 08:20:45 +01:00
|
|
|
req_to: Optional[str] = REQ("to", default=None),
|
2022-03-30 20:17:53 +02:00
|
|
|
req_sender: Optional[str] = REQ("sender", default=None, documentation_pending=True),
|
2021-02-12 08:19:30 +01:00
|
|
|
forged_str: Optional[str] = REQ("forged", default=None, documentation_pending=True),
|
|
|
|
topic_name: Optional[str] = REQ_topic(),
|
2021-02-12 08:20:45 +01:00
|
|
|
message_content: str = REQ("content"),
|
2021-02-12 08:19:30 +01:00
|
|
|
widget_content: Optional[str] = REQ(default=None, documentation_pending=True),
|
|
|
|
local_id: Optional[str] = REQ(default=None),
|
|
|
|
queue_id: Optional[str] = REQ(default=None),
|
2023-04-14 21:17:26 +02:00
|
|
|
scheduled_message_id: Optional[int] = REQ(
|
|
|
|
default=None, json_validator=check_int, documentation_pending=True
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
delivery_type: str = REQ("delivery_type", default="send_now", documentation_pending=True),
|
|
|
|
defer_until: Optional[str] = REQ("deliver_at", default=None, documentation_pending=True),
|
|
|
|
tz_guess: Optional[str] = REQ("tz_guess", default=None, documentation_pending=True),
|
2021-07-29 18:08:59 +02:00
|
|
|
time: Optional[float] = REQ(default=None, converter=to_float, documentation_pending=True),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2023-04-17 17:02:07 +02:00
|
|
|
recipient_type_name = req_type
|
|
|
|
if recipient_type_name == "direct":
|
|
|
|
# For now, use "private" from Message.API_RECIPIENT_TYPES.
|
|
|
|
# TODO: Use "direct" here, as well as in events and
|
|
|
|
# message (created, schdeduled, drafts) objects/dicts.
|
|
|
|
recipient_type_name = "private"
|
2023-04-18 17:23:58 +02:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
# If req_to is None, then we default to an
|
|
|
|
# empty list of recipients.
|
|
|
|
message_to: Union[Sequence[int], Sequence[str]] = []
|
|
|
|
|
|
|
|
if req_to is not None:
|
2023-04-18 17:23:58 +02:00
|
|
|
if recipient_type_name == "stream":
|
2020-06-22 23:25:37 +02:00
|
|
|
stream_indicator = extract_stream_indicator(req_to)
|
|
|
|
|
|
|
|
# For legacy reasons check_send_message expects
|
|
|
|
# a list of streams, instead of a single stream.
|
|
|
|
#
|
|
|
|
# Also, mypy can't detect that a single-item
|
|
|
|
# list populated from a Union[int, str] is actually
|
|
|
|
# a Union[Sequence[int], Sequence[str]].
|
2020-06-23 00:51:34 +02:00
|
|
|
if isinstance(stream_indicator, int):
|
|
|
|
message_to = [stream_indicator]
|
|
|
|
else:
|
|
|
|
message_to = [stream_indicator]
|
2020-06-22 23:25:37 +02:00
|
|
|
else:
|
|
|
|
message_to = extract_private_recipients(req_to)
|
|
|
|
|
|
|
|
# Temporary hack: We're transitioning `forged` from accepting
|
|
|
|
# `yes` to accepting `true` like all of our normal booleans.
|
|
|
|
forged = forged_str is not None and forged_str in ["yes", "true"]
|
|
|
|
|
2021-08-21 19:24:20 +02:00
|
|
|
client = RequestNotes.get_notes(request).client
|
2021-07-09 18:10:51 +02:00
|
|
|
assert client is not None
|
2021-07-24 20:37:35 +02:00
|
|
|
can_forge_sender = user_profile.can_forge_sender
|
2020-12-20 14:21:42 +01:00
|
|
|
if forged and not can_forge_sender:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("User not authorized for this query"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2023-02-27 23:54:43 +01:00
|
|
|
realm = user_profile.realm
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
if client.name in ["zephyr_mirror", "irc_mirror", "jabber_mirror", "JabberMirror"]:
|
|
|
|
# Here's how security works for mirroring:
|
|
|
|
#
|
|
|
|
# For private messages, the message must be (1) both sent and
|
|
|
|
# received exclusively by users in your realm, and (2)
|
|
|
|
# received by the forwarding user.
|
|
|
|
#
|
|
|
|
# For stream messages, the message must be (1) being forwarded
|
|
|
|
# by an API superuser for your realm and (2) being sent to a
|
|
|
|
# mirrored stream.
|
|
|
|
#
|
|
|
|
# The most important security checks are in
|
|
|
|
# `create_mirrored_message_users` below, which checks the
|
|
|
|
# same-realm constraint.
|
2022-03-30 20:17:53 +02:00
|
|
|
if req_sender is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Missing sender"))
|
2023-04-18 17:23:58 +02:00
|
|
|
if recipient_type_name != "private" and not can_forge_sender:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("User not authorized for this query"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# For now, mirroring only works with recipient emails, not for
|
|
|
|
# recipient user IDs.
|
|
|
|
if not all(isinstance(to_item, str) for to_item in message_to):
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Mirroring not allowed with recipient user IDs"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
# We need this manual cast so that mypy doesn't complain about
|
|
|
|
# create_mirrored_message_users not being able to accept a Sequence[int]
|
|
|
|
# type parameter.
|
|
|
|
message_to = cast(Sequence[str], message_to)
|
|
|
|
|
|
|
|
try:
|
2022-03-30 20:17:53 +02:00
|
|
|
mirror_sender = create_mirrored_message_users(
|
2023-04-18 17:23:58 +02:00
|
|
|
client, user_profile, message_to, req_sender, recipient_type_name
|
2022-03-30 20:17:53 +02:00
|
|
|
)
|
2022-11-17 09:30:48 +01:00
|
|
|
except InvalidMirrorInputError:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid mirrored message"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
if client.name == "zephyr_mirror" and not user_profile.realm.is_zephyr_mirror_realm:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Zephyr mirroring is not allowed in this organization"))
|
2020-06-22 23:25:37 +02:00
|
|
|
sender = mirror_sender
|
|
|
|
else:
|
2022-03-30 20:17:53 +02:00
|
|
|
if req_sender is not None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Invalid mirrored message"))
|
2020-06-22 23:25:37 +02:00
|
|
|
sender = user_profile
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if (delivery_type == "send_later" or delivery_type == "remind") and defer_until is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Missing deliver_at in a request for delayed message delivery"))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if (delivery_type == "send_later" or delivery_type == "remind") and defer_until is not None:
|
2022-01-31 14:09:23 +01:00
|
|
|
deliver_at = handle_deferred_message(
|
2021-02-12 08:19:30 +01:00
|
|
|
sender,
|
|
|
|
client,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name,
|
2021-02-12 08:19:30 +01:00
|
|
|
message_to,
|
|
|
|
topic_name,
|
|
|
|
message_content,
|
2023-04-14 21:17:26 +02:00
|
|
|
scheduled_message_id,
|
2021-02-12 08:19:30 +01:00
|
|
|
delivery_type,
|
|
|
|
defer_until,
|
|
|
|
tz_guess,
|
|
|
|
forwarder_user_profile=user_profile,
|
|
|
|
realm=realm,
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"deliver_at": deliver_at})
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
ret = check_send_message(
|
|
|
|
sender,
|
|
|
|
client,
|
2023-04-18 17:23:58 +02:00
|
|
|
recipient_type_name,
|
2021-02-12 08:19:30 +01:00
|
|
|
message_to,
|
|
|
|
topic_name,
|
|
|
|
message_content,
|
|
|
|
forged=forged,
|
2021-07-29 18:08:59 +02:00
|
|
|
forged_timestamp=time,
|
2021-02-12 08:19:30 +01:00
|
|
|
forwarder_user_profile=user_profile,
|
|
|
|
realm=realm,
|
|
|
|
local_id=local_id,
|
|
|
|
sender_queue_id=queue_id,
|
|
|
|
widget_content=widget_content,
|
|
|
|
)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"id": ret})
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def zcommand_backend(
|
2021-02-12 08:20:45 +01:00
|
|
|
request: HttpRequest, user_profile: UserProfile, command: str = REQ("command")
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data=process_zcommands(command, user_profile))
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 23:25:37 +02:00
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def render_message_backend(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, content: str = REQ()
|
|
|
|
) -> HttpResponse:
|
2020-06-22 23:25:37 +02:00
|
|
|
message = Message()
|
|
|
|
message.sender = user_profile
|
2022-09-27 21:42:31 +02:00
|
|
|
message.realm = user_profile.realm
|
2020-06-22 23:25:37 +02:00
|
|
|
message.content = content
|
2021-08-21 19:24:20 +02:00
|
|
|
client = RequestNotes.get_notes(request).client
|
2021-07-09 18:10:51 +02:00
|
|
|
assert client is not None
|
|
|
|
message.sending_client = client
|
2020-06-22 23:25:37 +02:00
|
|
|
|
2021-06-17 12:20:40 +02:00
|
|
|
rendering_result = render_markdown(message, content, realm=user_profile.realm)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request, data={"rendered": rendering_result.rendered_content})
|