@gakr-gakr/whatsapp 0.1.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.
- package/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +67 -0
- package/auth-presence.ts +80 -0
- package/autobot.plugin.json +23 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/doctor-contract-api.ts +8 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +76 -0
- package/runtime-api.ts +84 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +17 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.ts +176 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.ts +494 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.ts +332 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +218 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/listener-log.ts +28 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.ts +333 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.ts +584 -0
- package/src/auto-reply/monitor/runtime-api.ts +36 -0
- package/src/auto-reply/monitor/status-reaction.ts +108 -0
- package/src/auto-reply/monitor-state.ts +114 -0
- package/src/auto-reply/monitor.ts +720 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +62 -0
- package/src/auto-reply.impl.ts +6 -0
- package/src/auto-reply.ts +1 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.ts +85 -0
- package/src/channel-outbound.ts +87 -0
- package/src/channel-react-action.runtime.ts +10 -0
- package/src/channel-react-action.ts +247 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.ts +32 -0
- package/src/channel.ts +356 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.ts +680 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +71 -0
- package/src/directory-config.ts +40 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.ts +56 -0
- package/src/document-filename.ts +17 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.ts +42 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.ts +187 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.ts +484 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.ts +128 -0
- package/src/inbound/monitor.ts +1042 -0
- package/src/inbound/outbound-mentions.ts +260 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.ts +203 -0
- package/src/inbound/send-result.ts +109 -0
- package/src/inbound/types.ts +107 -0
- package/src/inbound-policy.ts +215 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.ts +542 -0
- package/src/login.ts +83 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
- package/src/normalize-target.ts +148 -0
- package/src/normalize.ts +8 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.ts +256 -0
- package/src/outbound-media-contract.ts +307 -0
- package/src/outbound-media.runtime.ts +41 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/qa-driver.runtime.ts +189 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.ts +24 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.ts +58 -0
- package/src/runtime-api.ts +59 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.ts +342 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +32 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.ts +327 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +450 -0
- package/src/setup-surface.ts +71 -0
- package/src/setup-test-helpers.ts +217 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +55 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +221 -0
- package/src/text-runtime.ts +18 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import fsSync from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { formatCliCommand } from "autobot/plugin-sdk/cli-runtime";
|
|
5
|
+
import { DEFAULT_ACCOUNT_ID } from "autobot/plugin-sdk/routing";
|
|
6
|
+
import { info, success } from "autobot/plugin-sdk/runtime-env";
|
|
7
|
+
import { getChildLogger } from "autobot/plugin-sdk/runtime-env";
|
|
8
|
+
import { defaultRuntime, type RuntimeEnv } from "autobot/plugin-sdk/runtime-env";
|
|
9
|
+
import { replaceFileAtomic } from "autobot/plugin-sdk/security-runtime";
|
|
10
|
+
import { resolveOAuthDir } from "./auth-store.runtime.js";
|
|
11
|
+
import { hasWebCredsSync, resolveWebCredsBackupPath, resolveWebCredsPath } from "./creds-files.js";
|
|
12
|
+
import {
|
|
13
|
+
waitForCredsSaveQueueWithTimeout,
|
|
14
|
+
type CredsQueueWaitResult,
|
|
15
|
+
} from "./creds-persistence.js";
|
|
16
|
+
import { resolveComparableIdentity, type WhatsAppSelfIdentity } from "./identity.js";
|
|
17
|
+
import { resolveUserPath, type WebChannel } from "./text-runtime.js";
|
|
18
|
+
export { hasWebCredsSync, resolveWebCredsBackupPath, resolveWebCredsPath };
|
|
19
|
+
|
|
20
|
+
export const WHATSAPP_AUTH_UNSTABLE_CODE = "whatsapp-auth-unstable";
|
|
21
|
+
|
|
22
|
+
const authStoreLogger = getChildLogger({ module: "web-auth-store" });
|
|
23
|
+
const emptyWebSelfId = () => ({ e164: null, jid: null, lid: null }) as const;
|
|
24
|
+
export type WhatsAppWebAuthState = "linked" | "not-linked" | "unstable";
|
|
25
|
+
|
|
26
|
+
export class WhatsAppAuthUnstableError extends Error {
|
|
27
|
+
readonly code = WHATSAPP_AUTH_UNSTABLE_CODE;
|
|
28
|
+
|
|
29
|
+
constructor(message = "WhatsApp auth state is still stabilizing; retry shortly.") {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "WhatsAppAuthUnstableError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function resolveDefaultWebAuthDir(): string {
|
|
36
|
+
return path.join(resolveOAuthDir(), "whatsapp", DEFAULT_ACCOUNT_ID);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const WA_WEB_AUTH_DIR = resolveDefaultWebAuthDir();
|
|
40
|
+
|
|
41
|
+
export function readCredsJsonRaw(filePath: string): string | null {
|
|
42
|
+
try {
|
|
43
|
+
if (!fsSync.existsSync(filePath)) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const stats = fsSync.statSync(filePath);
|
|
47
|
+
if (!stats.isFile() || stats.size <= 1) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return fsSync.readFileSync(filePath, "utf-8");
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function waitForWebAuthBarrier(
|
|
57
|
+
authDir: string,
|
|
58
|
+
context: string,
|
|
59
|
+
): Promise<CredsQueueWaitResult> {
|
|
60
|
+
const result = await waitForCredsSaveQueueWithTimeout(authDir);
|
|
61
|
+
if (result === "timed_out") {
|
|
62
|
+
authStoreLogger.warn(
|
|
63
|
+
{
|
|
64
|
+
authDir,
|
|
65
|
+
context,
|
|
66
|
+
},
|
|
67
|
+
"timed out waiting for queued WhatsApp creds save before auth read",
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function restoreCredsFromBackupIfNeeded(authDir: string): Promise<boolean> {
|
|
74
|
+
const logger = getChildLogger({ module: "web-session" });
|
|
75
|
+
try {
|
|
76
|
+
const credsPath = resolveWebCredsPath(authDir);
|
|
77
|
+
const backupPath = resolveWebCredsBackupPath(authDir);
|
|
78
|
+
const raw = readCredsJsonRaw(credsPath);
|
|
79
|
+
if (raw) {
|
|
80
|
+
// Validate that creds.json is parseable.
|
|
81
|
+
JSON.parse(raw);
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const backupRaw = readCredsJsonRaw(backupPath);
|
|
86
|
+
if (!backupRaw) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const backupStats = await fs.lstat(backupPath).catch(() => null);
|
|
90
|
+
if (!backupStats?.isFile()) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Ensure backup is parseable before restoring.
|
|
95
|
+
JSON.parse(backupRaw);
|
|
96
|
+
await replaceFileAtomic({
|
|
97
|
+
filePath: credsPath,
|
|
98
|
+
content: backupRaw,
|
|
99
|
+
dirMode: 0o700,
|
|
100
|
+
mode: 0o600,
|
|
101
|
+
tempPrefix: ".creds.restore",
|
|
102
|
+
});
|
|
103
|
+
logger.warn({ credsPath }, "restored corrupted WhatsApp creds.json from backup");
|
|
104
|
+
return true;
|
|
105
|
+
} catch {
|
|
106
|
+
// ignore
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function webAuthExists(authDir: string = resolveDefaultWebAuthDir()) {
|
|
112
|
+
const resolvedAuthDir = resolveUserPath(authDir);
|
|
113
|
+
const credsPath = resolveWebCredsPath(resolvedAuthDir);
|
|
114
|
+
try {
|
|
115
|
+
await fs.access(resolvedAuthDir);
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const stats = await fs.stat(credsPath);
|
|
121
|
+
if (!stats.isFile() || stats.size <= 1) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const raw = await fs.readFile(credsPath, "utf-8");
|
|
125
|
+
JSON.parse(raw);
|
|
126
|
+
return true;
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function resolveWebAuthState(params: {
|
|
133
|
+
linked: boolean;
|
|
134
|
+
barrierResult: CredsQueueWaitResult;
|
|
135
|
+
}): WhatsAppWebAuthState {
|
|
136
|
+
if (params.barrierResult === "timed_out") {
|
|
137
|
+
return "unstable";
|
|
138
|
+
}
|
|
139
|
+
return params.linked ? "linked" : "not-linked";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function readWebAuthStateCore(
|
|
143
|
+
authDir: string,
|
|
144
|
+
context: string,
|
|
145
|
+
): Promise<{ authDir: string; linked: boolean; state: WhatsAppWebAuthState }> {
|
|
146
|
+
const resolvedAuthDir = resolveUserPath(authDir);
|
|
147
|
+
const barrierResult = await waitForWebAuthBarrier(resolvedAuthDir, context);
|
|
148
|
+
const linked = await webAuthExists(resolvedAuthDir);
|
|
149
|
+
return {
|
|
150
|
+
authDir: resolvedAuthDir,
|
|
151
|
+
linked,
|
|
152
|
+
state: resolveWebAuthState({ linked, barrierResult }),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function formatWhatsAppWebAuthStatusState(state: WhatsAppWebAuthState): string {
|
|
157
|
+
switch (state) {
|
|
158
|
+
case "linked":
|
|
159
|
+
return "linked";
|
|
160
|
+
case "not-linked":
|
|
161
|
+
return "not linked";
|
|
162
|
+
case "unstable":
|
|
163
|
+
return "auth stabilizing";
|
|
164
|
+
}
|
|
165
|
+
const exhaustive: never = state;
|
|
166
|
+
return exhaustive;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export async function readWebAuthState(
|
|
170
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
171
|
+
): Promise<WhatsAppWebAuthState> {
|
|
172
|
+
return (await readWebAuthStateCore(authDir, "readWebAuthState")).state;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function readWebAuthSnapshot(authDir: string = resolveDefaultWebAuthDir()) {
|
|
176
|
+
const auth = await readWebAuthStateCore(authDir, "readWebAuthSnapshot");
|
|
177
|
+
return {
|
|
178
|
+
state: auth.state,
|
|
179
|
+
authAgeMs: auth.state === "linked" ? getWebAuthAgeMs(auth.authDir) : null,
|
|
180
|
+
selfId: auth.state === "linked" ? readWebSelfId(auth.authDir) : emptyWebSelfId(),
|
|
181
|
+
} as const;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function readWebAuthExistsBestEffort(authDir: string = resolveDefaultWebAuthDir()) {
|
|
185
|
+
const state = await readWebAuthState(authDir);
|
|
186
|
+
return {
|
|
187
|
+
exists: state === "linked",
|
|
188
|
+
timedOut: state === "unstable",
|
|
189
|
+
} as const;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function readWebAuthExistsForDecision(
|
|
193
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
194
|
+
): Promise<{ outcome: "stable"; exists: boolean } | { outcome: "unstable" }> {
|
|
195
|
+
const state = await readWebAuthState(authDir);
|
|
196
|
+
if (state === "unstable") {
|
|
197
|
+
return { outcome: "unstable" };
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
outcome: "stable",
|
|
201
|
+
exists: state === "linked",
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function readWebAuthSnapshotBestEffort(authDir: string = resolveDefaultWebAuthDir()) {
|
|
206
|
+
const snapshot = await readWebAuthSnapshot(authDir);
|
|
207
|
+
return {
|
|
208
|
+
linked: snapshot.state === "linked",
|
|
209
|
+
timedOut: snapshot.state === "unstable",
|
|
210
|
+
authAgeMs: snapshot.authAgeMs,
|
|
211
|
+
selfId: snapshot.selfId,
|
|
212
|
+
} as const;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function isBaileysAuthFileName(name: string): boolean {
|
|
216
|
+
if (name === "oauth.json") {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
if (name === "creds.json" || name === "creds.json.bak") {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
if (!name.endsWith(".json")) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return /^(app-state-sync|session|sender-key|pre-key)-/.test(name);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function clearBaileysAuthFiles(authDir: string) {
|
|
229
|
+
const rootStats = await fs.lstat(authDir).catch(() => null);
|
|
230
|
+
if (!rootStats?.isDirectory() || rootStats.isSymbolicLink()) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const entries = await fs.readdir(authDir, { withFileTypes: true });
|
|
234
|
+
await Promise.all(
|
|
235
|
+
entries.map(async (entry) => {
|
|
236
|
+
if (!entry.isFile()) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (!isBaileysAuthFileName(entry.name)) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
await fs.rm(path.join(authDir, entry.name), { force: true });
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function shouldClearOnLogout(authDir: string, isLegacyAuthDir: boolean): Promise<boolean> {
|
|
248
|
+
try {
|
|
249
|
+
const stats = await fs.lstat(authDir);
|
|
250
|
+
if (!stats.isDirectory() || stats.isSymbolicLink()) {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
if (isLegacyAuthDir) {
|
|
254
|
+
const entries = await fs.readdir(authDir, { withFileTypes: true });
|
|
255
|
+
return entries.some((entry) => {
|
|
256
|
+
if (!entry.isFile()) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
return isBaileysAuthFileName(entry.name);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const credsStats = await fs.lstat(resolveWebCredsPath(authDir)).catch(() => null);
|
|
263
|
+
if (credsStats?.isFile()) {
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
const backupStats = await fs.lstat(resolveWebCredsBackupPath(authDir)).catch(() => null);
|
|
267
|
+
return backupStats?.isFile() === true;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
const codeValue =
|
|
270
|
+
error && typeof error === "object" && "code" in error
|
|
271
|
+
? (error as { code?: unknown }).code
|
|
272
|
+
: undefined;
|
|
273
|
+
const code = typeof codeValue === "string" ? codeValue : "";
|
|
274
|
+
return code !== "ENOENT";
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function isPathInsideDirectory(baseDir: string, targetPath: string): boolean {
|
|
279
|
+
const relativePath = path.relative(baseDir, targetPath);
|
|
280
|
+
return relativePath !== "" && !relativePath.startsWith("..") && !path.isAbsolute(relativePath);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
async function pathHasSymlinkComponent(baseDir: string, targetPath: string): Promise<boolean> {
|
|
284
|
+
const relativePath = path.relative(baseDir, targetPath);
|
|
285
|
+
let currentPath = baseDir;
|
|
286
|
+
for (const segment of relativePath.split(path.sep)) {
|
|
287
|
+
currentPath = path.join(currentPath, segment);
|
|
288
|
+
const stats = await fs.lstat(currentPath).catch(() => null);
|
|
289
|
+
if (!stats || stats.isSymbolicLink()) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
type WebAuthDirOwnership =
|
|
297
|
+
| { kind: "owned"; authDir: string }
|
|
298
|
+
| { kind: "unsafe-owned" }
|
|
299
|
+
| { kind: "external" };
|
|
300
|
+
|
|
301
|
+
async function isLegacyWebAuthDir(authDir: string): Promise<boolean> {
|
|
302
|
+
const legacyAuthDir = path.resolve(resolveOAuthDir());
|
|
303
|
+
const resolvedAuthDir = path.resolve(authDir);
|
|
304
|
+
if (resolvedAuthDir !== legacyAuthDir) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
const stats = await fs.lstat(resolvedAuthDir).catch(() => null);
|
|
308
|
+
return stats?.isDirectory() === true && !stats.isSymbolicLink();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function classifyWebAuthDirOwnership(authDir: string): Promise<WebAuthDirOwnership> {
|
|
312
|
+
const whatsappAuthBase = path.resolve(resolveOAuthDir(), "whatsapp");
|
|
313
|
+
const resolvedAuthDir = path.resolve(authDir);
|
|
314
|
+
if (!isPathInsideDirectory(whatsappAuthBase, resolvedAuthDir)) {
|
|
315
|
+
return { kind: "external" };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const [baseRealPath, authDirRealPath] = await Promise.all([
|
|
319
|
+
fs.realpath(whatsappAuthBase).catch(() => null),
|
|
320
|
+
fs.realpath(resolvedAuthDir).catch(() => null),
|
|
321
|
+
]);
|
|
322
|
+
if (!baseRealPath || !authDirRealPath) {
|
|
323
|
+
return { kind: "unsafe-owned" };
|
|
324
|
+
}
|
|
325
|
+
if (!isPathInsideDirectory(baseRealPath, authDirRealPath)) {
|
|
326
|
+
return { kind: "unsafe-owned" };
|
|
327
|
+
}
|
|
328
|
+
if (await pathHasSymlinkComponent(whatsappAuthBase, resolvedAuthDir)) {
|
|
329
|
+
return { kind: "unsafe-owned" };
|
|
330
|
+
}
|
|
331
|
+
return { kind: "owned", authDir: resolvedAuthDir };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export async function logoutWeb(params: {
|
|
335
|
+
authDir?: string;
|
|
336
|
+
isLegacyAuthDir?: boolean;
|
|
337
|
+
runtime?: RuntimeEnv;
|
|
338
|
+
}) {
|
|
339
|
+
const runtime = params.runtime ?? defaultRuntime;
|
|
340
|
+
const resolvedAuthDir = resolveUserPath(params.authDir ?? resolveDefaultWebAuthDir());
|
|
341
|
+
const barrierResult = await waitForWebAuthBarrier(resolvedAuthDir, "logoutWeb");
|
|
342
|
+
if (barrierResult === "timed_out") {
|
|
343
|
+
runtime.log(
|
|
344
|
+
info("WhatsApp auth state is still stabilizing; clearing cached credentials anyway."),
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
if (!(await shouldClearOnLogout(resolvedAuthDir, Boolean(params.isLegacyAuthDir)))) {
|
|
348
|
+
runtime.log(info("No WhatsApp Web session found; nothing to delete."));
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
if (params.isLegacyAuthDir) {
|
|
352
|
+
if (!(await isLegacyWebAuthDir(resolvedAuthDir))) {
|
|
353
|
+
runtime.log(
|
|
354
|
+
info("Skipped WhatsApp Web credential cleanup outside the managed legacy auth directory."),
|
|
355
|
+
);
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
await clearBaileysAuthFiles(resolvedAuthDir);
|
|
359
|
+
} else {
|
|
360
|
+
const ownership = await classifyWebAuthDirOwnership(resolvedAuthDir);
|
|
361
|
+
if (ownership.kind === "owned") {
|
|
362
|
+
await fs.rm(ownership.authDir, { recursive: true, force: true });
|
|
363
|
+
} else if (ownership.kind === "unsafe-owned") {
|
|
364
|
+
runtime.log(
|
|
365
|
+
info(
|
|
366
|
+
"Skipped WhatsApp Web credential cleanup because the auth directory crosses a symlink boundary.",
|
|
367
|
+
),
|
|
368
|
+
);
|
|
369
|
+
return false;
|
|
370
|
+
} else {
|
|
371
|
+
runtime.log(
|
|
372
|
+
info("Skipped WhatsApp Web credential cleanup outside the managed auth directory."),
|
|
373
|
+
);
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
runtime.log(success("Cleared WhatsApp Web credentials."));
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export function readWebSelfId(authDir: string = resolveDefaultWebAuthDir()) {
|
|
382
|
+
// Read the cached WhatsApp Web identity (jid + E.164) from disk if present.
|
|
383
|
+
try {
|
|
384
|
+
const credsPath = resolveWebCredsPath(resolveUserPath(authDir));
|
|
385
|
+
if (!fsSync.existsSync(credsPath)) {
|
|
386
|
+
return emptyWebSelfId();
|
|
387
|
+
}
|
|
388
|
+
const raw = fsSync.readFileSync(credsPath, "utf-8");
|
|
389
|
+
const parsed = JSON.parse(raw) as { me?: { id?: string; lid?: string } } | undefined;
|
|
390
|
+
const identity = resolveComparableIdentity(
|
|
391
|
+
{
|
|
392
|
+
jid: parsed?.me?.id ?? null,
|
|
393
|
+
lid: parsed?.me?.lid ?? null,
|
|
394
|
+
},
|
|
395
|
+
authDir,
|
|
396
|
+
);
|
|
397
|
+
return {
|
|
398
|
+
e164: identity.e164 ?? null,
|
|
399
|
+
jid: identity.jid ?? null,
|
|
400
|
+
lid: identity.lid ?? null,
|
|
401
|
+
} as const;
|
|
402
|
+
} catch {
|
|
403
|
+
return emptyWebSelfId();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export async function readWebSelfIdentity(
|
|
408
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
409
|
+
fallback?: { id?: string | null; lid?: string | null } | null,
|
|
410
|
+
): Promise<WhatsAppSelfIdentity> {
|
|
411
|
+
const resolvedAuthDir = resolveUserPath(authDir);
|
|
412
|
+
try {
|
|
413
|
+
const raw = await fs.readFile(resolveWebCredsPath(resolvedAuthDir), "utf-8");
|
|
414
|
+
const parsed = JSON.parse(raw) as { me?: { id?: string; lid?: string } } | undefined;
|
|
415
|
+
return resolveComparableIdentity(
|
|
416
|
+
{
|
|
417
|
+
jid: parsed?.me?.id ?? null,
|
|
418
|
+
lid: parsed?.me?.lid ?? null,
|
|
419
|
+
},
|
|
420
|
+
resolvedAuthDir,
|
|
421
|
+
);
|
|
422
|
+
} catch {
|
|
423
|
+
return resolveComparableIdentity(
|
|
424
|
+
{
|
|
425
|
+
jid: fallback?.id ?? null,
|
|
426
|
+
lid: fallback?.lid ?? null,
|
|
427
|
+
},
|
|
428
|
+
resolvedAuthDir,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export async function readWebSelfIdentityForDecision(
|
|
434
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
435
|
+
fallback?: { id?: string | null; lid?: string | null } | null,
|
|
436
|
+
): Promise<{ outcome: "stable"; identity: WhatsAppSelfIdentity } | { outcome: "unstable" }> {
|
|
437
|
+
const resolvedAuthDir = resolveUserPath(authDir);
|
|
438
|
+
const result = await waitForWebAuthBarrier(resolvedAuthDir, "readWebSelfIdentityForDecision");
|
|
439
|
+
if (result === "timed_out") {
|
|
440
|
+
return { outcome: "unstable" };
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
outcome: "stable",
|
|
444
|
+
identity: await readWebSelfIdentity(resolvedAuthDir, fallback),
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Return the age (in milliseconds) of the cached WhatsApp web auth state, or null when missing.
|
|
450
|
+
* Helpful for heartbeats/observability to spot stale credentials.
|
|
451
|
+
*/
|
|
452
|
+
export function getWebAuthAgeMs(authDir: string = resolveDefaultWebAuthDir()): number | null {
|
|
453
|
+
try {
|
|
454
|
+
const stats = fsSync.statSync(resolveWebCredsPath(resolveUserPath(authDir)));
|
|
455
|
+
return Date.now() - stats.mtimeMs;
|
|
456
|
+
} catch {
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export function logWebSelfId(
|
|
462
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
463
|
+
runtime: RuntimeEnv = defaultRuntime,
|
|
464
|
+
includeChannelPrefix = false,
|
|
465
|
+
) {
|
|
466
|
+
// Human-friendly log of the currently linked personal web session.
|
|
467
|
+
const { e164, jid, lid } = readWebSelfId(authDir);
|
|
468
|
+
const parts = [jid ? `jid ${jid}` : null, lid ? `lid ${lid}` : null].filter(
|
|
469
|
+
(value): value is string => Boolean(value),
|
|
470
|
+
);
|
|
471
|
+
const details =
|
|
472
|
+
e164 || parts.length > 0
|
|
473
|
+
? `${e164 ?? "unknown"}${parts.length > 0 ? ` (${parts.join(", ")})` : ""}`
|
|
474
|
+
: "unknown";
|
|
475
|
+
const prefix = includeChannelPrefix ? "Web Channel: " : "";
|
|
476
|
+
runtime.log(info(`${prefix}${details}`));
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export async function pickWebChannel(
|
|
480
|
+
pref: WebChannel | "auto",
|
|
481
|
+
authDir: string = resolveDefaultWebAuthDir(),
|
|
482
|
+
): Promise<WebChannel> {
|
|
483
|
+
const choice: WebChannel = pref === "auto" ? "web" : pref;
|
|
484
|
+
const auth = await readWebAuthExistsForDecision(authDir);
|
|
485
|
+
if (auth.outcome === "unstable") {
|
|
486
|
+
throw new WhatsAppAuthUnstableError();
|
|
487
|
+
}
|
|
488
|
+
if (!auth.exists) {
|
|
489
|
+
throw new Error(
|
|
490
|
+
`No WhatsApp Web session found. Run \`${formatCliCommand("autobot channels login --channel whatsapp --verbose")}\` to link.`,
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
return choice;
|
|
494
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
evaluateSessionFreshness,
|
|
3
|
+
loadSessionStore,
|
|
4
|
+
resolveSessionKey,
|
|
5
|
+
resolveSessionResetPolicy,
|
|
6
|
+
resolveSessionResetType,
|
|
7
|
+
resolveStorePath,
|
|
8
|
+
resolveThreadFlag,
|
|
9
|
+
resolveChannelResetConfig,
|
|
10
|
+
updateLastRoute,
|
|
11
|
+
} from "autobot/plugin-sdk/session-store-runtime";
|
|
12
|
+
export {
|
|
13
|
+
getRuntimeConfig,
|
|
14
|
+
getRuntimeConfigSourceSnapshot,
|
|
15
|
+
} from "autobot/plugin-sdk/runtime-config-snapshot";
|
|
16
|
+
export { resolveChannelContextVisibilityMode } from "autobot/plugin-sdk/context-visibility-runtime";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_WEB_MEDIA_BYTES = 5 * 1024 * 1024;
|