Files
matrix/matrixbot/config.py
T
jaredandClaude Opus 5 9e5d3ec83d
Lint / JS (eslint) (push) Successful in 40s
Lint / Python (ruff) (push) Successful in 10s
Lint / Python deps (pip-audit) (push) Successful in 1m44s
Lint / Secret scan (gitleaks) (push) Successful in 33s
Lint / Shell (shellcheck) (push) Successful in 11s
feat(matrixbot): stop auto-joining invites, restrict commands to two rooms
#general is now published to the public room directory, so the bot is
reachable by strangers. Two hardening changes:

- Invites are only accepted from INVITE_ALLOWED_USERS (defaults to
  ADMIN_USERS). Anything else is declined via room_leave so hostile
  invites do not accumulate as pending. Previously any invite from
  anyone was auto-joined.
- Commands and the passive scramble/riddle answer checks only run in
  COMMAND_ROOMS (#commands and #management). The gate sits early in
  Callbacks.message, so the bot stays silent everywhere else, including
  #general. Set COMMAND_ROOMS="*" to restore the old behaviour.

Reaction handlers are deliberately left ungated so the welcome flow
keeps working; game votes elsewhere cannot match an active game.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-16 01:37:45 -04:00

64 lines
2.4 KiB
Python

import os
from dotenv import load_dotenv
load_dotenv()
# Required
MATRIX_HOMESERVER = os.getenv("MATRIX_HOMESERVER", "https://matrix.lotusguild.org")
MATRIX_USER_ID = os.getenv("MATRIX_USER_ID", "@lotusbot:matrix.lotusguild.org")
MATRIX_ACCESS_TOKEN = os.getenv("MATRIX_ACCESS_TOKEN", "")
MATRIX_DEVICE_ID = os.getenv("MATRIX_DEVICE_ID", "")
MATRIX_PASSWORD = os.getenv("MATRIX_PASSWORD", "")
# Bot settings
BOT_PREFIX = os.getenv("BOT_PREFIX", "!")
ADMIN_USERS = [u.strip() for u in os.getenv("ADMIN_USERS", "").split(",") if u.strip()]
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
# Rooms where commands and interactive games are allowed. The bot stays silent
# everywhere else, so it never responds in public rooms like #general.
# Override with a comma-separated COMMAND_ROOMS; set to "*" to allow all rooms.
DEFAULT_COMMAND_ROOMS = (
"!ou56mVZQ8ZB7AhDYPmBV5_BR28WMZ4x5zwZkPCqjq1s", # #commands
"!mEvR5fe3jMmzwd-FwNygD72OY_yu8H3UP_N-57oK7MI", # #management
)
_command_rooms = os.getenv("COMMAND_ROOMS", ",".join(DEFAULT_COMMAND_ROOMS))
COMMAND_ROOMS = [r.strip() for r in _command_rooms.split(",") if r.strip()]
# Users whose invites the bot will accept. Invites from anyone else are
# declined: the homeserver is publicly listed, so auto-joining any invite lets
# a stranger pull the bot into an arbitrary room. Defaults to ADMIN_USERS.
_invite_allowed = os.getenv("INVITE_ALLOWED_USERS", "")
INVITE_ALLOWED_USERS = [u.strip() for u in _invite_allowed.split(",") if u.strip()] or list(ADMIN_USERS)
# Integrations
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://10.10.10.157:11434")
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "phi4-mini:latest")
CREATIVE_MODEL = os.getenv("CREATIVE_MODEL", "huihui_ai/llama3.2-abliterate:3b")
ASK_MODEL = os.getenv("ASK_MODEL", "phi4-mini:latest")
MINECRAFT_RCON_HOST = os.getenv("MINECRAFT_RCON_HOST", "10.10.10.68")
MINECRAFT_RCON_PORT = int(os.getenv("MINECRAFT_RCON_PORT", "25575"))
MINECRAFT_RCON_PASSWORD = os.getenv("MINECRAFT_RCON_PASSWORD", "")
# Constants
MAX_INPUT_LENGTH = 500
MAX_DICE_SIDES = 100
MAX_DICE_COUNT = 20
COOLDOWN_SECONDS = int(os.getenv("COOLDOWN_SECONDS", "120"))
RCON_TIMEOUT = 5.0
MIN_USERNAME_LENGTH = 3
MAX_USERNAME_LENGTH = 16
class ConfigValidator:
REQUIRED = ["MATRIX_HOMESERVER", "MATRIX_USER_ID"]
@classmethod
def validate(cls):
errors = []
for var in cls.REQUIRED:
if not os.getenv(var):
errors.append(f"Missing required: {var}")
return errors