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
@@ -31,30 +31,30 @@ async function primePermissions() {
31
31
  }
32
32
  }
33
33
 
34
- function isOwnerJidSync(jid) {
34
+ function isOwnerJidSync(jid, groupMetadata) {
35
35
  if (!mod) return false;
36
36
  try {
37
- return mod.isOwnerJid(jid);
37
+ return mod.isOwnerJid(jid, groupMetadata);
38
38
  } catch (err) {
39
39
  logger.error({ err }, "[permissions.cjs] isOwnerJidSync failed");
40
40
  return false;
41
41
  }
42
42
  }
43
43
 
44
- function isBotAdminUserSync(jid) {
44
+ function isBotAdminUserSync(jid, groupMetadata) {
45
45
  if (!mod) return false;
46
46
  try {
47
- return mod.isBotAdminUser(jid);
47
+ return mod.isBotAdminUser(jid, groupMetadata);
48
48
  } catch (err) {
49
49
  logger.error({ err }, "[permissions.cjs] isBotAdminUserSync failed");
50
50
  return false;
51
51
  }
52
52
  }
53
53
 
54
- function hasBotPrivilegesSync(jid) {
54
+ function hasBotPrivilegesSync(jid, groupMetadata) {
55
55
  if (!mod) return false;
56
56
  try {
57
- return mod.hasBotPrivileges(jid);
57
+ return mod.hasBotPrivileges(jid, groupMetadata);
58
58
  } catch (err) {
59
59
  logger.error({ err }, "[permissions.cjs] hasBotPrivilegesSync failed");
60
60
  return false;
@@ -123,6 +123,14 @@ function getSenderCandidatesSync(msg, sock) {
123
123
  return collectNormalized(raw);
124
124
  }
125
125
 
126
+ function participantIdsSync(participant) {
127
+ if (mod?.participantIds) return mod.participantIds(participant);
128
+ if (!participant) return [];
129
+ return [participant.id, participant.lid, participant.phoneNumber, participant.jid].filter(
130
+ Boolean,
131
+ );
132
+ }
133
+
126
134
  function findGroupParticipantSync(groupMetadata, jidOrList) {
127
135
  if (mod?.findGroupParticipant) {
128
136
  try {
@@ -156,9 +164,9 @@ function rosterJidForSync(groupMetadata, jidOrList) {
156
164
  return collectNormalized(list)[0] || list.find(Boolean) || null;
157
165
  }
158
166
 
159
- async function isOwnerJid(jid) {
167
+ async function isOwnerJid(jid, groupMetadata) {
160
168
  const m = await getMod();
161
- return m.isOwnerJid(jid);
169
+ return m.isOwnerJid(jid, groupMetadata);
162
170
  }
163
171
 
164
172
  async function isAdminInGroup(groupMetadata, jid) {
@@ -171,9 +179,9 @@ async function isBotAdminInGroup(groupMetadata, sock) {
171
179
  return m.isBotAdminInGroup(groupMetadata, sock);
172
180
  }
173
181
 
174
- async function bootstrapOwners(values) {
182
+ async function bootstrapOwners(values, verifiedSelf) {
175
183
  const m = await getMod();
176
- return m.bootstrapOwners(values);
184
+ return m.bootstrapOwners(values, verifiedSelf);
177
185
  }
178
186
 
179
187
  async function addBootstrapOwner(value) {
@@ -181,9 +189,9 @@ async function addBootstrapOwner(value) {
181
189
  return m.addBootstrapOwner(value);
182
190
  }
183
191
 
184
- async function isBotAdminUser(jid) {
192
+ async function isBotAdminUser(jid, groupMetadata) {
185
193
  const m = await getMod();
186
- return m.isBotAdminUser(jid);
194
+ return m.isBotAdminUser(jid, groupMetadata);
187
195
  }
188
196
 
189
197
  async function grantRole(target, role) {
@@ -191,9 +199,9 @@ async function grantRole(target, role) {
191
199
  return m.grantRole(target, role);
192
200
  }
193
201
 
194
- async function revokeRole(target, role) {
202
+ async function revokeRole(target, role, verifiedAliases) {
195
203
  const m = await getMod();
196
- return m.revokeRole(target, role);
204
+ return m.revokeRole(target, role, verifiedAliases);
197
205
  }
198
206
 
199
207
  async function listRoles() {
@@ -220,5 +228,6 @@ module.exports = {
220
228
  sameUserSync,
221
229
  getSenderCandidatesSync,
222
230
  findGroupParticipantSync,
231
+ participantIdsSync,
223
232
  rosterJidForSync,
224
233
  };
@@ -1,68 +1,54 @@
1
- // Centralized permissions module.
2
- //
3
- // Until now, owner detection was scattered across at least five places, each
4
- // using a slightly different (and mutually inconsistent) check:
5
- // * permissions.middleware.js — DB lookup + config.owners + fromMe
6
- // * group.cjs — config.owners only (always empty)
7
- // * group/media.cjs — config.owners on a `{}` parameter (throws!)
8
- // * group/antilink.cjs — same as media.cjs
9
- // * antispam.middleware.js — a legacy owner mirror (only the bot itself)
10
- // * gemini.cjs — display-name match (fragile)
11
- //
12
- // This module is the single source of truth. It exposes:
13
- //
14
- // bootstrapOwners(list)
15
- // Called once at connection time with the bot's own JID/LID plus every
16
- // owner already recorded in user_metadata. Stored in a Set so subsequent
17
- // checks are O(1) and don't hit the database on the hot path.
18
- //
19
- // isOwnerJid(jidOrLidOrPhone)
20
- // Checks every direction we know about:
21
- // - bootstrap set
22
- // - user_metadata table (is_owner = 1) by jid, lid, or phone
23
- // - cross-format: if given a LID, look up its PN and recheck; vice versa
24
- // - phone-digit fallback (handles `:XX` device suffixes etc.)
25
- //
26
- // isAdminInGroup(groupMetadata, jid)
27
- // LID/PN-aware admin check.
28
- //
29
- // isBotAdminInGroup(groupMetadata, sock)
30
- // Same idea for the bot itself.
31
- //
32
- // sameUser(a, b)
33
- // True if two identifiers refer to the same person (LID/PN/phone equiv).
34
-
35
- import normalizeJid, { isLidJid, isPnJid } from "./normalizeJid.esm.js";
1
+ // Centralized role and WhatsApp identity checks. Numeric equality across the
2
+ // LID and PN namespaces is never evidence of identity.
3
+ import normalizeJid from "./normalizeJid.esm.js";
4
+ import { createRequire } from "node:module";
36
5
  import {
37
6
  isUserBotAdmin as dbIsBotAdmin,
38
7
  isUserOwner as dbIsOwner,
39
8
  getAllBotAdmins,
40
9
  getAllOwners,
41
- getLidForPn,
42
- getPnForLid,
43
10
  setUserRole,
44
11
  } from "./storage.esm.js";
45
12
 
13
+ const require = createRequire(import.meta.url);
14
+ const { roleIdentityAliases, withImmediateTransaction } = require("../db/store.cjs");
15
+
46
16
  // Two rosters, same mechanics:
47
17
  // owners — full access, including OWNER_ONLY commands
48
18
  // bot admins — treated as an admin in every chat (group admin OR this role
49
19
  // satisfies ADMINS_ONLY / ADMINS_OWNER), but not OWNER_ONLY
50
20
  const bootstrap = new Set();
51
21
  const bootstrapAdminSet = new Set();
52
-
53
- function digitsOnly(value) {
54
- if (!value) return null;
55
- const m = String(value).match(/\d+/);
56
- return m ? m[0] : null;
57
- }
22
+ const linkedSelf = new Set();
58
23
 
59
24
  function pushInto(set, value) {
60
- if (!value) return;
61
- const norm = normalizeJid(value);
25
+ const norm = identityForms(value)[0];
62
26
  if (norm) set.add(norm);
63
- const phone = digitsOnly(value);
64
- if (phone) {
65
- set.add(`${phone}@s.whatsapp.net`);
27
+ }
28
+
29
+ // Connection bootstrap passes primary JIDs plus legacy user_lid columns.
30
+ // Do not freeze a secondary column into an independent grant. Current mapping
31
+ // aliases are resolved at check time, and unverified contact links grant nothing.
32
+ function replaceBootstrap(set, values, listAll) {
33
+ set.clear();
34
+ if (!Array.isArray(values)) return;
35
+ const secondary = new Set();
36
+ const primary = new Set();
37
+ try {
38
+ for (const row of listAll()) {
39
+ const jid = identityForms(row.jid)[0];
40
+ const lid = identityForms(row.lid)[0];
41
+ if (jid) primary.add(jid);
42
+ if (lid && lid !== jid && values.some((value) => identityForms(value)[0] === jid)) {
43
+ secondary.add(lid);
44
+ }
45
+ }
46
+ } catch {
47
+ return; // A bootstrap list may contain unverified contact aliases.
48
+ }
49
+ for (const value of values) {
50
+ const norm = identityForms(value)[0];
51
+ if (!secondary.has(norm) || primary.has(norm)) pushInto(set, value);
66
52
  }
67
53
  }
68
54
 
@@ -72,12 +58,14 @@ function pushBootstrap(value) {
72
58
 
73
59
  /**
74
60
  * Replace the bootstrap owner set. Pass an array of JIDs/LIDs/phones; we
75
- * normalize and de-dupe them.
61
+ * normalize and de-dupe them. verifiedSelf contains only paired credentials.
76
62
  */
77
- export function bootstrapOwners(values) {
78
- bootstrap.clear();
79
- if (!Array.isArray(values)) return;
80
- values.forEach(pushBootstrap);
63
+ export function bootstrapOwners(values, verifiedSelf = []) {
64
+ replaceBootstrap(bootstrap, values, getAllOwners);
65
+ // Cleared by lifecycle bootstrap (including unlink), never by revocation.
66
+ // Supplied only from paired credentials, never from contact metadata.
67
+ linkedSelf.clear();
68
+ for (const value of verifiedSelf) pushInto(linkedSelf, value);
81
69
  }
82
70
 
83
71
  /** Add an owner to the bootstrap set (does not touch the DB). */
@@ -86,16 +74,12 @@ export function addBootstrapOwner(value) {
86
74
  }
87
75
 
88
76
  export function getBootstrapOwners() {
89
- return [...bootstrap];
77
+ return [...new Set([...bootstrap, ...linkedSelf])];
90
78
  }
91
79
 
92
80
  /** Same as bootstrapOwners, for the bot-admin roster. */
93
81
  export function bootstrapAdmins(values) {
94
- bootstrapAdminSet.clear();
95
- if (!Array.isArray(values)) return;
96
- values.forEach((value) => {
97
- pushInto(bootstrapAdminSet, value);
98
- });
82
+ replaceBootstrap(bootstrapAdminSet, values, getAllBotAdmins);
99
83
  }
100
84
 
101
85
  export function addBootstrapAdmin(value) {
@@ -106,80 +90,38 @@ export function getBootstrapAdmins() {
106
90
  return [...bootstrapAdminSet];
107
91
  }
108
92
 
109
- /**
110
- * Every identifier that could stand for the same person: the normalized form,
111
- * its LID/PN counterpart, and the bare-phone JID. Both role checks walk this
112
- * list so a user granted a role by phone still matches when they message under
113
- * a LID (and vice-versa).
114
- */
93
+ /** Exact normalized identity and its current authoritative mapping only. */
115
94
  function identityForms(jid) {
116
- const forms = [];
117
- const push = (value) => {
118
- const norm = normalizeJid(value);
119
- if (norm && !forms.includes(norm)) forms.push(norm);
120
- };
121
-
122
- const norm = normalizeJid(jid);
123
- if (!norm) return forms;
124
- push(norm);
125
-
126
- if (isLidJid(norm)) {
127
- try {
128
- push(getPnForLid(norm));
129
- } catch {}
130
- }
131
- if (isPnJid(norm)) {
132
- try {
133
- push(getLidForPn(norm));
134
- } catch {}
95
+ if (!jid) return [];
96
+ try {
97
+ return roleIdentityAliases(jid);
98
+ } catch {
99
+ // A failed mapping read cannot establish another identity.
100
+ const norm = normalizeJid(jid);
101
+ return norm?.includes("@") ? [norm] : [];
135
102
  }
136
-
137
- const phone = digitsOnly(norm);
138
- if (phone) push(`${phone}@s.whatsapp.net`);
139
-
140
- return forms;
141
103
  }
142
104
 
143
- /**
144
- * Shared role resolution: bootstrap set, then the DB, then a phone-digit walk
145
- * over the stored roster (covers rows saved with only a phone or only a LID).
146
- */
147
- function hasRole(jid, { set, dbCheck, listAll }) {
148
- if (!jid) return false;
149
- const forms = identityForms(jid);
150
- if (!forms.length) return false;
151
-
105
+ function hasRole(jid, { set, dbCheck, permanent }, groupMetadata) {
106
+ const forms = new Set(identityForms(jid));
107
+ // A transport roster object carrying both forms is proof even before the
108
+ // mapping table learns the pair. Never consult user_metadata aliases here.
109
+ const participant = findGroupParticipant(groupMetadata, jid);
110
+ for (const id of participantIds(participant)) {
111
+ for (const form of identityForms(id)) forms.add(form);
112
+ }
152
113
  for (const form of forms) {
153
- if (set.has(form)) return true;
114
+ if (set.has(form) || permanent?.has(form)) return true;
154
115
  try {
155
116
  if (dbCheck(form)) return true;
156
117
  } catch {}
157
118
  }
158
-
159
- const phone = digitsOnly(forms[0]);
160
- if (phone) {
161
- try {
162
- for (const entry of listAll()) {
163
- const entryPhone =
164
- digitsOnly(entry.phone) || digitsOnly(entry.jid) || digitsOnly(entry.lid);
165
- if (entryPhone && entryPhone === phone) return true;
166
- }
167
- } catch {}
168
- }
169
-
170
119
  return false;
171
120
  }
172
121
 
173
- /**
174
- * Comprehensive owner check (bootstrap set + user_metadata + LID/PN
175
- * cross-lookup + phone-digit fallback).
176
- */
177
- export function isOwnerJid(jid) {
178
- return hasRole(jid, {
179
- set: bootstrap,
180
- dbCheck: dbIsOwner,
181
- listAll: getAllOwners,
182
- });
122
+ /** Owner check by exact primary identity or verified current mapping. */
123
+ export function isOwnerJid(jid, groupMetadata) {
124
+ return hasRole(jid, { set: bootstrap, dbCheck: dbIsOwner, permanent: linkedSelf }, groupMetadata);
183
125
  }
184
126
 
185
127
  /**
@@ -188,18 +130,21 @@ export function isOwnerJid(jid) {
188
130
  * the AI) that makes the user an admin everywhere the bot works.
189
131
  * Owners always pass.
190
132
  */
191
- export function isBotAdminUser(jid) {
192
- if (isOwnerJid(jid)) return true;
193
- return hasRole(jid, {
194
- set: bootstrapAdminSet,
195
- dbCheck: dbIsBotAdmin,
196
- listAll: getAllBotAdmins,
197
- });
133
+ export function isBotAdminUser(jid, groupMetadata) {
134
+ if (isOwnerJid(jid, groupMetadata)) return true;
135
+ return hasRole(
136
+ jid,
137
+ {
138
+ set: bootstrapAdminSet,
139
+ dbCheck: dbIsBotAdmin,
140
+ },
141
+ groupMetadata,
142
+ );
198
143
  }
199
144
 
200
145
  /** Owner or bot-admin — "this person may drive the bot". */
201
- export function hasBotPrivileges(jid) {
202
- return isOwnerJid(jid) || isBotAdminUser(jid);
146
+ export function hasBotPrivileges(jid, groupMetadata) {
147
+ return isOwnerJid(jid, groupMetadata) || isBotAdminUser(jid, groupMetadata);
203
148
  }
204
149
 
205
150
  /**
@@ -219,21 +164,33 @@ export function grantRole(target, role = "admin") {
219
164
  }
220
165
 
221
166
  /**
222
- * Revoke a bot-level role. The in-memory roster is rebuilt from what's left so
223
- * a revoked user stops matching without a restart.
167
+ * Revoke a bot-level role and remove its proven aliases from the bootstrap
168
+ * set so the revocation takes effect without a restart. verifiedAliases must
169
+ * come from trusted target participant metadata.
224
170
  *
225
171
  * @returns {object|null} the stored user record
226
172
  */
227
- export function revokeRole(target, role = "admin") {
173
+ export function revokeRole(target, role = "admin", verifiedAliases = []) {
228
174
  const wanted = String(role).toLowerCase() === "owner" ? "owner" : "admin";
229
- const record = setUserRole(target, wanted, false);
230
-
231
- const forms = identityForms(record?.jid || target);
175
+ // Extra identities come from the target's trusted roster participant, never
176
+ // from model arguments or secondary user_metadata columns.
177
+ const forms = new Set([target, ...verifiedAliases].flatMap(identityForms));
178
+ if (wanted === "owner" && [...forms].some((form) => linkedSelf.has(form))) {
179
+ throw Object.assign(
180
+ new Error("The linked account is always an owner. Unlink WhatsApp to change it."),
181
+ { code: "LINKED_ACCOUNT_OWNER" },
182
+ );
183
+ }
184
+ const record = withImmediateTransaction(() => {
185
+ const result = setUserRole(target, wanted, false);
186
+ for (const form of forms) {
187
+ if (form !== result?.jid) setUserRole(form, wanted, false);
188
+ }
189
+ return result;
190
+ });
232
191
  const set = wanted === "owner" ? bootstrap : bootstrapAdminSet;
233
192
  for (const form of forms) {
234
193
  set.delete(form);
235
- const phone = digitsOnly(form);
236
- if (phone) set.delete(`${phone}@s.whatsapp.net`);
237
194
  }
238
195
  return record;
239
196
  }
@@ -257,37 +214,13 @@ export function listRoles() {
257
214
  }
258
215
 
259
216
  /**
260
- * True if the two identifiers refer to the same physical user — handles LID
261
- * vs PN vs phone-with-device-suffix confusion.
217
+ * Exact identity (including bare PN/device suffix forms) or a current mapping.
262
218
  */
263
219
  export function sameUser(a, b) {
264
220
  if (!a || !b) return false;
265
- const na = normalizeJid(a);
266
- const nb = normalizeJid(b);
267
- if (na === nb) return true;
268
- const da = digitsOnly(na);
269
- const db = digitsOnly(nb);
270
- if (da && db && da === db) return true;
271
- // Cross-format lookup
272
- try {
273
- if (isLidJid(na)) {
274
- const pn = getPnForLid(na);
275
- if (pn && normalizeJid(pn) === nb) return true;
276
- }
277
- if (isLidJid(nb)) {
278
- const pn = getPnForLid(nb);
279
- if (pn && normalizeJid(pn) === na) return true;
280
- }
281
- if (isPnJid(na)) {
282
- const lid = getLidForPn(na);
283
- if (lid && normalizeJid(lid) === nb) return true;
284
- }
285
- if (isPnJid(nb)) {
286
- const lid = getLidForPn(nb);
287
- if (lid && normalizeJid(lid) === na) return true;
288
- }
289
- } catch {}
290
- return false;
221
+ const forms = identityForms(a);
222
+ const other = identityForms(b)[0];
223
+ return Boolean(other) && forms.includes(other);
291
224
  }
292
225
 
293
226
  /**