123 lines
3.0 KiB
Python
123 lines
3.0 KiB
Python
from pathlib import Path
|
|
import tomllib
|
|
import os
|
|
|
|
|
|
addr: str = "127.0.0.1"
|
|
port: int = 5000
|
|
users_can_register: bool = False
|
|
the_funny_number: int = 1337
|
|
cat: str = "/etc/vona/cat.jpg"
|
|
|
|
server_name: str = ""
|
|
signing_key: str = ""
|
|
support: dict = {"contacts": []}
|
|
|
|
_CONFIG_PATH = Path(os.getenv("VONA_CONFIG", "/etc/vona/config.toml"))
|
|
|
|
|
|
def _fatal(msg: str) -> None:
|
|
print(f"[FATL] {msg}")
|
|
os._exit(1)
|
|
|
|
|
|
def _warn(msg: str) -> None:
|
|
print(f"[WARN] {msg}")
|
|
|
|
|
|
def _load_toml(path: Path) -> dict:
|
|
try:
|
|
with path.open("rb") as f:
|
|
return tomllib.load(f)
|
|
except FileNotFoundError:
|
|
_fatal(f"Configuration file not found at {path}")
|
|
except PermissionError:
|
|
_fatal(f"Permission denied when accessing configuration {path}")
|
|
except tomllib.TOMLDecodeError as e:
|
|
_fatal(f"Invalid TOML configuration: {e}")
|
|
|
|
|
|
def _read_signing_key_from_path(path_value) -> str | None:
|
|
p = Path(path_value)
|
|
|
|
if not p.exists():
|
|
_fatal(f"signing_key_path {p} does not exist")
|
|
try:
|
|
return p.read_text(encoding="utf-8").strip()
|
|
except Exception as e:
|
|
_fatal(f"Failed to read signing_key_path {p}: {e}")
|
|
|
|
|
|
def _validate_cat_path(cat_path: str) -> Path:
|
|
p = Path(cat_path)
|
|
if not p.exists():
|
|
_fatal(f"Cat photo at {p} does not exist")
|
|
|
|
return p
|
|
|
|
|
|
def _apply_config(cfg: dict) -> None:
|
|
global addr, port, server_name, signing_key, cat, support, users_can_register, db
|
|
|
|
if "address" in cfg:
|
|
addr = str(cfg["address"])
|
|
|
|
if "port" in cfg:
|
|
try:
|
|
port = int(cfg["port"])
|
|
except (TypeError, ValueError):
|
|
_warn(f"Invalid port in config: {cfg.get('port')}; using default {port}")
|
|
|
|
if "server_name" in cfg:
|
|
server_name = str(cfg["server_name"])
|
|
else:
|
|
_fatal("`server_name` is not in configuration")
|
|
|
|
if "signing_key" in cfg and "signing_key_path" in cfg:
|
|
_warn("Both `signing_key` and `signing_key_path` present. Using `signing_key`.")
|
|
|
|
if "signing_key" in cfg:
|
|
signing_key = str(cfg["signing_key"]).strip()
|
|
elif "signing_key_path" in cfg:
|
|
sk = _read_signing_key_from_path(cfg["signing_key_path"])
|
|
|
|
if sk:
|
|
signing_key = sk
|
|
else:
|
|
_fatal(f"{cfg["signing_key_path"]} does not have a signing key.")
|
|
|
|
else:
|
|
_fatal(
|
|
"A signing key is not present in the configuration. "
|
|
"A key can be generated using `python3 -m vona.utils.makekey`."
|
|
)
|
|
|
|
if "cat" in cfg:
|
|
cat = str(cfg["cat"])
|
|
|
|
cat_path = _validate_cat_path(cat)
|
|
cat = str(cat_path)
|
|
|
|
support_obj = {"contacts": []}
|
|
if "support" in cfg and isinstance(cfg["support"], dict):
|
|
_support = cfg["support"]
|
|
contact = {"role": "m.role.admin"}
|
|
if "mxid" in _support:
|
|
contact["matrix_id"] = str(_support["mxid"])
|
|
if "email" in _support:
|
|
contact["email_address"] = str(_support["email"])
|
|
if len(contact) > 1:
|
|
support_obj["contacts"].append(contact)
|
|
else:
|
|
_warn("No support contacts are defined")
|
|
|
|
support = support_obj
|
|
|
|
if "enable_registration" in cfg and isinstance(cfg["enable_registration"], bool):
|
|
users_can_register = cfg["enable_registration"]
|
|
|
|
print("[INFO] Configuration file was valid")
|
|
|
|
|
|
_apply_config(_load_toml(_CONFIG_PATH))
|