2022-01-11 21:49:40 +01:00
|
|
|
import unicodedata
|
2022-01-11 21:47:44 +01:00
|
|
|
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
|
|
|
from zerver.models import Stream
|
|
|
|
|
2022-01-11 22:03:11 +01:00
|
|
|
# There are 66 Unicode non-characters; see
|
|
|
|
# https://www.unicode.org/faq/private_use.html#nonchar4
|
2022-01-22 07:55:14 +01:00
|
|
|
unicode_non_chars = {
|
2022-01-11 22:03:11 +01:00
|
|
|
chr(x)
|
2023-09-12 23:19:57 +02:00
|
|
|
for r in [
|
|
|
|
range(0xFDD0, 0xFDF0), # FDD0 through FDEF, inclusive
|
|
|
|
range(0xFFFE, 0x110000, 0x10000), # 0xFFFE, 0x1FFFE, ... 0x10FFFE inclusive
|
|
|
|
range(0xFFFF, 0x110000, 0x10000), # 0xFFFF, 0x1FFFF, ... 0x10FFFF inclusive
|
|
|
|
]
|
|
|
|
for x in r
|
2022-01-22 07:55:14 +01:00
|
|
|
}
|
2022-01-11 22:03:11 +01:00
|
|
|
|
|
|
|
|
2022-08-20 18:11:55 +02:00
|
|
|
def is_character_printable(char: str) -> bool:
|
|
|
|
unicode_category = unicodedata.category(char)
|
|
|
|
if (unicode_category in ["Cc", "Cs"]) or char in unicode_non_chars:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def check_string_is_printable(var: str) -> int | None:
|
2022-01-11 22:03:11 +01:00
|
|
|
# Return position (1-indexed!) of the character which is not
|
|
|
|
# printable, None if no such character is present.
|
2022-01-12 00:59:11 +01:00
|
|
|
for i, char in enumerate(var):
|
2022-08-20 18:11:55 +02:00
|
|
|
if not is_character_printable(char):
|
2022-01-11 22:03:11 +01:00
|
|
|
return i + 1
|
|
|
|
return None
|
|
|
|
|
2022-01-11 21:47:44 +01:00
|
|
|
|
|
|
|
def check_stream_name(stream_name: str) -> None:
|
|
|
|
if stream_name.strip() == "":
|
2024-04-16 15:52:21 +02:00
|
|
|
raise JsonableError(_("Channel name can't be empty."))
|
2022-01-11 21:57:53 +01:00
|
|
|
|
2022-01-11 21:47:44 +01:00
|
|
|
if len(stream_name) > Stream.MAX_NAME_LENGTH:
|
|
|
|
raise JsonableError(
|
2024-04-16 15:52:21 +02:00
|
|
|
_("Channel name too long (limit: {max_length} characters).").format(
|
2023-07-17 22:40:33 +02:00
|
|
|
max_length=Stream.MAX_NAME_LENGTH
|
|
|
|
)
|
2022-01-11 21:47:44 +01:00
|
|
|
)
|
2022-01-11 22:05:20 +01:00
|
|
|
|
|
|
|
invalid_character_pos = check_string_is_printable(stream_name)
|
|
|
|
if invalid_character_pos is not None:
|
|
|
|
raise JsonableError(
|
2024-04-16 15:52:21 +02:00
|
|
|
_("Invalid character in channel name, at position {position}.").format(
|
2023-07-17 22:40:33 +02:00
|
|
|
position=invalid_character_pos
|
|
|
|
)
|
2022-01-11 22:05:20 +01:00
|
|
|
)
|
2022-01-11 21:49:40 +01:00
|
|
|
|
|
|
|
|
2024-01-14 14:38:50 +01:00
|
|
|
def check_stream_topic(topic_name: str) -> None:
|
|
|
|
if topic_name.strip() == "":
|
2022-01-11 22:07:06 +01:00
|
|
|
raise JsonableError(_("Topic can't be empty!"))
|
2022-01-11 21:49:40 +01:00
|
|
|
|
2024-01-14 14:38:50 +01:00
|
|
|
invalid_character_pos = check_string_is_printable(topic_name)
|
2022-01-11 22:03:11 +01:00
|
|
|
if invalid_character_pos is not None:
|
|
|
|
raise JsonableError(
|
2023-07-17 22:40:33 +02:00
|
|
|
_("Invalid character in topic, at position {position}!").format(
|
|
|
|
position=invalid_character_pos
|
|
|
|
)
|
2022-01-11 22:03:11 +01:00
|
|
|
)
|