netresearch/matrix-skill / skills/matrix-communication
matrix-communication
FlaggedUse when communicating via Matrix chat, notifying teams, or managing E2EE. Triggers on #room:server references, Matrix URLs, and chat requests.
scripts/matrix-e2ee-setup.py
View on GitHub#!/usr/bin/env -S uv run --script
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Netresearch DTT GmbH
# /// script
# requires-python = ">=3.11"
# dependencies = ["matrix-nio[e2e]<0.26"]
# ///
"""Set up E2EE device for Matrix Skill.
This creates a dedicated Matrix device with encryption keys.
Run once to set up E2EE, then matrix-send-e2ee.py works without password.
Requires libolm system library:
Debian/Ubuntu: sudo apt install libolm-dev
Fedora: sudo dnf install libolm-devel
macOS 26+: unsupported, python-olm build fails (Clang 17); see setup-guide.md
Usage:
matrix-e2ee-setup.py # Interactive password prompt
MATRIX_PASSWORD=... matrix-e2ee-setup.py # Non-interactive
matrix-e2ee-setup.py --allow-secret-argument PASSWORD # Argument (visible in ps)
matrix-e2ee-setup.py --status
matrix-e2ee-setup.py --logout
matrix-e2ee-setup.py --logout --purge-all
matrix-e2ee-setup.py --help
Options:
--status Check if E2EE device is set up
--logout Remove the credentials and this device's store files
--purge-all With --logout: wipe every other device's store files too
--help Show this help
--logout is scoped to the device in credentials.json. The store directory is
shared by every device ever set up here, and their megolm history lives in it.
"""
import asyncio
import json
import os
import sys
# Add script directory to path for _lib imports
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _lib import (
check_e2ee_dependencies,
delete_credentials,
get_store_path,
load_config,
load_credentials,
prefer_ipv4,
save_credentials,
store_lock,
suppress_nio_logging,
)
from _lib.secret_input import ALLOW_FLAG, SecretInputError, read_secret
# Check dependencies before importing nio
check_e2ee_dependencies()
from nio import (
AsyncClient,
AsyncClientConfig,
LoginResponse,
)
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
async def setup_device(config: dict, password: str) -> dict:
"""Create a new E2EE device using password login."""
store_path = get_store_path()
client_config = AsyncClientConfig(
store_sync_tokens=True,
encryption_enabled=True,
)
# Extract localpart from user_id for login (some servers require this)
# @user:server -> user
user_id = config["user_id"]
login_user = (
user_id.split(":")[0].lstrip("@") if user_id.startswith("@") else user_id
)
client = AsyncClient(
homeserver=config["homeserver"],
user=login_user,
store_path=str(store_path),
config=client_config,
)
try:
# Login to create new device with hostname suffix
import socket
hostname = socket.gethostname()
device_name = f"Matrix Skill E2EE @ {hostname}"
login_response = await client.login(
password=password,
device_name=device_name,
)
if isinstance(login_response, LoginResponse):
# Save credentials (password NOT saved)
save_credentials(
user_id=login_response.user_id,
device_id=login_response.device_id,
access_token=login_response.access_token,
)
return {
"success": True,
"device_id": login_response.device_id,
"user_id": login_response.user_id,
}
else:
return {"error": str(login_response)}
finally:
await client.close()
def show_status(config: dict):
"""Show current E2EE setup status."""
creds = load_credentials()
if creds and creds.get("user_id") == config["user_id"]:
print("E2EE Status: SET UP")
print(f" User: {creds['user_id']}")
print(f" Device: {creds['device_id']}")
print(f" Store: {get_store_path()}")
else:
print("E2EE Status: NOT SET UP")
print()
print("Run setup; it prompts for your Matrix password:")
print(" matrix-e2ee-setup.py")
print("Non-interactive: MATRIX_PASSWORD=... matrix-e2ee-setup.py")
def main():
import argparse
parser = argparse.ArgumentParser(description="Set up E2EE device for Matrix Skill")
parser.add_argument(
"password",
nargs="?",
help="Matrix account password - refused unless "
f"{ALLOW_FLAG} is given; use the prompt or MATRIX_PASSWORD instead",
)
parser.add_argument(
ALLOW_FLAG,
dest="allow_secret_argument",
action="store_true",
help="Accept the password as a command-line argument (visible to other "
"local users in the process list)",
)
parser.add_argument("--status", action="store_true", help="Check E2EE setup status")
parser.add_argument(
"--logout",
action="store_true",
help="Remove the stored credentials and this device's store files",
)
parser.add_argument(
"--purge-all",
action="store_true",
help="With --logout: also delete the store files of every OTHER device "
"in the store directory (destroys their megolm history)",
)
parser.add_argument("--json", action="store_true", help="Output as JSON")
parser.add_argument("--debug", action="store_true", help="Show debug info")
args = parser.parse_args()
prefer_ipv4()
if not args.debug:
suppress_nio_logging()
config = load_config(require_user_id=True)
if args.status:
if args.json:
creds = load_credentials()
if creds and creds.get("user_id") == config["user_id"]:
print(
json.dumps(
{"status": "configured", "device_id": creds["device_id"]}
)
)
else:
print(json.dumps({"status": "not_configured"}))
else:
show_status(config)
return
if args.logout:
creds = load_credentials()
if creds:
removed = delete_credentials(purge_all=args.purge_all)
if args.json:
print(
json.dumps(
{
"success": True,
"message": "Credentials removed",
"device_id": creds.get("device_id"),
"removed": removed,
}
)
)
else:
scope = (
"every device in the store"
if args.purge_all
else f"device {creds.get('device_id')}"
)
print(f"E2EE credentials removed for {scope}:")
for name in removed:
print(f" {name}")
print("Note: The device still exists on the server.")
print("To fully remove it, go to Element > Settings > Sessions")
else:
if args.json:
print(json.dumps({"success": False, "message": "No credentials found"}))
else:
print("No E2EE credentials found.")
return
# Check if already set up
creds = load_credentials()
if creds and creds.get("user_id") == config["user_id"]:
if args.json:
print(
json.dumps(
{"status": "already_configured", "device_id": creds["device_id"]}
)
)
else:
print("E2EE already set up!")
print(f"Device: {creds['device_id']}")
print()
print("To reconfigure, first run: matrix-e2ee-setup.py --logout")
return
# Password: MATRIX_PASSWORD or an interactive prompt. A command-line
# argument only with --allow-secret-argument (see _lib/secret_input.py).
if not args.password and not os.environ.get("MATRIX_PASSWORD"):
print(f"Setting up E2EE device for {config['user_id']}")
print("Password is used once to create device, then not stored.")
print()
try:
password = read_secret(
args.password or "",
"MATRIX_PASSWORD",
f"Matrix password for {config['user_id']}: ",
allow_argument=args.allow_secret_argument,
)
except (KeyboardInterrupt, EOFError):
print("\nAborted.")
sys.exit(1)
except SecretInputError as exc:
print(f"Error: {exc}", file=sys.stderr)
sys.exit(1)
# Run setup under the store lock: the new device's store is created in the
# directory a running daemon or command is using.
with store_lock():
result = asyncio.run(setup_device(config, password))
if "error" in result:
if args.json:
print(json.dumps(result))
else:
print(f"Error: {result['error']}", file=sys.stderr)
sys.exit(1)
if args.json:
print(json.dumps(result))
else:
print("E2EE device created successfully!")
print(f" Device ID: {result['device_id']}")
print(f" User: {result['user_id']}")
print()
print("You can now use matrix-send-e2ee.py without password.")
print("The device appears as 'Matrix Skill E2EE' in your sessions.")
if __name__ == "__main__":
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |