levix-bot 4.2.1 → 4.3.1

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 (55) hide show
  1. package/app.cjs +50 -4
  2. package/package.json +1 -1
  3. package/public/dashboard/assets/index-B6RhYFIy.js +446 -0
  4. package/public/dashboard/assets/index-vEwOcxiH.css +2 -0
  5. package/public/dashboard/index.html +2 -2
  6. package/public/dashboard.css +71 -1
  7. package/public/gateway.js +41 -0
  8. package/src/bootstrap/core.js +4 -0
  9. package/src/bootstrap/panel.js +2 -0
  10. package/src/commands/autoschedule.cjs +11 -14
  11. package/src/commands/blacklist.cjs +1 -1
  12. package/src/commands/block.cjs +1 -1
  13. package/src/commands/calc.cjs +9 -9
  14. package/src/commands/gemini.cjs +26 -26
  15. package/src/commands/group/add.cjs +2 -2
  16. package/src/commands/group/demote.cjs +2 -2
  17. package/src/commands/group/members.cjs +2 -2
  18. package/src/commands/group/rules.cjs +1 -1
  19. package/src/commands/group/setname.cjs +3 -3
  20. package/src/commands/group/setpp.cjs +3 -3
  21. package/src/commands/group/setrules.cjs +1 -1
  22. package/src/commands/group/tagadmins.cjs +1 -1
  23. package/src/commands/group.cjs +1 -1
  24. package/src/commands/lang.cjs +3 -3
  25. package/src/commands/listschedules.cjs +1 -1
  26. package/src/commands/loop.cjs +3 -3
  27. package/src/commands/memory.cjs +18 -18
  28. package/src/commands/notes.cjs +1 -1
  29. package/src/commands/perm.cjs +2 -2
  30. package/src/commands/poll.cjs +3 -3
  31. package/src/commands/rand.cjs +2 -2
  32. package/src/commands/restart.cjs +46 -17
  33. package/src/commands/schedule.cjs +9 -11
  34. package/src/commands/send.cjs +116 -0
  35. package/src/commands/status.cjs +2 -2
  36. package/src/commands/stopbot.cjs +68 -0
  37. package/src/commands/stt.cjs +1 -1
  38. package/src/commands/tts.cjs +2 -2
  39. package/src/commands/unblock.cjs +1 -1
  40. package/src/config/defaults.cjs +2 -1
  41. package/src/config/settings.cjs +4 -2
  42. package/src/core/session-holder.cjs +23 -0
  43. package/src/handlers/command.handler.js +4 -6
  44. package/src/panel/password-reset.cjs +187 -0
  45. package/src/routes/dashboard.api.esm.js +48 -78
  46. package/src/services/aiAgent.cjs +2 -2
  47. package/src/services/aiProviders.cjs +1 -1
  48. package/src/services/aiTools.cjs +27 -27
  49. package/src/services/settingsTransfer.cjs +246 -0
  50. package/src/utils/memory.cjs +7 -7
  51. package/views/login.ejs +209 -9
  52. package/views/setup.ejs +19 -5
  53. package/public/dashboard/assets/index-4AuqfnH5.js +0 -431
  54. package/public/dashboard/assets/index-LvfJ8ybs.css +0 -2
  55. package/src/commands/shutdown.cjs +0 -55
