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