spamcheck.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 New Vector Ltd.
  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. class SpamChecker(object):
  16. def __init__(self, hs):
  17. self.spam_checker = None
  18. module = None
  19. config = None
  20. try:
  21. module, config = hs.config.spam_checker
  22. except Exception:
  23. pass
  24. if module is not None:
  25. self.spam_checker = module(config=config)
  26. def check_event_for_spam(self, event):
  27. """Checks if a given event is considered "spammy" by this server.
  28. If the server considers an event spammy, then it will be rejected if
  29. sent by a local user. If it is sent by a user on another server, then
  30. users receive a blank event.
  31. Args:
  32. event (synapse.events.EventBase): the event to be checked
  33. Returns:
  34. bool: True if the event is spammy.
  35. """
  36. if self.spam_checker is None:
  37. return False
  38. return self.spam_checker.check_event_for_spam(event)
  39. def user_may_invite(self, inviter_userid, invitee_userid, room_id):
  40. """Checks if a given user may send an invite
  41. If this method returns false, the invite will be rejected.
  42. Args:
  43. userid (string): The sender's user ID
  44. Returns:
  45. bool: True if the user may send an invite, otherwise False
  46. """
  47. if self.spam_checker is None:
  48. return True
  49. return self.spam_checker.user_may_invite(inviter_userid, invitee_userid, room_id)
  50. def user_may_create_room(self, userid):
  51. """Checks if a given user may create a room
  52. If this method returns false, the creation request will be rejected.
  53. Args:
  54. userid (string): The sender's user ID
  55. Returns:
  56. bool: True if the user may create a room, otherwise False
  57. """
  58. if self.spam_checker is None:
  59. return True
  60. return self.spam_checker.user_may_create_room(userid)
  61. def user_may_create_room_alias(self, userid, room_alias):
  62. """Checks if a given user may create a room alias
  63. If this method returns false, the association request will be rejected.
  64. Args:
  65. userid (string): The sender's user ID
  66. room_alias (string): The alias to be created
  67. Returns:
  68. bool: True if the user may create a room alias, otherwise False
  69. """
  70. if self.spam_checker is None:
  71. return True
  72. return self.spam_checker.user_may_create_room_alias(userid, room_alias)
  73. def user_may_publish_room(self, userid, room_id):
  74. """Checks if a given user may publish a room to the directory
  75. If this method returns false, the publish request will be rejected.
  76. Args:
  77. userid (string): The sender's user ID
  78. room_id (string): The ID of the room that would be published
  79. Returns:
  80. bool: True if the user may publish the room, otherwise False
  81. """
  82. if self.spam_checker is None:
  83. return True
  84. return self.spam_checker.user_may_publish_room(userid, room_id)