Spaces:
Paused

joddabod commited on
Commit
5cf8a54
·
verified ·
1 Parent(s): f29a84a

deploy bbuilder host

Browse files
app.py CHANGED
@@ -9,6 +9,7 @@ is deliberately open — the self-ping uses it, and it leaks nothing but bot nam
9
  from __future__ import annotations
10
 
11
  import asyncio
 
12
  import json
13
  import os
14
  import queue
@@ -83,7 +84,7 @@ Auth = Depends(require_key)
83
  ## Bump when the API contract changes. /health reports it so a deploy can tell whether the
84
  ## new code is actually live — the old process answers /health perfectly well while the new
85
  ## one is still building, which otherwise makes a deploy look successful when it isn't.
86
- API_VERSION = 2
87
 
88
 
89
  @app.get("/health")
@@ -201,6 +202,64 @@ def restart_bot(bot_id: str) -> dict:
201
  return {"ok": True, "message": msg, "bot": sup.get(bot_id).public()}
202
 
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  @app.get("/api/bots/{bot_id}/logs", dependencies=[Auth])
205
  def get_logs(bot_id: str, since: int = 0) -> dict:
206
  bot = sup.get(bot_id)
 
9
  from __future__ import annotations
10
 
11
  import asyncio
12
+ import hmac
13
  import json
14
  import os
15
  import queue
 
84
  ## Bump when the API contract changes. /health reports it so a deploy can tell whether the
85
  ## new code is actually live — the old process answers /health perfectly well while the new
86
  ## one is still building, which otherwise makes a deploy look successful when it isn't.
87
+ API_VERSION = 3
88
 
89
 
90
  @app.get("/health")
 
202
  return {"ok": True, "message": msg, "bot": sup.get(bot_id).public()}
203
 
204
 
205
+ def _bot_id_for_token(token: str) -> str | None:
206
+ """Find the bot whose stored Nerimity token matches.
207
+
208
+ Holding a bot's token is proof of ownership of that bot, so this lets someone manage
209
+ their bot from any machine without knowing the id it was deployed under. Compared with
210
+ compare_digest so the endpoint can't be used as a timing oracle to recover a token.
211
+ """
212
+ token = token.strip()
213
+ if not token:
214
+ return None
215
+ for bot in sup.bots.values():
216
+ secret = bot.dir / "secret.json"
217
+ if not secret.exists():
218
+ continue
219
+ try:
220
+ stored = str(json.loads(secret.read_text()).get("token", ""))
221
+ except (json.JSONDecodeError, OSError):
222
+ continue
223
+ if stored and hmac.compare_digest(stored, token):
224
+ return bot.id
225
+ return None
226
+
227
+
228
+ # Deliberately NOT under /api/bots/… : "/api/bots/by-token/start" would be matched by
229
+ # "/api/bots/{bot_id}/start" with bot_id="by-token", because that route is registered first.
230
+ @app.post("/api/token/{action}", dependencies=[Auth])
231
+ async def by_token(action: str, request: Request) -> dict:
232
+ """Start, stop, restart or delete a bot identified only by its Nerimity token."""
233
+ if action not in ("start", "stop", "restart", "delete"):
234
+ raise HTTPException(400, "unknown action")
235
+
236
+ form = await request.form()
237
+ bot_id = _bot_id_for_token(str(form.get("token") or ""))
238
+ if bot_id is None:
239
+ raise HTTPException(404, "no bot on this host is using that token")
240
+
241
+ if action == "delete":
242
+ sup.delete(bot_id)
243
+ store.delete_bot(bot_id)
244
+ return {"ok": True, "id": bot_id, "message": "deleted"}
245
+
246
+ handler = {"start": sup.start, "stop": sup.stop, "restart": sup.restart}[action]
247
+ ok, msg = handler(bot_id)
248
+ if not ok:
249
+ raise HTTPException(400, msg)
250
+ return {"ok": True, "id": bot_id, "message": msg, "bot": sup.get(bot_id).public()}
251
+
252
+
253
+ @app.post("/api/token", dependencies=[Auth])
254
+ async def resolve_token(request: Request) -> dict:
255
+ """Look up which bot a token belongs to, so the editor can attach to its log stream."""
256
+ form = await request.form()
257
+ bot_id = _bot_id_for_token(str(form.get("token") or ""))
258
+ if bot_id is None:
259
+ raise HTTPException(404, "no bot on this host is using that token")
260
+ return {"ok": True, "id": bot_id, "bot": sup.get(bot_id).public()}
261
+
262
+
263
  @app.get("/api/bots/{bot_id}/logs", dependencies=[Auth])
