plugins/tools/set-default-voice-permissions.py
unimatrixgit dc8a2bd290
All checks were successful
Default Voice Permissions Operations Build / default-voice-permissions (push) Successful in 6s
DuneRealms Plugin CI / plugins (push) Successful in 2m50s
Replace default voice wildcard with player permissions
2026-12-02 12:13:35 +01:00

113 lines
4.7 KiB
Python

"""Replace the default LuckPerms voice wildcard with the three player actions."""
import argparse
import importlib.util
import json
from pathlib import Path
import sys
import time
ROOT = Path('/opt/dunerealms/servers/giedi_prime')
UNIT = 'dune-giedi_prime.service'
CONTROLLER = Path('/opt/dunerealms/server-control')
PINS = {
'LuckPerms-Bukkit.jar': 'af18d92f0b8d85b73835546e3376630dea62417291ed8571930e6875436a64be',
'voicechat-bukkit-2.6.25.jar': 'f39f2e840873aa08e0ccec733fdc824ed4c3b9282e027fd6f18a79006eebad20',
}
PLAY = ('voicechat.listen', 'voicechat.speak', 'voicechat.groups')
WILDCARD = 'voicechat.*'
TOUCHED = {*PLAY, WILDCARD}
spec = importlib.util.spec_from_file_location('dune_native_console', Path(__file__).with_name('allow-giedi-nether-pearl.py'))
console = importlib.util.module_from_spec(spec)
spec.loader.exec_module(console)
def global_grant(name):
return ('default', name, 1, 'global', 'global', 0, '{}')
def default_voice(rows):
return {row for row in rows if row[0] == 'default' and row[1] in TOUCHED}
def fixed_plan(rows):
scope = default_voice(rows)
allowed = {global_grant(name) for name in TOUCHED}
if scope - allowed:
raise ValueError('Default voice permissions have an unreviewed value or context')
needed = tuple(name for name in PLAY if global_grant(name) not in scope)
wildcard = global_grant(WILDCARD) in scope
if needed and not wildcard:
raise ValueError('Expected the live default wildcard before projecting the precise replacement')
# Refuse to treat default as unprivileged if an independent admin assignment appears.
if any(row[0] == 'default' and row[1] in {'*', 'voicechat.admin'} and row[2] == 1 for row in rows):
raise ValueError('A separate default admin grant requires review')
return needed, wildcard
def wait_exact(read, previous, expected, *, attempts=40, pause=time.sleep):
for _ in range(attempts):
observed = read()
if observed == expected:
return
if observed != previous:
raise RuntimeError('Unexpected permission drift. Check native console before continuing.')
pause(.25)
raise RuntimeError('LuckPerms persistence was not confirmed. Check native console before continuing.')
def update(read, issue, apply):
before = read()
needed, wildcard = fixed_plan(before)
result = {'group': 'default', 'ordinaryNodes': PLAY, 'missingNodes': needed, 'wildcardPresent': wildcard}
if not apply:
return {**result, 'status': 'planned'}
current = set(before)
for node in needed:
previous = current.copy()
issue(f'luckperms group default permission set {node} true')
current.add(global_grant(node))
wait_exact(read, previous, current)
if wildcard:
if read() != current or not {global_grant(node) for node in PLAY} <= current:
raise RuntimeError('Concrete voice permissions must be persistent before removing the wildcard')
previous = current.copy()
issue('luckperms group default permission unset voicechat.*')
current.remove(global_grant(WILDCARD))
wait_exact(read, previous, current)
return {**result, 'status': 'verified', 'allOtherGroupRowsVerified': len(before - default_voice(before))}
def read_groups():
# Existing server-control database connection. This query is read-only.
sys.path.insert(0, str(CONTROLLER))
from agent import permissions_database
sql = 'SELECT name,permission,value,server,world,expiry,contexts FROM luckperms_group_permissions'
with permissions_database() as connection, connection.cursor() as query:
query.execute(sql)
return {tuple(row[key] for key in ('name', 'permission', 'value', 'server', 'world', 'expiry', 'contexts'))
for row in query.fetchall()}
def run(apply):
if console.service('is-active', UNIT) != 'active' or console.service('show', UNIT, '--property=WorkingDirectory', '--value') != str(ROOT):
raise ValueError('Expected the active Giedi Prime LuckPerms console')
if any(console.sha(ROOT / 'plugins' / name) != pin for name, pin in PINS.items()):
raise ValueError('Installed LuckPerms or VoiceChat differs from the reviewed build')
return update(read_groups, lambda line: console.send(ROOT / 'console.fifo', line), apply)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--apply', action='store_true')
args = parser.parse_args()
if args.apply:
import fcntl
with Path('/run/lock/dunerealms-default-voice-permissions.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
result = run(True)
else:
result = run(False)
print(json.dumps(result, indent=2))