gifted-baileys 1.5.4 → 1.5.5
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 +1 -1
- package/WAProto/WAProto.proto +88 -969
- package/WAProto/index.d.ts +1256 -13195
- package/WAProto/index.js +74730 -125106
- package/package.json +9 -27
- package/src/Defaults/baileys-version.json +3 -0
- package/{lib → src}/Defaults/index.js +14 -7
- package/src/Defaults/index.ts +131 -0
- package/src/Signal/libsignal.js +180 -0
- package/src/Signal/libsignal.ts +141 -0
- package/src/Socket/Client/abstract-socket-client.ts +19 -0
- package/{lib → src}/Socket/Client/index.js +3 -2
- package/src/Socket/Client/index.ts +3 -0
- package/src/Socket/Client/mobile-socket-client.js +78 -0
- package/src/Socket/Client/mobile-socket-client.ts +66 -0
- package/src/Socket/Client/web-socket-client.js +75 -0
- package/src/Socket/Client/web-socket-client.ts +57 -0
- package/{lib → src}/Socket/business.js +33 -28
- package/src/Socket/business.ts +281 -0
- package/{lib → src}/Socket/chats.js +176 -174
- package/src/Socket/chats.ts +1030 -0
- package/{lib → src}/Socket/groups.js +68 -80
- package/src/Socket/groups.ts +356 -0
- package/{lib → src}/Socket/index.js +1 -4
- package/src/Socket/index.ts +13 -0
- package/{lib → src}/Socket/messages-recv.js +211 -378
- package/src/Socket/messages-recv.ts +985 -0
- package/{lib → src}/Socket/messages-send.js +177 -452
- package/src/Socket/messages-send.ts +871 -0
- package/{lib → src}/Socket/newsletter.js +98 -107
- package/src/Socket/newsletter.ts +282 -0
- package/{lib → src}/Socket/registration.js +48 -56
- package/src/Socket/registration.ts +250 -0
- package/{lib → src}/Socket/socket.js +77 -77
- package/src/Socket/socket.ts +777 -0
- package/src/Store/index.ts +3 -0
- package/{lib → src}/Store/make-cache-manager-store.js +34 -25
- package/src/Store/make-cache-manager-store.ts +100 -0
- package/{lib → src}/Store/make-in-memory-store.js +32 -36
- package/src/Store/make-in-memory-store.ts +475 -0
- package/src/Store/make-ordered-dictionary.ts +86 -0
- package/{lib → src}/Store/object-repository.js +1 -1
- package/src/Store/object-repository.ts +32 -0
- package/src/Tests/test.app-state-sync.js +204 -0
- package/src/Tests/test.app-state-sync.ts +207 -0
- package/src/Tests/test.event-buffer.js +270 -0
- package/src/Tests/test.event-buffer.ts +319 -0
- package/src/Tests/test.key-store.js +76 -0
- package/src/Tests/test.key-store.ts +92 -0
- package/src/Tests/test.libsignal.js +141 -0
- package/src/Tests/test.libsignal.ts +186 -0
- package/src/Tests/test.media-download.js +93 -0
- package/src/Tests/test.media-download.ts +76 -0
- package/src/Tests/test.messages.js +33 -0
- package/src/Tests/test.messages.ts +37 -0
- package/src/Tests/utils.js +34 -0
- package/src/Tests/utils.ts +36 -0
- package/src/Types/Auth.ts +113 -0
- package/src/Types/Call.ts +15 -0
- package/src/Types/Chat.ts +106 -0
- package/{lib/Types/Contact.d.ts → src/Types/Contact.ts} +9 -8
- package/src/Types/Events.ts +93 -0
- package/src/Types/GroupMetadata.ts +53 -0
- package/src/Types/Label.ts +36 -0
- package/{lib/Types/LabelAssociation.d.ts → src/Types/LabelAssociation.ts} +22 -16
- package/src/Types/Message.ts +288 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +68 -0
- package/{lib/Types/Socket.d.ts → src/Types/Socket.ts} +68 -61
- package/src/Types/State.ts +29 -0
- package/src/Types/index.ts +59 -0
- package/{lib → src}/Utils/auth-utils.js +90 -73
- package/src/Utils/auth-utils.ts +222 -0
- package/src/Utils/baileys-event-stream.js +92 -0
- package/src/Utils/baileys-event-stream.ts +66 -0
- package/{lib → src}/Utils/business.js +43 -15
- package/src/Utils/business.ts +275 -0
- package/{lib → src}/Utils/chat-utils.js +94 -87
- package/src/Utils/chat-utils.ts +860 -0
- package/{lib → src}/Utils/crypto.js +2 -4
- package/src/Utils/crypto.ts +131 -0
- package/src/Utils/decode-wa-message.js +211 -0
- package/src/Utils/decode-wa-message.ts +228 -0
- package/{lib → src}/Utils/event-buffer.js +13 -4
- package/src/Utils/event-buffer.ts +613 -0
- package/{lib → src}/Utils/generics.js +86 -67
- package/src/Utils/generics.ts +434 -0
- package/{lib → src}/Utils/history.js +39 -13
- package/src/Utils/history.ts +112 -0
- package/src/Utils/index.ts +17 -0
- package/{lib → src}/Utils/link-preview.js +54 -17
- package/src/Utils/link-preview.ts +122 -0
- package/src/Utils/logger.ts +3 -0
- package/src/Utils/lt-hash.ts +61 -0
- package/{lib → src}/Utils/make-mutex.js +13 -4
- package/src/Utils/make-mutex.ts +44 -0
- package/{lib → src}/Utils/messages-media.js +255 -193
- package/src/Utils/messages-media.ts +847 -0
- package/{lib → src}/Utils/messages.js +118 -588
- package/src/Utils/messages.ts +956 -0
- package/src/Utils/noise-handler.ts +197 -0
- package/{lib → src}/Utils/process-message.js +30 -27
- package/src/Utils/process-message.ts +414 -0
- package/{lib → src}/Utils/signal.js +42 -25
- package/src/Utils/signal.ts +177 -0
- package/{lib → src}/Utils/use-multi-file-auth-state.js +28 -27
- package/src/Utils/use-multi-file-auth-state.ts +90 -0
- package/{lib → src}/Utils/validate-connection.js +9 -40
- package/src/Utils/validate-connection.ts +238 -0
- package/src/WABinary/constants.ts +42 -0
- package/src/WABinary/decode.ts +265 -0
- package/{lib → src}/WABinary/encode.js +10 -16
- package/src/WABinary/encode.ts +236 -0
- package/src/WABinary/generic-utils.ts +121 -0
- package/src/WABinary/index.ts +5 -0
- package/src/WABinary/jid-utils.ts +68 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.ts +15382 -0
- package/src/WAM/encode.ts +174 -0
- package/src/WAM/index.ts +3 -0
- package/{lib → src}/index.js +0 -1
- package/src/index.ts +13 -0
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/index.d.ts +0 -284
- package/lib/Signal/libsignal.d.ts +0 -3
- package/lib/Signal/libsignal.js +0 -161
- package/lib/Socket/Client/abstract-socket-client.d.ts +0 -15
- package/lib/Socket/Client/index.d.ts +0 -2
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -12
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/types.d.ts +0 -17
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.d.ts +0 -12
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/business.d.ts +0 -170
- package/lib/Socket/chats.d.ts +0 -81
- package/lib/Socket/groups.d.ts +0 -115
- package/lib/Socket/index.d.ts +0 -172
- package/lib/Socket/messages-recv.d.ts +0 -158
- package/lib/Socket/messages-send.d.ts +0 -155
- package/lib/Socket/newsletter.d.ts +0 -132
- package/lib/Socket/registration.d.ts +0 -264
- package/lib/Socket/socket.d.ts +0 -44
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/index.d.ts +0 -3
- package/lib/Store/make-cache-manager-store.d.ts +0 -14
- package/lib/Store/make-in-memory-store.d.ts +0 -118
- package/lib/Store/make-ordered-dictionary.d.ts +0 -13
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -109
- package/lib/Types/Call.d.ts +0 -13
- package/lib/Types/Chat.d.ts +0 -107
- package/lib/Types/Events.d.ts +0 -172
- package/lib/Types/GroupMetadata.d.ts +0 -56
- package/lib/Types/Label.d.ts +0 -46
- package/lib/Types/Message.d.ts +0 -433
- package/lib/Types/Newsletter.d.ts +0 -92
- package/lib/Types/Product.d.ts +0 -78
- package/lib/Types/Signal.d.ts +0 -57
- package/lib/Types/State.d.ts +0 -27
- package/lib/Types/USync.d.ts +0 -25
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
- package/lib/Utils/business.d.ts +0 -22
- package/lib/Utils/chat-utils.d.ts +0 -70
- package/lib/Utils/crypto.d.ts +0 -40
- package/lib/Utils/decode-wa-message.d.ts +0 -36
- package/lib/Utils/decode-wa-message.js +0 -226
- package/lib/Utils/event-buffer.d.ts +0 -35
- package/lib/Utils/generics.d.ts +0 -88
- package/lib/Utils/history.d.ts +0 -19
- package/lib/Utils/index.d.ts +0 -17
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -2
- package/lib/Utils/lt-hash.d.ts +0 -12
- package/lib/Utils/make-mutex.d.ts +0 -7
- package/lib/Utils/messages-media.d.ts +0 -113
- package/lib/Utils/messages.d.ts +0 -77
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -41
- package/lib/Utils/signal.d.ts +0 -33
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -12
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -27
- package/lib/WABinary/decode.d.ts +0 -6
- package/lib/WABinary/encode.d.ts +0 -2
- package/lib/WABinary/generic-utils.d.ts +0 -14
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -31
- package/lib/WABinary/types.d.ts +0 -18
- package/lib/WAM/BinaryInfo.d.ts +0 -8
- package/lib/WAM/constants.d.ts +0 -38
- package/lib/WAM/encode.d.ts +0 -2
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -57
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -30
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -42
- package/lib/WAUSync/Protocols/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/index.js +0 -20
- package/lib/WAUSync/USyncQuery.d.ts +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -79
- package/lib/WAUSync/USyncUser.d.ts +0 -10
- package/lib/WAUSync/USyncUser.js +0 -22
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/WAUSync/index.js +0 -19
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
- /package/{lib → src}/Socket/Client/abstract-socket-client.js +0 -0
- /package/{lib → src}/Store/index.js +0 -0
- /package/{lib → src}/Store/make-ordered-dictionary.js +0 -0
- /package/{lib → src}/Types/Auth.js +0 -0
- /package/{lib → src}/Types/Call.js +0 -0
- /package/{lib → src}/Types/Chat.js +0 -0
- /package/{lib → src}/Types/Contact.js +0 -0
- /package/{lib → src}/Types/Events.js +0 -0
- /package/{lib → src}/Types/GroupMetadata.js +0 -0
- /package/{lib → src}/Types/Label.js +0 -0
- /package/{lib → src}/Types/LabelAssociation.js +0 -0
- /package/{lib → src}/Types/Message.js +0 -0
- /package/{lib → src}/Types/Newsletter.js +0 -0
- /package/{lib → src}/Types/Product.js +0 -0
- /package/{lib → src}/Types/Signal.js +0 -0
- /package/{lib → src}/Types/Socket.js +0 -0
- /package/{lib → src}/Types/State.js +0 -0
- /package/{lib → src}/Types/index.js +0 -0
- /package/{lib → src}/Utils/index.js +0 -0
- /package/{lib → src}/Utils/logger.js +0 -0
- /package/{lib → src}/Utils/lt-hash.js +0 -0
- /package/{lib → src}/Utils/noise-handler.js +0 -0
- /package/{lib → src}/WABinary/constants.js +0 -0
- /package/{lib → src}/WABinary/decode.js +0 -0
- /package/{lib → src}/WABinary/generic-utils.js +0 -0
- /package/{lib → src}/WABinary/index.js +0 -0
- /package/{lib → src}/WABinary/jid-utils.js +0 -0
- /package/{lib → src}/WABinary/types.js +0 -0
- /package/{lib → src}/WAM/BinaryInfo.js +0 -0
- /package/{lib → src}/WAM/constants.js +0 -0
- /package/{lib → src}/WAM/encode.js +0 -0
- /package/{lib → src}/WAM/index.js +0 -0
- /package/{lib → src}/gifted +0 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios'
|
|
2
|
+
import type { Logger } from 'pino'
|
|
3
|
+
import { proto } from '../../WAProto'
|
|
4
|
+
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, SocketConfig, WAMessageStubType } from '../Types'
|
|
5
|
+
import { getContentType, normalizeMessageContent } from '../Utils/messages'
|
|
6
|
+
import { areJidsSameUser, isJidBroadcast, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
|
|
7
|
+
import { aesDecryptGCM, hmacSign } from './crypto'
|
|
8
|
+
import { getKeyAuthor, toNumber } from './generics'
|
|
9
|
+
import { downloadAndProcessHistorySyncNotification } from './history'
|
|
10
|
+
|
|
11
|
+
type ProcessMessageContext = {
|
|
12
|
+
shouldProcessHistoryMsg: boolean
|
|
13
|
+
creds: AuthenticationCreds
|
|
14
|
+
keyStore: SignalKeyStoreWithTransaction
|
|
15
|
+
ev: BaileysEventEmitter
|
|
16
|
+
getMessage: SocketConfig['getMessage']
|
|
17
|
+
logger?: Logger
|
|
18
|
+
options: AxiosRequestConfig<{}>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const REAL_MSG_STUB_TYPES = new Set([
|
|
22
|
+
WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
23
|
+
WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
24
|
+
WAMessageStubType.CALL_MISSED_VIDEO,
|
|
25
|
+
WAMessageStubType.CALL_MISSED_VOICE
|
|
26
|
+
])
|
|
27
|
+
|
|
28
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
|
|
29
|
+
WAMessageStubType.GROUP_PARTICIPANT_ADD
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
/** Cleans a received message to further processing */
|
|
33
|
+
export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
|
|
34
|
+
// ensure remoteJid and participant doesn't have device or agent in it
|
|
35
|
+
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid!)
|
|
36
|
+
message.key.participant = message.key.participant ? jidNormalizedUser(message.key.participant!) : undefined
|
|
37
|
+
const content = normalizeMessageContent(message.message)
|
|
38
|
+
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
|
|
39
|
+
if(content?.reactionMessage) {
|
|
40
|
+
normaliseKey(content.reactionMessage.key!)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if(content?.pollUpdateMessage) {
|
|
44
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey!)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function normaliseKey(msgKey: proto.IMessageKey) {
|
|
48
|
+
// if the reaction is from another user
|
|
49
|
+
// we've to correctly map the key to this user's perspective
|
|
50
|
+
if(!message.key.fromMe) {
|
|
51
|
+
// if the sender believed the message being reacted to is not from them
|
|
52
|
+
// we've to correct the key to be from them, or some other participant
|
|
53
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
54
|
+
? areJidsSameUser(msgKey.participant || msgKey.remoteJid!, meId)
|
|
55
|
+
// if the message being reacted to, was from them
|
|
56
|
+
// fromMe automatically becomes false
|
|
57
|
+
: false
|
|
58
|
+
// set the remoteJid to being the same as the chat the message came from
|
|
59
|
+
msgKey.remoteJid = message.key.remoteJid
|
|
60
|
+
// set participant of the message
|
|
61
|
+
msgKey.participant = msgKey.participant || message.key.participant
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const isRealMessage = (message: proto.IWebMessageInfo, meId: string) => {
|
|
67
|
+
const normalizedContent = normalizeMessageContent(message.message)
|
|
68
|
+
const hasSomeContent = !!getContentType(normalizedContent)
|
|
69
|
+
return (
|
|
70
|
+
!!normalizedContent
|
|
71
|
+
|| REAL_MSG_STUB_TYPES.has(message.messageStubType!)
|
|
72
|
+
|| (
|
|
73
|
+
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType!)
|
|
74
|
+
&& message.messageStubParameters?.some(p => areJidsSameUser(meId, p))
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
&& hasSomeContent
|
|
78
|
+
&& !normalizedContent?.protocolMessage
|
|
79
|
+
&& !normalizedContent?.reactionMessage
|
|
80
|
+
&& !normalizedContent?.pollUpdateMessage
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => (
|
|
84
|
+
!message.key.fromMe && !message.messageStubType
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the ID of the chat from the given key.
|
|
89
|
+
* Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
|
|
90
|
+
*/
|
|
91
|
+
export const getChatId = ({ remoteJid, participant, fromMe }: proto.IMessageKey) => {
|
|
92
|
+
if(
|
|
93
|
+
isJidBroadcast(remoteJid!)
|
|
94
|
+
&& !isJidStatusBroadcast(remoteJid!)
|
|
95
|
+
&& !fromMe
|
|
96
|
+
) {
|
|
97
|
+
return participant!
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return remoteJid!
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type PollContext = {
|
|
104
|
+
/** normalised jid of the person that created the poll */
|
|
105
|
+
pollCreatorJid: string
|
|
106
|
+
/** ID of the poll creation message */
|
|
107
|
+
pollMsgId: string
|
|
108
|
+
/** poll creation message enc key */
|
|
109
|
+
pollEncKey: Uint8Array
|
|
110
|
+
/** jid of the person that voted */
|
|
111
|
+
voterJid: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Decrypt a poll vote
|
|
116
|
+
* @param vote encrypted vote
|
|
117
|
+
* @param ctx additional info about the poll required for decryption
|
|
118
|
+
* @returns list of SHA256 options
|
|
119
|
+
*/
|
|
120
|
+
export function decryptPollVote(
|
|
121
|
+
{ encPayload, encIv }: proto.Message.IPollEncValue,
|
|
122
|
+
{
|
|
123
|
+
pollCreatorJid,
|
|
124
|
+
pollMsgId,
|
|
125
|
+
pollEncKey,
|
|
126
|
+
voterJid,
|
|
127
|
+
}: PollContext
|
|
128
|
+
) {
|
|
129
|
+
const sign = Buffer.concat(
|
|
130
|
+
[
|
|
131
|
+
toBinary(pollMsgId),
|
|
132
|
+
toBinary(pollCreatorJid),
|
|
133
|
+
toBinary(voterJid),
|
|
134
|
+
toBinary('Poll Vote'),
|
|
135
|
+
new Uint8Array([1])
|
|
136
|
+
]
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
const key0 = hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
|
|
140
|
+
const decKey = hmacSign(sign, key0, 'sha256')
|
|
141
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
|
|
142
|
+
|
|
143
|
+
const decrypted = aesDecryptGCM(encPayload!, decKey, encIv!, aad)
|
|
144
|
+
return proto.Message.PollVoteMessage.decode(decrypted)
|
|
145
|
+
|
|
146
|
+
function toBinary(txt: string) {
|
|
147
|
+
return Buffer.from(txt)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const processMessage = async(
|
|
152
|
+
message: proto.IWebMessageInfo,
|
|
153
|
+
{
|
|
154
|
+
shouldProcessHistoryMsg,
|
|
155
|
+
ev,
|
|
156
|
+
creds,
|
|
157
|
+
keyStore,
|
|
158
|
+
logger,
|
|
159
|
+
options,
|
|
160
|
+
getMessage
|
|
161
|
+
}: ProcessMessageContext
|
|
162
|
+
) => {
|
|
163
|
+
const meId = creds.me!.id
|
|
164
|
+
const { accountSettings } = creds
|
|
165
|
+
|
|
166
|
+
const chat: Partial<Chat> = { id: jidNormalizedUser(getChatId(message.key)) }
|
|
167
|
+
const isRealMsg = isRealMessage(message, meId)
|
|
168
|
+
|
|
169
|
+
if(isRealMsg) {
|
|
170
|
+
chat.conversationTimestamp = toNumber(message.messageTimestamp)
|
|
171
|
+
// only increment unread count if not CIPHERTEXT and from another person
|
|
172
|
+
if(shouldIncrementChatUnread(message)) {
|
|
173
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const content = normalizeMessageContent(message.message)
|
|
178
|
+
|
|
179
|
+
// unarchive chat if it's a real message, or someone reacted to our message
|
|
180
|
+
// and we've the unarchive chats setting on
|
|
181
|
+
if(
|
|
182
|
+
(isRealMsg || content?.reactionMessage?.key?.fromMe)
|
|
183
|
+
&& accountSettings?.unarchiveChats
|
|
184
|
+
) {
|
|
185
|
+
chat.archived = false
|
|
186
|
+
chat.readOnly = false
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const protocolMsg = content?.protocolMessage
|
|
190
|
+
if(protocolMsg) {
|
|
191
|
+
switch (protocolMsg.type) {
|
|
192
|
+
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
193
|
+
const histNotification = protocolMsg!.historySyncNotification!
|
|
194
|
+
const process = shouldProcessHistoryMsg
|
|
195
|
+
const isLatest = !creds.processedHistoryMessages?.length
|
|
196
|
+
|
|
197
|
+
logger?.info({
|
|
198
|
+
histNotification,
|
|
199
|
+
process,
|
|
200
|
+
id: message.key.id,
|
|
201
|
+
isLatest,
|
|
202
|
+
}, 'got history notification')
|
|
203
|
+
|
|
204
|
+
if(process) {
|
|
205
|
+
ev.emit('creds.update', {
|
|
206
|
+
processedHistoryMessages: [
|
|
207
|
+
...(creds.processedHistoryMessages || []),
|
|
208
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
209
|
+
]
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
const data = await downloadAndProcessHistorySyncNotification(
|
|
213
|
+
histNotification,
|
|
214
|
+
options
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
ev.emit('messaging-history.set', { ...data, isLatest })
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
break
|
|
221
|
+
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
222
|
+
const keys = protocolMsg.appStateSyncKeyShare!.keys
|
|
223
|
+
if(keys?.length) {
|
|
224
|
+
let newAppStateSyncKeyId = ''
|
|
225
|
+
await keyStore.transaction(
|
|
226
|
+
async() => {
|
|
227
|
+
const newKeys: string[] = []
|
|
228
|
+
for(const { keyData, keyId } of keys) {
|
|
229
|
+
const strKeyId = Buffer.from(keyId!.keyId!).toString('base64')
|
|
230
|
+
newKeys.push(strKeyId)
|
|
231
|
+
|
|
232
|
+
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData! } })
|
|
233
|
+
|
|
234
|
+
newAppStateSyncKeyId = strKeyId
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
logger?.info(
|
|
238
|
+
{ newAppStateSyncKeyId, newKeys },
|
|
239
|
+
'injecting new app state sync keys'
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
|
|
245
|
+
} else {
|
|
246
|
+
logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
break
|
|
250
|
+
case proto.Message.ProtocolMessage.Type.REVOKE:
|
|
251
|
+
ev.emit('messages.update', [
|
|
252
|
+
{
|
|
253
|
+
key: {
|
|
254
|
+
...message.key,
|
|
255
|
+
id: protocolMsg.key!.id
|
|
256
|
+
},
|
|
257
|
+
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
|
|
258
|
+
}
|
|
259
|
+
])
|
|
260
|
+
break
|
|
261
|
+
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
262
|
+
Object.assign(chat, {
|
|
263
|
+
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
|
|
264
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
265
|
+
})
|
|
266
|
+
break
|
|
267
|
+
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
268
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage!
|
|
269
|
+
if(response) {
|
|
270
|
+
const { peerDataOperationResult } = response
|
|
271
|
+
for(const result of peerDataOperationResult!) {
|
|
272
|
+
const { placeholderMessageResendResponse: retryResponse } = result
|
|
273
|
+
if(retryResponse) {
|
|
274
|
+
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes!)
|
|
275
|
+
ev.emit('messages.update', [
|
|
276
|
+
{ key: webMessageInfo.key, update: { message: webMessageInfo.message } }
|
|
277
|
+
])
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
break
|
|
283
|
+
}
|
|
284
|
+
} else if(content?.reactionMessage) {
|
|
285
|
+
const reaction: proto.IReaction = {
|
|
286
|
+
...content.reactionMessage,
|
|
287
|
+
key: message.key,
|
|
288
|
+
}
|
|
289
|
+
ev.emit('messages.reaction', [{
|
|
290
|
+
reaction,
|
|
291
|
+
key: content.reactionMessage!.key!,
|
|
292
|
+
}])
|
|
293
|
+
} else if(message.messageStubType) {
|
|
294
|
+
const jid = message.key!.remoteJid!
|
|
295
|
+
//let actor = whatsappID (message.participant)
|
|
296
|
+
let participants: string[]
|
|
297
|
+
const emitParticipantsUpdate = (action: ParticipantAction) => (
|
|
298
|
+
ev.emit('group-participants.update', { id: jid, author: message.participant!, participants, action })
|
|
299
|
+
)
|
|
300
|
+
const emitGroupUpdate = (update: Partial<GroupMetadata>) => {
|
|
301
|
+
ev.emit('groups.update', [{ id: jid, ...update, author: message.participant ?? undefined }])
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid))
|
|
305
|
+
|
|
306
|
+
switch (message.messageStubType) {
|
|
307
|
+
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
308
|
+
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
309
|
+
participants = message.messageStubParameters || []
|
|
310
|
+
emitParticipantsUpdate('remove')
|
|
311
|
+
// mark the chat read only if you left the group
|
|
312
|
+
if(participantsIncludesMe()) {
|
|
313
|
+
chat.readOnly = true
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
break
|
|
317
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
318
|
+
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
319
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
320
|
+
participants = message.messageStubParameters || []
|
|
321
|
+
if(participantsIncludesMe()) {
|
|
322
|
+
chat.readOnly = false
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
emitParticipantsUpdate('add')
|
|
326
|
+
break
|
|
327
|
+
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
328
|
+
participants = message.messageStubParameters || []
|
|
329
|
+
emitParticipantsUpdate('demote')
|
|
330
|
+
break
|
|
331
|
+
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
332
|
+
participants = message.messageStubParameters || []
|
|
333
|
+
emitParticipantsUpdate('promote')
|
|
334
|
+
break
|
|
335
|
+
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
336
|
+
const announceValue = message.messageStubParameters?.[0]
|
|
337
|
+
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
|
|
338
|
+
break
|
|
339
|
+
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
340
|
+
const restrictValue = message.messageStubParameters?.[0]
|
|
341
|
+
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
|
|
342
|
+
break
|
|
343
|
+
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
344
|
+
const name = message.messageStubParameters?.[0]
|
|
345
|
+
chat.name = name
|
|
346
|
+
emitGroupUpdate({ subject: name })
|
|
347
|
+
break
|
|
348
|
+
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
349
|
+
const code = message.messageStubParameters?.[0]
|
|
350
|
+
emitGroupUpdate({ inviteCode: code })
|
|
351
|
+
break
|
|
352
|
+
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
353
|
+
const memberAddValue = message.messageStubParameters?.[0]
|
|
354
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
|
|
355
|
+
break
|
|
356
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
357
|
+
const approvalMode = message.messageStubParameters?.[0]
|
|
358
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
|
|
359
|
+
break
|
|
360
|
+
}
|
|
361
|
+
} else if(content?.pollUpdateMessage) {
|
|
362
|
+
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey!
|
|
363
|
+
// we need to fetch the poll creation message to get the poll enc key
|
|
364
|
+
const pollMsg = await getMessage(creationMsgKey)
|
|
365
|
+
if(pollMsg) {
|
|
366
|
+
const meIdNormalised = jidNormalizedUser(meId)
|
|
367
|
+
const pollCreatorJid = getKeyAuthor(creationMsgKey, meIdNormalised)
|
|
368
|
+
const voterJid = getKeyAuthor(message.key!, meIdNormalised)
|
|
369
|
+
const pollEncKey = pollMsg.messageContextInfo?.messageSecret!
|
|
370
|
+
|
|
371
|
+
try {
|
|
372
|
+
const voteMsg = decryptPollVote(
|
|
373
|
+
content.pollUpdateMessage.vote!,
|
|
374
|
+
{
|
|
375
|
+
pollEncKey,
|
|
376
|
+
pollCreatorJid,
|
|
377
|
+
pollMsgId: creationMsgKey.id!,
|
|
378
|
+
voterJid,
|
|
379
|
+
}
|
|
380
|
+
)
|
|
381
|
+
ev.emit('messages.update', [
|
|
382
|
+
{
|
|
383
|
+
key: creationMsgKey,
|
|
384
|
+
update: {
|
|
385
|
+
pollUpdates: [
|
|
386
|
+
{
|
|
387
|
+
pollUpdateMessageKey: message.key,
|
|
388
|
+
vote: voteMsg,
|
|
389
|
+
senderTimestampMs: (content.pollUpdateMessage.senderTimestampMs! as Long).toNumber(),
|
|
390
|
+
}
|
|
391
|
+
]
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
])
|
|
395
|
+
} catch(err) {
|
|
396
|
+
logger?.warn(
|
|
397
|
+
{ err, creationMsgKey },
|
|
398
|
+
'failed to decrypt poll vote'
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
} else {
|
|
402
|
+
logger?.warn(
|
|
403
|
+
{ creationMsgKey },
|
|
404
|
+
'poll creation message not found, cannot decrypt update'
|
|
405
|
+
)
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if(Object.keys(chat).length > 1) {
|
|
410
|
+
ev.emit('chats.update', [chat])
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export default processMessage
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids = exports.parseAndInjectE2ESessions = exports.xmppPreKey = exports.xmppSignedPreKey = exports.generateOrGetPreKeys = exports.getPreKeys = exports.createSignalIdentity = void 0;
|
|
4
13
|
const Defaults_1 = require("../Defaults");
|
|
@@ -12,13 +21,13 @@ const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
|
12
21
|
};
|
|
13
22
|
};
|
|
14
23
|
exports.createSignalIdentity = createSignalIdentity;
|
|
15
|
-
const getPreKeys =
|
|
24
|
+
const getPreKeys = (_a, min_1, limit_1) => __awaiter(void 0, [_a, min_1, limit_1], void 0, function* ({ get }, min, limit) {
|
|
16
25
|
const idList = [];
|
|
17
26
|
for (let id = min; id < limit; id++) {
|
|
18
27
|
idList.push(id.toString());
|
|
19
28
|
}
|
|
20
29
|
return get('pre-key', idList);
|
|
21
|
-
};
|
|
30
|
+
});
|
|
22
31
|
exports.getPreKeys = getPreKeys;
|
|
23
32
|
const generateOrGetPreKeys = (creds, range) => {
|
|
24
33
|
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
@@ -56,7 +65,7 @@ const xmppPreKey = (pair, id) => ({
|
|
|
56
65
|
]
|
|
57
66
|
});
|
|
58
67
|
exports.xmppPreKey = xmppPreKey;
|
|
59
|
-
const parseAndInjectE2ESessions =
|
|
68
|
+
const parseAndInjectE2ESessions = (node, repository) => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
69
|
const extractKey = (key) => (key ? ({
|
|
61
70
|
keyId: (0, WABinary_1.getBinaryNodeChildUInt)(key, 'id', 3),
|
|
62
71
|
publicKey: (0, crypto_1.generateSignalPubKey)((0, WABinary_1.getBinaryNodeChildBuffer)(key, 'value')),
|
|
@@ -66,13 +75,13 @@ const parseAndInjectE2ESessions = async (node, repository) => {
|
|
|
66
75
|
for (const node of nodes) {
|
|
67
76
|
(0, WABinary_1.assertNodeErrorFree)(node);
|
|
68
77
|
}
|
|
69
|
-
|
|
78
|
+
yield Promise.all(nodes.map((node) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
79
|
const signedKey = (0, WABinary_1.getBinaryNodeChild)(node, 'skey');
|
|
71
80
|
const key = (0, WABinary_1.getBinaryNodeChild)(node, 'key');
|
|
72
81
|
const identity = (0, WABinary_1.getBinaryNodeChildBuffer)(node, 'identity');
|
|
73
82
|
const jid = node.attrs.jid;
|
|
74
83
|
const registrationId = (0, WABinary_1.getBinaryNodeChildUInt)(node, 'registration', 4);
|
|
75
|
-
|
|
84
|
+
yield repository.injectE2ESession({
|
|
76
85
|
jid,
|
|
77
86
|
session: {
|
|
78
87
|
registrationId: registrationId,
|
|
@@ -81,23 +90,31 @@ const parseAndInjectE2ESessions = async (node, repository) => {
|
|
|
81
90
|
preKey: extractKey(key)
|
|
82
91
|
}
|
|
83
92
|
});
|
|
84
|
-
}));
|
|
85
|
-
};
|
|
93
|
+
})));
|
|
94
|
+
});
|
|
86
95
|
exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
|
|
87
96
|
const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
|
|
97
|
+
var _a;
|
|
88
98
|
const { user: myUser, device: myDevice } = (0, WABinary_1.jidDecode)(myJid);
|
|
89
99
|
const extracted = [];
|
|
90
|
-
for (const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
(
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
for (const node of result.content) {
|
|
101
|
+
const list = (_a = (0, WABinary_1.getBinaryNodeChild)(node, 'list')) === null || _a === void 0 ? void 0 : _a.content;
|
|
102
|
+
if (list && Array.isArray(list)) {
|
|
103
|
+
for (const item of list) {
|
|
104
|
+
const { user } = (0, WABinary_1.jidDecode)(item.attrs.jid);
|
|
105
|
+
const devicesNode = (0, WABinary_1.getBinaryNodeChild)(item, 'devices');
|
|
106
|
+
const deviceListNode = (0, WABinary_1.getBinaryNodeChild)(devicesNode, 'device-list');
|
|
107
|
+
if (Array.isArray(deviceListNode === null || deviceListNode === void 0 ? void 0 : deviceListNode.content)) {
|
|
108
|
+
for (const { tag, attrs } of deviceListNode.content) {
|
|
109
|
+
const device = +attrs.id;
|
|
110
|
+
if (tag === 'device' && // ensure the "device" tag
|
|
111
|
+
(!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
112
|
+
(myUser !== user || myDevice !== device) && // either different user or if me user, not this device
|
|
113
|
+
(device === 0 || !!attrs['key-index']) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
|
114
|
+
) {
|
|
115
|
+
extracted.push({ user, device });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
101
118
|
}
|
|
102
119
|
}
|
|
103
120
|
}
|
|
@@ -109,20 +126,20 @@ exports.extractDeviceJids = extractDeviceJids;
|
|
|
109
126
|
* get the next N keys for upload or processing
|
|
110
127
|
* @param count number of pre-keys to get or generate
|
|
111
128
|
*/
|
|
112
|
-
const getNextPreKeys =
|
|
129
|
+
const getNextPreKeys = (_a, count_1) => __awaiter(void 0, [_a, count_1], void 0, function* ({ creds, keys }, count) {
|
|
113
130
|
const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
|
|
114
131
|
const update = {
|
|
115
132
|
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
116
133
|
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
117
134
|
};
|
|
118
|
-
|
|
119
|
-
const preKeys =
|
|
135
|
+
yield keys.set({ 'pre-key': newPreKeys });
|
|
136
|
+
const preKeys = yield (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
120
137
|
return { update, preKeys };
|
|
121
|
-
};
|
|
138
|
+
});
|
|
122
139
|
exports.getNextPreKeys = getNextPreKeys;
|
|
123
|
-
const getNextPreKeysNode =
|
|
140
|
+
const getNextPreKeysNode = (state, count) => __awaiter(void 0, void 0, void 0, function* () {
|
|
124
141
|
const { creds } = state;
|
|
125
|
-
const { update, preKeys } =
|
|
142
|
+
const { update, preKeys } = yield (0, exports.getNextPreKeys)(state, count);
|
|
126
143
|
const node = {
|
|
127
144
|
tag: 'iq',
|
|
128
145
|
attrs: {
|
|
@@ -139,5 +156,5 @@ const getNextPreKeysNode = async (state, count) => {
|
|
|
139
156
|
]
|
|
140
157
|
};
|
|
141
158
|
return { update, node };
|
|
142
|
-
};
|
|
159
|
+
});
|
|
143
160
|
exports.getNextPreKeysNode = getNextPreKeysNode;
|