2022-06-28 00:43:57 +02:00
|
|
|
import sys
|
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_schedule_message,
|
|
|
|
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
|
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
|
2021-07-29 18:08:59 +02:00
|
|
|
from zerver.lib.validator import 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,
|
|
|
|
email_to_domain,
|
|
|
|
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
|
|
|
|
|
|
|
class InvalidMirrorInput(Exception):
|
|
|
|
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,
|
|
|
|
message_type: str,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> UserProfile:
|
2020-06-22 23:25:37 +02:00
|
|
|
|
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}
|
2022-03-30 20:17:53 +02:00
|
|
|
if message_type == "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:
|
|
|
|
raise InvalidMirrorInput("Unrecognized mirroring client")
|
|
|
|
|
|
|
|
for email in referenced_users:
|
|
|
|
# Check that all referenced users are in our realm:
|
|
|
|
if not user_check(user_profile, email):
|
|
|
|
raise InvalidMirrorInput("At least one user cannot be mirrored")
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
domain = email_to_domain(email)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
domain = email_to_domain(email).replace("irc.", "")
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
domain = email_to_domain(email)
|
|
|
|
|
|
|
|
# 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,
|
|
|
|
message_type_name: str,
|
|
|
|
message_to: Union[Sequence[str], Sequence[int]],
|
|
|
|
topic_name: Optional[str],
|
|
|
|
message_content: str,
|
|
|
|
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,
|
|
|
|
message_type_name,
|
|
|
|
message_to,
|
|
|
|
topic_name,
|
|
|
|
message_content,
|
|
|
|
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,
|
2021-02-12 08:20:45 +01:00
|
|
|
message_type_name: str = REQ("type"),
|
|
|
|
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),
|
2021-02-12 08:20:45 +01:00
|
|
|
realm_str: Optional[str] = REQ("realm_str", default=None, documentation_pending=True),
|
2021-02-12 08:19:30 +01:00
|
|
|
local_id: Optional[str] = REQ(default=None),
|
|
|
|
queue_id: Optional[str] = REQ(default=None),
|
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:
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
if message_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
|
|
|
|
|
|
|
realm = None
|
|
|
|
if realm_str and realm_str != user_profile.realm.string_id:
|
2021-03-25 15:42:51 +01:00
|
|
|
# The realm_str parameter does nothing, because it has to match
|
|
|
|
# the user's realm - but we keep it around for backward compatibility.
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("User not authorized for this query"))
|
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"))
|
2020-12-20 14:21:42 +01:00
|
|
|
if message_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(
|
|
|
|
client, user_profile, message_to, req_sender, message_type_name
|
|
|
|
)
|
2020-06-22 23:25:37 +02:00
|
|
|
except InvalidMirrorInput:
|
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,
|
|
|
|
message_type_name,
|
|
|
|
message_to,
|
|
|
|
topic_name,
|
|
|
|
message_content,
|
|
|
|
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,
|
|
|
|
message_type_name,
|
|
|
|
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
|
|
|
|
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})
|