diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index d5772020..a67d9dba 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -1600,6 +1600,38 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } } + /** + * Sanitizes caller phone number to fix known decoder bugs + * + * Issue: Brazilian landline numbers (12 digits) incorrectly get a trailing zero + * Example: 551738025555 → 5517380255550 (off-by-one error) + * + * This helper removes the trailing zero if: + * - Number is 13 digits (incorrect length) + * - Starts with '55' (Brazil country code) + * + * @param pn - Raw phone number from caller_pn attribute + * @returns Sanitized phone number + */ + const sanitizeCallerPn = (pn: string | undefined): string | undefined => { + if (!pn) { + return undefined + } + + // Fix Brazilian landline off-by-one bug + // Remove trailing zero if 13 digits and starts with Brazil code + if (pn.length === 13 && pn.startsWith('55')) { + const sanitized = pn.slice(0, -1) + logger.debug( + { original: pn, sanitized }, + 'Call: Sanitized Brazilian landline number (removed trailing zero)' + ) + return sanitized + } + + return pn + } + const handleCall = async (node: BinaryNode) => { const { attrs } = node const [infoChild] = getAllBinaryNodeChildren(node) @@ -1625,6 +1657,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { call.isVideo = !!getBinaryNodeChild(infoChild, 'video') call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid'] call.groupJid = infoChild.attrs['group-jid'] + // Extract and sanitize caller phone number + call.callerPn = sanitizeCallerPn(infoChild.attrs['caller_pn']) await callOfferCache.set(call.id, call) } @@ -1634,6 +1668,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { if (existingCall) { call.isVideo = existingCall.isVideo call.isGroup = existingCall.isGroup + call.groupJid = existingCall.groupJid + // Preserve callerPn across call state updates + call.callerPn = call.callerPn || existingCall.callerPn } // delete data once call has ended diff --git a/src/Types/Call.ts b/src/Types/Call.ts index 80972f62..83fe08b5 100644 --- a/src/Types/Call.ts +++ b/src/Types/Call.ts @@ -11,4 +11,6 @@ export type WACallEvent = { status: WACallUpdateType offline: boolean latencyMs?: number + /** Phone number of the caller (sanitized to fix Brazilian landline bug) */ + callerPn?: string }