264
  def get_logs(bot_id: str, since: int = 0) -> dict:
265
  bot = sup.get(bot_id)
catalog.json CHANGED
@@ -1013,7 +1013,7 @@
1013
  "event": "",
1014
  "guard": "",
1015
  "is_expression": false,
1016
- "js": "await __ctx.message?.reply('', { htmlEmbed: __bb.toStr({HTML}) });",
1017
  "shape": "stack",
1018
  "text": "reply with html %1"
1019
  },
@@ -1051,6 +1051,23 @@
1051
  "shape": "stack",
1052
  "text": "send %1"
1053
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1054
  "message_send_to": {
1055
  "args": [
1056
  {
 
1013
  "event": "",
1014
  "guard": "",
1015
  "is_expression": false,
1016
+ "js": "await __ctx.message?.reply('\\u200c', { htmlEmbed: __bb.toStr({HTML}) });",
1017
  "shape": "stack",
1018
  "text": "reply with html %1"
1019
  },
 
1051
  "shape": "stack",
1052
  "text": "send %1"
1053
  },
1054
+ "message_send_html": {
1055
+ "args": [
1056
+ {
1057
+ "default": "<b>hi</b>",
1058
+ "name": "HTML",
1059
+ "options": [],
1060
+ "type": "string"
1061
+ }
1062
+ ],
1063
+ "category": "messages",
1064
+ "event": "",
1065
+ "guard": "",
1066
+ "is_expression": false,
1067
+ "js": "await __ctx.channel?.send('\\u200c', { htmlEmbed: __bb.toStr({HTML}) });",
1068
+ "shape": "stack",
1069
+ "text": "send html %1"
1070
+ },
1071
  "message_send_to": {
1072
  "args": [
1073
  {
fixtures/control_flow.bbproj CHANGED
@@ -5,23 +5,23 @@
5
  "scripts": [
6
  {
7
  "fields": {},
8
- "id": "b792_3",
9
  "next": {
10
  "fields": {
11
  "COND": ""
12
  },
13
- "id": "b792_4",
14
  "inputs": {
15
  "COND": {
16
  "fields": {
17
  "A": "x",
18
  "B": "y"
19
  },
20
- "id": "b792_5",
21
  "inputs": {
22
  "A": {
23
  "fields": {},
24
- "id": "b792_6",
25
  "opcode": "message_text"
26
  }
27
  },
@@ -32,17 +32,17 @@
32
  "fields": {
33
  "TIMES": "3"
34
  },
35
- "id": "b825_9",
36
  "next": {
37
  "fields": {},
38
- "id": "b836_11",
39
  "opcode": "control_forever",
40
  "substacks": {
41
  "SUBSTACK": {
42
  "fields": {
43
  "SECS": "60"
44
  },
45
- "id": "b836_12",
46
  "opcode": "control_wait"
47
  }
48
  }
@@ -53,7 +53,7 @@
53
  "fields": {
54
  "SECS": "0.5"
55
  },
56
- "id": "b825_10",
57
  "opcode": "control_wait"
58
  }
59
  }
@@ -64,14 +64,14 @@
64
  "fields": {
65
  "TEXT": "yes"
66
  },
67
- "id": "b821_7",
68
  "opcode": "message_reply"
69
  },
70
  "SUBSTACK2": {
71
  "fields": {
72
  "TEXT": "no"
73
  },
74
- "id": "b822_8",
75
  "opcode": "message_reply"
76
  }
77
  }
 
5
  "scripts": [
6
  {
7
  "fields": {},
8
+ "id": "b801_3",
9
  "next": {
10
  "fields": {
11
  "COND": ""
12
  },
13
+ "id": "b801_4",
14
  "inputs": {
15
  "COND": {
16
  "fields": {
17
  "A": "x",
18
  "B": "y"
19
  },
20
+ "id": "b801_5",
21
  "inputs": {
22
  "A": {
23
  "fields": {},
24
+ "id": "b801_6",
25
  "opcode": "message_text"
26
  }
27
  },
 
32
  "fields": {
33
  "TIMES": "3"
34
  },
35
+ "id": "b833_9",
36
  "next": {
37
  "fields": {},
38
+ "id": "b844_11",
39
  "opcode": "control_forever",
40
  "substacks": {
41
  "SUBSTACK": {
42
  "fields": {
43
  "SECS": "60"
44
  },
45
+ "id": "b844_12",
46
  "opcode": "control_wait"
47
  }
48
  }
 
53
  "fields": {
54
  "SECS": "0.5"
55
  },
56
+ "id": "b833_10",
57
  "opcode": "control_wait"
58
  }
59
  }
 
64
  "fields": {
65
  "TEXT": "yes"
66
  },
67
+ "id": "b830_7",
68
  "opcode": "message_reply"
69
  },
70
  "SUBSTACK2": {
71
  "fields": {
72
  "TEXT": "no"
73
  },
74
+ "id": "b830_8",
75
  "opcode": "message_reply"
76
  }
77
  }
fixtures/data.bbproj CHANGED
@@ -5,43 +5,43 @@
5
  "scripts": [
6
  {
7
  "fields": {},
8
- "id": "b845_13",
9
  "next": {
10
  "fields": {
11
  "VALUE": "0",
12
  "VARIABLE": "score"
13
  },
14
- "id": "b845_14",
15
  "next": {
16
  "fields": {
17
  "ITEM": "apple",
18
  "LIST": "fruit"
19
  },
20
- "id": "b857_15",
21
  "next": {
22
  "fields": {
23
  "VALUE": "1",
24
  "VARIABLE": "score"
25
  },
26
- "id": "b863_16",
27
  "next": {
28
  "fields": {
29
  "TEXT": "Hello!"
30
  },
31
- "id": "b864_17",
32
  "inputs": {
33
  "TEXT": {
34
  "fields": {
35
  "A": "score is ",
36
  "B": ""
37
  },
38
- "id": "b864_18",
39
  "inputs": {
40
  "B": {
41
  "fields": {
42
  "VARIABLE": "score"
43
  },
44
- "id": "b864_19",
45
  "opcode": "data_variable"
46
  }
47
  },
 
5
  "scripts": [
6
  {
7
  "fields": {},
8
+ "id": "b852_13",
9
  "next": {
10
  "fields": {
11
  "VALUE": "0",
12
  "VARIABLE": "score"
13
  },
14
+ "id": "b852_14",
15
  "next": {
16
  "fields": {
17
  "ITEM": "apple",
18
  "LIST": "fruit"
19
  },
20
+ "id": "b862_15",
21
  "next": {
22
  "fields": {
23
  "VALUE": "1",
24
  "VARIABLE": "score"
25
  },
26
+ "id": "b868_16",
27
  "next": {
28
  "fields": {
29
  "TEXT": "Hello!"
30
  },
31
+ "id": "b869_17",
32
  "inputs": {
33
  "TEXT": {
34
  "fields": {
35
  "A": "score is ",
36
  "B": ""
37
  },
38
+ "id": "b869_18",
39
  "inputs": {
40
  "B": {
41
  "fields": {
42
  "VARIABLE": "score"
43
  },
44
+ "id": "b869_19",
45
  "opcode": "data_variable"
46
  }
47
  },
fixtures/every_block.bbproj CHANGED
The diff for this file is too large to render. See raw diff
 
fixtures/orphan.bbproj CHANGED
@@ -7,7 +7,7 @@
7
  "fields": {
8
  "TEXT": "never runs"
9
  },
10
- "id": "b1511_157",
11
  "opcode": "message_reply",
12
  "x": 40.0,
13
  "y": 40.0
 
7
  "fields": {
8
  "TEXT": "never runs"
9
  },
10
+ "id": "b1543_158",
11
  "opcode": "message_reply",
12
  "x": 40.0,
13
  "y": 40.0
fixtures/ping_pong.bbproj CHANGED
@@ -8,12 +8,12 @@
8
  "HELP": "replies with pong",
9
  "NAME": "ping"
10
  },
11
- "id": "b650_1",
12
  "next": {
13
  "fields": {
14
  "TEXT": "Pong!"
15
  },
16
- "id": "b650_2",
17
  "opcode": "message_reply"
18
  },
19
  "opcode": "event_command",
 
8
  "HELP": "replies with pong",
9
  "NAME": "ping"
10
  },
11
+ "id": "b659_1",
12
  "next": {
13
  "fields": {
14
  "TEXT": "Pong!"
15
  },
16
+ "id": "b659_2",
17
  "opcode": "message_reply"
18
  },
19
  "opcode": "event_command",