feat: Initializes Matrix Redactor Bot project
Introduces a basic framework for Matrix Redactor Bot, including .gitignore for environment files and compiled Python files. Adds LICENSE with MIT license terms and a README for project overview. Configures a sample YAML file for bot setup. Sets up a Python package with pyproject.toml, specifying dependencies and entry points. Implements basic redactor bot function to redact media and links sent by low-privileged users in Matrix rooms.
This commit is contained in:
commit
8e421abb76
7 changed files with 121 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
venv/
|
||||
*.pyc
|
||||
__pycache__/
|
||||
config.yaml
|
19
LICENSE
Normal file
19
LICENSE
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2024 Private.coffee Team <support@private.coffee>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
1
README.md
Normal file
1
README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# Matrix Redactor Bot
|
3
config.dist.yaml
Normal file
3
config.dist.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
homeserver: https://matrix.example.com
|
||||
user_id: "@example:example.com"
|
||||
access_token: syt_1234567890
|
26
pyproject.toml
Normal file
26
pyproject.toml
Normal file
|
@ -0,0 +1,26 @@
|
|||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "matrix_redactorbot"
|
||||
version = "0.1.5"
|
||||
authors = [{ name = "Private.coffee Team", email = "support@private.coffee" }]
|
||||
description = "A simple bot to redact media and links sent by unprivileged users in Matrix rooms."
|
||||
readme = "README.md"
|
||||
license = { file = "LICENSE" }
|
||||
requires-python = ">=3.10"
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
]
|
||||
dependencies = ["matrix-nio", "pyyaml"]
|
||||
|
||||
[project.scripts]
|
||||
redactorbot = "matrix_redactorbot.main:main"
|
||||
|
||||
[project.urls]
|
||||
"Homepage" = "https://git.private.coffee/privatecoffee/matrix-redactorbot"
|
||||
"Bug Tracker" = "https://git.private.coffee/privatecoffee/matrix-redactorbot/issues"
|
||||
"Source Code" = "https://git.private.coffee/privatecoffee/matrix-redactorbot"
|
0
src/matrix_redactorbot/__init__.py
Normal file
0
src/matrix_redactorbot/__init__.py
Normal file
68
src/matrix_redactorbot/main.py
Normal file
68
src/matrix_redactorbot/main.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
import re
|
||||
import yaml
|
||||
import nio
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessage, RoomMessageText
|
||||
|
||||
# Regular expression to identify URLs in the text
|
||||
url_regex = re.compile(r"(https?://\S+)|(www\.\S+)")
|
||||
|
||||
|
||||
class RedactorBot:
|
||||
def __init__(self, homeserver, user_id, access_token, sync_timeout):
|
||||
self.client = AsyncClient(homeserver, user_id)
|
||||
self.client.access_token = access_token
|
||||
self.sync_timeout = sync_timeout
|
||||
self.client.add_event_callback(self.message_callback, RoomMessage)
|
||||
|
||||
async def message_callback(self, room: MatrixRoom, event: RoomMessage):
|
||||
if isinstance(event, RoomMessageText):
|
||||
power_levels_event = room.power_levels
|
||||
user_power_level = power_levels_event.users.get(event.sender, 0)
|
||||
|
||||
# Check whether the sender is a power level 0 user
|
||||
if user_power_level == 0:
|
||||
# Check for media content or links
|
||||
if "url" in event.source["content"] or url_regex.search(event.body):
|
||||
response = await self.client.room_redact(
|
||||
room_id=room.room_id,
|
||||
event_id=event.event_id,
|
||||
reason="Message redacted due to insufficient power level",
|
||||
)
|
||||
if isinstance(response, nio.RedactResponse):
|
||||
print(
|
||||
f"Redacted message from {event.sender} in {room.display_name}"
|
||||
)
|
||||
else:
|
||||
print(f"Failed to redact message: {response}")
|
||||
|
||||
async def run(self):
|
||||
await self.client.sync_forever(timeout=self.sync_timeout)
|
||||
|
||||
|
||||
def load_config(file_path="config.yaml"):
|
||||
with open(file_path, "r") as config_file:
|
||||
return yaml.safe_load(config_file)
|
||||
|
||||
|
||||
# Load the configuration
|
||||
config = load_config()
|
||||
|
||||
bot = RedactorBot(
|
||||
homeserver=config["homeserver"],
|
||||
user_id=config["user_id"],
|
||||
access_token=config["access_token"],
|
||||
sync_timeout=config.get("sync_timeout", 30000),
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(bot.run())
|
||||
except KeyboardInterrupt:
|
||||
print("Bot stopped by user")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue