levix-bot 4.9.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/README.md +57 -3
  2. package/SETUP.md +63 -7
  3. package/app.cjs +159 -46
  4. package/deploy/install.sh +10 -5
  5. package/package.json +9 -6
  6. package/public/dashboard/assets/40af6d88b63079a0-BZ8ojJNu.woff2 +0 -0
  7. package/public/dashboard/assets/481acf6760031cee-BypgxfGb.woff2 +0 -0
  8. package/public/dashboard/assets/6d3351fc535f8ef3-D2-eand5.woff2 +0 -0
  9. package/public/dashboard/assets/8ab0af53a3bda9d3-CyCXRvzh.woff2 +0 -0
  10. package/public/dashboard/assets/8f262393e9249e61-TQp-UTiE.woff2 +0 -0
  11. package/public/dashboard/assets/94d65887a83f1282-DmpS2jIq.woff2 +0 -0
  12. package/public/dashboard/assets/99e400330f36ae77-D3oMJlXt.woff2 +0 -0
  13. package/public/dashboard/assets/9d1f41b13e519d90-BVNSOH3d.woff2 +0 -0
  14. package/public/dashboard/assets/a005256cb957ecc7-CoYeBiSI.woff2 +0 -0
  15. package/public/dashboard/assets/b19c2080652a3c2b-DIC32ArD.woff2 +0 -0
  16. package/public/dashboard/assets/c4064400db3750ce-BehTv68k.woff2 +0 -0
  17. package/public/dashboard/assets/c69d345fa1068c2d-CmI34b-g.woff2 +0 -0
  18. package/public/dashboard/assets/cc27b30471250057-6fWv1k7M.woff2 +0 -0
  19. package/public/dashboard/assets/d8c30647abfeffe6-eXO_dkmS.woff2 +0 -0
  20. package/public/dashboard/assets/eb83d8d40af4411f-Buze_B52.woff2 +0 -0
  21. package/public/dashboard/assets/effc6711249e7349-qRpaaN48.woff2 +0 -0
  22. package/public/dashboard/assets/gateway-BDYet0pI.css +1 -0
  23. package/public/dashboard/assets/gateway-BYt6-s1O.js +1 -0
  24. package/public/dashboard/assets/index-B9wFpD_z.css +2 -0
  25. package/public/dashboard/assets/index-DWcQmu0_.js +124 -0
  26. package/public/dashboard/assets/index-Da-j89O-.js +458 -0
  27. package/public/dashboard/font-licenses/jetbrainsmono-OFL.txt +93 -0
  28. package/public/dashboard/font-licenses/plusjakartasans-OFL.txt +93 -0
  29. package/public/dashboard/font-licenses/tajawal-OFL.txt +93 -0
  30. package/public/dashboard/gateway.html +17 -0
  31. package/public/dashboard/index.html +3 -5
  32. package/public/ui-boot.js +33 -14
  33. package/scheduler.cjs +76 -23
  34. package/src/auth/auth-storage.cjs +54 -7
  35. package/src/auth/use-database-auth-state.js +70 -39
  36. package/src/automation/engine.js +412 -0
  37. package/src/automation/limits.cjs +6 -0
  38. package/src/automation/manage.js +290 -0
  39. package/src/automation/reactions.js +471 -0
  40. package/src/automation/rules.cjs +114 -0
  41. package/src/automation/status.cjs +76 -0
  42. package/src/backup/account.cjs +29 -0
  43. package/src/backup/activation.cjs +247 -0
  44. package/src/backup/content.cjs +36 -0
  45. package/src/backup/create.cjs +280 -0
  46. package/src/backup/extensions.cjs +101 -0
  47. package/src/backup/format.cjs +565 -0
  48. package/src/backup/jobs.cjs +111 -0
  49. package/src/backup/journal.cjs +41 -0
  50. package/src/backup/plan.cjs +223 -0
  51. package/src/backup/recovery.cjs +110 -0
  52. package/src/backup/restore.cjs +460 -0
  53. package/src/backup/uploads.cjs +146 -0
  54. package/src/bootstrap/core.js +50 -12
  55. package/src/bootstrap/panel.js +53 -2
  56. package/src/bootstrap/session-startup-policy.js +8 -1
  57. package/src/cli.js +163 -22
  58. package/src/commands/autodelete.cjs +41 -57
  59. package/src/commands/autoschedule.cjs +7 -33
  60. package/src/commands/blacklist.cjs +17 -35
  61. package/src/commands/block.cjs +4 -18
  62. package/src/commands/calc.cjs +22 -74
  63. package/src/commands/debt.cjs +28 -59
  64. package/src/commands/deleteschedule.cjs +7 -23
  65. package/src/commands/digit.cjs +5 -39
  66. package/src/commands/gemini.cjs +135 -218
  67. package/src/commands/group/add.cjs +28 -76
  68. package/src/commands/group/all.cjs +4 -15
  69. package/src/commands/group/antilink.cjs +28 -41
  70. package/src/commands/group/antispam.cjs +25 -35
  71. package/src/commands/group/approveall.cjs +7 -21
  72. package/src/commands/group/clearwarns.cjs +4 -18
  73. package/src/commands/group/deletenote.cjs +4 -18
  74. package/src/commands/group/demote.cjs +6 -23
  75. package/src/commands/group/flagged.cjs +10 -19
  76. package/src/commands/group/kick.cjs +26 -36
  77. package/src/commands/group/media.cjs +21 -36
  78. package/src/commands/group/members.cjs +16 -28
  79. package/src/commands/group/note.cjs +3 -14
  80. package/src/commands/group/policy.cjs +16 -37
  81. package/src/commands/group/promote.cjs +5 -22
  82. package/src/commands/group/removeall.cjs +6 -38
  83. package/src/commands/group/rules.cjs +4 -19
  84. package/src/commands/group/save.cjs +4 -18
  85. package/src/commands/group/setname.cjs +5 -19
  86. package/src/commands/group/setpp.cjs +8 -26
  87. package/src/commands/group/setrules.cjs +4 -15
  88. package/src/commands/group/setwarn.cjs +6 -23
  89. package/src/commands/group/tagadmins.cjs +5 -16
  90. package/src/commands/group/warn.cjs +5 -16
  91. package/src/commands/group/warns.cjs +12 -33
  92. package/src/commands/group/welcome.cjs +17 -34
  93. package/src/commands/group.cjs +91 -68
  94. package/src/commands/help.cjs +48 -89
  95. package/src/commands/lang.cjs +38 -97
  96. package/src/commands/listschedules.cjs +19 -30
  97. package/src/commands/loop.cjs +9 -55
  98. package/src/commands/md.cjs +2 -13
  99. package/src/commands/memory.cjs +53 -87
  100. package/src/commands/mod.cjs +40 -79
  101. package/src/commands/notes.cjs +4 -15
  102. package/src/commands/pack.cjs +26 -49
  103. package/src/commands/packs.cjs +8 -18
  104. package/src/commands/perm.cjs +48 -51
  105. package/src/commands/ping.cjs +2 -10
  106. package/src/commands/poll.cjs +5 -38
  107. package/src/commands/prayer.cjs +55 -36
  108. package/src/commands/qr.cjs +4 -18
  109. package/src/commands/rand.cjs +9 -58
  110. package/src/commands/restart.cjs +15 -33
  111. package/src/commands/schedule.cjs +5 -25
  112. package/src/commands/score.cjs +6 -27
  113. package/src/commands/send.cjs +4 -18
  114. package/src/commands/setprefix.cjs +11 -23
  115. package/src/commands/shortlink.cjs +13 -39
  116. package/src/commands/status.cjs +188 -76
  117. package/src/commands/sticker.cjs +5 -16
  118. package/src/commands/stickers.cjs +16 -20
  119. package/src/commands/stopbot.cjs +9 -25
  120. package/src/commands/stt.cjs +75 -107
  121. package/src/commands/todo.cjs +11 -31
  122. package/src/commands/toimage.cjs +11 -27
  123. package/src/commands/translate.cjs +53 -36
  124. package/src/commands/tts.cjs +46 -40
  125. package/src/commands/unblock.cjs +3 -17
  126. package/src/commands/update.cjs +23 -0
  127. package/src/commands/weather.cjs +40 -47
  128. package/src/config/ai-identity.cjs +14 -1
  129. package/src/config/baileys.config.js +16 -3
  130. package/src/config/data-dir.cjs +37 -0
  131. package/src/config/defaults.cjs +1 -0
  132. package/src/config/exit-codes.cjs +11 -0
  133. package/src/config/lock.cjs +55 -81
  134. package/src/config/official-links.cjs +61 -0
  135. package/src/config/paths.cjs +4 -33
  136. package/src/config/release.cjs +87 -0
  137. package/src/config/settings.cjs +199 -10
  138. package/src/core/connection.js +77 -9
  139. package/src/core/events.js +119 -33
  140. package/src/core/session.js +536 -84
  141. package/src/core/socket.js +81 -10
  142. package/src/db/copy.cjs +136 -0
  143. package/src/db/db.cjs +241 -36
  144. package/src/db/startup-guard.js +5 -0
  145. package/src/db/store.cjs +561 -54
  146. package/src/diagnostics/baileysLog.cjs +87 -0
  147. package/src/diagnostics/debugLog.cjs +16 -4
  148. package/src/diagnostics/device.cjs +4 -2
  149. package/src/diagnostics/nodeRuntime.cjs +131 -0
  150. package/src/diagnostics/signalSessionInspect.cjs +38 -0
  151. package/src/domain/command.js +24 -22
  152. package/src/extensions/autoReply.cjs +521 -0
  153. package/src/extensions/context.cjs +73 -0
  154. package/src/extensions/contract.cjs +69 -0
  155. package/src/extensions/entitlement/client.cjs +381 -0
  156. package/src/extensions/entitlement/index.cjs +629 -0
  157. package/src/extensions/entitlement/installation.cjs +117 -0
  158. package/src/extensions/entitlement/keys.cjs +72 -0
  159. package/src/extensions/entitlement/state.cjs +201 -0
  160. package/src/extensions/entitlement/target.cjs +7 -0
  161. package/src/extensions/entitlement/token.cjs +225 -0
  162. package/src/extensions/host.cjs +519 -0
  163. package/src/extensions/templateKeys.cjs +81 -0
  164. package/src/handlers/command.handler.js +191 -64
  165. package/src/handlers/group.handler.js +78 -18
  166. package/src/handlers/message.handler.js +56 -25
  167. package/src/i18n/catalog.generated.cjs +3537 -0
  168. package/src/index.js +51 -12
  169. package/src/legal/bundled/ar.json +102 -0
  170. package/src/legal/bundled/en.json +102 -0
  171. package/src/legal/bundled/es-419.json +102 -0
  172. package/src/legal/bundled/pt-BR.json +102 -0
  173. package/src/legal/cli.cjs +200 -0
  174. package/src/legal/consent.cjs +187 -0
  175. package/src/legal/document.cjs +156 -0
  176. package/src/legal/keys.cjs +100 -0
  177. package/src/legal/release.cjs +226 -0
  178. package/src/legal/remote.cjs +419 -0
  179. package/src/legal/terms.cjs +142 -0
  180. package/src/messaging/escape.cjs +31 -0
  181. package/src/messaging/health.cjs +65 -0
  182. package/src/messaging/identity.cjs +42 -0
  183. package/src/messaging/message.cjs +136 -0
  184. package/src/messaging/parser.cjs +88 -0
  185. package/src/messaging/router.cjs +217 -0
  186. package/src/middleware/permissions.middleware.js +34 -2
  187. package/src/panel/account-profile.cjs +489 -0
  188. package/src/panel/fallback-page.cjs +177 -0
  189. package/src/panel/password-reset.cjs +13 -14
  190. package/src/panel/recovery-email.cjs +3 -1
  191. package/src/routes/backup.api.esm.js +175 -0
  192. package/src/routes/dashboard.api.esm.js +183 -7
  193. package/src/routes/plus.api.esm.js +172 -0
  194. package/src/routes/reactions.api.esm.js +60 -0
  195. package/src/services/aiAgent.cjs +56 -43
  196. package/src/services/aiDocs.cjs +187 -0
  197. package/src/services/aiImage.cjs +183 -0
  198. package/src/services/aiMedia.cjs +146 -0
  199. package/src/services/aiProviders.cjs +76 -80
  200. package/src/services/aiRouter.cjs +2 -2
  201. package/src/services/aiToolAuth.cjs +76 -4
  202. package/src/services/aiTools.cjs +431 -105
  203. package/src/services/autoDelete.cjs +9 -1
  204. package/src/services/levix-docs.generated.json +2623 -0
  205. package/src/services/memoryPolicy.cjs +56 -0
  206. package/src/services/updateCheck.cjs +362 -0
  207. package/src/stickers/errors.cjs +53 -97
  208. package/src/stickers/library.cjs +13 -6
  209. package/src/stickers/owner.cjs +1 -1
  210. package/src/utils/commandContext.cjs +13 -0
  211. package/src/utils/commandDocs.cjs +42 -20
  212. package/src/utils/geminiHttpOptions.cjs +56 -0
  213. package/src/utils/geminiMedia.cjs +28 -3
  214. package/src/utils/groupMetadataCache.cjs +386 -21
  215. package/src/utils/httpsFetch.cjs +113 -0
  216. package/src/utils/i18n.cjs +117 -7
  217. package/src/utils/linkPreview.cjs +90 -0
  218. package/src/utils/logger.cjs +6 -0
  219. package/src/utils/markdownParser.cjs +3 -3
  220. package/src/utils/memory.cjs +375 -28
  221. package/src/utils/messageContent.cjs +7 -1
  222. package/src/utils/moderationAction.cjs +116 -73
  223. package/src/utils/moderationSummary.cjs +32 -56
  224. package/src/utils/permissionLevel.cjs +7 -28
  225. package/src/utils/permissions.cjs +23 -14
  226. package/src/utils/permissions.esm.js +99 -166
  227. package/src/utils/recentMessageCache.esm.js +241 -37
  228. package/src/utils/recurrence.cjs +78 -66
  229. package/src/utils/sendBotMessage.cjs +14 -29
  230. package/src/utils/sendBotMessage.esm.js +28 -13
  231. package/src/utils/statusMessage.cjs +94 -33
  232. package/src/utils/stickerBot.cjs +18 -16
  233. package/src/utils/storage.cjs +2 -0
  234. package/src/utils/storage.esm.js +2 -0
  235. package/src/utils/thumbnail.cjs +5 -21
  236. package/public/dashboard/assets/index-B1sOW2VX.js +0 -561
  237. package/public/dashboard/assets/index-CK8osB4o.css +0 -2
  238. package/public/dashboard.css +0 -3127
  239. package/public/dashboard.js +0 -1981
  240. package/public/gateway-i18n.js +0 -321
  241. package/public/gateway.js +0 -70
  242. package/views/dashboard.ejs +0 -775
  243. package/views/login.ejs +0 -408
  244. package/views/qr.ejs +0 -155
  245. package/views/setup.ejs +0 -118
