@hansaka02/baileys 7.3.4 → 7.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -247
- package/lib/Defaults/baileys-version.json +2 -2
- package/lib/Defaults/connection.js +1 -1
- package/lib/Defaults/constants.js +13 -1
- package/lib/Defaults/history.js +3 -1
- package/lib/Signal/Group/sender-chain-key.js +1 -14
- package/lib/Signal/Group/sender-key-distribution-message.js +2 -2
- package/lib/Signal/Group/sender-key-record.js +2 -11
- package/lib/Signal/Group/sender-key-state.js +11 -57
- package/lib/Signal/libsignal.js +200 -116
- package/lib/Signal/lid-mapping.js +121 -68
- package/lib/Socket/Client/websocket.js +9 -2
- package/lib/Socket/business.js +5 -1
- package/lib/Socket/chats.js +180 -89
- package/lib/Socket/community.js +169 -41
- package/lib/Socket/groups.js +25 -21
- package/lib/Socket/messages-recv.js +458 -333
- package/lib/Socket/messages-send.js +517 -572
- package/lib/Socket/mex.js +61 -0
- package/lib/Socket/newsletter.js +159 -252
- package/lib/Socket/socket.js +283 -100
- package/lib/Types/Newsletter.js +32 -25
- package/lib/Utils/auth-utils.js +189 -354
- package/lib/Utils/browser-utils.js +43 -0
- package/lib/Utils/chat-utils.js +166 -41
- package/lib/Utils/decode-wa-message.js +77 -35
- package/lib/Utils/event-buffer.js +80 -24
- package/lib/Utils/generics.js +28 -128
- package/lib/Utils/history.js +10 -8
- package/lib/Utils/index.js +1 -1
- package/lib/Utils/link-preview.js +17 -32
- package/lib/Utils/lt-hash.js +28 -22
- package/lib/Utils/make-mutex.js +26 -28
- package/lib/Utils/message-retry-manager.js +51 -3
- package/lib/Utils/messages-media.js +343 -151
- package/lib/Utils/messages.js +806 -792
- package/lib/Utils/noise-handler.js +33 -2
- package/lib/Utils/pre-key-manager.js +126 -0
- package/lib/Utils/process-message.js +115 -55
- package/lib/Utils/signal.js +45 -18
- package/lib/Utils/validate-connection.js +52 -29
- package/lib/WABinary/constants.js +1268 -1268
- package/lib/WABinary/decode.js +58 -4
- package/lib/WABinary/encode.js +54 -7
- package/lib/WABinary/jid-utils.js +58 -11
- package/lib/WAM/constants.js +19064 -11563
- package/lib/WAM/encode.js +57 -8
- package/lib/WAUSync/USyncQuery.js +35 -19
- package/package.json +9 -8
- package/lib/Socket/usync.js +0 -83
package/lib/WABinary/decode.js
CHANGED
|
@@ -4,7 +4,10 @@ Object.defineProperty(exports, "__esModule", { value: true })
|
|
|
4
4
|
|
|
5
5
|
const { promisify } = require("util")
|
|
6
6
|
const { inflate } = require("zlib")
|
|
7
|
-
const {
|
|
7
|
+
const {
|
|
8
|
+
jidEncode,
|
|
9
|
+
WAJIDDomains
|
|
10
|
+
} = require("./jid-utils")
|
|
8
11
|
const constants = require("./constants")
|
|
9
12
|
const inflatePromise = promisify(inflate)
|
|
10
13
|
|
|
@@ -12,9 +15,12 @@ const decompressingIfRequired = async (buffer) => {
|
|
|
12
15
|
if (2 & buffer.readUInt8()) {
|
|
13
16
|
buffer = await inflatePromise(buffer.slice(1))
|
|
14
17
|
}
|
|
15
|
-
|
|
18
|
+
|
|
19
|
+
else {
|
|
20
|
+
// nodes with no compression have a 0x00 prefix, we remove that
|
|
16
21
|
buffer = buffer.slice(1)
|
|
17
22
|
}
|
|
23
|
+
|
|
18
24
|
return buffer
|
|
19
25
|
}
|
|
20
26
|
|
|
@@ -28,7 +34,9 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
28
34
|
|
|
29
35
|
const next = () => {
|
|
30
36
|
const value = buffer[indexRef.index]
|
|
37
|
+
|
|
31
38
|
indexRef.index += 1
|
|
39
|
+
|
|
32
40
|
return value
|
|
33
41
|
}
|
|
34
42
|
|
|
@@ -39,8 +47,11 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
39
47
|
|
|
40
48
|
const readBytes = (n) => {
|
|
41
49
|
checkEOS(n)
|
|
50
|
+
|
|
42
51
|
const value = buffer.slice(indexRef.index, indexRef.index + n)
|
|
52
|
+
|
|
43
53
|
indexRef.index += n
|
|
54
|
+
|
|
44
55
|
return value
|
|
45
56
|
}
|
|
46
57
|
|
|
@@ -50,11 +61,14 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
50
61
|
|
|
51
62
|
const readInt = (n, littleEndian = false) => {
|
|
52
63
|
checkEOS(n)
|
|
64
|
+
|
|
53
65
|
let val = 0
|
|
66
|
+
|
|
54
67
|
for (let i = 0; i < n; i++) {
|
|
55
68
|
const shift = littleEndian ? i : n - 1 - i
|
|
56
69
|
val |= next() << (shift * 8)
|
|
57
70
|
}
|
|
71
|
+
|
|
58
72
|
return val
|
|
59
73
|
}
|
|
60
74
|
|
|
@@ -67,6 +81,7 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
67
81
|
if (value >= 0 && value < 16) {
|
|
68
82
|
return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
|
|
69
83
|
}
|
|
84
|
+
|
|
70
85
|
throw new Error('invalid hex: ' + value)
|
|
71
86
|
}
|
|
72
87
|
|
|
@@ -74,6 +89,7 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
74
89
|
if (value >= 0 && value <= 9) {
|
|
75
90
|
return '0'.charCodeAt(0) + value
|
|
76
91
|
}
|
|
92
|
+
|
|
77
93
|
switch (value) {
|
|
78
94
|
case 10:
|
|
79
95
|
return '-'.charCodeAt(0)
|
|
@@ -90,9 +106,11 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
90
106
|
if (tag === TAGS.NIBBLE_8) {
|
|
91
107
|
return unpackNibble(value)
|
|
92
108
|
}
|
|
109
|
+
|
|
93
110
|
else if (tag === TAGS.HEX_8) {
|
|
94
111
|
return unpackHex(value)
|
|
95
112
|
}
|
|
113
|
+
|
|
96
114
|
else {
|
|
97
115
|
throw new Error('unknown tag: ' + tag)
|
|
98
116
|
}
|
|
@@ -100,15 +118,20 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
100
118
|
|
|
101
119
|
const readPacked8 = (tag) => {
|
|
102
120
|
const startByte = readByte()
|
|
121
|
+
|
|
103
122
|
let value = ''
|
|
123
|
+
|
|
104
124
|
for (let i = 0; i < (startByte & 127); i++) {
|
|
105
125
|
const curByte = readByte()
|
|
126
|
+
|
|
106
127
|
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
|
|
107
128
|
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
|
|
108
129
|
}
|
|
130
|
+
|
|
109
131
|
if (startByte >> 7 !== 0) {
|
|
110
132
|
value = value.slice(0, -1)
|
|
111
133
|
}
|
|
134
|
+
|
|
112
135
|
return value
|
|
113
136
|
}
|
|
114
137
|
|
|
@@ -132,23 +155,42 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
132
155
|
const readJidPair = () => {
|
|
133
156
|
const i = readString(readByte())
|
|
134
157
|
const j = readString(readByte())
|
|
158
|
+
|
|
135
159
|
if (j) {
|
|
136
160
|
return (i || '') + '@' + j
|
|
137
161
|
}
|
|
162
|
+
|
|
138
163
|
throw new Error('invalid jid pair: ' + i + ', ' + j)
|
|
139
164
|
}
|
|
140
165
|
|
|
141
166
|
const readAdJid = () => {
|
|
142
|
-
const
|
|
167
|
+
const rawDomainType = readByte()
|
|
168
|
+
const domainType = Number(rawDomainType)
|
|
143
169
|
const device = readByte()
|
|
144
170
|
const user = readString(readByte())
|
|
145
|
-
|
|
171
|
+
|
|
172
|
+
let server = 's.whatsapp.net' // default whatsapp server
|
|
173
|
+
|
|
174
|
+
if (domainType === WAJIDDomains.LID) {
|
|
175
|
+
server = 'lid'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
else if (domainType === WAJIDDomains.HOSTED) {
|
|
179
|
+
server = 'hosted'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
else if (domainType === WAJIDDomains.HOSTED_LID) {
|
|
183
|
+
server = 'hosted.lid'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return jidEncode(user, server, device)
|
|
146
187
|
}
|
|
147
188
|
|
|
148
189
|
const readString = (tag) => {
|
|
149
190
|
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
|
|
150
191
|
return SINGLE_BYTE_TOKENS[tag] || ''
|
|
151
192
|
}
|
|
193
|
+
|
|
152
194
|
switch (tag) {
|
|
153
195
|
case TAGS.DICTIONARY_0:
|
|
154
196
|
case TAGS.DICTIONARY_1:
|
|
@@ -178,21 +220,27 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
178
220
|
const readList = (tag) => {
|
|
179
221
|
const items = []
|
|
180
222
|
const size = readListSize(tag)
|
|
223
|
+
|
|
181
224
|
for (let i = 0; i < size; i++) {
|
|
182
225
|
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
|
|
183
226
|
}
|
|
227
|
+
|
|
184
228
|
return items
|
|
185
229
|
}
|
|
186
230
|
|
|
187
231
|
const getTokenDouble = (index1, index2) => {
|
|
188
232
|
const dict = DOUBLE_BYTE_TOKENS[index1]
|
|
233
|
+
|
|
189
234
|
if (!dict) {
|
|
190
235
|
throw new Error(`Invalid double token dict (${index1})`)
|
|
191
236
|
}
|
|
237
|
+
|
|
192
238
|
const value = dict[index2]
|
|
239
|
+
|
|
193
240
|
if (typeof value === 'undefined') {
|
|
194
241
|
throw new Error(`Invalid double token (${index2})`)
|
|
195
242
|
}
|
|
243
|
+
|
|
196
244
|
return value
|
|
197
245
|
}
|
|
198
246
|
|
|
@@ -213,6 +261,7 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
213
261
|
|
|
214
262
|
// read the attributes in
|
|
215
263
|
const attributesLength = (listSize - 1) >> 1
|
|
264
|
+
|
|
216
265
|
for (let i = 0; i < attributesLength; i++) {
|
|
217
266
|
const key = readString(readByte())
|
|
218
267
|
const value = readString(readByte())
|
|
@@ -221,11 +270,14 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
221
270
|
|
|
222
271
|
if (listSize % 2 === 0) {
|
|
223
272
|
const tag = readByte()
|
|
273
|
+
|
|
224
274
|
if (isListTag(tag)) {
|
|
225
275
|
data = readList(tag)
|
|
226
276
|
}
|
|
277
|
+
|
|
227
278
|
else {
|
|
228
279
|
let decoded
|
|
280
|
+
|
|
229
281
|
switch (tag) {
|
|
230
282
|
case TAGS.BINARY_8:
|
|
231
283
|
decoded = readBytes(readByte())
|
|
@@ -240,9 +292,11 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
240
292
|
decoded = readString(tag)
|
|
241
293
|
break
|
|
242
294
|
}
|
|
295
|
+
|
|
243
296
|
data = decoded
|
|
244
297
|
}
|
|
245
298
|
}
|
|
299
|
+
|
|
246
300
|
return {
|
|
247
301
|
tag: header,
|
|
248
302
|
attrs,
|
package/lib/WABinary/encode.js
CHANGED
|
@@ -27,22 +27,25 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const pushInt16 = (value) => {
|
|
30
|
-
pushBytes([(value >> 8) & 0xff, value & 0xff])
|
|
30
|
+
pushBytes([(value >> 8) & 0xff, value & 0xff]);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
const pushInt20 = (value) =>
|
|
33
|
+
const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
|
|
34
34
|
const writeByteLength = (length) => {
|
|
35
35
|
if (length >= 4294967296) {
|
|
36
|
-
throw new Error('string too large to encode: ' + length)
|
|
36
|
+
throw new Error('string too large to encode: ' + length);
|
|
37
37
|
}
|
|
38
|
+
|
|
38
39
|
if (length >= 1 << 20) {
|
|
39
40
|
pushByte(TAGS.BINARY_32)
|
|
40
41
|
pushInt(length, 4) // 32 bit integer
|
|
41
42
|
}
|
|
43
|
+
|
|
42
44
|
else if (length >= 256) {
|
|
43
45
|
pushByte(TAGS.BINARY_20)
|
|
44
46
|
pushInt20(length)
|
|
45
47
|
}
|
|
48
|
+
|
|
46
49
|
else {
|
|
47
50
|
pushByte(TAGS.BINARY_8)
|
|
48
51
|
pushByte(length)
|
|
@@ -51,6 +54,7 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
51
54
|
|
|
52
55
|
const writeStringRaw = (str) => {
|
|
53
56
|
const bytes = Buffer.from(str, 'utf-8')
|
|
57
|
+
|
|
54
58
|
writeByteLength(bytes.length)
|
|
55
59
|
pushBytes(bytes)
|
|
56
60
|
}
|
|
@@ -59,17 +63,21 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
59
63
|
if (typeof device !== 'undefined') {
|
|
60
64
|
pushByte(TAGS.AD_JID)
|
|
61
65
|
pushByte(domainType || 0)
|
|
62
|
-
pushByte(device || 0)
|
|
66
|
+
pushByte(device || 0)
|
|
63
67
|
writeString(user)
|
|
64
68
|
}
|
|
69
|
+
|
|
65
70
|
else {
|
|
66
71
|
pushByte(TAGS.JID_PAIR)
|
|
72
|
+
|
|
67
73
|
if (user.length) {
|
|
68
74
|
writeString(user)
|
|
69
75
|
}
|
|
76
|
+
|
|
70
77
|
else {
|
|
71
78
|
pushByte(TAGS.LIST_EMPTY)
|
|
72
79
|
}
|
|
80
|
+
|
|
73
81
|
writeString(server)
|
|
74
82
|
}
|
|
75
83
|
}
|
|
@@ -86,6 +94,7 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
86
94
|
if (char >= '0' && char <= '9') {
|
|
87
95
|
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
|
88
96
|
}
|
|
97
|
+
|
|
89
98
|
throw new Error(`invalid byte for nibble "${char}"`)
|
|
90
99
|
}
|
|
91
100
|
}
|
|
@@ -94,15 +103,19 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
94
103
|
if (char >= '0' && char <= '9') {
|
|
95
104
|
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
|
96
105
|
}
|
|
106
|
+
|
|
97
107
|
if (char >= 'A' && char <= 'F') {
|
|
98
108
|
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
|
|
99
109
|
}
|
|
110
|
+
|
|
100
111
|
if (char >= 'a' && char <= 'f') {
|
|
101
112
|
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
|
|
102
113
|
}
|
|
114
|
+
|
|
103
115
|
if (char === '\0') {
|
|
104
116
|
return 15
|
|
105
117
|
}
|
|
118
|
+
|
|
106
119
|
throw new Error(`Invalid hex char "${char}"`)
|
|
107
120
|
}
|
|
108
121
|
|
|
@@ -110,21 +123,29 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
110
123
|
if (str.length > TAGS.PACKED_MAX) {
|
|
111
124
|
throw new Error('Too many bytes to pack')
|
|
112
125
|
}
|
|
126
|
+
|
|
113
127
|
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
|
|
128
|
+
|
|
114
129
|
let roundedLength = Math.ceil(str.length / 2.0)
|
|
130
|
+
|
|
115
131
|
if (str.length % 2 !== 0) {
|
|
116
132
|
roundedLength |= 128
|
|
117
133
|
}
|
|
134
|
+
|
|
118
135
|
pushByte(roundedLength)
|
|
136
|
+
|
|
119
137
|
const packFunction = type === 'nibble' ? packNibble : packHex
|
|
120
138
|
const packBytePair = (v1, v2) => {
|
|
121
139
|
const result = (packFunction(v1) << 4) | packFunction(v2)
|
|
122
140
|
return result
|
|
123
141
|
}
|
|
142
|
+
|
|
124
143
|
const strLengthHalf = Math.floor(str.length / 2)
|
|
144
|
+
|
|
125
145
|
for (let i = 0; i < strLengthHalf; i++) {
|
|
126
146
|
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
|
|
127
147
|
}
|
|
148
|
+
|
|
128
149
|
if (str.length % 2 !== 0) {
|
|
129
150
|
pushByte(packBytePair(str[str.length - 1], '\x00'))
|
|
130
151
|
}
|
|
@@ -134,12 +155,15 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
134
155
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
135
156
|
return false
|
|
136
157
|
}
|
|
158
|
+
|
|
137
159
|
for (const char of str) {
|
|
138
160
|
const isInNibbleRange = char >= '0' && char <= '9'
|
|
161
|
+
|
|
139
162
|
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
|
140
163
|
return false
|
|
141
164
|
}
|
|
142
165
|
}
|
|
166
|
+
|
|
143
167
|
return true
|
|
144
168
|
}
|
|
145
169
|
|
|
@@ -147,38 +171,49 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
147
171
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
148
172
|
return false
|
|
149
173
|
}
|
|
174
|
+
|
|
150
175
|
for (const char of str) {
|
|
151
176
|
const isInNibbleRange = char >= '0' && char <= '9'
|
|
177
|
+
|
|
152
178
|
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
|
153
179
|
return false
|
|
154
180
|
}
|
|
155
181
|
}
|
|
182
|
+
|
|
156
183
|
return true
|
|
157
184
|
}
|
|
158
185
|
|
|
159
186
|
const writeString = (str) => {
|
|
160
|
-
|
|
187
|
+
if (str === undefined || str === null) {
|
|
161
188
|
pushByte(TAGS.LIST_EMPTY)
|
|
162
189
|
return
|
|
163
190
|
}
|
|
191
|
+
|
|
164
192
|
const tokenIndex = TOKEN_MAP[str]
|
|
193
|
+
|
|
165
194
|
if (tokenIndex) {
|
|
166
195
|
if (typeof tokenIndex.dict === 'number') {
|
|
167
196
|
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
|
|
168
197
|
}
|
|
198
|
+
|
|
169
199
|
pushByte(tokenIndex.index)
|
|
170
200
|
}
|
|
201
|
+
|
|
171
202
|
else if (isNibble(str)) {
|
|
172
203
|
writePackedBytes(str, 'nibble')
|
|
173
204
|
}
|
|
205
|
+
|
|
174
206
|
else if (isHex(str)) {
|
|
175
207
|
writePackedBytes(str, 'hex')
|
|
176
208
|
}
|
|
209
|
+
|
|
177
210
|
else if (str) {
|
|
178
211
|
const decodedJid = jidDecode(str)
|
|
212
|
+
|
|
179
213
|
if (decodedJid) {
|
|
180
214
|
writeJid(decodedJid)
|
|
181
215
|
}
|
|
216
|
+
|
|
182
217
|
else {
|
|
183
218
|
writeStringRaw(str)
|
|
184
219
|
}
|
|
@@ -189,9 +224,11 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
189
224
|
if (listSize === 0) {
|
|
190
225
|
pushByte(TAGS.LIST_EMPTY)
|
|
191
226
|
}
|
|
227
|
+
|
|
192
228
|
else if (listSize < 256) {
|
|
193
229
|
pushBytes([TAGS.LIST_8, listSize])
|
|
194
230
|
}
|
|
231
|
+
|
|
195
232
|
else {
|
|
196
233
|
pushByte(TAGS.LIST_16)
|
|
197
234
|
pushInt16(listSize)
|
|
@@ -202,35 +239,45 @@ const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
|
202
239
|
throw new Error('Invalid node: tag cannot be undefined')
|
|
203
240
|
}
|
|
204
241
|
|
|
205
|
-
const validAttributes = Object.keys(attrs).filter(k =>
|
|
242
|
+
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null)
|
|
243
|
+
|
|
206
244
|
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
|
|
207
245
|
writeString(tag)
|
|
246
|
+
|
|
208
247
|
for (const key of validAttributes) {
|
|
209
248
|
if (typeof attrs[key] === 'string') {
|
|
210
249
|
writeString(key)
|
|
211
250
|
writeString(attrs[key])
|
|
212
251
|
}
|
|
213
252
|
}
|
|
253
|
+
|
|
214
254
|
if (typeof content === 'string') {
|
|
215
255
|
writeString(content)
|
|
216
256
|
}
|
|
257
|
+
|
|
217
258
|
else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
218
259
|
writeByteLength(content.length)
|
|
219
260
|
pushBytes(content)
|
|
220
261
|
}
|
|
262
|
+
|
|
221
263
|
else if (Array.isArray(content)) {
|
|
222
|
-
|
|
264
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'))
|
|
265
|
+
|
|
223
266
|
writeListStart(validContent.length)
|
|
267
|
+
|
|
224
268
|
for (const item of validContent) {
|
|
225
269
|
encodeBinaryNodeInner(item, opts, buffer)
|
|
226
270
|
}
|
|
227
271
|
}
|
|
272
|
+
|
|
228
273
|
else if (typeof content === 'undefined') {
|
|
229
274
|
// do nothing
|
|
230
275
|
}
|
|
276
|
+
|
|
231
277
|
else {
|
|
232
278
|
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
|
|
233
279
|
}
|
|
280
|
+
|
|
234
281
|
return buffer
|
|
235
282
|
}
|
|
236
283
|
|
|
@@ -14,39 +14,76 @@ const STORIES_JID = 'status@broadcast'
|
|
|
14
14
|
|
|
15
15
|
const META_AI_JID = '13135550002@c.us'
|
|
16
16
|
|
|
17
|
+
const WAJIDDomains = {
|
|
18
|
+
WHATSAPP: 0,
|
|
19
|
+
LID: 1,
|
|
20
|
+
HOSTED: 128,
|
|
21
|
+
HOSTED_LID: 129
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const getServerFromDomainType = (initialServer, domainType) => {
|
|
25
|
+
switch (domainType) {
|
|
26
|
+
case WAJIDDomains.LID:
|
|
27
|
+
return 'lid'
|
|
28
|
+
case WAJIDDomains.HOSTED:
|
|
29
|
+
return 'hosted'
|
|
30
|
+
case WAJIDDomains.HOSTED_LID:
|
|
31
|
+
return 'hosted.lid'
|
|
32
|
+
case WAJIDDomains.WHATSAPP:
|
|
33
|
+
default:
|
|
34
|
+
return initialServer
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
17
38
|
const jidEncode = (user, server, device, agent) => {
|
|
18
39
|
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
|
|
19
40
|
}
|
|
20
41
|
|
|
21
42
|
const jidDecode = (jid) => {
|
|
43
|
+
// todo: investigate how to implement hosted ids in this case
|
|
22
44
|
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
|
|
45
|
+
|
|
23
46
|
if (sepIdx < 0) {
|
|
24
47
|
return undefined
|
|
25
48
|
}
|
|
49
|
+
|
|
26
50
|
const server = jid.slice(sepIdx + 1)
|
|
27
51
|
const userCombined = jid.slice(0, sepIdx)
|
|
28
52
|
const [userAgent, device] = userCombined.split(':')
|
|
29
|
-
const user = userAgent.split('_')
|
|
53
|
+
const [user, agent] = userAgent.split('_')
|
|
54
|
+
|
|
55
|
+
let domainType = WAJIDDomains.WHATSAPP
|
|
56
|
+
|
|
57
|
+
if (server === 'lid') {
|
|
58
|
+
domainType = WAJIDDomains.LID
|
|
59
|
+
}
|
|
60
|
+
else if (server === 'hosted') {
|
|
61
|
+
domainType = WAJIDDomains.HOSTED
|
|
62
|
+
}
|
|
63
|
+
else if (server === 'hosted.lid') {
|
|
64
|
+
domainType = WAJIDDomains.HOSTED_LID
|
|
65
|
+
}
|
|
66
|
+
else if (agent) {
|
|
67
|
+
domainType = parseInt(agent)
|
|
68
|
+
}
|
|
30
69
|
return {
|
|
31
70
|
server: server,
|
|
32
|
-
user,
|
|
33
|
-
domainType
|
|
71
|
+
user: user,
|
|
72
|
+
domainType,
|
|
34
73
|
device: device ? +device : undefined
|
|
35
74
|
}
|
|
36
75
|
}
|
|
37
76
|
|
|
38
77
|
/** is the jid a user */
|
|
39
|
-
const areJidsSameUser = (jid1, jid2) =>
|
|
40
|
-
return jidDecode(jid1)?.user === jidDecode(jid2)?.user
|
|
41
|
-
}
|
|
78
|
+
const areJidsSameUser = (jid1, jid2) => jidDecode(jid1)?.user === jidDecode(jid2)?.user
|
|
42
79
|
|
|
43
80
|
/** is the jid Meta AI */
|
|
44
81
|
const isJidMetaAI = (jid) => jid?.endsWith('@bot')
|
|
45
82
|
|
|
46
|
-
/** is the jid a user */
|
|
47
|
-
const
|
|
83
|
+
/** is the jid a PN user */
|
|
84
|
+
const isPnUser = (jid) => jid?.endsWith('@s.whatsapp.net')
|
|
48
85
|
|
|
49
|
-
/** is the
|
|
86
|
+
/** is the jid a LID */
|
|
50
87
|
const isLidUser = (jid) => jid?.endsWith('@lid')
|
|
51
88
|
|
|
52
89
|
/** is the jid a broadcast */
|
|
@@ -61,6 +98,12 @@ const isJidStatusBroadcast = (jid) => jid === 'status@broadcast'
|
|
|
61
98
|
/** is the jid a newsletter */
|
|
62
99
|
const isJidNewsletter = (jid) => jid?.endsWith('@newsletter')
|
|
63
100
|
|
|
101
|
+
/** is the jid a hosted PN */
|
|
102
|
+
const isHostedPnUser = (jid) => jid?.endsWith('@hosted')
|
|
103
|
+
|
|
104
|
+
/** is the jid a hosted LID */
|
|
105
|
+
const isHostedLidUser = (jid) => jid?.endsWith('@hosted.lid')
|
|
106
|
+
|
|
64
107
|
/** is the jid a bot */
|
|
65
108
|
const botRegexp = /^1313555\d{4}$|^131655500\d{2}$/
|
|
66
109
|
const isJidBot = (jid) => (jid && botRegexp.test(jid.split('@')[0]) && jid.endsWith('@c.us'))
|
|
@@ -88,17 +131,21 @@ module.exports = {
|
|
|
88
131
|
PSA_WID,
|
|
89
132
|
STORIES_JID,
|
|
90
133
|
META_AI_JID,
|
|
134
|
+
WAJIDDomains,
|
|
91
135
|
jidEncode,
|
|
92
136
|
jidDecode,
|
|
93
137
|
areJidsSameUser,
|
|
94
138
|
isJidMetaAI,
|
|
95
|
-
|
|
139
|
+
isPnUser,
|
|
96
140
|
isLidUser,
|
|
97
141
|
isJidBroadcast,
|
|
98
142
|
isJidGroup,
|
|
99
143
|
isJidStatusBroadcast,
|
|
100
144
|
isJidNewsletter,
|
|
145
|
+
isHostedPnUser,
|
|
146
|
+
isHostedLidUser,
|
|
101
147
|
isJidBot,
|
|
102
148
|
transferDevice,
|
|
103
|
-
jidNormalizedUser
|
|
149
|
+
jidNormalizedUser,
|
|
150
|
+
getServerFromDomainType
|
|
104
151
|
}
|