Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/messages-recv.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.makeMessagesRecvSocket = void 0;3const WAProto_1 = require("../../WAProto");4const Defaults_1 = require("../Defaults");5const Types_1 = require("../Types");6const Utils_1 = require("../Utils");7const make_mutex_1 = require("../Utils/make-mutex");8const process_message_1 = require("../Utils/process-message");9const WABinary_1 = require("../WABinary");10const groups_1 = require("./groups");11const messages_send_1 = require("./messages-send");12const makeMessagesRecvSocket = (config) => {13const { logger, retryRequestDelayMs, getMessage } = config;14const sock = (0, messages_send_1.makeMessagesSocket)(config);15const { ev, authState, ws, processingMutex, upsertMessage, resyncAppState, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, } = sock;16/** this mutex ensures that each retryRequest will wait for the previous one to finish */17const retryMutex = (0, make_mutex_1.makeMutex)();18const msgRetryMap = config.msgRetryCounterMap || {};19const callOfferData = {};20let sendActiveReceipts = false;21const sendMessageAck = async ({ tag, attrs }) => {22const stanza = {23tag: 'ack',24attrs: {25id: attrs.id,26to: attrs.from,27class: tag,28}29};30if (!!attrs.participant) {31stanza.attrs.participant = attrs.participant;32}33if (!!attrs.recipient) {34stanza.attrs.recipient = attrs.recipient;35}36if (tag !== 'message' && attrs.type) {37stanza.attrs.type = attrs.type;38}39logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');40await sendNode(stanza);41};42const sendRetryRequest = async (node, forceIncludeKeys = false) => {43const msgId = node.attrs.id;44let retryCount = msgRetryMap[msgId] || 0;45if (retryCount >= 5) {46logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');47delete msgRetryMap[msgId];48return;49}50retryCount += 1;51msgRetryMap[msgId] = retryCount;52const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;53const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);54await authState.keys.transaction(async () => {55const receipt = {56tag: 'receipt',57attrs: {58id: msgId,59type: 'retry',60to: node.attrs.from61},62content: [63{64tag: 'retry',65attrs: {66count: retryCount.toString(),67id: node.attrs.id,68t: node.attrs.t,69v: '1'70}71},72{73tag: 'registration',74attrs: {},75content: (0, Utils_1.encodeBigEndian)(authState.creds.registrationId)76}77]78};79if (node.attrs.recipient) {80receipt.attrs.recipient = node.attrs.recipient;81}82if (node.attrs.participant) {83receipt.attrs.participant = node.attrs.participant;84}85if (retryCount > 1 || forceIncludeKeys) {86const { update, preKeys } = await (0, Utils_1.getNextPreKeys)(authState, 1);87const [keyId] = Object.keys(preKeys);88const key = preKeys[+keyId];89const content = receipt.content;90content.push({91tag: 'keys',92attrs: {},93content: [94{ tag: 'type', attrs: {}, content: Buffer.from(Defaults_1.KEY_BUNDLE_TYPE) },95{ tag: 'identity', attrs: {}, content: identityKey.public },96(0, Utils_1.xmppPreKey)(key, +keyId),97(0, Utils_1.xmppSignedPreKey)(signedPreKey),98{ tag: 'device-identity', attrs: {}, content: deviceIdentity }99]100});101ev.emit('creds.update', update);102}103await sendNode(receipt);104logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt');105});106};107const handleEncryptNotification = async (node) => {108const from = node.attrs.from;109if (from === WABinary_1.S_WHATSAPP_NET) {110const countChild = (0, WABinary_1.getBinaryNodeChild)(node, 'count');111const count = +countChild.attrs.value;112const shouldUploadMorePreKeys = count < Defaults_1.MIN_PREKEY_COUNT;113logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count');114if (shouldUploadMorePreKeys) {115await uploadPreKeys();116}117}118else {119const identityNode = (0, WABinary_1.getBinaryNodeChild)(node, 'identity');120if (identityNode) {121logger.info({ jid: from }, 'identity changed');122// not handling right now123// signal will override new identity anyway124}125else {126logger.info({ node }, 'unknown encrypt notification');127}128}129};130const processNotification = async (node) => {131const result = {};132const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);133const nodeType = node.attrs.type;134if (nodeType === 'w:gp2') {135switch (child === null || child === void 0 ? void 0 : child.tag) {136case 'create':137const metadata = (0, groups_1.extractGroupMetadata)(child);138result.messageStubType = Types_1.WAMessageStubType.GROUP_CREATE;139result.messageStubParameters = [metadata.subject];140result.key = { participant: metadata.owner };141ev.emit('chats.upsert', [{142id: metadata.id,143name: metadata.subject,144conversationTimestamp: metadata.creation,145}]);146ev.emit('groups.upsert', [metadata]);147break;148case 'ephemeral':149case 'not_ephemeral':150result.message = {151protocolMessage: {152type: WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,153ephemeralExpiration: +(child.attrs.expiration || 0)154}155};156break;157case 'promote':158case 'demote':159case 'remove':160case 'add':161case 'leave':162const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;163result.messageStubType = Types_1.WAMessageStubType[stubType];164const participants = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);165if (participants.length === 1 &&166// if recv. "remove" message and sender removed themselves167// mark as left168(0, WABinary_1.areJidsSameUser)(participants[0], node.attrs.participant) &&169child.tag === 'remove') {170result.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;171}172result.messageStubParameters = participants;173break;174case 'subject':175result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;176result.messageStubParameters = [child.attrs.subject];177break;178case 'announcement':179case 'not_announcement':180result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;181result.messageStubParameters = [(child.tag === 'announcement') ? 'on' : 'off'];182break;183case 'locked':184case 'unlocked':185result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT;186result.messageStubParameters = [(child.tag === 'locked') ? 'on' : 'off'];187break;188}189}190else if (nodeType === 'mediaretry') {191const event = (0, Utils_1.decodeMediaRetryNode)(node);192ev.emit('messages.media-update', [event]);193}194else if (nodeType === 'encrypt') {195await handleEncryptNotification(node);196}197else if (nodeType === 'devices') {198const devices = (0, WABinary_1.getBinaryNodeChildren)(child, 'device');199if ((0, WABinary_1.areJidsSameUser)(child.attrs.jid, authState.creds.me.id)) {200const deviceJids = devices.map(d => d.attrs.jid);201logger.info({ deviceJids }, 'got my own devices');202}203}204else if (nodeType === 'server_sync') {205const update = (0, WABinary_1.getBinaryNodeChild)(node, 'collection');206if (update) {207const name = update.attrs.name;208await resyncAppState([name], undefined);209}210}211if (Object.keys(result).length) {212return result;213}214};215const willSendMessageAgain = (id, participant) => {216const key = `${id}:${participant}`;217const retryCount = msgRetryMap[key] || 0;218return retryCount < 5;219};220const updateSendMessageAgainCount = (id, participant) => {221const key = `${id}:${participant}`;222msgRetryMap[key] = (msgRetryMap[key] || 0) + 1;223};224const sendMessagesAgain = async (key, ids, retryNode) => {225var _a;226const msgs = await Promise.all(ids.map(id => getMessage({ ...key, id })));227const remoteJid = key.remoteJid;228const participant = key.participant || remoteJid;229// if it's the primary jid sending the request230// just re-send the message to everyone231// prevents the first message decryption failure232const sendToAll = !((_a = (0, WABinary_1.jidDecode)(participant)) === null || _a === void 0 ? void 0 : _a.device);233await assertSessions([participant], true);234if ((0, WABinary_1.isJidGroup)(remoteJid)) {235await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } });236}237logger.debug({ participant, sendToAll }, 'forced new session for retry recp');238for (let i = 0; i < msgs.length; i++) {239const msg = msgs[i];240if (msg) {241updateSendMessageAgainCount(ids[i], participant);242const msgRelayOpts = { messageId: ids[i] };243if (sendToAll) {244msgRelayOpts.useUserDevicesCache = false;245}246else {247msgRelayOpts.participant = {248jid: participant,249count: +retryNode.attrs.count250};251}252await relayMessage(key.remoteJid, msg, msgRelayOpts);253}254else {255logger.debug({ jid: key.remoteJid, id: ids[i] }, 'recv retry request, but message not available');256}257}258};259const handleReceipt = async (node) => {260var _a;261const { attrs, content } = node;262const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(attrs.participant || attrs.from, (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);263const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? attrs.from : attrs.recipient;264const fromMe = !attrs.recipient || (attrs.type === 'retry' && isNodeFromMe);265const ids = [attrs.id];266if (Array.isArray(content)) {267const items = (0, WABinary_1.getBinaryNodeChildren)(content[0], 'item');268ids.push(...items.map(i => i.attrs.id));269}270const key = {271remoteJid,272id: '',273fromMe,274participant: attrs.participant275};276await Promise.all([277processingMutex.mutex(async () => {278const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);279if (typeof status !== 'undefined' &&280(281// basically, we only want to know when a message from us has been delivered to/read by the other person282// or another device of ours has read some messages283status > WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ||284!isNodeFromMe)) {285if ((0, WABinary_1.isJidGroup)(remoteJid)) {286if (attrs.participant) {287const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';288ev.emit('message-receipt.update', ids.map(id => ({289key: { ...key, id },290receipt: {291userJid: (0, WABinary_1.jidNormalizedUser)(attrs.participant),292[updateKey]: +attrs.t293}294})));295}296}297else {298ev.emit('messages.update', ids.map(id => ({299key: { ...key, id },300update: { status }301})));302}303}304if (attrs.type === 'retry') {305// correctly set who is asking for the retry306key.participant = key.participant || attrs.from;307const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');308if (willSendMessageAgain(ids[0], key.participant)) {309if (key.fromMe) {310try {311logger.debug({ attrs, key }, 'recv retry request');312await sendMessagesAgain(key, ids, retryNode);313}314catch (error) {315logger.error({ key, ids, trace: error.stack }, 'error in sending message again');316}317}318else {319logger.info({ attrs, key }, 'recv retry for not fromMe message');320}321}322else {323logger.info({ attrs, key }, 'will not send message again, as sent too many times');324}325}326}),327sendMessageAck(node)328]);329};330const handleNotification = async (node) => {331const remoteJid = node.attrs.from;332await Promise.all([333processingMutex.mutex(async () => {334const msg = await processNotification(node);335if (msg) {336const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);337msg.key = {338remoteJid,339fromMe,340participant: node.attrs.participant,341id: node.attrs.id,342...(msg.key || {})343};344msg.participant = node.attrs.participant;345msg.messageTimestamp = +node.attrs.t;346const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);347await upsertMessage(fullMsg, 'append');348}349}),350sendMessageAck(node)351]);352};353const handleMessage = async (node) => {354const { fullMessage: msg, category, author, decryptionTask } = (0, Utils_1.decodeMessageStanza)(node, authState);355await Promise.all([356processingMutex.mutex(async () => {357await decryptionTask;358// message failed to decrypt359if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {360logger.error({ key: msg.key, params: msg.messageStubParameters }, 'failure in decrypting message');361retryMutex.mutex(async () => {362if (ws.readyState === ws.OPEN) {363const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');364await sendRetryRequest(node, !encNode);365if (retryRequestDelayMs) {366await (0, Utils_1.delay)(retryRequestDelayMs);367}368}369else {370logger.debug({ node }, 'connection closed, ignoring retry req');371}372});373}374else {375// no type in the receipt => message delivered376let type = undefined;377let participant = msg.key.participant;378if (category === 'peer') { // special peer message379type = 'peer_msg';380}381else if (msg.key.fromMe) { // message was sent by us from a different device382type = 'sender';383// need to specially handle this case384if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {385participant = author;386}387}388else if (!sendActiveReceipts) {389type = 'inactive';390}391await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);392// send ack for history message393const isAnyHistoryMsg = (0, Utils_1.isHistoryMsg)(msg.message);394if (isAnyHistoryMsg) {395const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);396await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');397}398}399(0, process_message_1.cleanMessage)(msg, authState.creds.me.id);400await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');401}),402sendMessageAck(node)403]);404};405const handleCall = async (node) => {406const { attrs } = node;407const [infoChild] = (0, WABinary_1.getAllBinaryNodeChildren)(node);408const callId = infoChild.attrs['call-id'];409const from = infoChild.attrs.from || infoChild.attrs['call-creator'];410const status = (0, Utils_1.getCallStatusFromNode)(infoChild);411const call = {412chatId: attrs.from,413from,414id: callId,415date: new Date(+attrs.t * 1000),416offline: !!attrs.offline,417status,418};419if (status === 'offer') {420call.isVideo = !!(0, WABinary_1.getBinaryNodeChild)(infoChild, 'video');421call.isGroup = infoChild.attrs.type === 'group';422callOfferData[call.id] = call;423}424// use existing call info to populate this event425if (callOfferData[call.id]) {426call.isVideo = callOfferData[call.id].isVideo;427call.isGroup = callOfferData[call.id].isGroup;428}429// delete data once call has ended430if (status === 'reject' || status === 'accept' || status === 'timeout') {431delete callOfferData[call.id];432}433ev.emit('call', [call]);434await sendMessageAck(node);435};436const handleBadAck = async ({ attrs }) => {437// current hypothesis is that if pash is sent in the ack438// it means -- the message hasn't reached all devices yet439// we'll retry sending the message here440if (attrs.phash) {441logger.info({ attrs }, 'received phash in ack, resending message...');442const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id };443const msg = await getMessage(key);444if (msg) {445await relayMessage(key.remoteJid, msg, { messageId: key.id, useUserDevicesCache: false });446}447else {448logger.warn({ attrs }, 'could not send message again, as it was not found');449}450}451};452const flushBufferIfLastOfflineNode = (node, identifier, exec) => {453const task = exec(node)454.catch(err => onUnexpectedError(err, identifier));455const offline = node.attrs.offline;456if (offline) {457ev.processInBuffer(task);458}459};460// called when all offline notifs are handled461ws.on('CB:ib,,offline', async (node) => {462const child = (0, WABinary_1.getBinaryNodeChild)(node, 'offline');463const offlineNotifs = +((child === null || child === void 0 ? void 0 : child.attrs.count) || 0);464logger.info(`handled ${offlineNotifs} offline messages/notifications`);465await ev.flush();466ev.emit('connection.update', { receivedPendingNotifications: true });467});468// recv a message469ws.on('CB:message', (node) => {470flushBufferIfLastOfflineNode(node, 'processing message', handleMessage);471});472ws.on('CB:call', async (node) => {473flushBufferIfLastOfflineNode(node, 'handling call', handleCall);474});475ws.on('CB:receipt', node => {476flushBufferIfLastOfflineNode(node, 'handling receipt', handleReceipt);477});478ws.on('CB:notification', async (node) => {479flushBufferIfLastOfflineNode(node, 'handling notification', handleNotification);480});481ws.on('CB:ack,class:message', (node) => {482handleBadAck(node)483.catch(error => onUnexpectedError(error, 'handling bad ack'));484});485ev.on('call', ([call]) => {486// missed call + group call notification message generation487if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {488const msg = {489key: {490remoteJid: call.chatId,491id: call.id,492fromMe: false493},494messageTimestamp: (0, Utils_1.unixTimestampSeconds)(call.date),495};496if (call.status === 'timeout') {497if (call.isGroup) {498msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE;499}500else {501msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_VOICE;502}503}504else {505msg.message = { call: { callKey: Buffer.from(call.id) } };506}507const protoMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);508upsertMessage(protoMsg, call.offline ? 'append' : 'notify');509}510});511ev.on('connection.update', ({ isOnline }) => {512if (typeof isOnline !== 'undefined') {513sendActiveReceipts = isOnline;514logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);515}516});517return {518...sock,519sendMessageAck,520sendRetryRequest521};522};523exports.makeMessagesRecvSocket = makeMessagesRecvSocket;524525526