2019-04-01 05:24:38 -04:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2019-10-02 08:29:01 -04:00
|
|
|
|
2021-07-20 07:59:23 -04:00
|
|
|
from typing import Callable, Dict, Optional
|
2019-10-02 08:29:01 -04:00
|
|
|
|
2019-04-01 05:24:38 -04:00
|
|
|
import attr
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class EventFormatVersions:
|
2019-04-01 05:24:38 -04:00
|
|
|
"""This is an internal enum for tracking the version of the event format,
|
|
|
|
independently from the room version.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
V1 = 1 # $id:server event id format
|
|
|
|
V2 = 2 # MSC1659-style $hash event id format: introduced for room v3
|
|
|
|
V3 = 3 # MSC1884-style $hash format: introduced for room v4
|
2019-04-01 05:24:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
KNOWN_EVENT_FORMAT_VERSIONS = {
|
|
|
|
EventFormatVersions.V1,
|
|
|
|
EventFormatVersions.V2,
|
2019-05-20 10:54:42 -04:00
|
|
|
EventFormatVersions.V3,
|
2019-04-01 05:24:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class StateResolutionVersions:
|
2019-04-01 05:24:38 -04:00
|
|
|
"""Enum to identify the state resolution algorithms"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
V1 = 1 # room v1 state res
|
|
|
|
V2 = 2 # MSC1442 state res: room v2 and later
|
2019-04-01 05:24:38 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomDisposition:
|
2019-04-01 05:24:38 -04:00
|
|
|
STABLE = "stable"
|
|
|
|
UNSTABLE = "unstable"
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(slots=True, frozen=True)
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomVersion:
|
2019-04-01 05:24:38 -04:00
|
|
|
"""An object which describes the unique attributes of a room version."""
|
|
|
|
|
2021-01-05 07:41:48 -05:00
|
|
|
identifier = attr.ib(type=str) # the identifier for this version
|
|
|
|
disposition = attr.ib(type=str) # one of the RoomDispositions
|
|
|
|
event_format = attr.ib(type=int) # one of the EventFormatVersions
|
|
|
|
state_res = attr.ib(type=int) # one of the StateResolutionVersions
|
|
|
|
enforce_key_validity = attr.ib(type=bool)
|
2019-04-01 05:24:38 -04:00
|
|
|
|
2021-06-09 14:39:51 -04:00
|
|
|
# Before MSC2432, m.room.aliases had special auth rules and redaction rules
|
2020-05-14 12:38:17 -04:00
|
|
|
special_case_aliases_auth = attr.ib(type=bool)
|
2020-05-14 13:24:01 -04:00
|
|
|
# Strictly enforce canonicaljson, do not allow:
|
|
|
|
# * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
|
|
|
|
# * Floats
|
|
|
|
# * NaN, Infinity, -Infinity
|
|
|
|
strict_canonicaljson = attr.ib(type=bool)
|
2021-01-05 07:41:48 -05:00
|
|
|
# MSC2209: Check 'notifications' key while verifying
|
2020-05-14 12:38:17 -04:00
|
|
|
# m.room.power_levels auth rules.
|
|
|
|
limit_notifications_power_levels = attr.ib(type=bool)
|
2021-01-05 07:41:48 -05:00
|
|
|
# MSC2174/MSC2176: Apply updated redaction rules algorithm.
|
|
|
|
msc2176_redaction_rules = attr.ib(type=bool)
|
2021-03-31 16:39:08 -04:00
|
|
|
# MSC3083: Support the 'restricted' join_rule.
|
|
|
|
msc3083_join_rules = attr.ib(type=bool)
|
2021-06-09 14:39:51 -04:00
|
|
|
# MSC2403: Allows join_rules to be set to 'knock', changes auth rules to allow sending
|
|
|
|
# m.room.membership event with membership 'knock'.
|
|
|
|
msc2403_knocking = attr.ib(type=bool)
|
2020-01-27 11:14:54 -05:00
|
|
|
|
2019-04-01 05:24:38 -04:00
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomVersions:
|
2019-04-01 05:24:38 -04:00
|
|
|
V1 = RoomVersion(
|
|
|
|
"1",
|
|
|
|
RoomDisposition.STABLE,
|
|
|
|
EventFormatVersions.V1,
|
|
|
|
StateResolutionVersions.V1,
|
2019-06-05 05:38:25 -04:00
|
|
|
enforce_key_validity=False,
|
2020-01-27 11:14:54 -05:00
|
|
|
special_case_aliases_auth=True,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=False,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2019-04-01 05:24:38 -04:00
|
|
|
)
|
|
|
|
V2 = RoomVersion(
|
|
|
|
"2",
|
|
|
|
RoomDisposition.STABLE,
|
|
|
|
EventFormatVersions.V1,
|
|
|
|
StateResolutionVersions.V2,
|
2019-06-05 05:38:25 -04:00
|
|
|
enforce_key_validity=False,
|
2020-01-27 11:14:54 -05:00
|
|
|
special_case_aliases_auth=True,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=False,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2019-04-01 05:24:38 -04:00
|
|
|
)
|
|
|
|
V3 = RoomVersion(
|
|
|
|
"3",
|
|
|
|
RoomDisposition.STABLE,
|
|
|
|
EventFormatVersions.V2,
|
|
|
|
StateResolutionVersions.V2,
|
2019-06-05 05:38:25 -04:00
|
|
|
enforce_key_validity=False,
|
2020-01-27 11:14:54 -05:00
|
|
|
special_case_aliases_auth=True,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=False,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2019-04-01 05:24:38 -04:00
|
|
|
)
|
2019-05-21 11:22:54 -04:00
|
|
|
V4 = RoomVersion(
|
|
|
|
"4",
|
|
|
|
RoomDisposition.STABLE,
|
2019-05-20 10:54:42 -04:00
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
2019-06-05 05:38:25 -04:00
|
|
|
enforce_key_validity=False,
|
2020-01-27 11:14:54 -05:00
|
|
|
special_case_aliases_auth=True,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=False,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2019-06-05 05:38:25 -04:00
|
|
|
)
|
2019-06-05 05:58:51 -04:00
|
|
|
V5 = RoomVersion(
|
|
|
|
"5",
|
|
|
|
RoomDisposition.STABLE,
|
2019-06-05 05:38:25 -04:00
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
2019-06-05 05:58:51 -04:00
|
|
|
enforce_key_validity=True,
|
2020-01-27 11:14:54 -05:00
|
|
|
special_case_aliases_auth=True,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=False,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2020-01-27 11:14:54 -05:00
|
|
|
)
|
2020-05-15 09:30:10 -04:00
|
|
|
V6 = RoomVersion(
|
|
|
|
"6",
|
|
|
|
RoomDisposition.STABLE,
|
2020-01-27 11:14:54 -05:00
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
|
|
|
enforce_key_validity=True,
|
2020-03-09 08:58:25 -04:00
|
|
|
special_case_aliases_auth=False,
|
2020-05-14 13:24:01 -04:00
|
|
|
strict_canonicaljson=True,
|
2020-05-14 12:38:17 -04:00
|
|
|
limit_notifications_power_levels=True,
|
2021-01-05 07:41:48 -05:00
|
|
|
msc2176_redaction_rules=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2021-01-05 07:41:48 -05:00
|
|
|
)
|
|
|
|
MSC2176 = RoomVersion(
|
|
|
|
"org.matrix.msc2176",
|
|
|
|
RoomDisposition.UNSTABLE,
|
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
|
|
|
enforce_key_validity=True,
|
|
|
|
special_case_aliases_auth=False,
|
|
|
|
strict_canonicaljson=True,
|
|
|
|
limit_notifications_power_levels=True,
|
|
|
|
msc2176_redaction_rules=True,
|
2021-03-31 16:39:08 -04:00
|
|
|
msc3083_join_rules=False,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
2021-03-31 16:39:08 -04:00
|
|
|
)
|
|
|
|
MSC3083 = RoomVersion(
|
|
|
|
"org.matrix.msc3083",
|
|
|
|
RoomDisposition.UNSTABLE,
|
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
|
|
|
enforce_key_validity=True,
|
|
|
|
special_case_aliases_auth=False,
|
|
|
|
strict_canonicaljson=True,
|
|
|
|
limit_notifications_power_levels=True,
|
|
|
|
msc2176_redaction_rules=False,
|
|
|
|
msc3083_join_rules=True,
|
2021-06-09 14:39:51 -04:00
|
|
|
msc2403_knocking=False,
|
|
|
|
)
|
2021-06-15 07:45:14 -04:00
|
|
|
V7 = RoomVersion(
|
|
|
|
"7",
|
|
|
|
RoomDisposition.STABLE,
|
2021-06-09 14:39:51 -04:00
|
|
|
EventFormatVersions.V3,
|
|
|
|
StateResolutionVersions.V2,
|
|
|
|
enforce_key_validity=True,
|
|
|
|
special_case_aliases_auth=False,
|
|
|
|
strict_canonicaljson=True,
|
|
|
|
limit_notifications_power_levels=True,
|
|
|
|
msc2176_redaction_rules=False,
|
|
|
|
msc3083_join_rules=False,
|
|
|
|
msc2403_knocking=True,
|
2019-05-20 10:54:42 -04:00
|
|
|
)
|
2019-04-01 05:24:38 -04:00
|
|
|
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
|
2019-06-20 05:32:02 -04:00
|
|
|
v.identifier: v
|
|
|
|
for v in (
|
2019-04-01 05:24:38 -04:00
|
|
|
RoomVersions.V1,
|
|
|
|
RoomVersions.V2,
|
|
|
|
RoomVersions.V3,
|
2019-05-21 11:22:54 -04:00
|
|
|
RoomVersions.V4,
|
2019-06-05 05:58:51 -04:00
|
|
|
RoomVersions.V5,
|
2020-05-15 09:30:10 -04:00
|
|
|
RoomVersions.V6,
|
2021-01-05 07:41:48 -05:00
|
|
|
RoomVersions.MSC2176,
|
2021-05-26 14:35:16 -04:00
|
|
|
RoomVersions.MSC3083,
|
2021-06-15 07:45:14 -04:00
|
|
|
RoomVersions.V7,
|
2019-04-01 05:24:38 -04:00
|
|
|
)
|
2021-07-20 07:59:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class RoomVersionCapability:
|
|
|
|
"""An object which describes the unique attributes of a room version."""
|
|
|
|
|
|
|
|
identifier: str # the identifier for this capability
|
|
|
|
preferred_version: Optional[RoomVersion]
|
|
|
|
support_check_lambda: Callable[[RoomVersion], bool]
|
|
|
|
|
|
|
|
|
|
|
|
MSC3244_CAPABILITIES = {
|
|
|
|
cap.identifier: {
|
|
|
|
"preferred": cap.preferred_version.identifier
|
|
|
|
if cap.preferred_version is not None
|
|
|
|
else None,
|
|
|
|
"support": [
|
|
|
|
v.identifier
|
|
|
|
for v in KNOWN_ROOM_VERSIONS.values()
|
|
|
|
if cap.support_check_lambda(v)
|
|
|
|
],
|
|
|
|
}
|
|
|
|
for cap in (
|
|
|
|
RoomVersionCapability(
|
|
|
|
"knock",
|
|
|
|
RoomVersions.V7,
|
|
|
|
lambda room_version: room_version.msc2403_knocking,
|
|
|
|
),
|
|
|
|
RoomVersionCapability(
|
|
|
|
"restricted",
|
|
|
|
None,
|
|
|
|
lambda room_version: room_version.msc3083_join_rules,
|
|
|
|
),
|
|
|
|
)
|
2021-07-15 06:02:43 -04:00
|
|
|
}
|