@@ -1,16 +1,48 @@
1
1
  import { makeWASocket } from "@whiskeysockets/baileys";
2
2
  import { createRequire } from "module";
3
+ import { invalidateReactionMessage } from "../automation/reactions.js";
3
4
  import { useDatabaseAuthState } from "../auth/use-database-auth-state.js";
4
5
  import { getBaileysConfig, setRecentMessageGetter } from "../config/baileys.config.js";
5
- import { getMessageFromRecent } from "../utils/recentMessageCache.esm.js";
6
+ import {
7
+ getMessageFromRecent,
8
+ getRecentCacheEpoch,
9
+ rememberMessage,
10
+ } from "../utils/recentMessageCache.esm.js";
6
11
 
7
12
  const require = createRequire(import.meta.url);
8
13
  const logger = require("../utils/logger.cjs");
9
14
  const { withMediaThumbnail } = require("../utils/thumbnail.cjs");
10
15
  const settings = require("../config/settings.cjs");
11
16
  const { isLinkedCreds } = require("../utils/linkedCreds.cjs");
17
+ const { fetchHttpsBuffer } = require("../utils/httpsFetch.cjs");
18
+ const { getHttpsUrlInfo } = require("../utils/linkPreview.cjs");
19
+ const { t } = require("../utils/i18n.cjs");
12
20
  const socketDispatchers = new WeakMap();
