Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/decode-wa-message.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.decodeMessageStanza = void 0;3const boom_1 = require("@hapi/boom");4const WAProto_1 = require("../../WAProto");5const WABinary_1 = require("../WABinary");6const generics_1 = require("./generics");7const signal_1 = require("./signal");8const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node';9const decodeMessageStanza = (stanza, auth) => {10let msgType;11let chatId;12let author;13const msgId = stanza.attrs.id;14const from = stanza.attrs.from;15const participant = stanza.attrs.participant;16const recipient = stanza.attrs.recipient;17const isMe = (jid) => (0, WABinary_1.areJidsSameUser)(jid, auth.creds.me.id);18if ((0, WABinary_1.isJidUser)(from)) {19if (recipient) {20if (!isMe(from)) {21throw new boom_1.Boom('receipient present, but msg not from me', { data: stanza });22}23chatId = recipient;24}25else {26chatId = from;27}28msgType = 'chat';29author = from;30}31else if ((0, WABinary_1.isJidGroup)(from)) {32if (!participant) {33throw new boom_1.Boom('No participant in group message');34}35msgType = 'group';36author = participant;37chatId = from;38}39else if ((0, WABinary_1.isJidBroadcast)(from)) {40if (!participant) {41throw new boom_1.Boom('No participant in group message');42}43const isParticipantMe = isMe(participant);44if ((0, WABinary_1.isJidStatusBroadcast)(from)) {45msgType = isParticipantMe ? 'direct_peer_status' : 'other_status';46}47else {48msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast';49}50chatId = from;51author = participant;52}53else {54throw new boom_1.Boom('Unknown message type', { data: stanza });55}56const sender = msgType === 'chat' ? author : chatId;57const fromMe = isMe(stanza.attrs.participant || stanza.attrs.from);58const pushname = stanza.attrs.notify;59const key = {60remoteJid: chatId,61fromMe,62id: msgId,63participant64};65const fullMessage = {66key,67messageTimestamp: +stanza.attrs.t,68pushName: pushname69};70if (key.fromMe) {71fullMessage.status = WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK;72}73return {74fullMessage,75category: stanza.attrs.category,76author,77decryptionTask: (async () => {78var _a;79let decryptables = 0;80if (Array.isArray(stanza.content)) {81for (const { tag, attrs, content } of stanza.content) {82if (tag === 'verified_name' && content instanceof Uint8Array) {83const cert = WAProto_1.proto.VerifiedNameCertificate.decode(content);84const details = WAProto_1.proto.VerifiedNameCertificate.Details.decode(cert.details);85fullMessage.verifiedBizName = details.verifiedName;86}87if (tag !== 'enc') {88continue;89}90if (!(content instanceof Uint8Array)) {91continue;92}93decryptables += 1;94let msgBuffer;95try {96const e2eType = attrs.type;97switch (e2eType) {98case 'skmsg':99msgBuffer = await (0, signal_1.decryptGroupSignalProto)(sender, author, content, auth);100break;101case 'pkmsg':102case 'msg':103const user = (0, WABinary_1.isJidUser)(sender) ? sender : author;104msgBuffer = await (0, signal_1.decryptSignalProto)(user, e2eType, content, auth);105break;106default:107throw new Error(`Unknown e2e type: ${e2eType}`);108}109let msg = WAProto_1.proto.Message.decode((0, generics_1.unpadRandomMax16)(msgBuffer));110msg = ((_a = msg.deviceSentMessage) === null || _a === void 0 ? void 0 : _a.message) || msg;111if (msg.senderKeyDistributionMessage) {112await (0, signal_1.processSenderKeyMessage)(author, msg.senderKeyDistributionMessage, auth);113}114if (fullMessage.message) {115Object.assign(fullMessage.message, msg);116}117else {118fullMessage.message = msg;119}120}121catch (error) {122fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;123fullMessage.messageStubParameters = [error.message];124}125}126}127// if nothing was found to decrypt128if (!decryptables) {129fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT;130fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];131}132})()133};134};135exports.decodeMessageStanza = decodeMessageStanza;136137138