@sixcore/baileys 1.0.0
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/LICENSE +21 -0
- package/README.md +606 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +4357 -0
- package/WAProto/index.d.ts +50383 -0
- package/WAProto/index.js +155693 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/generate-proto.sh +1 -0
- package/WASignalGroup/group.proto +42 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +108 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.d.ts +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.d.ts +170 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +81 -0
- package/lib/Socket/chats.js +950 -0
- package/lib/Socket/groups.d.ts +115 -0
- package/lib/Socket/groups.js +315 -0
- package/lib/Socket/index.d.ts +172 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +158 -0
- package/lib/Socket/messages-recv.js +972 -0
- package/lib/Socket/messages-send.d.ts +155 -0
- package/lib/Socket/messages-send.js +1087 -0
- package/lib/Socket/newsletter.d.ts +132 -0
- package/lib/Socket/newsletter.js +236 -0
- package/lib/Socket/registration.d.ts +264 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.d.ts +44 -0
- package/lib/Socket/socket.js +643 -0
- package/lib/Socket/usync.d.ts +37 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +431 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +109 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +107 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +433 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Newsletter.d.ts +92 -0
- package/lib/Types/Newsletter.js +32 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +116 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +227 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +70 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +40 -0
- package/lib/Utils/crypto.js +199 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +234 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +517 -0
- package/lib/Utils/generics.d.ts +88 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.d.ts +2 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.d.ts +113 -0
- package/lib/Utils/messages-media.js +643 -0
- package/lib/Utils/messages.d.ts +77 -0
- package/lib/Utils/messages.js +1221 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +160 -0
- package/lib/Utils/process-message.d.ts +41 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +295 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-single-file-auth-state.js +75 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +205 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +6 -0
- package/lib/WABinary/decode.js +264 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +252 -0
- package/lib/WABinary/generic-utils.d.ts +14 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +8 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +38 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +2 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +28 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +31 -0
- package/package.json +51 -0
- package/src/Defaults/baileys-version.json +3 -0
- package/src/Defaults/index.ts +133 -0
- package/src/Signal/Group/ciphertext-message.ts +9 -0
- package/src/Signal/Group/group-session-builder.ts +56 -0
- package/src/Signal/Group/group_cipher.ts +117 -0
- package/src/Signal/Group/index.ts +11 -0
- package/src/Signal/Group/keyhelper.ts +28 -0
- package/src/Signal/Group/sender-chain-key.ts +34 -0
- package/src/Signal/Group/sender-key-distribution-message.ts +95 -0
- package/src/Signal/Group/sender-key-message.ts +96 -0
- package/src/Signal/Group/sender-key-name.ts +66 -0
- package/src/Signal/Group/sender-key-record.ts +69 -0
- package/src/Signal/Group/sender-key-state.ts +134 -0
- package/src/Signal/Group/sender-message-key.ts +36 -0
- package/src/Signal/libsignal.ts +447 -0
- package/src/Signal/lid-mapping.ts +209 -0
- package/src/Socket/Client/index.ts +2 -0
- package/src/Socket/Client/types.ts +22 -0
- package/src/Socket/Client/websocket.ts +56 -0
- package/src/Socket/business.ts +421 -0
- package/src/Socket/chats.ts +1223 -0
- package/src/Socket/communities.ts +477 -0
- package/src/Socket/groups.ts +361 -0
- package/src/Socket/index.ts +22 -0
- package/src/Socket/messages-recv.ts +1563 -0
- package/src/Socket/messages-send.ts +1210 -0
- package/src/Socket/mex.ts +58 -0
- package/src/Socket/newsletter.ts +229 -0
- package/src/Socket/socket.ts +1072 -0
- package/src/Types/Auth.ts +115 -0
- package/src/Types/Bussines.ts +20 -0
- package/src/Types/Call.ts +14 -0
- package/src/Types/Chat.ts +138 -0
- package/src/Types/Contact.ts +24 -0
- package/src/Types/Events.ts +132 -0
- package/src/Types/GroupMetadata.ts +70 -0
- package/src/Types/Label.ts +48 -0
- package/src/Types/LabelAssociation.ts +35 -0
- package/src/Types/Message.ts +424 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +76 -0
- package/src/Types/Socket.ts +150 -0
- package/src/Types/State.ts +43 -0
- package/src/Types/USync.ts +27 -0
- package/src/Types/globals.d.ts +8 -0
- package/src/Types/index.ts +67 -0
- package/src/Utils/auth-utils.ts +331 -0
- package/src/Utils/browser-utils.ts +31 -0
- package/src/Utils/business.ts +286 -0
- package/src/Utils/chat-utils.ts +933 -0
- package/src/Utils/crypto.ts +184 -0
- package/src/Utils/decode-wa-message.ts +355 -0
- package/src/Utils/event-buffer.ts +662 -0
- package/src/Utils/generics.ts +470 -0
- package/src/Utils/history.ts +114 -0
- package/src/Utils/index.ts +18 -0
- package/src/Utils/link-preview.ts +111 -0
- package/src/Utils/logger.ts +13 -0
- package/src/Utils/lt-hash.ts +65 -0
- package/src/Utils/make-mutex.ts +45 -0
- package/src/Utils/message-retry-manager.ts +229 -0
- package/src/Utils/messages-media.ts +820 -0
- package/src/Utils/messages.ts +1137 -0
- package/src/Utils/noise-handler.ts +192 -0
- package/src/Utils/pre-key-manager.ts +126 -0
- package/src/Utils/process-message.ts +622 -0
- package/src/Utils/signal.ts +214 -0
- package/src/Utils/use-multi-file-auth-state.ts +136 -0
- package/src/Utils/validate-connection.ts +253 -0
- package/src/WABinary/constants.ts +1305 -0
- package/src/WABinary/decode.ts +281 -0
- package/src/WABinary/encode.ts +253 -0
- package/src/WABinary/generic-utils.ts +127 -0
- package/src/WABinary/index.ts +5 -0
- package/src/WABinary/jid-utils.ts +128 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.ts +22889 -0
- package/src/WAM/encode.ts +169 -0
- package/src/WAM/index.ts +3 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.ts +32 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +78 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +35 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.ts +44 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +76 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +33 -0
- package/src/WAUSync/Protocols/index.ts +4 -0
- package/src/WAUSync/USyncQuery.ts +133 -0
- package/src/WAUSync/USyncUser.ts +32 -0
- package/src/WAUSync/index.ts +3 -0
- package/src/index.ts +13 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { BinaryInfo } from './BinaryInfo'
|
|
2
|
+
import {
|
|
3
|
+
FLAG_BYTE,
|
|
4
|
+
FLAG_EVENT,
|
|
5
|
+
FLAG_EXTENDED,
|
|
6
|
+
FLAG_FIELD,
|
|
7
|
+
FLAG_GLOBAL,
|
|
8
|
+
type Value,
|
|
9
|
+
WEB_EVENTS,
|
|
10
|
+
WEB_GLOBALS
|
|
11
|
+
} from './constants'
|
|
12
|
+
|
|
13
|
+
const getHeaderBitLength = (key: number) => (key < 256 ? 2 : 3)
|
|
14
|
+
|
|
15
|
+
export const encodeWAM = (binaryInfo: BinaryInfo) => {
|
|
16
|
+
binaryInfo.buffer = []
|
|
17
|
+
|
|
18
|
+
encodeWAMHeader(binaryInfo)
|
|
19
|
+
encodeEvents(binaryInfo)
|
|
20
|
+
|
|
21
|
+
const totalSize = binaryInfo.buffer.map(a => a.length).reduce((a, b) => a + b)
|
|
22
|
+
const buffer = Buffer.alloc(totalSize)
|
|
23
|
+
let offset = 0
|
|
24
|
+
for (const buffer_ of binaryInfo.buffer) {
|
|
25
|
+
buffer_.copy(buffer, offset)
|
|
26
|
+
offset += buffer_.length
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return buffer
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function encodeWAMHeader(binaryInfo: BinaryInfo) {
|
|
33
|
+
const headerBuffer = Buffer.alloc(8) // starting buffer
|
|
34
|
+
headerBuffer.write('WAM', 0, 'utf8')
|
|
35
|
+
headerBuffer.writeUInt8(binaryInfo.protocolVersion, 3)
|
|
36
|
+
headerBuffer.writeUInt8(1, 4) //random flag
|
|
37
|
+
headerBuffer.writeUInt16BE(binaryInfo.sequence, 5)
|
|
38
|
+
headerBuffer.writeUInt8(0, 7) // regular channel
|
|
39
|
+
|
|
40
|
+
binaryInfo.buffer.push(headerBuffer)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function encodeGlobalAttributes(binaryInfo: BinaryInfo, globals: { [key: string]: Value }) {
|
|
44
|
+
for (const [key, _value] of Object.entries(globals)) {
|
|
45
|
+
const id = WEB_GLOBALS.find(a => a?.name === key)!.id
|
|
46
|
+
let value = _value
|
|
47
|
+
if (typeof value === 'boolean') {
|
|
48
|
+
value = value ? 1 : 0
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
binaryInfo.buffer.push(serializeData(id, value, FLAG_GLOBAL))
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function encodeEvents(binaryInfo: BinaryInfo) {
|
|
56
|
+
for (const [name, { props, globals }] of binaryInfo.events.map(a => Object.entries(a)[0]!)) {
|
|
57
|
+
encodeGlobalAttributes(binaryInfo, globals)
|
|
58
|
+
const event = WEB_EVENTS.find(a => a.name === name)!
|
|
59
|
+
|
|
60
|
+
const props_ = Object.entries(props)
|
|
61
|
+
|
|
62
|
+
let extended = false
|
|
63
|
+
|
|
64
|
+
for (const [, value] of props_) {
|
|
65
|
+
extended ||= value !== null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED
|
|
69
|
+
binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag))
|
|
70
|
+
|
|
71
|
+
for (let i = 0; i < props_.length; i++) {
|
|
72
|
+
const [key, _value] = props_[i]!
|
|
73
|
+
const id = event.props[key]?.[0]
|
|
74
|
+
extended = i < props_.length - 1
|
|
75
|
+
let value = _value
|
|
76
|
+
if (typeof value === 'boolean') {
|
|
77
|
+
value = value ? 1 : 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const fieldFlag = extended ? FLAG_EVENT : FLAG_FIELD | FLAG_EXTENDED
|
|
81
|
+
binaryInfo.buffer.push(serializeData(id!, value, fieldFlag))
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function serializeData(key: number, value: Value, flag: number): Buffer {
|
|
87
|
+
const bufferLength = getHeaderBitLength(key)
|
|
88
|
+
let buffer: Buffer
|
|
89
|
+
let offset = 0
|
|
90
|
+
if (value === null) {
|
|
91
|
+
if (flag === FLAG_GLOBAL) {
|
|
92
|
+
buffer = Buffer.alloc(bufferLength)
|
|
93
|
+
offset = serializeHeader(buffer, offset, key, flag)
|
|
94
|
+
return buffer
|
|
95
|
+
}
|
|
96
|
+
} else if (typeof value === 'number' && Number.isInteger(value)) {
|
|
97
|
+
// is number
|
|
98
|
+
if (value === 0 || value === 1) {
|
|
99
|
+
buffer = Buffer.alloc(bufferLength)
|
|
100
|
+
offset = serializeHeader(buffer, offset, key, flag | ((value + 1) << 4))
|
|
101
|
+
return buffer
|
|
102
|
+
} else if (-128 <= value && value < 128) {
|
|
103
|
+
buffer = Buffer.alloc(bufferLength + 1)
|
|
104
|
+
offset = serializeHeader(buffer, offset, key, flag | (3 << 4))
|
|
105
|
+
buffer.writeInt8(value, offset)
|
|
106
|
+
return buffer
|
|
107
|
+
} else if (-32768 <= value && value < 32768) {
|
|
108
|
+
buffer = Buffer.alloc(bufferLength + 2)
|
|
109
|
+
offset = serializeHeader(buffer, offset, key, flag | (4 << 4))
|
|
110
|
+
buffer.writeInt16LE(value, offset)
|
|
111
|
+
return buffer
|
|
112
|
+
} else if (-2147483648 <= value && value < 2147483648) {
|
|
113
|
+
buffer = Buffer.alloc(bufferLength + 4)
|
|
114
|
+
offset = serializeHeader(buffer, offset, key, flag | (5 << 4))
|
|
115
|
+
buffer.writeInt32LE(value, offset)
|
|
116
|
+
return buffer
|
|
117
|
+
} else {
|
|
118
|
+
buffer = Buffer.alloc(bufferLength + 8)
|
|
119
|
+
offset = serializeHeader(buffer, offset, key, flag | (7 << 4))
|
|
120
|
+
buffer.writeDoubleLE(value, offset)
|
|
121
|
+
return buffer
|
|
122
|
+
}
|
|
123
|
+
} else if (typeof value === 'number') {
|
|
124
|
+
// is float
|
|
125
|
+
buffer = Buffer.alloc(bufferLength + 8)
|
|
126
|
+
offset = serializeHeader(buffer, offset, key, flag | (7 << 4))
|
|
127
|
+
buffer.writeDoubleLE(value, offset)
|
|
128
|
+
return buffer
|
|
129
|
+
} else if (typeof value === 'string') {
|
|
130
|
+
// is string
|
|
131
|
+
const utf8Bytes = Buffer.byteLength(value, 'utf8')
|
|
132
|
+
if (utf8Bytes < 256) {
|
|
133
|
+
buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes)
|
|
134
|
+
offset = serializeHeader(buffer, offset, key, flag | (8 << 4))
|
|
135
|
+
buffer.writeUint8(utf8Bytes, offset++)
|
|
136
|
+
} else if (utf8Bytes < 65536) {
|
|
137
|
+
buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes)
|
|
138
|
+
offset = serializeHeader(buffer, offset, key, flag | (9 << 4))
|
|
139
|
+
buffer.writeUInt16LE(utf8Bytes, offset)
|
|
140
|
+
offset += 2
|
|
141
|
+
} else {
|
|
142
|
+
buffer = Buffer.alloc(bufferLength + 4 + utf8Bytes)
|
|
143
|
+
offset = serializeHeader(buffer, offset, key, flag | (10 << 4))
|
|
144
|
+
buffer.writeUInt32LE(utf8Bytes, offset)
|
|
145
|
+
offset += 4
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
buffer.write(value, offset, 'utf8')
|
|
149
|
+
return buffer
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
throw 'missing'
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function serializeHeader(buffer: Buffer, offset: number, key: number, flag: number) {
|
|
156
|
+
if (key < 256) {
|
|
157
|
+
buffer.writeUInt8(flag, offset)
|
|
158
|
+
offset += 1
|
|
159
|
+
buffer.writeUInt8(key, offset)
|
|
160
|
+
offset += 1
|
|
161
|
+
} else {
|
|
162
|
+
buffer.writeUInt8(flag | FLAG_BYTE, offset)
|
|
163
|
+
offset += 1
|
|
164
|
+
buffer.writeUInt16LE(key, offset)
|
|
165
|
+
offset += 2
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return offset
|
|
169
|
+
}
|
package/src/WAM/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import { assertNodeErrorFree, type BinaryNode } from '../../WABinary'
|
|
3
|
+
import { USyncUser } from '../USyncUser'
|
|
4
|
+
|
|
5
|
+
export class USyncContactProtocol implements USyncQueryProtocol {
|
|
6
|
+
name = 'contact'
|
|
7
|
+
|
|
8
|
+
getQueryElement(): BinaryNode {
|
|
9
|
+
return {
|
|
10
|
+
tag: 'contact',
|
|
11
|
+
attrs: {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getUserElement(user: USyncUser): BinaryNode {
|
|
16
|
+
//TODO: Implement type / username fields (not yet supported)
|
|
17
|
+
return {
|
|
18
|
+
tag: 'contact',
|
|
19
|
+
attrs: {},
|
|
20
|
+
content: user.phone
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
parser(node: BinaryNode): boolean {
|
|
25
|
+
if (node.tag === 'contact') {
|
|
26
|
+
assertNodeErrorFree(node)
|
|
27
|
+
return node?.attrs?.type === 'in'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import { assertNodeErrorFree, type BinaryNode, getBinaryNodeChild } from '../../WABinary'
|
|
3
|
+
//import { USyncUser } from '../USyncUser'
|
|
4
|
+
|
|
5
|
+
export type KeyIndexData = {
|
|
6
|
+
timestamp: number
|
|
7
|
+
signedKeyIndex?: Uint8Array
|
|
8
|
+
expectedTimestamp?: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type DeviceListData = {
|
|
12
|
+
id: number
|
|
13
|
+
keyIndex?: number
|
|
14
|
+
isHosted?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type ParsedDeviceInfo = {
|
|
18
|
+
deviceList?: DeviceListData[]
|
|
19
|
+
keyIndex?: KeyIndexData
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class USyncDeviceProtocol implements USyncQueryProtocol {
|
|
23
|
+
name = 'devices'
|
|
24
|
+
|
|
25
|
+
getQueryElement(): BinaryNode {
|
|
26
|
+
return {
|
|
27
|
+
tag: 'devices',
|
|
28
|
+
attrs: {
|
|
29
|
+
version: '2'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getUserElement(/* user: USyncUser */): BinaryNode | null {
|
|
35
|
+
//TODO: Implement device phashing, ts and expectedTs
|
|
36
|
+
//TODO: if all are not present, return null <- current behavior
|
|
37
|
+
//TODO: otherwise return a node w tag 'devices' w those as attrs
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
parser(node: BinaryNode): ParsedDeviceInfo {
|
|
42
|
+
const deviceList: DeviceListData[] = []
|
|
43
|
+
let keyIndex: KeyIndexData | undefined = undefined
|
|
44
|
+
|
|
45
|
+
if (node.tag === 'devices') {
|
|
46
|
+
assertNodeErrorFree(node)
|
|
47
|
+
const deviceListNode = getBinaryNodeChild(node, 'device-list')
|
|
48
|
+
const keyIndexNode = getBinaryNodeChild(node, 'key-index-list')
|
|
49
|
+
|
|
50
|
+
if (Array.isArray(deviceListNode?.content)) {
|
|
51
|
+
for (const { tag, attrs } of deviceListNode.content) {
|
|
52
|
+
const id = +attrs.id!
|
|
53
|
+
const keyIndex = +attrs['key-index']!
|
|
54
|
+
if (tag === 'device') {
|
|
55
|
+
deviceList.push({
|
|
56
|
+
id,
|
|
57
|
+
keyIndex,
|
|
58
|
+
isHosted: !!(attrs['is_hosted'] && attrs['is_hosted'] === 'true')
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (keyIndexNode?.tag === 'key-index-list') {
|
|
65
|
+
keyIndex = {
|
|
66
|
+
timestamp: +keyIndexNode.attrs['ts']!,
|
|
67
|
+
signedKeyIndex: keyIndexNode?.content as Uint8Array,
|
|
68
|
+
expectedTimestamp: keyIndexNode.attrs['expected_ts'] ? +keyIndexNode.attrs['expected_ts'] : undefined
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
deviceList,
|
|
75
|
+
keyIndex
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import { assertNodeErrorFree, type BinaryNode } from '../../WABinary'
|
|
3
|
+
|
|
4
|
+
export type DisappearingModeData = {
|
|
5
|
+
duration: number
|
|
6
|
+
setAt?: Date
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class USyncDisappearingModeProtocol implements USyncQueryProtocol {
|
|
10
|
+
name = 'disappearing_mode'
|
|
11
|
+
|
|
12
|
+
getQueryElement(): BinaryNode {
|
|
13
|
+
return {
|
|
14
|
+
tag: 'disappearing_mode',
|
|
15
|
+
attrs: {}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getUserElement(): null {
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
parser(node: BinaryNode): DisappearingModeData | undefined {
|
|
24
|
+
if (node.tag === 'disappearing_mode') {
|
|
25
|
+
assertNodeErrorFree(node)
|
|
26
|
+
const duration: number = +node?.attrs.duration!
|
|
27
|
+
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
duration,
|
|
31
|
+
setAt
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import { assertNodeErrorFree, type BinaryNode } from '../../WABinary'
|
|
3
|
+
|
|
4
|
+
export type StatusData = {
|
|
5
|
+
status?: string | null
|
|
6
|
+
setAt?: Date
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class USyncStatusProtocol implements USyncQueryProtocol {
|
|
10
|
+
name = 'status'
|
|
11
|
+
|
|
12
|
+
getQueryElement(): BinaryNode {
|
|
13
|
+
return {
|
|
14
|
+
tag: 'status',
|
|
15
|
+
attrs: {}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getUserElement(): null {
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
parser(node: BinaryNode): StatusData | undefined {
|
|
24
|
+
if (node.tag === 'status') {
|
|
25
|
+
assertNodeErrorFree(node)
|
|
26
|
+
let status: string | null = node?.content?.toString() ?? null
|
|
27
|
+
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
|
|
28
|
+
if (!status) {
|
|
29
|
+
if (node.attrs?.code && +node.attrs.code === 401) {
|
|
30
|
+
status = ''
|
|
31
|
+
} else {
|
|
32
|
+
status = null
|
|
33
|
+
}
|
|
34
|
+
} else if (typeof status === 'string' && status.length === 0) {
|
|
35
|
+
status = null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
status,
|
|
40
|
+
setAt
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import { type BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../../WABinary'
|
|
3
|
+
import { USyncUser } from '../USyncUser'
|
|
4
|
+
|
|
5
|
+
export type BotProfileCommand = {
|
|
6
|
+
name: string
|
|
7
|
+
description: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type BotProfileInfo = {
|
|
11
|
+
jid: string
|
|
12
|
+
name: string
|
|
13
|
+
attributes: string
|
|
14
|
+
description: string
|
|
15
|
+
category: string
|
|
16
|
+
isDefault: boolean
|
|
17
|
+
prompts: string[]
|
|
18
|
+
personaId: string
|
|
19
|
+
commands: BotProfileCommand[]
|
|
20
|
+
commandsDescription: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class USyncBotProfileProtocol implements USyncQueryProtocol {
|
|
24
|
+
name = 'bot'
|
|
25
|
+
|
|
26
|
+
getQueryElement(): BinaryNode {
|
|
27
|
+
return {
|
|
28
|
+
tag: 'bot',
|
|
29
|
+
attrs: {},
|
|
30
|
+
content: [{ tag: 'profile', attrs: { v: '1' } }]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getUserElement(user: USyncUser): BinaryNode {
|
|
35
|
+
return {
|
|
36
|
+
tag: 'bot',
|
|
37
|
+
attrs: {},
|
|
38
|
+
content: [{ tag: 'profile', attrs: { persona_id: user.personaId! } }]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
parser(node: BinaryNode): BotProfileInfo {
|
|
43
|
+
const botNode = getBinaryNodeChild(node, 'bot')
|
|
44
|
+
const profile = getBinaryNodeChild(botNode, 'profile')
|
|
45
|
+
|
|
46
|
+
const commandsNode = getBinaryNodeChild(profile, 'commands')
|
|
47
|
+
const promptsNode = getBinaryNodeChild(profile, 'prompts')
|
|
48
|
+
|
|
49
|
+
const commands: BotProfileCommand[] = []
|
|
50
|
+
const prompts: string[] = []
|
|
51
|
+
|
|
52
|
+
for (const command of getBinaryNodeChildren(commandsNode, 'command')) {
|
|
53
|
+
commands.push({
|
|
54
|
+
name: getBinaryNodeChildString(command, 'name')!,
|
|
55
|
+
description: getBinaryNodeChildString(command, 'description')!
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const prompt of getBinaryNodeChildren(promptsNode, 'prompt')) {
|
|
60
|
+
prompts.push(`${getBinaryNodeChildString(prompt, 'emoji')!} ${getBinaryNodeChildString(prompt, 'text')!}`)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
isDefault: !!getBinaryNodeChild(profile, 'default'),
|
|
65
|
+
jid: node.attrs.jid!,
|
|
66
|
+
name: getBinaryNodeChildString(profile, 'name')!,
|
|
67
|
+
attributes: getBinaryNodeChildString(profile, 'attributes')!,
|
|
68
|
+
description: getBinaryNodeChildString(profile, 'description')!,
|
|
69
|
+
category: getBinaryNodeChildString(profile, 'category')!,
|
|
70
|
+
personaId: profile!.attrs['persona_id']!,
|
|
71
|
+
commandsDescription: getBinaryNodeChildString(commandsNode, 'description')!,
|
|
72
|
+
commands,
|
|
73
|
+
prompts
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../../Types/USync'
|
|
2
|
+
import type { BinaryNode } from '../../WABinary'
|
|
3
|
+
import type { USyncUser } from '../USyncUser'
|
|
4
|
+
|
|
5
|
+
export class USyncLIDProtocol implements USyncQueryProtocol {
|
|
6
|
+
name = 'lid'
|
|
7
|
+
|
|
8
|
+
getQueryElement(): BinaryNode {
|
|
9
|
+
return {
|
|
10
|
+
tag: 'lid',
|
|
11
|
+
attrs: {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getUserElement(user: USyncUser): BinaryNode | null {
|
|
16
|
+
if (user.lid) {
|
|
17
|
+
return {
|
|
18
|
+
tag: 'lid',
|
|
19
|
+
attrs: { jid: user.lid }
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
return null
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
parser(node: BinaryNode): string | null {
|
|
27
|
+
if (node.tag === 'lid') {
|
|
28
|
+
return node.attrs.val!
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { USyncQueryProtocol } from '../Types/USync'
|
|
2
|
+
import { type BinaryNode, getBinaryNodeChild } from '../WABinary'
|
|
3
|
+
import { USyncBotProfileProtocol } from './Protocols/UsyncBotProfileProtocol'
|
|
4
|
+
import { USyncLIDProtocol } from './Protocols/UsyncLIDProtocol'
|
|
5
|
+
import {
|
|
6
|
+
USyncContactProtocol,
|
|
7
|
+
USyncDeviceProtocol,
|
|
8
|
+
USyncDisappearingModeProtocol,
|
|
9
|
+
USyncStatusProtocol
|
|
10
|
+
} from './Protocols'
|
|
11
|
+
import { USyncUser } from './USyncUser'
|
|
12
|
+
|
|
13
|
+
export type USyncQueryResultList = { [protocol: string]: unknown; id: string }
|
|
14
|
+
|
|
15
|
+
export type USyncQueryResult = {
|
|
16
|
+
list: USyncQueryResultList[]
|
|
17
|
+
sideList: USyncQueryResultList[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class USyncQuery {
|
|
21
|
+
protocols: USyncQueryProtocol[]
|
|
22
|
+
users: USyncUser[]
|
|
23
|
+
context: string
|
|
24
|
+
mode: string
|
|
25
|
+
|
|
26
|
+
constructor() {
|
|
27
|
+
this.protocols = []
|
|
28
|
+
this.users = []
|
|
29
|
+
this.context = 'interactive'
|
|
30
|
+
this.mode = 'query'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
withMode(mode: string) {
|
|
34
|
+
this.mode = mode
|
|
35
|
+
return this
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
withContext(context: string) {
|
|
39
|
+
this.context = context
|
|
40
|
+
return this
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
withUser(user: USyncUser) {
|
|
44
|
+
this.users.push(user)
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
parseUSyncQueryResult(result: BinaryNode | undefined): USyncQueryResult | undefined {
|
|
49
|
+
if (!result || result.attrs.type !== 'result') {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const protocolMap = Object.fromEntries(
|
|
54
|
+
this.protocols.map(protocol => {
|
|
55
|
+
return [protocol.name, protocol.parser]
|
|
56
|
+
})
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
const queryResult: USyncQueryResult = {
|
|
60
|
+
// TODO: implement errors etc.
|
|
61
|
+
list: [],
|
|
62
|
+
sideList: []
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const usyncNode = getBinaryNodeChild(result, 'usync')
|
|
66
|
+
|
|
67
|
+
//TODO: implement error backoff, refresh etc.
|
|
68
|
+
//TODO: see if there are any errors in the result node
|
|
69
|
+
//const resultNode = getBinaryNodeChild(usyncNode, 'result')
|
|
70
|
+
|
|
71
|
+
const listNode = usyncNode ? getBinaryNodeChild(usyncNode, 'list') : undefined
|
|
72
|
+
|
|
73
|
+
if (listNode?.content && Array.isArray(listNode.content)) {
|
|
74
|
+
queryResult.list = listNode.content.reduce((acc: USyncQueryResultList[], node) => {
|
|
75
|
+
const id = node?.attrs.jid
|
|
76
|
+
if (id) {
|
|
77
|
+
const data = Array.isArray(node?.content)
|
|
78
|
+
? Object.fromEntries(
|
|
79
|
+
node.content
|
|
80
|
+
.map(content => {
|
|
81
|
+
const protocol = content.tag
|
|
82
|
+
const parser = protocolMap[protocol]
|
|
83
|
+
if (parser) {
|
|
84
|
+
return [protocol, parser(content)]
|
|
85
|
+
} else {
|
|
86
|
+
return [protocol, null]
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
.filter(([, b]) => b !== null) as [string, unknown][]
|
|
90
|
+
)
|
|
91
|
+
: {}
|
|
92
|
+
acc.push({ ...data, id })
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return acc
|
|
96
|
+
}, [])
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//TODO: implement side list
|
|
100
|
+
//const sideListNode = getBinaryNodeChild(usyncNode, 'side_list')
|
|
101
|
+
return queryResult
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
withDeviceProtocol() {
|
|
105
|
+
this.protocols.push(new USyncDeviceProtocol())
|
|
106
|
+
return this
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
withContactProtocol() {
|
|
110
|
+
this.protocols.push(new USyncContactProtocol())
|
|
111
|
+
return this
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
withStatusProtocol() {
|
|
115
|
+
this.protocols.push(new USyncStatusProtocol())
|
|
116
|
+
return this
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
withDisappearingModeProtocol() {
|
|
120
|
+
this.protocols.push(new USyncDisappearingModeProtocol())
|
|
121
|
+
return this
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
withBotProfileProtocol() {
|
|
125
|
+
this.protocols.push(new USyncBotProfileProtocol())
|
|
126
|
+
return this
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
withLIDProtocol() {
|
|
130
|
+
this.protocols.push(new USyncLIDProtocol())
|
|
131
|
+
return this
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class USyncUser {
|
|
2
|
+
id?: string
|
|
3
|
+
lid?: string
|
|
4
|
+
phone?: string
|
|
5
|
+
type?: string
|
|
6
|
+
personaId?: string
|
|
7
|
+
|
|
8
|
+
withId(id: string) {
|
|
9
|
+
this.id = id
|
|
10
|
+
return this
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
withLid(lid: string) {
|
|
14
|
+
this.lid = lid
|
|
15
|
+
return this
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
withPhone(phone: string) {
|
|
19
|
+
this.phone = phone
|
|
20
|
+
return this
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
withType(type: string) {
|
|
24
|
+
this.type = type
|
|
25
|
+
return this
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
withPersonaId(personaId: string) {
|
|
29
|
+
this.personaId = personaId
|
|
30
|
+
return this
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import makeWASocket from './Socket/index'
|
|
2
|
+
|
|
3
|
+
export * from '../WAProto/index.js'
|
|
4
|
+
export * from './Utils/index'
|
|
5
|
+
export * from './Types/index'
|
|
6
|
+
export * from './Defaults/index'
|
|
7
|
+
export * from './WABinary/index'
|
|
8
|
+
export * from './WAM/index'
|
|
9
|
+
export * from './WAUSync/index'
|
|
10
|
+
|
|
11
|
+
export type WASocket = ReturnType<typeof makeWASocket>
|
|
12
|
+
export { makeWASocket }
|
|
13
|
+
export default makeWASocket
|