13
21
 
22
+ // Never leave a remote media URL for Baileys to fetch again without validation.
23
+ // Local paths, buffers and streams keep their existing send path. Download once
24
+ // for the actual send, then build any thumbnail from those same bytes.
25
+ async function withHttpsMedia(sock, content) {
26
+ if (!content || typeof content !== "object") return content;
27
+ let payload = content;
28
+ for (const field of ["image", "video", "audio", "document", "sticker"]) {
29
+ const media = content[field];
30
+ const raw = typeof media === "string" ? media : media?.url;
31
+ if (!raw || !/^https?:/i.test(String(raw).trim())) continue;
32
+ try {
33
+ const { buffer } = await fetchHttpsBuffer(String(raw), {
34
+ maxBytes: settings.get("thumbnail_remote_max_mb") * 1024 * 1024,
35
+ timeoutMs: settings.get("thumbnail_remote_timeout_ms"),
36
+ dispatcher: socketDispatchers.get(sock),
37
+ });
38
+ payload = { ...payload, [field]: buffer };
39
+ } catch (cause) {
40
+ throw new Error(t("bot.core.socket.remoteMediaFailed"), { cause });
41
+ }
42
+ }
43
+ return payload;
44
+ }
45
+
14
46
  /** Fetch a WhatsApp image through the transport owned by this live socket. */
