1515# limitations under the License.
1616
1717import inspect
18- from typing import Dict
18+ from typing import Any , Dict , List
1919
2020from synapse .spam_checker_api import SpamCheckerApi
2121
2626
2727class SpamChecker (object ):
2828 def __init__ (self , hs : "synapse.server.HomeServer" ):
29- self .spam_checker = None
29+ self .spam_checkers = [] # type: List[Any]
3030
31- module = None
32- config = None
33- try :
34- module , config = hs .config .spam_checker
35- except Exception :
36- pass
37-
38- if module is not None :
31+ for module , config in hs .config .spam_checkers :
3932 # Older spam checkers don't accept the `api` argument, so we
4033 # try and detect support.
4134 spam_args = inspect .getfullargspec (module )
4235 if "api" in spam_args .args :
4336 api = SpamCheckerApi (hs )
44- self .spam_checker = module (config = config , api = api )
37+ self .spam_checkers . append ( module (config = config , api = api ) )
4538 else :
46- self .spam_checker = module (config = config )
39+ self .spam_checkers . append ( module (config = config ) )
4740
4841 def check_event_for_spam (self , event : "synapse.events.EventBase" ) -> bool :
4942 """Checks if a given event is considered "spammy" by this server.
@@ -58,10 +51,11 @@ def check_event_for_spam(self, event: "synapse.events.EventBase") -> bool:
5851 Returns:
5952 True if the event is spammy.
6053 """
61- if self .spam_checker is None :
62- return False
54+ for spam_checker in self .spam_checkers :
55+ if spam_checker .check_event_for_spam (event ):
56+ return True
6357
64- return self . spam_checker . check_event_for_spam ( event )
58+ return False
6559
6660 def user_may_invite (
6761 self , inviter_userid : str , invitee_userid : str , room_id : str
@@ -78,12 +72,14 @@ def user_may_invite(
7872 Returns:
7973 True if the user may send an invite, otherwise False
8074 """
81- if self .spam_checker is None :
82- return True
75+ for spam_checker in self .spam_checkers :
76+ if (
77+ spam_checker .user_may_invite (inviter_userid , invitee_userid , room_id )
78+ is False
79+ ):
80+ return False
8381
84- return self .spam_checker .user_may_invite (
85- inviter_userid , invitee_userid , room_id
86- )
82+ return True
8783
8884 def user_may_create_room (self , userid : str ) -> bool :
8985 """Checks if a given user may create a room
@@ -96,10 +92,11 @@ def user_may_create_room(self, userid: str) -> bool:
9692 Returns:
9793 True if the user may create a room, otherwise False
9894 """
99- if self .spam_checker is None :
100- return True
95+ for spam_checker in self .spam_checkers :
96+ if spam_checker .user_may_create_room (userid ) is False :
97+ return False
10198
102- return self . spam_checker . user_may_create_room ( userid )
99+ return True
103100
104101 def user_may_create_room_alias (self , userid : str , room_alias : str ) -> bool :
105102 """Checks if a given user may create a room alias
@@ -113,10 +110,11 @@ def user_may_create_room_alias(self, userid: str, room_alias: str) -> bool:
113110 Returns:
114111 True if the user may create a room alias, otherwise False
115112 """
116- if self .spam_checker is None :
117- return True
113+ for spam_checker in self .spam_checkers :
114+ if spam_checker .user_may_create_room_alias (userid , room_alias ) is False :
115+ return False
118116
119- return self . spam_checker . user_may_create_room_alias ( userid , room_alias )
117+ return True
120118
121119 def user_may_publish_room (self , userid : str , room_id : str ) -> bool :
122120 """Checks if a given user may publish a room to the directory
@@ -130,10 +128,11 @@ def user_may_publish_room(self, userid: str, room_id: str) -> bool:
130128 Returns:
131129 True if the user may publish the room, otherwise False
132130 """
133- if self .spam_checker is None :
134- return True
131+ for spam_checker in self .spam_checkers :
132+ if spam_checker .user_may_publish_room (userid , room_id ) is False :
133+ return False
135134
136- return self . spam_checker . user_may_publish_room ( userid , room_id )
135+ return True
137136
138137 def check_username_for_spam (self , user_profile : Dict [str , str ]) -> bool :
139138 """Checks if a user ID or display name are considered "spammy" by this server.
@@ -150,13 +149,14 @@ def check_username_for_spam(self, user_profile: Dict[str, str]) -> bool:
150149 Returns:
151150 True if the user is spammy.
152151 """
153- if self .spam_checker is None :
154- return False
155-
156- # For backwards compatibility, if the method does not exist on the spam checker, fallback to not interfering.
157- checker = getattr (self .spam_checker , "check_username_for_spam" , None )
158- if not checker :
159- return False
160- # Make a copy of the user profile object to ensure the spam checker
161- # cannot modify it.
162- return checker (user_profile .copy ())
152+ for spam_checker in self .spam_checkers :
153+ # For backwards compatibility, only run if the method exists on the
154+ # spam checker
155+ checker = getattr (spam_checker , "check_username_for_spam" , None )
156+ if checker :
157+ # Make a copy of the user profile object to ensure the spam checker
158+ # cannot modify it.
159+ if checker (user_profile .copy ()):
160+ return True
161+
162+ return False
0 commit comments