@@ -0,0 +1,246 @@
1
+ // Share settings as a file: export what the operator changed, import it back
2
+ // on another install — the "my friend set their bot up like mine" flow.
3
+ //
4
+ // Two rules make the file safe to pass around:
5
+ //
6
+ // * Secrets never leave. API keys, the proxy password, the panel password —
7
+ // every `type: "secret"` setting is skipped on export AND refused on
8
+ // import, so a shared file can't carry a credential and can't overwrite
9
+ // the importer's own. Everyone fills their own keys in the panel.
10
+ // * Only overrides are exported. A setting the operator never touched stays
11
+ // out of the file, so the importer keeps following their own shipped
12
+ // defaults wherever the sharer also hadn't changed anything.
13
+ //
14
+ // What is covered: every non-secret `setting:*` row (AI provider and models,
15
+ // language, delays, thumbnails, port and proxy shape, …) plus the command
16
+ // table — prefix, permission overrides, alias overrides, disabled commands.
17
+ // Not covered on purpose: per-group config and the memory files (they are
18
+ // personal data, not settings) and the AI persona (a behaviour file the
19
+ // operator authors, shared by copy-paste if wanted).
20
+
21
+ const settings = require("../config/settings.cjs");
22
+ const runtimeConfig = require("../config/runtime-config.cjs");
23
+ const logger = require("../utils/logger.cjs");
24
+ const {
25
+ invalidateDiscoveryCache,
26
+ applyCapabilityGuards,
27
+ resolveCapabilities,
28
+ } = require("./modelRegistry.cjs");
29
+
30
+ const FORMAT = "levix-settings";
31
+ const VERSION = 1;
32
+
33
+ function isPlainObject(value) {
34
+ return !!value && typeof value === "object" && !Array.isArray(value);
35
+ }
36
+
37
+ /**
38
+ * The file a friend shares. Hand the JSON straight to the importer UI.
39
+ */
40
+ function exportSettings() {
41
+ const exported = {};
42
+ for (const key of settings.SETTING_KEYS) {
43
+ if (settings.SECRET_SETTING_KEYS.includes(key)) continue;
44
+ if (settings.sourceOf(key) !== "dashboard") continue;
45
+ exported[key] = settings.get(key);
46
+ }
47
+
48
+ // Hand-edited rows can't happen through the panel, but a copy that passed
49
+ // through someone's editor might — export only the levels the code acts on.
50
+ const permissions = {};
51
+ for (const [command, level] of Object.entries(runtimeConfig.permissionOverrides())) {
52
+ if (runtimeConfig.PERMISSION_LEVELS.includes(level)) permissions[command] = level;
53
+ }
54
+ const aliases = {};
55
+ for (const [command, list] of Object.entries(runtimeConfig.aliasOverrides())) {
56
+ if (Array.isArray(list)) aliases[command] = list.map(String);
57
+ }
58
+
59
+ return {
60
+ format: FORMAT,
61
+ version: VERSION,
62
+ exportedAt: new Date().toISOString(),
63
+ settings: exported,
64
+ commands: {
65
+ prefix: runtimeConfig.getPrefix(),
66
+ permissions,
67
+ aliases,
68
+ disabled: runtimeConfig.disabledCommands(),
69
+ },
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Apply one non-secret setting exactly the way PATCH /dashboard/api/settings
75
+ * does: model-capability guards around vision/STT, discovery-cache drops when
76
+ * a credential or endpoint moves. The PATCH route calls this too, so a hand
77
+ * edit and an import can't drift apart.
78
+ *
79
+ * @returns the value actually in force afterwards
80
+ */
81
+ function applySettingChange(key, value) {
82
+ if (key === "ai_vision_enabled" || key === "ai_stt_enabled") {
83
+ const provider = String(settings.get("ai_provider") || "gemini");
84
+ const modelKey =
85
+ provider === "gemini"
86
+ ? "gemini_model"
87
+ : provider === "anthropic"
88
+ ? "anthropic_model"
89
+ : "openai_model";
90
+ const resolved = resolveCapabilities(provider, settings.get(modelKey));
91
+ const next = applyCapabilityGuards({
92
+ visionEnabled: key === "ai_vision_enabled" ? value : settings.get("ai_vision_enabled"),
93
+ sttEnabled: key === "ai_stt_enabled" ? value : settings.get("ai_stt_enabled"),
94
+ capabilities: resolved.capabilities,
95
+ capabilitySource: resolved.capabilitySource,
96
+ autoDetect: settings.get("ai_auto_detect_capabilities"),
97
+ });
98
+ if (key === "ai_vision_enabled") value = next.visionEnabled;
99
+ if (key === "ai_stt_enabled") value = next.sttEnabled;
100
+ }
101
+
102
+ settings.set(key, value);
103
+
104
+ if (key.includes("api_key") || key.includes("base_url")) {
105
+ const prov = key.startsWith("gemini")
106
+ ? "gemini"
107
+ : key.startsWith("anthropic")
108
+ ? "anthropic"
109
+ : key.startsWith("openai")
110
+ ? "openai"
111
+ : null;
112
+ if (prov) invalidateDiscoveryCache(prov);
113
+ else invalidateDiscoveryCache();
114
+ }
115
+ if (
116
+ key === "ai_provider" ||
117
+ key === "gemini_model" ||
118
+ key === "openai_model" ||
119
+ key === "anthropic_model"
120
+ ) {
121
+ const provider = key === "ai_provider" ? String(value || "gemini") : key.split("_")[0];
122
+ const modelKey =
123
+ provider === "gemini"
124
+ ? "gemini_model"
125
+ : provider === "anthropic"
126
+ ? "anthropic_model"
127
+ : "openai_model";
128
+ const modelId = key.endsWith("_model") ? value : settings.get(modelKey);
129
+ const resolved = resolveCapabilities(provider, modelId);
130
+ const next = applyCapabilityGuards({
131
+ visionEnabled: settings.get("ai_vision_enabled"),
132
+ sttEnabled: settings.get("ai_stt_enabled"),
133
+ capabilities: resolved.capabilities,
134
+ capabilitySource: resolved.capabilitySource,
135
+ autoDetect: settings.get("ai_auto_detect_capabilities"),
136
+ });
137
+ if (Boolean(settings.get("ai_vision_enabled")) !== next.visionEnabled) {
138
+ settings.set("ai_vision_enabled", next.visionEnabled);
139
+ }
140
+ if (Boolean(settings.get("ai_stt_enabled")) !== next.sttEnabled) {
141
+ settings.set("ai_stt_enabled", next.sttEnabled);
142
+ }
143
+ }
144
+ return settings.get(key);
145
+ }
146
+
147
+ /**
148
+ * Apply a shared file to this install. One bad value never stops the rest —
149
+ * it is reported and the import carries on.
150
+ *
151
+ * @returns {object} a report the panel shows: what landed, what didn't, and
152
+ * which keys wait for a restart.
153
+ */
154
+ function applyImport(payload) {
155
+ if (!isPlainObject(payload) || payload.format !== FORMAT) {
156
+ throw new Error("That file isn't a Levix settings export");
157
+ }
158
+ if (payload.version !== VERSION) {
159
+ throw new Error(`Unsupported export version: ${String(payload.version)}`);
160
+ }
161
+
162
+ const report = { applied: [], skipped: [], restartNeeded: [], counts: {} };
163
+
164
+ for (const [key, value] of Object.entries(
165
+ isPlainObject(payload.settings) ? payload.settings : {},
166
+ )) {
167
+ if (!settings.SETTING_KEYS.includes(key)) {
168
+ report.skipped.push({ key, reason: "Unknown setting" });
169
+ continue;
170
+ }
171
+ if (settings.SECRET_SETTING_KEYS.includes(key)) {
172
+ // A hand-crafted file doesn't get to plant a credential.
173
+ report.skipped.push({ key, reason: "Secret — set it in the panel" });
174
+ continue;
175
+ }
176
+ try {
177
+ applySettingChange(key, value);
178
+ report.applied.push(key);
179
+ if (settings.RESTART_SETTING_KEYS.includes(key)) report.restartNeeded.push(key);
180
+ } catch (error) {
181
+ report.skipped.push({ key, reason: error.message });
182
+ }
183
+ }
184
+
185
+ const commands = isPlainObject(payload.commands) ? payload.commands : {};
186
+ const commandReport = { permissions: 0, aliases: 0, disabled: 0 };
187
+
188
+ if (typeof commands.prefix === "string" && commands.prefix.trim()) {
189
+ try {
190
+ runtimeConfig.setPrefix(commands.prefix);
191
+ report.applied.push("prefix");
192
+ } catch (error) {
193
+ report.skipped.push({ key: "prefix", reason: error.message });
194
+ }
195
+ }
196
+
197
+ for (const [command, level] of Object.entries(
198
+ isPlainObject(commands.permissions) ? commands.permissions : {},
199
+ )) {
200
+ try {
201
+ runtimeConfig.setPermission(command, level);
202
+ commandReport.permissions += 1;
203
+ } catch (error) {
204
+ report.skipped.push({ key: `permission:${command}`, reason: error.message });
205
+ }
206
+ }
207
+
208
+ for (const [command, list] of Object.entries(
209
+ isPlainObject(commands.aliases) ? commands.aliases : {},
210
+ )) {
211
+ try {
212
+ runtimeConfig.setAliases(command, Array.isArray(list) ? list : null);
213
+ commandReport.aliases += 1;
214
+ } catch (error) {
215
+ report.skipped.push({ key: `aliases:${command}`, reason: error.message });
216
+ }
217
+ }
218
+
219
+ // The disabled list replaces the local one, so the two installs actually end
220
+ // up alike: everything the file doesn't name runs again.
221
+ const disabledWanted = Array.isArray(commands.disabled) ? commands.disabled.map(String) : [];
222
+ const currentlyDisabled = runtimeConfig.disabledCommands();
223
+ for (const command of currentlyDisabled) {
224
+ if (!disabledWanted.includes(command)) runtimeConfig.setEnabled(command, true);
225
+ }
226
+ for (const command of disabledWanted) {
227
+ runtimeConfig.setEnabled(command, false);
228
+ commandReport.disabled += 1;
229
+ }
230
+ if (commands.disabled !== undefined) report.applied.push("disabled_commands");
231
+
232
+ report.counts = commandReport;
233
+ logger.info(
234
+ { applied: report.applied.length, skipped: report.skipped.length },
235
+ "[settings] import applied",
236
+ );
237
+ return report;
238
+ }
239
+
240
+ module.exports = {
241
+ FORMAT,
242
+ VERSION,
243
+ exportSettings,
244
+ applyImport,
245
+ applySettingChange,
246
+ };
@@ -86,8 +86,8 @@ function headerFor(scope, chatId, chatName) {
86
86
  return [
87
87
  "# 🧠 الذاكرة العامة",
88
88
  "",
89
- "> معلومات محفوظة لكل المحادثات. ملف Markdown عادي — عدّله بإيدك وقت ما تحب.",
90
- "> كل سطر بيبدأ بـ `-` هو معلومة محفوظة.",
89
+ "> معلومات محفوظة لكل المحادثات. ملف Markdown عادي — يمكنك تعديله يدويًا في أي وقت.",
90
+ "> كل سطر يبدأ بـ `-` يمثّل معلومة محفوظة.",
91
91
  "",
92
92
  "",
93
93
  ].join("\n");
@@ -98,8 +98,8 @@ function headerFor(scope, chatId, chatName) {
98
98
  `<!-- chat: ${chatId || "unknown"} -->`,
99
99
  chatName ? `<!-- name: ${chatName} -->` : null,
100
100
  "",
101
- "> معلومات محفوظة لهذه المحادثة فقط. ملف Markdown عادي — عدّله بإيدك وقت ما تحب.",
102
- "> كل سطر بيبدأ بـ `-` هو معلومة محفوظة.",
101
+ "> معلومات محفوظة لهذه المحادثة فقط. ملف Markdown عادي — يمكنك تعديله يدويًا في أي وقت.",
102
+ "> كل سطر يبدأ بـ `-` يمثّل معلومة محفوظة.",
103
103
  "",
104
104
  "",
105
105
  ]
@@ -212,7 +212,7 @@ function addMemory({ scope = "chat", chatId, content, by, chatName } = {}) {
212
212
  const text = String(content || "")
213
213
  .trim()
214
214
  .slice(0, MAX_ENTRY_CHARS);
215
- if (!text) throw new Error(tr("There is nothing to save", "مفيش محتوى أحفظه"));
215
+ if (!text) throw new Error(tr("There is nothing to save", "لا يوجد محتوى لحفظه"));
216
216
 
217
217
  const filePath = memoryFilePath(normalizedScope, chatId);
218
218
  ensureDirs();
@@ -380,13 +380,13 @@ function buildMemoryContext(chatId, { limitChars = maxContextChars() } = {}) {
380
380
  }
381
381
  if (chatText) {
382
382
  blocks.push(
383
- `### ذاكرة المحادثة دي (${safeFileName(chatId)}.md)\n${clipTail(chatText, perScope)}`,
383
+ `### ذاكرة هذه المحادثة (${safeFileName(chatId)}.md)\n${clipTail(chatText, perScope)}`,
384
384
  );
385
385
  }
386
386
 
387
387
  return [
388
388
  "## 🧠 الذاكرة الدائمة",
389
- "دي معلومات اتحفظت قبل كده — سطور بالعلامة `-` اتسجلت بالبوت أو بصاحب البوت، وأي نص حر مكتوب هنا اتكتب بإيد صاحب البوت. اعتبر كل ده حقيقة واستخدمه من غير ما تقول إنك بتقرأ من ملف.",
389
+ "هذه معلومات حُفظت سابقًا. الأسطر التي تبدأ بـ `-` أضافها البوت أو مالكه، وأي نص حر كتبه مالك البوت يدويًا. تعامل مع هذه المعلومات بوصفها حقائق، واستخدمها دون الإشارة إلى أنك تقرؤها من ملف.",
390
390
  "",
391
391
  ...blocks,
392
392
  ].join("\n");
package/views/login.ejs CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  <section class="gateway-panel">
34
34
  <div class="gateway-panel-inner">
35
- <form class="card auth-card" action="/login" method="post">
35
+ <form class="card auth-card" id="login-card" action="/login" method="post">
36
36
  <div class="auth-head">
37
37
  <span class="eyebrow">Secure access</span>
38
38
  <h1>Welcome back</h1>
@@ -41,14 +41,20 @@
41
41
 
42
42
  <label class="field">
43
43
  <span>Control panel password</span>
44
- <input
45
- type="password"
46
- name="password"
47
- autocomplete="current-password"
48
- placeholder="Enter your password"
49
- autofocus
50
- required
51
- />
44
+ <div class="input-wrap">
45
+ <input
46
+ type="password"
47
+ name="password"
48
+ autocomplete="current-password"
49
+ placeholder="Enter your password"
50
+ autofocus
51
+ required
52
+ />
53
+ <button type="button" class="eye-btn" aria-label="Show password">
54
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
55
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
56
+ </button>
57
+ </div>
52
58
  </label>
53
59
 
54
60
  <button type="submit" class="btn btn-primary auth-submit">
@@ -59,11 +65,205 @@
59
65
  <% if (error) { %>
60
66
  <div class="error" role="alert"><%= error %></div>
61
67
  <% } %>
68
+
69
+ <div class="auth-alt">
70
+ <button type="button" class="link-btn" id="forgot-link">Forgot password?</button>
71
+ </div>
62
72
  </form>
63
73
 
74
+ <div class="card auth-card" id="reset-card" hidden>
75
+ <div class="auth-head">
76
+ <span class="eyebrow">Password reset</span>
77
+ <h1>Reset your password</h1>
78
+ <p class="sub">
79
+ Levix sends a one-time code to your WhatsApp — the linked account's own chat.
80
+ The code works for 10 minutes.
81
+ </p>
82
+ </div>
83
+
84
+ <button type="button" class="btn btn-primary auth-submit" id="reset-send">
85
+ Send code to WhatsApp
86
+ <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
87
+ </button>
88
+
89
+ <form id="reset-form" hidden>
90
+ <label class="field">
91
+ <span>Reset code</span>
92
+ <input
93
+ type="text"
94
+ id="reset-code"
95
+ inputmode="numeric"
96
+ autocomplete="one-time-code"
97
+ maxlength="6"
98
+ placeholder="6 digits"
99
+ required
100
+ />
101
+ </label>
102
+
103
+ <label class="field">
104
+ <span>New password</span>
105
+ <div class="input-wrap">
106
+ <input
107
+ type="password"
108
+ id="reset-pass"
109
+ autocomplete="new-password"
110
+ placeholder="At least <%= minLength %> characters"
111
+ minlength="<%= minLength %>"
112
+ required
113
+ />
114
+ <button type="button" class="eye-btn" aria-label="Show password">
115
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
116
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
117
+ </button>
118
+ </div>
119
+ </label>
120
+
121
+ <label class="field">
122
+ <span>Repeat password</span>
123
+ <div class="input-wrap">
124
+ <input
125
+ type="password"
126
+ id="reset-confirm"
127
+ autocomplete="new-password"
128
+ placeholder="Type it again"
129
+ minlength="<%= minLength %>"
130
+ required
131
+ />
132
+ <button type="button" class="eye-btn" aria-label="Show password">
133
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
134
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
135
+ </button>
136
+ </div>
137
+ </label>
138
+
139
+ <button type="submit" class="btn btn-primary auth-submit">
140
+ Set new password
141
+ <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
142
+ </button>
143
+ </form>
144
+
145
+ <div class="error" id="reset-error" role="alert" hidden></div>
146
+ <div class="auth-alt">
147
+ <button type="button" class="link-btn" id="reset-back">Back to sign in</button>
148
+ </div>
149
+ </div>
150
+
64
151
  <div class="foot"><%= brand.name %> by <%= brand.developer %> · <%= brand.studio %></div>
65
152
  </div>
66
153
  </section>
67
154
  </main>
155
+
156
+ <script src="/gateway.js"></script>
157
+ <script>
158
+ (function () {
159
+ var loginCard = document.getElementById("login-card");
160
+ var resetCard = document.getElementById("reset-card");
161
+ var sendBtn = document.getElementById("reset-send");
162
+ var form = document.getElementById("reset-form");
163
+ var errorBox = document.getElementById("reset-error");
164
+
165
+ function showReset() {
166
+ loginCard.hidden = true;
167
+ resetCard.hidden = false;
168
+ }
169
+ function showLogin() {
170
+ resetCard.hidden = true;
171
+ loginCard.hidden = false;
172
+ document.getElementById("forgot-link").focus();
173
+ }
174
+ function showError(text) {
175
+ errorBox.textContent = text;
176
+ errorBox.hidden = false;
177
+ }
178
+ function hideError() {
179
+ errorBox.hidden = true;
180
+ }
181
+ async function postJson(url, body) {
182
+ const response = await fetch(url, {
183
+ method: "POST",
184
+ headers: { "Content-Type": "application/json" },
185
+ body: JSON.stringify(body || {}),
186
+ });
187
+ let data = {};
188
+ try {
189
+ data = await response.json();
190
+ } catch {}
191
+ return data;
192
+ }
193
+
194
+ document.getElementById("forgot-link").addEventListener("click", showReset);
195
+ document.getElementById("reset-back").addEventListener("click", showLogin);
196
+
197
+ sendBtn.addEventListener("click", async function () {
198
+ sendBtn.disabled = true;
199
+ hideError();
200
+ try {
201
+ const result = await postJson("/reset/request");
202
+ if (result.ok) {
203
+ sendBtn.hidden = true;
204
+ form.hidden = false;
205
+ document.getElementById("reset-code").focus();
206
+ } else if (result.reason === "throttled") {
207
+ showError("Too many requests. Try again in " + (result.retryAfter || 60) + " seconds.");
208
+ } else if (result.reason === "no_session") {
209
+ showError(
210
+ "WhatsApp isn't connected, so the code can't be sent. " +
211
+ "Sign in on the server and run `levix reset-password`, or start the WhatsApp session first."
212
+ );
213
+ } else if (result.reason === "send_failed") {
214
+ showError("Couldn't send the WhatsApp message. Try again in a minute.");
215
+ } else {
216
+ showError("Couldn't start the reset. Try again.");
217
+ }
218
+ } catch {
219
+ showError("Couldn't reach the server. Try again.");
220
+ }
221
+ sendBtn.disabled = false;
222
+ });
223
+
224
+ form.addEventListener("submit", async function (event) {
225
+ event.preventDefault();
226
+ hideError();
227
+ try {
228
+ const result = await postJson("/reset/confirm", {
229
+ code: document.getElementById("reset-code").value,
230
+ password: document.getElementById("reset-pass").value,
231
+ confirm: document.getElementById("reset-confirm").value,
232
+ });
233
+ if (result.ok) {
234
+ form.hidden = true;
235
+ sendBtn.hidden = true;
236
+ showError("Password updated. Taking you to sign in…");
237
+ setTimeout(function () {
238
+ window.location.href = "/";
239
+ }, 900);
240
+ return;
241
+ }
242
+ if (result.reason === "wrong_code") {
243
+ showError(
244
+ "Wrong code." +
245
+ (result.attemptsLeft ? " " + result.attemptsLeft + " attempts left." : "")
246
+ );
247
+ } else if (
248
+ result.reason === "expired" ||
249
+ result.reason === "no_pending" ||
250
+ result.reason === "too_many_attempts"
251
+ ) {
252
+ showError("That code is no longer valid. Send a new one.");
253
+ form.hidden = true;
254
+ sendBtn.hidden = false;
255
+ } else if (result.reason === "mismatch") {
256
+ showError("The two passwords don't match.");
257
+ } else if (result.reason === "invalid_password") {
258
+ showError(result.message || "That password isn't allowed.");
259
+ } else {
260
+ showError("Couldn't reset the password. Try again.");
261
+ }
262
+ } catch {
263
+ showError("Couldn't reach the server. Try again.");
264
+ }
265
+ });
266
+ })();
267
+ </script>
68
268
  </body>
69
269
  </html>
package/views/setup.ejs CHANGED
@@ -50,15 +50,27 @@
50
50
 
51
51
  <label class="field">
52
52
  <span>Password</span>
53
- <input type="password" name="password" autocomplete="new-password"
54
- placeholder="At least <%= minLength %> characters"
55
- minlength="<%= minLength %>" <%= needsCode ? "" : "autofocus" %> required />
53
+ <div class="input-wrap">
54
+ <input type="password" name="password" autocomplete="new-password"
55
+ placeholder="At least <%= minLength %> characters"
56
+ minlength="<%= minLength %>" <%= needsCode ? "" : "autofocus" %> required />
57
+ <button type="button" class="eye-btn" aria-label="Show password">
58
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
59
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
60
+ </button>
61
+ </div>
56
62
  </label>
57
63
 
58
64
  <label class="field">
59
65
  <span>Repeat password</span>
60
- <input type="password" name="confirm" autocomplete="new-password"
61
- placeholder="Type it again" minlength="<%= minLength %>" required />
66
+ <div class="input-wrap">
67
+ <input type="password" name="confirm" autocomplete="new-password"
68
+ placeholder="Type it again" minlength="<%= minLength %>" required />
69
+ <button type="button" class="eye-btn" aria-label="Show password">
70
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
71
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
72
+ </button>
73
+ </div>
62
74
  </label>
63
75
 
64
76
  <button type="submit" class="btn btn-primary auth-submit">
@@ -75,5 +87,7 @@
75
87
  </div>
76
88
  </section>
77
89
  </main>
90
+
91
+ <script src="/gateway.js"></script>
78
92
  </body>
79
93
  </html>