Spaces:
Paused
Paused
File size: 16,449 Bytes
3151305 8e12048 3151305 8e12048 3151305 8e12048 3151305 8e12048 3151305 8e12048 3151305 8e12048 3151305 8e12048 3151305 8e12048 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | """Compile a .bbproj block tree into a runnable nerimity.js bot, server-side.
This is the whole security model. The client uploads *block structure* — opcodes, literal
field values and nesting — never JavaScript. Every line of emitted code comes from
catalog.json, which ships with the host and is not user-writable. An unknown opcode is
rejected outright, and every literal is escaped through json.dumps or validated as a
number, so a field value cannot break out of its expression and become code.
Ported from the editor's original GDScript generator; catalog.json is generated from the
same catalogue the editor renders from, so the two cannot drift.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
from typing import Any
CATALOG_PATH = Path(__file__).parent / "catalog.json"
INDENT = " "
# event name -> (handler signature, __ctx body)
EVENT_CONTEXTS: dict[str, tuple[str, str]] = {
"Ready": ("()", "{}"),
"MessageCreate": ("(message)",
"{ message, channel: message?.channel, user: message?.user, "
"member: message?.member, server: message?.channel?.server }"),
"MessageUpdate": ("(message)",
"{ message, channel: message?.channel, user: message?.user, "
"member: message?.member, server: message?.channel?.server }"),
"MessageDelete": ("(payload)",
"{ channel: client.channels?.cache?.get(payload?.channelId) }"),
"MessageButtonClick": ("(button)",
"{ button, message: button?.message, user: button?.user, channel: button?.channel }"),
"MessageReactionAdded": ("(reaction, user)",
"{ reaction, user, message: reaction?.message, channel: reaction?.channel }"),
"MessageReactionRemoved": ("(reaction, user)",
"{ reaction, user, message: reaction?.message, channel: reaction?.channel }"),
"ServerMemberJoined": ("(member)", "{ member, user: member?.user, server: member?.server }"),
"ServerMemberLeft": ("(member)", "{ member, user: member?.user, server: member?.server }"),
}
MAX_BLOCKS = 2000 # a project this large is a bug or an attack, not a bot
MAX_DEPTH = 60
MAX_COMMAND_OPTIONS = 20 # keeps a command hat's preamble bounded
# Command options are written the way people write lists: "count sides" or "count, sides".
OPTION_SEPARATOR = re.compile(r"[\s,]+")
class CompileError(Exception):
"""Raised for anything malformed; the message goes back to the editor verbatim."""
class Compiler:
def __init__(self, catalog: dict | None = None) -> None:
if catalog is None:
catalog = json.loads(CATALOG_PATH.read_text())
self.blocks: dict[str, dict] = catalog["blocks"]
self.variables: dict[str, str] = {}
self.lists: dict[str, str] = {}
self.warnings: list[str] = []
self._count = 0
# -- helpers ------------------------------------------------------------
def spec(self, opcode: str) -> dict:
spec = self.blocks.get(opcode)
if spec is None:
raise CompileError(f"unknown block '{opcode}'")
return spec
@staticmethod
def _ident(prefix: str, name: str) -> str:
safe = re.sub(r"[^a-z0-9_]", "_", str(name).lower())
if not safe or safe[0].isdigit():
safe = "x" + safe
return prefix + safe
def _arg_def(self, spec: dict, name: str) -> dict | None:
for a in spec["args"]:
if a["name"] == name:
return a
return None
@staticmethod
def _command_options(node: dict) -> list[str]:
"""The option names declared on a command hat, in order and de-duplicated.
Only the literal field is read. A reporter plugged into OPTIONS is ignored — option
names have to be known while compiling, because each one becomes a variable.
"""
raw = str((node.get("fields") or {}).get("OPTIONS", "") or "").strip()
if not raw:
return []
out: list[str] = []
for name in OPTION_SEPARATOR.split(raw):
if name and name not in out:
out.append(name)
return out[:MAX_COMMAND_OPTIONS]
# -- literals -----------------------------------------------------------
def _literal(self, spec: dict, arg: dict, fields: dict) -> str:
raw = fields.get(arg["name"], arg["default"])
raw = "" if raw is None else str(raw)
kind = arg["type"]
if kind == "variable":
self.variables.setdefault(raw, self._ident("v_", raw))
return self.variables[raw]
if kind == "list":
self.lists.setdefault(raw, self._ident("l_", raw))
return self.lists[raw]
if kind == "boolean":
return "false" # empty hexagonal socket
if kind == "dropdown":
# Only values the catalogue offers; never echo an arbitrary string back into
# the template, even though json.dumps would already neutralise it.
options = arg.get("options") or []
if options and raw not in options:
raw = str(arg["default"] or options[0])
return json.dumps(raw)
if kind == "number":
try:
float(raw)
return raw
except ValueError:
return json.dumps(raw)
return json.dumps(raw)
def _compile_arg(self, spec: dict, arg: dict, node: dict, depth: int) -> str:
child = (node.get("inputs") or {}).get(arg["name"])
if child:
return self._expression(child, depth + 1)
return self._literal(spec, arg, node.get("fields") or {})
def _substitute(self, spec: dict, template: str, node: dict, depth: int) -> str:
out = template
for arg in spec["args"]:
if arg["type"] == "substack":
continue
out = out.replace("{%s}" % arg["name"], self._compile_arg(spec, arg, node, depth))
return out
# -- expressions --------------------------------------------------------
def _expression(self, node: dict, depth: int) -> str:
self._guard_limits(depth)
spec = self.spec(node.get("opcode", ""))
js = self._substitute(spec, spec["js"], node, depth)
if js.startswith("await "):
return "(%s)" % js
return js
# -- statements ---------------------------------------------------------
def _guard_limits(self, depth: int) -> None:
self._count += 1
if self._count > MAX_BLOCKS:
raise CompileError(f"project has more than {MAX_BLOCKS} blocks")
if depth > MAX_DEPTH:
raise CompileError(f"blocks nested deeper than {MAX_DEPTH}")
def _stack(self, node: dict | None, depth: int) -> str:
lines: list[str] = []
current = node
seen = 0
while current:
piece = self._statement(current, depth)
if piece:
lines.append(piece)
current = current.get("next")
seen += 1
if seen > MAX_BLOCKS:
raise CompileError("block chain is impossibly long")
return "\n".join(lines)
def _statement(self, node: dict, depth: int) -> str:
self._guard_limits(depth)
spec = self.spec(node.get("opcode", ""))
if not spec["js"].strip():
return ""
pad = INDENT * depth
template = self._substitute(spec, spec["js"], node, depth)
substack_index = 0
for arg in spec["args"]:
if arg["type"] != "substack":
continue
token = "{SUBSTACK}" if substack_index == 0 else "{SUBSTACK%d}" % (substack_index + 1)
inner = self._stack((node.get("substacks") or {}).get(arg["name"]), depth + 1)
template = template.replace(token, inner)
substack_index += 1
if node.get("opcode") == "control_stop":
if str((node.get("fields") or {}).get("WHAT", "this script")) == "all":
template = "process.exit(0);"
block_id = self._safe_id(node.get("id", ""))
out = ['%s__bb.step("%s");' % (pad, block_id)]
for line in template.split("\n"):
if not line.strip():
continue
out.append(line if line.startswith(INDENT * (depth + 1)) else pad + line)
return "\n".join(out)
@staticmethod
def _safe_id(block_id: Any) -> str:
# Block ids are echoed into a string literal for error attribution; keep them to a
# character set that cannot terminate it.
return re.sub(r"[^A-Za-z0-9_.-]", "", str(block_id))[:64]
# -- top level ----------------------------------------------------------
def _collect_data(self, node: dict | None) -> None:
if not node:
return
spec = self.spec(node.get("opcode", ""))
fields = node.get("fields") or {}
for arg in spec["args"]:
value = str(fields.get(arg["name"], arg["default"]) or "")
if arg["type"] == "variable":
self.variables.setdefault(value, self._ident("v_", value))
elif arg["type"] == "list":
self.lists.setdefault(value, self._ident("l_", value))
# A command hat's options are variables too, declared by naming them rather than by
# sitting in a variable slot.
if node.get("opcode") == "event_command":
for option in self._command_options(node):
self.variables.setdefault(option, self._ident("v_", option))
for child in (node.get("inputs") or {}).values():
self._collect_data(child)
for child in (node.get("substacks") or {}).values():
self._collect_data(child)
self._collect_data(node.get("next"))
def _hat(self, node: dict) -> str:
spec = self.spec(node["opcode"])
body = self._stack(node.get("next"), 1)
if spec["event"] == "__broadcast":
name_arg = self._arg_def(spec, "NAME")
name = self._literal(spec, name_arg, node.get("fields") or {}) if name_arg else "''"
lines = ["__bb.onBroadcast(%s, __bb.handler('broadcast', async () => {" % name,
INDENT + "const __ctx = {};"]
if body:
lines.append(body)
lines.append("}));")
return "\n".join(lines)
signature, ctx = EVENT_CONTEXTS.get(spec["event"], ("()", "{}"))
lines = ["client.on(Events.%s, __bb.handler('%s', async %s => {"
% (spec["event"], node["opcode"], signature),
INDENT + "const __ctx = %s;" % ctx]
if spec["event"] == "Ready":
lines.append(INDENT + "__bb.ready(client);")
else:
lines.append(INDENT + "if (__ctx.user && client.user && "
"__ctx.user.id === client.user.id) return;")
if spec["guard"]:
lines.append(INDENT + "if (!(%s)) return;"
% self._substitute(spec, spec["guard"], node, 0))
# Options are read after the guard, so they only overwrite their variables when this
# command is the one that actually ran.
if node["opcode"] == "event_command":
options = self._command_options(node)
if options:
lines.append(INDENT + "const __args = __ctx.message?.command?.args ?? [];")
for index, option in enumerate(options):
ident = self.variables.setdefault(option, self._ident("v_", option))
lines.append(INDENT + "%s = __bb.commandOption(__args, %d, %d);"
% (ident, index, len(options)))
if body:
lines.append(body)
lines.append("}));")
return "\n".join(lines)
def _command_registration(self, scripts: list[dict]) -> str:
entries: list[str] = []
seen: set[str] = set()
for node in scripts:
if node.get("opcode") != "event_command":
continue
if (node.get("inputs") or {}).get("OPTIONS"):
self.warnings.append(
"A command hat uses a block for its options. Option names have to be "
"plain text, because each one becomes a variable — that command will "
"run with no options.")
if (node.get("inputs") or {}).get("NAME"):
self.warnings.append(
"A command hat uses a block for its name, so it can't be registered "
"automatically — type it as plain text to make it appear in Nerimity's "
"command list.")
continue
fields = node.get("fields") or {}
name = str(fields.get("NAME", "")).strip().lstrip("/")
if not name or name in seen:
continue
seen.add(name)
help_text = str(fields.get("HELP", "")).strip() or name
# Nerimity shows `args` beside the command in its picker, so the option names
# double as the usage hint people read before typing.
usage = " ".join(self._command_options(node))
entries.append(" { name: %s, description: %s, args: %s }"
% (json.dumps(name), json.dumps(help_text), json.dumps(usage)))
if not entries:
return ""
return "\n".join([
"client.on(Events.Ready, __bb.handler('register_commands', async () => {",
" await __bb.registerCommands(client, process.env.NERIMITY_TOKEN, [",
",\n".join(entries),
" ]);",
"}));",
])
def compile(self, project: dict, name: str = "bot") -> str:
scripts = project.get("scripts")
if not isinstance(scripts, list):
raise CompileError("project has no scripts array")
self.variables.clear()
self.lists.clear()
self.warnings.clear()
self._count = 0
for node in scripts:
if not isinstance(node, dict):
raise CompileError("a script entry is not an object")
self._collect_data(node)
handlers: list[str] = []
orphans = 0
for node in scripts:
if self.spec(node.get("opcode", ""))["shape"] != "hat":
orphans += 1
continue
handlers.append(self._hat(node))
if orphans:
self.warnings.append(
"%d loose block stack%s no event block on top, so %s never run."
% (orphans, " has" if orphans == 1 else "s have",
"it will" if orphans == 1 else "they will"))
if not handlers:
self.warnings.append(
"This project has no event blocks, so the bot will connect and do nothing.")
out = [
"'use strict';",
"// Generated by the bbuilder host from block structure — do not edit.",
"// project: %s" % re.sub(r"[\r\n]", " ", str(name))[:80],
"",
"const { Client, Events } = require('@nerimity/nerimity.js');",
"const __bb = require('./bb-runtime.js');",
"",
"const client = new Client();",
"",
]
if self.variables or self.lists:
out.append("// --- variables ---")
# Two different names can sanitise to the same identifier ("my var" and "my-var",
# or an option and a variable spelled with different capitals). They then share
# one variable, which is the sane reading — but it must only be declared once.
out += ["let %s = 0;" % v for v in dict.fromkeys(self.variables.values())]
out += ["let %s = [];" % v for v in dict.fromkeys(self.lists.values())]
out.append("")
registration = self._command_registration(scripts)
if registration:
out.append("// --- slash command registration ---")
out.append(registration)
out.append("")
for handler in handlers:
out.append(handler)
out.append("")
out.append("client.login(process.env.NERIMITY_TOKEN);")
out.append("")
return "\n".join(out)
def compile_project(project: dict, name: str = "bot") -> tuple[str, list[str]]:
c = Compiler()
return c.compile(project, name), list(c.warnings) |