Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/decode-wa-message.js
2593 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 = 'No message found';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) => WABinary_1.areJidsSameUser(jid, auth.creds.me.id);18if (WABinary_1.isJidUser(from)) {19if (recipient) {20if (!isMe(from)) {21throw new boom_1.Boom('');22}23chatId = recipient;24}25else {26chatId = from;27}28msgType = 'chat';29author = from;30}31else if (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 (WABinary_1.isJidBroadcast(from)) {40if (!participant) {41throw new boom_1.Boom('No participant in group message');42}43const isParticipantMe = isMe(participant);44if (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}53const sender = msgType === 'chat' ? author : chatId;54const fromMe = isMe(stanza.attrs.participant || stanza.attrs.from);55const pushname = stanza.attrs.notify;56const key = {57remoteJid: chatId,58fromMe,59id: msgId,60participant61};62const fullMessage = {63key,64messageTimestamp: +stanza.attrs.t,65pushName: pushname66};67if (key.fromMe) {68fullMessage.status = WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.SERVER_ACK;69}70return {71fullMessage,72decryptionTask: (async () => {73var _a;74let decryptables = 0;75if (Array.isArray(stanza.content)) {76for (const { tag, attrs, content } of stanza.content) {77if (tag !== 'enc') {78continue;79}80if (!(content instanceof Uint8Array)) {81continue;82}83decryptables += 1;84let msgBuffer;85try {86const e2eType = attrs.type;87switch (e2eType) {88case 'skmsg':89msgBuffer = await signal_1.decryptGroupSignalProto(sender, author, content, auth);90break;91case 'pkmsg':92case 'msg':93const user = WABinary_1.isJidUser(sender) ? sender : author;94msgBuffer = await signal_1.decryptSignalProto(user, e2eType, content, auth);95break;96}97let msg = WAProto_1.proto.Message.decode(generics_1.unpadRandomMax16(msgBuffer));98msg = ((_a = msg.deviceSentMessage) === null || _a === void 0 ? void 0 : _a.message) || msg;99if (msg.senderKeyDistributionMessage) {100await signal_1.processSenderKeyMessage(author, msg.senderKeyDistributionMessage, auth);101}102if (fullMessage.message) {103Object.assign(fullMessage.message, msg);104}105else {106fullMessage.message = msg;107}108}109catch (error) {110fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.WebMessageInfoStubType.CIPHERTEXT;111fullMessage.messageStubParameters = [error.message];112}113}114}115// if nothing was found to decrypt116if (!decryptables) {117fullMessage.messageStubType = WAProto_1.proto.WebMessageInfo.WebMessageInfoStubType.CIPHERTEXT;118fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT];119}120})()121};122};123exports.decodeMessageStanza = decodeMessageStanza;124125126