Files
openclaw/extensions/whatsapp/src/vcard.ts
scoootscooob 16505718e8 refactor: move WhatsApp channel implementation to extensions/ (#45725)
* refactor: move WhatsApp channel from src/web/ to extensions/whatsapp/

Move all WhatsApp implementation code (77 source/test files + 9 channel
plugin files) from src/web/ and src/channels/plugins/*/whatsapp* to
extensions/whatsapp/src/.

- Leave thin re-export shims at all original locations so cross-cutting
  imports continue to resolve
- Update plugin-sdk/whatsapp.ts to only re-export generic framework
  utilities; channel-specific functions imported locally by the extension
- Update vi.mock paths in 15 cross-cutting test files
- Rename outbound.ts -> send.ts to match extension naming conventions
  and avoid false positive in cfg-threading guard test
- Widen tsconfig.plugin-sdk.dts.json rootDir to support shim->extension
  cross-directory references

Part of the core-channels-to-extensions migration (PR 6/10).

* style: format WhatsApp extension files

* fix: correct stale import paths in WhatsApp extension tests

Fix vi.importActual, test mock, and hardcoded source paths that weren't
updated during the file move:
- media.test.ts: vi.importActual path
- onboarding.test.ts: vi.importActual path
- test-helpers.ts: test/mocks/baileys.js path
- monitor-inbox.test-harness.ts: incomplete media/store mock
- login.test.ts: hardcoded source file path
- message-action-runner.media.test.ts: vi.mock/importActual path
2026-03-14 02:44:55 -07:00

83 lines
2.0 KiB
TypeScript

type ParsedVcard = {
name?: string;
phones: string[];
};
const ALLOWED_VCARD_KEYS = new Set(["FN", "N", "TEL"]);
export function parseVcard(vcard?: string): ParsedVcard {
if (!vcard) {
return { phones: [] };
}
const lines = vcard.split(/\r?\n/);
let nameFromN: string | undefined;
let nameFromFn: string | undefined;
const phones: string[] = [];
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line) {
continue;
}
const colonIndex = line.indexOf(":");
if (colonIndex === -1) {
continue;
}
const key = line.slice(0, colonIndex).toUpperCase();
const rawValue = line.slice(colonIndex + 1).trim();
if (!rawValue) {
continue;
}
const baseKey = normalizeVcardKey(key);
if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) {
continue;
}
const value = cleanVcardValue(rawValue);
if (!value) {
continue;
}
if (baseKey === "FN" && !nameFromFn) {
nameFromFn = normalizeVcardName(value);
continue;
}
if (baseKey === "N" && !nameFromN) {
nameFromN = normalizeVcardName(value);
continue;
}
if (baseKey === "TEL") {
const phone = normalizeVcardPhone(value);
if (phone) {
phones.push(phone);
}
}
}
return { name: nameFromFn ?? nameFromN, phones };
}
function normalizeVcardKey(key: string): string | undefined {
const [primary] = key.split(";");
if (!primary) {
return undefined;
}
const segments = primary.split(".");
return segments[segments.length - 1] || undefined;
}
function cleanVcardValue(value: string): string {
return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
}
function normalizeVcardName(value: string): string {
return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
}
function normalizeVcardPhone(value: string): string {
const trimmed = value.trim();
if (!trimmed) {
return "";
}
if (trimmed.toLowerCase().startsWith("tel:")) {
return trimmed.slice(4).trim();
}
return trimmed;
}