You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
2.4 KiB

  1. # Copyright 2017 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List, Tuple
  15. from synapse.config import ConfigError
  16. from synapse.util.module_loader import load_module
  17. from ._base import Config
  18. class SpamCheckerConfig(Config):
  19. section = "spamchecker"
  20. def read_config(self, config, **kwargs):
  21. self.spam_checkers = [] # type: List[Tuple[Any, Dict]]
  22. spam_checkers = config.get("spam_checker") or []
  23. if isinstance(spam_checkers, dict):
  24. # The spam_checker config option used to only support one
  25. # spam checker, and thus was simply a dictionary with module
  26. # and config keys. Support this old behaviour by checking
  27. # to see if the option resolves to a dictionary
  28. self.spam_checkers.append(load_module(spam_checkers, ("spam_checker",)))
  29. elif isinstance(spam_checkers, list):
  30. for i, spam_checker in enumerate(spam_checkers):
  31. config_path = ("spam_checker", "<item %i>" % i)
  32. if not isinstance(spam_checker, dict):
  33. raise ConfigError("expected a mapping", config_path)
  34. self.spam_checkers.append(load_module(spam_checker, config_path))
  35. else:
  36. raise ConfigError("spam_checker syntax is incorrect")
  37. def generate_config_section(self, **kwargs):
  38. return """\
  39. # Spam checkers are third-party modules that can block specific actions
  40. # of local users, such as creating rooms and registering undesirable
  41. # usernames, as well as remote users by redacting incoming events.
  42. #
  43. spam_checker:
  44. #- module: "my_custom_project.SuperSpamChecker"
  45. # config:
  46. # example_option: 'things'
  47. #- module: "some_other_project.BadEventStopper"
  48. # config:
  49. # example_stop_events_from: ['@bad:example.com']
  50. """