2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Iterable, Sequence
|
2022-07-27 23:33:49 +02:00
|
|
|
from email.headerregistry import Address
|
2024-07-12 02:30:25 +02:00
|
|
|
from typing import cast
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
from django.core import validators
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
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,
|
|
|
|
)
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2023-10-03 03:22:59 +02:00
|
|
|
from zerver.lib.markdown import render_message_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.topic import REQ_topic
|
2023-12-14 07:11:00 +01:00
|
|
|
from zerver.lib.validator import check_bool, 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
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import Client, Message, RealmDomain, UserProfile
|
|
|
|
from zerver.models.users import get_user_including_cross_realm
|
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":
|
2024-07-14 21:17:13 +02:00
|
|
|
referenced_users.update(email.lower() for email in recipients)
|
2020-06-22 23:25:37 +02:00
|
|
|
|
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
|
|
|
|
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)),
|
2024-07-12 02:30:23 +02:00
|
|
|
req_to: str | None = REQ("to", default=None),
|
|
|
|
req_sender: str | None = REQ("sender", default=None, documentation_pending=True),
|
|
|
|
forged_str: str | None = REQ("forged", default=None, documentation_pending=True),
|
|
|
|
topic_name: str | None = REQ_topic(),
|
2021-02-12 08:20:45 +01:00
|
|
|
message_content: str = REQ("content"),
|
2024-07-12 02:30:23 +02:00
|
|
|
widget_content: str | None = REQ(default=None, documentation_pending=True),
|
|
|
|
local_id: str | None = REQ(default=None),
|
|
|
|
queue_id: str | None = REQ(default=None),
|
|
|
|
time: float | None = REQ(default=None, converter=to_float, documentation_pending=True),
|
|
|
|
read_by_sender: bool | None = REQ(json_validator=check_bool, default=None),
|
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"
|
2024-04-10 20:48:10 +02:00
|
|
|
elif recipient_type_name == "channel":
|
|
|
|
# For now, use "stream" from Message.API_RECIPIENT_TYPES.
|
|
|
|
# TODO: Use "channel" here, as well as in events and
|
|
|
|
# message (created, schdeduled, drafts) objects/dicts.
|
|
|
|
recipient_type_name = "stream"
|
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.
|
2024-07-12 02:30:23 +02:00
|
|
|
message_to: Sequence[int] | Sequence[str] = []
|
2020-06-22 23:25:37 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
#
|
2023-06-19 16:42:11 +02:00
|
|
|
# For direct messages, the message must be (1) both sent and
|
2020-06-22 23:25:37 +02:00
|
|
|
# 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
|
|
|
|
|
2023-12-14 07:11:00 +01:00
|
|
|
if read_by_sender is None:
|
|
|
|
# Legacy default: a message you sent from a non-API client is
|
|
|
|
# automatically marked as read for yourself.
|
|
|
|
read_by_sender = client.default_read_by_sender()
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
data: dict[str, int] = {}
|
2023-10-09 13:11:32 +02:00
|
|
|
sent_message_result = check_send_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,
|
|
|
|
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,
|
2023-12-14 07:11:00 +01:00
|
|
|
read_by_sender=read_by_sender,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-10-09 13:11:32 +02:00
|
|
|
data["id"] = sent_message_result.message_id
|
|
|
|
if sent_message_result.automatic_new_visibility_policy:
|
2024-01-29 00:32:21 +01:00
|
|
|
data["automatic_new_visibility_policy"] = (
|
|
|
|
sent_message_result.automatic_new_visibility_policy
|
|
|
|
)
|
2023-10-09 13:11:32 +02:00
|
|
|
return json_success(request, data=data)
|
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
|
|
|
|
2023-10-03 03:22:59 +02:00
|
|
|
rendering_result = render_message_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})
|