Files

26 lines
897 B
Python

import re
from .constants import BACKEND_MATRIX, BACKEND_TELEGRAM
SPLIT_RE = re.compile(r"[,;\s]+")
TELEGRAM_ID_RE = re.compile(r"^-?\d{1,20}$")
MATRIX_ID_RE = re.compile(r"^@[^:\s@]+:[^\s:@]+(:\d{1,5})?$")
def normalize_id(backend: str, sender_id: str) -> str | None:
value = str(sender_id).strip()
if backend == BACKEND_TELEGRAM:
return value if TELEGRAM_ID_RE.match(value) else None
if backend == BACKEND_MATRIX and MATRIX_ID_RE.match(value):
local, _, domain = value.partition(":")
return f"{local}:{domain.lower()}"
return None
def is_allowed(backend: str, sender_id: str, allowlist_csv: str | None) -> bool:
sender = normalize_id(backend, sender_id)
if sender is None or not allowlist_csv:
return False
entries = {normalize_id(backend, entry) for entry in SPLIT_RE.split(allowlist_csv) if entry}
return sender in entries