Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

spam_checker.md 3.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <h2 style="color:red">
  2. This page of the Synapse documentation is now deprecated. For up to date
  3. documentation on setting up or writing a spam checker module, please see
  4. <a href="modules.md">this page</a>.
  5. </h2>
  6. # Handling spam in Synapse
  7. Synapse has support to customize spam checking behavior. It can plug into a
  8. variety of events and affect how they are presented to users on your homeserver.
  9. The spam checking behavior is implemented as a Python class, which must be
  10. able to be imported by the running Synapse.
  11. ## Python spam checker class
  12. The Python class is instantiated with two objects:
  13. * Any configuration (see below).
  14. * An instance of `synapse.module_api.ModuleApi`.
  15. It then implements methods which return a boolean to alter behavior in Synapse.
  16. All the methods must be defined.
  17. There's a generic method for checking every event (`check_event_for_spam`), as
  18. well as some specific methods:
  19. * `user_may_invite`
  20. * `user_may_create_room`
  21. * `user_may_create_room_alias`
  22. * `user_may_publish_room`
  23. * `check_username_for_spam`
  24. * `check_registration_for_spam`
  25. * `check_media_file_for_spam`
  26. The details of each of these methods (as well as their inputs and outputs)
  27. are documented in the `synapse.events.spamcheck.SpamChecker` class.
  28. The `ModuleApi` class provides a way for the custom spam checker class to
  29. call back into the homeserver internals.
  30. Additionally, a `parse_config` method is mandatory and receives the plugin config
  31. dictionary. After parsing, It must return an object which will be
  32. passed to `__init__` later.
  33. ### Example
  34. ```python
  35. from synapse.spam_checker_api import RegistrationBehaviour
  36. class ExampleSpamChecker:
  37. def __init__(self, config, api):
  38. self.config = config
  39. self.api = api
  40. @staticmethod
  41. def parse_config(config):
  42. return config
  43. async def check_event_for_spam(self, foo):
  44. return False # allow all events
  45. async def user_may_invite(self, inviter_userid, invitee_userid, room_id):
  46. return True # allow all invites
  47. async def user_may_create_room(self, userid):
  48. return True # allow all room creations
  49. async def user_may_create_room_alias(self, userid, room_alias):
  50. return True # allow all room aliases
  51. async def user_may_publish_room(self, userid, room_id):
  52. return True # allow publishing of all rooms
  53. async def check_username_for_spam(self, user_profile):
  54. return False # allow all usernames
  55. async def check_registration_for_spam(
  56. self,
  57. email_threepid,
  58. username,
  59. request_info,
  60. auth_provider_id,
  61. ):
  62. return RegistrationBehaviour.ALLOW # allow all registrations
  63. async def check_media_file_for_spam(self, file_wrapper, file_info):
  64. return False # allow all media
  65. ```
  66. ## Configuration
  67. Modify the `spam_checker` section of your `homeserver.yaml` in the following
  68. manner:
  69. Create a list entry with the keys `module` and `config`.
  70. * `module` should point to the fully qualified Python class that implements your
  71. custom logic, e.g. `my_module.ExampleSpamChecker`.
  72. * `config` is a dictionary that gets passed to the spam checker class.
  73. ### Example
  74. This section might look like:
  75. ```yaml
  76. spam_checker:
  77. - module: my_module.ExampleSpamChecker
  78. config:
  79. # Enable or disable a specific option in ExampleSpamChecker.
  80. my_custom_option: true
  81. ```
  82. More spam checkers can be added in tandem by appending more items to the list. An
  83. action is blocked when at least one of the configured spam checkers flags it.
  84. ## Examples
  85. The [Mjolnir](https://github.com/matrix-org/mjolnir) project is a full fledged
  86. example using the Synapse spam checking API, including a bot for dynamic
  87. configuration.