15
47
  export async function fetchWhatsAppImage(sock, rawUrl, init = {}) {
16
48
  const url = new URL(rawUrl);
@@ -49,7 +81,8 @@ setRecentMessageGetter(getMessageFromRecent);
49
81
  * previews on it even uploads the page image. That made every AI answer with a
50
82
  * Sources block, every short link and every scheduled message with a URL cost
51
83
  * a silent page fetch. `linkPreview: null` skips it entirely; the operator
52
- * opts back in with the `link_previews_enabled` setting (default off).
84
+ * opts back in with the `link_previews_enabled` setting (default off). The
85
+ * per-send getUrlInfo hook below keeps that optional fetch HTTPS-only.
53
86
  *
54
87
  * This is the ONE choke point every outbound message passes through
55
88
  * (sendBotMessage, the scheduler, status edits, legacy sock.sendMessage), so
@@ -71,19 +104,53 @@ function withoutUnwantedLinkPreview(content) {
71
104
  * sendBotMessage and the handful of legacy commands still calling
72
105
  * sock.sendMessage directly.
73
106
  */
74
- function withOutboundSavers(sock) {
107
+ export function withOutboundSavers(sock) {
75
108
  const original = sock.sendMessage.bind(sock);
109
+ const getUrlInfo = (text) => getHttpsUrlInfo(text, { dispatcher: socketDispatchers.get(sock) });
76
110
 
77
111
  sock.sendMessage = async function sendMessageWithOutboundSavers(jid, content, options) {
78
- let payload = content;
112
+ const sendEpoch = getRecentCacheEpoch();
113
+ if (content?.delete) invalidateReactionMessage(sock, { ...content.delete, remoteJid: jid });
114
+ const signal = options?.signal;
115
+ signal?.throwIfAborted();
116
+ let payload = await withHttpsMedia(sock, content);
117
+ signal?.throwIfAborted();
79
118
  try {
80
- payload = await withMediaThumbnail(content);
119
+ payload = await withMediaThumbnail(payload);
81
120
  } catch (err) {
82
121
  logger.debug({ err: err?.message }, "[Socket] thumbnail step skipped");
83
- payload = content;
84
122
  }
85
123
  payload = withoutUnwantedLinkPreview(payload);
86
- return original(jid, payload, options);
124
+ // Put the hook last so callers cannot accidentally restore Baileys's unsafe
125
+ // downloader. Explicit linkPreview objects/null are still handled by Baileys.
126
+ // `levix` is a routing hint for the command proxy. It is not a Baileys field.
127
+ const wireOptions = { ...(options || {}), getUrlInfo };
128
+ delete wireOptions.levix;
129
+ delete wireOptions.signal;
130
+ delete wireOptions.deliveryGuard;
131
+ // Automation refreshes remote membership after preparation, then checks
132
+ // local policy synchronously. Nothing can interleave with the final send.
133
+ if (options?.deliveryGuard) {
134
+ await options.deliveryGuard.refresh();
135
+ options.deliveryGuard.assert();
136
+ }
137
+ // No await between this check and Baileys: cancellation cannot retract a
138
+ // send already initiated, but must prevent a new one after preparation.
139
+ signal?.throwIfAborted();
140
+ const result = await original(jid, payload, wireOptions);
141
+ try {
142
+ if (
143
+ getRecentCacheEpoch() === sendEpoch &&
144
+ result?.key?.id &&
145
+ result?.key?.remoteJid &&
146
+ result?.message
147
+ ) {
148
+ rememberMessage(result.key, result.message, sendEpoch);
149
+ }
150
+ } catch (err) {
151
+ logger.debug({ err: err?.message }, "[Socket] recent message cache step skipped");
152
+ }
153
+ return result;
87
154
  };
88
155
 
89
156
  return sock;
@@ -105,14 +172,17 @@ export async function createWhatsAppSocket({ proxy = null, pairingCode = false }
105
172
  : "[Socket] Initializing WhatsApp socket with database auth",
106
173
  );
107
174
 
108
- const { state, saveCreds, clearAll } = await useDatabaseAuthState();
175
+ const { state, saveCreds, clearAll, fence = () => {} } = await useDatabaseAuthState();
109
176
 
110
177
  // Whether this install is already paired. NOT `store.hasCredentials()`: the
111
178
  // auth state writes a `creds` row on its very first call, paired or not. See
112
179
  // linkedCreds.cjs for what counts as a finished link.
113
180
  const isPaired = isLinkedCreds(state?.creds);
114
181
 
115
- const cachedGroupMetadata = (jid) => groupMetadataCache.get(jid);
182
+ // The hook is built before makeWASocket returns, so the socket it fetches
183
+ // with is filled in immediately after. A stale roster is never returned.
184
+ let liveSock = null;
185
+ const cachedGroupMetadata = (jid) => groupMetadataCacheModule.groupMetadataForSend(liveSock, jid);
116
186
 
117
187
  const config = getBaileysConfig(cachedGroupMetadata, { pairingCode });
118
188
 
@@ -134,7 +204,8 @@ export async function createWhatsAppSocket({ proxy = null, pairingCode = false }
134
204
  ...config,
135
205
  }),
136
206
  );
207
+ liveSock = sock;
137
208
  socketDispatchers.set(sock, config.options?.dispatcher ?? null);
138
209
 
139
- return { sock, saveCreds, clearAll, isPaired };
210
+ return { sock, saveCreds, clearAll, fence, isPaired };
140
211
  }
@@ -0,0 +1,136 @@
1
+ // Copy a database without ever writing authentication material to the target.
2
+ const fs = require("node:fs");
3
+ const { DatabaseSync } = require("node:sqlite");
4
+ const { SECRET_SETTING_KEYS } = require("../config/settings.cjs");
5
+
6
+ const SECRET_TABLES = Object.freeze(["baileys_auth", "qr_codes"]);
7
+ const secretSettings = new Set(SECRET_SETTING_KEYS);
8
+ const quote = (name) => `"${name.replaceAll('"', '""')}"`;
9
+
10
+ // `allowKey` lets a caller with its own allowlist (the backup) carry named
11
+ // extension account rows. It never lifts auth:, secret:, plus: or a secret setting.
12
+ function isSecretBotKey(key, allowKey = () => false) {
13
+ if (typeof key !== "string") return true;
14
+ if (key.startsWith("auth:") || key.startsWith("secret:") || key.startsWith("plus:")) return true;
15
+ // Account data for an extension (`ext:<id>:acct:…`) is not an operator setting.
16
+ if (/^ext:[^:]+:acct:/.test(key)) return !allowKey(key);
17
+ return key.startsWith("setting:") && secretSettings.has(key.slice("setting:".length));
18
+ }
19
+
20
+ function tables(database, schema) {
21
+ return database.prepare(`SELECT name, sql FROM ${schema}.sqlite_master
22
+ WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY rowid`).all();
23
+ }
24
+
25
+ function writeSecretFreeCopy(sourcePath, targetPath, {
26
+ omitTables = [], keepBotKey = () => true, allowBotKey, onAfterRows,
27
+ } = {}) {
28
+ const omitted = new Set([...SECRET_TABLES, ...omitTables]);
29
+ let target;
30
+ let created = false;
31
+ try {
32
+ const handle = fs.openSync(targetPath, "wx", 0o600);
33
+ created = true;
34
+ fs.closeSync(handle);
35
+ target = new DatabaseSync(targetPath);
36
+ target.exec("PRAGMA temp_store = MEMORY");
37
+ target.exec("PRAGMA busy_timeout = 5000");
38
+ target.prepare("ATTACH DATABASE ? AS src").run(sourcePath);
39
+ // The first source read inside this transaction fixes one SQLite snapshot.
40
+ target.exec("BEGIN");
41
+ try {
42
+ const sourceTables = tables(target, "src");
43
+ for (const table of sourceTables) target.exec(table.sql);
44
+ for (const table of sourceTables) {
45
+ if (omitted.has(table.name)) continue;
46
+ const name = quote(table.name);
47
+ if (table.name === "bot_settings") {
48
+ const insert = target.prepare(`INSERT INTO main.${name} SELECT * FROM src.${name} WHERE key = ?`);
49
+ for (const { key } of target.prepare(`SELECT key FROM src.${name}`).all()) {
50
+ if (!isSecretBotKey(key, allowBotKey) && keepBotKey(key)) insert.run(key);
51
+ }
52
+ } else {
53
+ target.exec(`INSERT INTO main.${name} SELECT * FROM src.${name}`);
54
+ }
55
+ }
56
+ if (target.prepare("SELECT 1 FROM src.sqlite_master WHERE name = 'sqlite_sequence'").get()) {
57
+ const copied = sourceTables.filter(({ name }) => !omitted.has(name)).map(({ name }) => name);
58
+ const insert = target.prepare("INSERT INTO main.sqlite_sequence (name, seq) SELECT name, seq FROM src.sqlite_sequence WHERE name = ?");
59
+ // INSERTs may already have advanced the target sequence.
60
+ target.exec("DELETE FROM main.sqlite_sequence");
61
+ for (const name of copied) insert.run(name);
62
+ }
63
+ onAfterRows?.();
64
+ const objects = target.prepare(`SELECT sql FROM src.sqlite_master
65
+ WHERE type IN ('index', 'trigger', 'view') AND sql IS NOT NULL ORDER BY rowid`).all();
66
+ for (const { sql } of objects) target.exec(sql);
67
+ const version = target.prepare("PRAGMA src.user_version").get().user_version;
68
+ target.exec(`PRAGMA user_version = ${version}`);
69
+ target.exec("COMMIT");
70
+ } catch (error) {
71
+ try { target.exec("ROLLBACK"); } catch {}
72
+ throw error;
73
+ }
74
+ target.exec("DETACH DATABASE src");
75
+ target.close();
76
+ target = null;
77
+ const file = fs.openSync(targetPath, "r");
78
+ try { fs.fsyncSync(file); } finally { fs.closeSync(file); }
79
+ } catch (error) {
80
+ try { target?.close(); } catch {}
81
+ if (created) {
82
+ for (const suffix of ["", "-journal", "-wal", "-shm"]) {
83
+ try { fs.rmSync(targetPath + suffix, { force: true }); } catch {}
84
+ }
85
+ }
86
+ throw error;
87
+ }
88
+ }
89
+
90
+ function verifySecretFreeCopy(targetPath, sourceDatabase, {
91
+ expectedVersion, omitTables = [], keepBotKey = () => true, allowBotKey,
92
+ } = {}) {
93
+ const omitted = new Set([...SECRET_TABLES, ...omitTables]);
94
+ const copy = new DatabaseSync(targetPath, { readOnly: true });
95
+ try {
96
+ if (copy.prepare("PRAGMA integrity_check").get().integrity_check !== "ok") {
97
+ throw new Error("copy integrity check failed");
98
+ }
99
+ if (copy.prepare("PRAGMA user_version").get().user_version !== expectedVersion) {
100
+ throw new Error("copy schema version differs from source");
101
+ }
102
+ const sourceTables = tables(sourceDatabase, "main").map(({ name }) => name);
103
+ const copiedTables = tables(copy, "main").map(({ name }) => name);
104
+ if (JSON.stringify(sourceTables) !== JSON.stringify(copiedTables)) {
105
+ throw new Error("copy table schema differs from source");
106
+ }
107
+ for (const name of sourceTables) {
108
+ const identifier = quote(name);
109
+ const actual = copy.prepare(`SELECT count(*) AS n FROM ${identifier}`).get().n;
110
+ let expected = 0;
111
+ if (!omitted.has(name)) {
112
+ if (name === "bot_settings") {
113
+ expected = sourceDatabase.prepare(`SELECT key FROM ${identifier}`).all()
114
+ .filter(({ key }) => !isSecretBotKey(key, allowBotKey) && keepBotKey(key)).length;
115
+ } else {
116
+ expected = sourceDatabase.prepare(`SELECT count(*) AS n FROM ${identifier}`).get().n;
117
+ }
118
+ }
119
+ if (actual !== expected) throw new Error(`copy row count differs for ${name}`);
120
+ }
121
+ if (sourceTables.includes("bot_settings")) {
122
+ const copiedKeys = copy.prepare("SELECT key FROM bot_settings").all().map(({ key }) => key);
123
+ if (copiedKeys.some((key) => isSecretBotKey(key, allowBotKey))) {
124
+ throw new Error("copy contains a secret bot setting");
125
+ }
126
+ // A row count alone would accept an excluded key standing in for a missing one.
127
+ if (copiedKeys.some((key) => !keepBotKey(key))) {
128
+ throw new Error("copy contains an excluded bot setting");
129
+ }
130
+ }
131
+ } finally {
132
+ copy.close();
133
+ }
134
+ }
135
+
136
+ module.exports = { SECRET_TABLES, isSecretBotKey, writeSecretFreeCopy, verifySecretFreeCopy };
package/src/db/db.cjs CHANGED
@@ -13,12 +13,14 @@
13
13
  // prefix lookup, every permission check) read inline with no `await`.
14
14
  //
15
15
  // The file lives in the data directory (src/config/paths.cjs), together with
16
- // everything else the bot writes. The schema is created on open and versioned
16
+ // everything else the bot writes. The schema is created on first use and versioned
17
17
  // with `PRAGMA user_version`, so upgrading never asks the operator to run a
18
18
  // migration by hand.
19
19
 
20
20
  const path = require("node:path");
21
+ const fs = require("node:fs");
21
22
  const { DatabaseSync } = require("node:sqlite");
23
+ const { writeSecretFreeCopy, verifySecretFreeCopy } = require("./copy.cjs");
22
24
 
23
25
  const logger = require("../utils/logger.cjs");
24
26
  const { DATA_DIR, dataPath } = require("../config/paths.cjs");
@@ -29,17 +31,55 @@ const DB_PATH = path.join(DATA_DIR, "levix.db");
29
31
  // --- Open ---
30
32
  // ===================================================================
31
33
 
32
- const db = new DatabaseSync(DB_PATH);
34
+ let connection = null;
35
+ let startupGuard = false;
36
+ let prepared = false;
37
+ let configured = false;
33
38
 
34
- // WAL: a reader never blocks the writer. The bot is a single process, but the
35
- // scheduler and the HTTP handlers do interleave.
36
- db.exec("PRAGMA journal_mode = WAL");
37
- // NORMAL is the WAL-appropriate setting: a crash can lose the last commit or
38
- // two, never the file. FULL would fsync on every warning counter.
39
- db.exec("PRAGMA synchronous = NORMAL");
40
- db.exec("PRAGMA foreign_keys = ON");
41
- // Wait instead of throwing SQLITE_BUSY if the checkpointer holds the lock.
42
- db.exec("PRAGMA busy_timeout = 5000");
39
+ function guardStartup() {
40
+ startupGuard = true;
41
+ }
42
+
43
+ function openRaw() {
44
+ if (!connection) connection = new DatabaseSync(DB_PATH);
45
+ return connection;
46
+ }
47
+
48
+ function configure(database) {
49
+ // Keep this after the newer-schema check: refusing a downgrade must not
50
+ // change even the database header or create a WAL file.
51
+ database.exec("PRAGMA journal_mode = WAL");
52
+ database.exec("PRAGMA synchronous = NORMAL");
53
+ database.exec("PRAGMA foreign_keys = ON");
54
+ database.exec("PRAGMA busy_timeout = 5000");
55
+ configured = true;
56
+ }
57
+
58
+ function activeDatabase() {
59
+ if (startupGuard && !prepared) {
60
+ const error = new Error("database used before startup prepared it");
61
+ error.code = "DATABASE_NOT_PREPARED";
62
+ throw error;
63
+ }
64
+ if (!configured) {
65
+ const database = openRaw();
66
+ const from = database.prepare("PRAGMA user_version").get().user_version;
67
+ if (from > MIGRATIONS.length) throw newerSchemaError(from);
68
+ configure(database);
69
+ migrate(database);
70
+ }
71
+ return connection;
72
+ }
73
+
74
+ // A stable facade lets old callers keep `const { db } = require(...)` without
75
+ // opening the file when the store itself is imported.
76
+ const db = new Proxy({}, {
77
+ get(_target, key) {
78
+ const database = activeDatabase();
79
+ const value = database[key];
80
+ return typeof value === "function" ? value.bind(database) : value;
81
+ },
82
+ });
43
83
 
44
84
  // ===================================================================
45
85
  // --- Schema ---
@@ -672,39 +712,184 @@ const MIGRATIONS = [
672
712
  UPDATE moderation_flags SET last_notified_at = flagged_at WHERE last_notified_at IS NULL;
673
713
  `);
674
714
  },
715
+ // v11 — Free reaction automation. Original messages are NEVER stored here.
716
+ (database) => {
717
+ database.exec(`
718
+ CREATE TABLE reaction_rules (
719
+ id TEXT PRIMARY KEY,
720
+ chat_jid TEXT NOT NULL,
721
+ enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1)),
722
+ definition TEXT NOT NULL DEFAULT '{}'
723
+ );
724
+ CREATE INDEX idx_reaction_rules_chat ON reaction_rules (chat_jid, enabled);
725
+ CREATE TABLE reaction_events (
726
+ event_key TEXT PRIMARY KEY,
727
+ expires_at INTEGER NOT NULL
728
+ );
729
+ CREATE INDEX idx_reaction_events_expiry ON reaction_events (expires_at);
730
+ CREATE TABLE reaction_state (
731
+ slot_key TEXT PRIMARY KEY,
732
+ timestamp_ms INTEGER NOT NULL,
733
+ emoji TEXT NOT NULL,
734
+ expires_at INTEGER NOT NULL
735
+ );
736
+ CREATE INDEX idx_reaction_state_expiry ON reaction_state (expires_at);
737
+ CREATE TABLE reaction_invalidations (
738
+ invalidation_key TEXT PRIMARY KEY,
739
+ expires_at INTEGER NOT NULL
740
+ );
741
+ CREATE INDEX idx_reaction_invalidations_expiry ON reaction_invalidations (expires_at);
742
+ `);
743
+ },
744
+ // v12 — enablement epochs and claim-first execution. v11 is immutable.
745
+ (database) => {
746
+ database.exec(`
747
+ ALTER TABLE reaction_rules ADD COLUMN revision TEXT NOT NULL DEFAULT '';
748
+ ALTER TABLE reaction_rules ADD COLUMN enabled_at INTEGER;
749
+ ALTER TABLE reaction_rules ADD COLUMN priority INTEGER NOT NULL DEFAULT 0;
750
+ UPDATE reaction_rules SET enabled = 0;
751
+ CREATE TABLE reaction_executions (
752
+ event_key TEXT PRIMARY KEY,
753
+ rule_key TEXT NOT NULL UNIQUE,
754
+ action_key TEXT NOT NULL UNIQUE,
755
+ expires_at INTEGER NOT NULL
756
+ );
757
+ CREATE INDEX idx_reaction_executions_expiry ON reaction_executions(expires_at);
758
+ CREATE TABLE reaction_budgets (
759
+ budget_key TEXT PRIMARY KEY,
760
+ attempts INTEGER NOT NULL,
761
+ expires_at INTEGER NOT NULL
762
+ );
763
+ CREATE INDEX idx_reaction_budgets_expiry ON reaction_budgets(expires_at);
764
+ `);
765
+ },
675
766
  ];
676
767
 
677
768
  function migrate(database, migrations = MIGRATIONS) {
678
769
  const { user_version: current } = database.prepare("PRAGMA user_version").get();
679
-
770
+ if (current > migrations.length) throw newerSchemaError(current, migrations.length);
680
771
  if (current >= migrations.length) return;
772
+ // One transaction covers every pending step and the final version stamp.
773
+ // A crash or a failed later step leaves the entire old schema intact.
774
+ database.exec("BEGIN IMMEDIATE");
775
+ try {
776
+ for (let version = current; version < migrations.length; version += 1) {
777
+ logger.info(`[DB] Running schema step v${version + 1}`);
778
+ migrations[version](database);
779
+ }
780
+ database.exec(`PRAGMA user_version = ${migrations.length}`);
781
+ database.exec("COMMIT");
782
+ } catch (err) {
783
+ try { database.exec("ROLLBACK"); } catch {}
784
+ logger.error({ err }, "[DB] Migration rolled back");
785
+ throw err;
786
+ }
787
+ }
788
+
789
+ function newerSchemaError(from, known = MIGRATIONS.length) {
790
+ const error = new Error(
791
+ `This data directory was written by a newer Levix (schema ${from}; this build knows ${known}). Install that version or newer.`,
792
+ );
793
+ error.code = "SCHEMA_NEWER";
794
+ return error;
795
+ }
796
+
797
+ function schemaVersion(database) {
798
+ return database.prepare("PRAGMA user_version").get().user_version;
799
+ }
681
800
 
682
- for (let version = current; version < migrations.length; version += 1) {
683
- // One transaction per migration, covering the version stamp as well.
684
- // `user_version` lives in the database header and is written inside the
685
- // transaction like anything else, so a crash or a power cut halfway
686
- // through rolls the whole step back: the next start sees the old version
687
- // and runs it again from a known state. Without this a half-applied
688
- // migration would be recorded as complete.
689
- database.exec("BEGIN IMMEDIATE");
801
+ function fsyncDirectory(folder, filesystem = fs) {
802
+ try {
803
+ const directory = filesystem.openSync(folder, "r");
690
804
  try {
691
- migrations[version](database);
692
- // PRAGMA takes no bound parameter; the value is a loop counter, not
693
- // anything a user can reach.
694
- database.exec(`PRAGMA user_version = ${version + 1}`);
695
- database.exec("COMMIT");
696
- } catch (err) {
697
- try {
698
- database.exec("ROLLBACK");
699
- } catch {}
700
- logger.error({ err }, `[DB] Migration to v${version + 1} failed and was rolled back`);
701
- throw err;
805
+ filesystem.fsyncSync(directory);
806
+ } finally {
807
+ filesystem.closeSync(directory);
808
+ }
809
+ } catch (error) {
810
+ // Windows does not support opening or syncing a directory handle.
811
+ if (!["EISDIR", "EPERM", "EINVAL", "ENOTSUP", "EBADF"].includes(error.code)) throw error;
812
+ }
813
+ }
814
+
815
+ let copyHookForTest = null;
816
+ function setCopyHookForTest(fn) {
817
+ copyHookForTest = fn;
818
+ }
819
+
820
+ async function makePreMigrationCopy(database, from) {
821
+ const folder = dataPath("backups", "pre-migration");
822
+ let copyPath;
823
+ let partPath;
824
+ let renamed = false;
825
+ try {
826
+ fs.mkdirSync(folder, { recursive: true, mode: 0o700 });
827
+ fs.chmodSync(folder, 0o700);
828
+ for (const name of fs.readdirSync(folder)) {
829
+ if (/^levix-v\d+-\d{8}T\d{6}Z\.db\.part(?:-journal|-wal|-shm)?$/.test(name)) {
830
+ fs.rmSync(path.join(folder, name));
831
+ }
702
832
  }
703
- logger.info(`[DB] Migrated to schema v${version + 1}`);
833
+ const names = fs.readdirSync(folder).filter((name) => /^levix-v\d+-\d{8}T\d{6}Z\.db$/.test(name));
834
+ let instant = Date.now();
835
+ const latest = names.map((name) => name.match(/-(\d{8}T\d{6}Z)\.db$/)[1]).sort().at(-1);
836
+ if (latest) {
837
+ const parsed = Date.parse(`${latest.slice(0, 4)}-${latest.slice(4, 6)}-${latest.slice(6, 8)}T${latest.slice(9, 11)}:${latest.slice(11, 13)}:${latest.slice(13, 15)}Z`);
838
+ instant = Math.max(instant, parsed + 1000);
839
+ }
840
+ const stamp = new Date(instant).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "Z");
841
+ copyPath = path.join(folder, `levix-v${from}-${stamp}.db`);
842
+ partPath = `${copyPath}.part`;
843
+ const keepBotKey = (key) => key !== "legal:acceptance";
844
+ writeSecretFreeCopy(DB_PATH, partPath, {
845
+ keepBotKey,
846
+ onAfterRows: () => copyHookForTest?.("after-rows"),
847
+ });
848
+ verifySecretFreeCopy(partPath, database, { expectedVersion: from, keepBotKey });
849
+ copyHookForTest?.("before-rename");
850
+ fs.renameSync(partPath, copyPath);
851
+ renamed = true;
852
+ partPath = null;
853
+ fsyncDirectory(folder);
854
+ const copies = fs.readdirSync(folder).filter((name) => /^levix-v\d+-\d{8}T\d{6}Z\.db$/.test(name)).sort((a, b) => b.match(/-(\d{8}T\d{6}Z)/)[1].localeCompare(a.match(/-(\d{8}T\d{6}Z)/)[1]));
855
+ for (const old of copies.slice(2)) fs.rmSync(path.join(folder, old));
856
+ return copyPath;
857
+ } catch (cause) {
858
+ if (partPath) try { fs.rmSync(partPath, { force: true }); } catch {}
859
+ if (renamed) try { fs.rmSync(copyPath, { force: true }); } catch {}
860
+ const error = new Error(`Could not verify a pre-migration database copy: ${cause.message}`);
861
+ error.code = "BACKUP_FAILED";
862
+ error.cause = cause;
863
+ throw error;
864
+ }
865
+ }
866
+
867
+ async function prepareDatabase(migrations = MIGRATIONS) {
868
+ if (!require("../config/lock.cjs").isLockHeld()) {
869
+ throw new Error("database preparation requires the instance lock");
870
+ }
871
+ const database = openRaw();
872
+ const from = schemaVersion(database);
873
+ if (from > migrations.length) throw newerSchemaError(from, migrations.length);
874
+ let copyPath = null;
875
+ if (from > 0 && from < migrations.length) copyPath = await makePreMigrationCopy(database, from);
876
+ configure(database);
877
+ try {
878
+ migrate(database, migrations);
879
+ } catch (error) {
880
+ // SQLite errors already have a code; startup still needs one stable
881
+ // classification for every failed migration.
882
+ error.migrationCauseCode = error.code;
883
+ error.code = "MIGRATION_FAILED";
884
+ throw error;
704
885
  }
886
+ prepared = true;
887
+ return { from, to: migrations.length, copyPath };
705
888
  }
706
889
 
707
- migrate(db);
890
+ function openWithoutMigration() {
891
+ return openRaw();
892
+ }
708
893
 
709
894
  // ===================================================================
710
895
  // --- Helpers shared by the store ---
@@ -715,9 +900,10 @@ migrate(db);
715
900
  const statementCache = new Map();
716
901
 
717
902
  function q(sql) {
903
+ const database = activeDatabase();
718
904
  let statement = statementCache.get(sql);
719
905
  if (!statement) {
720
- statement = db.prepare(sql);
906
+ statement = database.prepare(sql);
721
907
  statementCache.set(sql, statement);
722
908
  }
723
909
  return statement;
@@ -735,6 +921,15 @@ function parseJson(text, fallback) {
735
921
 
736
922
  /** Delete whatever has aged out. Cheap; called at boot and on a timer. */
737
923
  function sweepExpired() {
924
+ try {
925
+ q("DELETE FROM reaction_events WHERE expires_at <= ?").run(Date.now());
926
+ q("DELETE FROM reaction_state WHERE expires_at <= ?").run(Date.now());
927
+ q("DELETE FROM reaction_invalidations WHERE expires_at <= ?").run(Date.now());
928
+ q("DELETE FROM reaction_executions WHERE expires_at <= ?").run(Date.now());
929
+ q("DELETE FROM reaction_budgets WHERE expires_at <= ?").run(Date.now());
930
+ } catch {
931
+ logger.warn("[DB] reaction metadata sweep failed");
932
+ }
738
933
  try {
739
934
  const { changes } = q("DELETE FROM forward_scores WHERE expires_at < ?").run(Date.now());
740
935
  if (changes) logger.debug(`[DB] Swept ${changes} expired forward score(s)`);
@@ -777,7 +972,7 @@ function sweepExpired() {
777
972
  /** Flush the WAL into the main file — called on shutdown and after a backup. */
778
973
  function checkpoint() {
779
974
  try {
780
- db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
975
+ if (connection && configured) connection.exec("PRAGMA wal_checkpoint(TRUNCATE)");
781
976
  } catch (err) {
782
977
  logger.error({ err }, "[DB] checkpoint failed");
783
978
  }
@@ -786,8 +981,12 @@ function checkpoint() {
786
981
  function close() {
787
982
  checkpoint();
788
983
  try {
789
- db.close();
984
+ connection?.close();
790
985
  } catch {}
986
+ connection = null;
987
+ statementCache.clear();
988
+ prepared = false;
989
+ configured = false;
791
990
  }
792
991
 
793
992
  module.exports = {
@@ -798,6 +997,12 @@ module.exports = {
798
997
  // only way to prove the rollback works.
799
998
  migrate,
800
999
  MIGRATIONS,
1000
+ guardStartup,
1001
+ prepareDatabase,
1002
+ openWithoutMigration,
1003
+ schemaVersion,
1004
+ fsyncDirectory,
1005
+ setCopyHookForTest,
801
1006
  parseJson,
802
1007
  sweepExpired,
803
1008
  checkpoint,
@@ -0,0 +1,5 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ // ESM evaluates this first dependency before the rest of src/index.js's graph.
4
+ const require = createRequire(import.meta.url);
5
+ require("./db.cjs").guardStartup();