room.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from synapse.api.constants import RoomCreationPreset
  17. from ._base import Config, ConfigError
  18. logger = logging.Logger(__name__)
  19. class RoomDefaultEncryptionTypes:
  20. """Possible values for the encryption_enabled_by_default_for_room_type config option"""
  21. ALL = "all"
  22. INVITE = "invite"
  23. OFF = "off"
  24. class RoomConfig(Config):
  25. section = "room"
  26. def read_config(self, config, **kwargs):
  27. # Whether new, locally-created rooms should have encryption enabled
  28. encryption_for_room_type = config.get(
  29. "encryption_enabled_by_default_for_room_type",
  30. RoomDefaultEncryptionTypes.OFF,
  31. )
  32. if encryption_for_room_type == RoomDefaultEncryptionTypes.ALL:
  33. self.encryption_enabled_by_default_for_room_presets = [
  34. RoomCreationPreset.PRIVATE_CHAT,
  35. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  36. RoomCreationPreset.PUBLIC_CHAT,
  37. ]
  38. elif encryption_for_room_type == RoomDefaultEncryptionTypes.INVITE:
  39. self.encryption_enabled_by_default_for_room_presets = [
  40. RoomCreationPreset.PRIVATE_CHAT,
  41. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  42. ]
  43. elif (
  44. encryption_for_room_type == RoomDefaultEncryptionTypes.OFF
  45. or encryption_for_room_type is False
  46. ):
  47. # PyYAML translates "off" into False if it's unquoted, so we also need to
  48. # check for encryption_for_room_type being False.
  49. self.encryption_enabled_by_default_for_room_presets = []
  50. else:
  51. raise ConfigError(
  52. "Invalid value for encryption_enabled_by_default_for_room_type"
  53. )
  54. def generate_config_section(self, **kwargs):
  55. return """\
  56. ## Rooms ##
  57. # Controls whether locally-created rooms should be end-to-end encrypted by
  58. # default.
  59. #
  60. # Possible options are "all", "invite", and "off". They are defined as:
  61. #
  62. # * "all": any locally-created room
  63. # * "invite": any room created with the "private_chat" or "trusted_private_chat"
  64. # room creation presets
  65. # * "off": this option will take no effect
  66. #
  67. # The default value is "off".
  68. #
  69. # Note that this option will only affect rooms created after it is set. It
  70. # will also not affect rooms created by other servers.
  71. #
  72. #encryption_enabled_by_default_for_room_type: invite
  73. """