diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c6f2cb --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +``` +spam_checker: + - module: limiter.Limiter + config: + limit: 750 + protected_rooms: + - "!foobarbaz:matrix.org" + - "!barbazquz:matrix.org" +``` diff --git a/limiter/__init__.py b/limiter/__init__.py new file mode 100644 index 0000000..4e96616 --- /dev/null +++ b/limiter/__init__.py @@ -0,0 +1 @@ +from .limiter import Limiter diff --git a/limiter/limiter.py b/limiter/limiter.py new file mode 100644 index 0000000..6855e3c --- /dev/null +++ b/limiter/limiter.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# Copyright 2019 The Matrix.org Foundation C.I.C. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +class Limiter(object): + def __init__(self, config, api): + self.api = api + self.limit = config.get("limit", 1000) + self.protected_rooms = config.get("protected_rooms", []) + + # --- spam checker interface below here --- + + def check_event_for_spam(self, event): + body = event.get("content", {}).get("body", "") + room_id = event.get("room_id", "") + if room_id in self.protected_rooms and len(body) > self.limit: + return True # It's over the limit + return False # not spam (as far as we're concerned) + + def user_may_invite(self, inviter_user_id, invitee_user_id, room_id): + return True # allowed (as far as we're concerned) + + def check_username_for_spam(self, user_profile): + return True + + def user_may_create_room(self, user_id): + return True # allowed + + def user_may_create_room_alias(self, user_id, room_alias): + return True # allowed + + def user_may_publish_room(self, user_id, room_id): + return True # allowed + + @staticmethod + def parse_config(config): + return config # no parsing needed diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7864cfe --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup, find_packages + +setup( + name="limiter", + version="0.0.1", + packages=find_packages(), + description="Limiter", + include_package_data=True, + zip_safe=True, + install_requires=[], +)