libzapitu 1.0.0-alpha.3

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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1294 -0
  3. package/WAProto/GenerateStatics.sh +2 -0
  4. package/WAProto/WAProto.proto +4633 -0
  5. package/WAProto/index.d.ts +53473 -0
  6. package/WAProto/index.js +165029 -0
  7. package/engine-requirements.js +10 -0
  8. package/lib/Defaults/baileys-version.json +3 -0
  9. package/lib/Defaults/index.d.ts +51 -0
  10. package/lib/Defaults/index.js +105 -0
  11. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  12. package/lib/Signal/Group/ciphertext-message.js +15 -0
  13. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  14. package/lib/Signal/Group/group-session-builder.js +64 -0
  15. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  16. package/lib/Signal/Group/group_cipher.js +96 -0
  17. package/lib/Signal/Group/index.d.ts +11 -0
  18. package/lib/Signal/Group/index.js +57 -0
  19. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  20. package/lib/Signal/Group/keyhelper.js +55 -0
  21. package/lib/Signal/Group/queue-job.d.ts +1 -0
  22. package/lib/Signal/Group/queue-job.js +57 -0
  23. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  24. package/lib/Signal/Group/sender-chain-key.js +34 -0
  25. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  26. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  27. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  28. package/lib/Signal/Group/sender-key-message.js +69 -0
  29. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  30. package/lib/Signal/Group/sender-key-name.js +51 -0
  31. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  32. package/lib/Signal/Group/sender-key-record.js +53 -0
  33. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  34. package/lib/Signal/Group/sender-key-state.js +99 -0
  35. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  36. package/lib/Signal/Group/sender-message-key.js +29 -0
  37. package/lib/Signal/libsignal.d.ts +3 -0
  38. package/lib/Signal/libsignal.js +174 -0
  39. package/lib/Socket/Client/index.d.ts +2 -0
  40. package/lib/Socket/Client/index.js +18 -0
  41. package/lib/Socket/Client/types.d.ts +15 -0
  42. package/lib/Socket/Client/types.js +13 -0
  43. package/lib/Socket/Client/websocket.d.ts +12 -0
  44. package/lib/Socket/Client/websocket.js +62 -0
  45. package/lib/Socket/business.d.ts +153 -0
  46. package/lib/Socket/business.js +260 -0
  47. package/lib/Socket/chats.d.ts +90 -0
  48. package/lib/Socket/chats.js +898 -0
  49. package/lib/Socket/groups.d.ts +132 -0
  50. package/lib/Socket/groups.js +331 -0
  51. package/lib/Socket/index.d.ts +153 -0
  52. package/lib/Socket/index.js +10 -0
  53. package/lib/Socket/messages-recv.d.ts +142 -0
  54. package/lib/Socket/messages-recv.js +1089 -0
  55. package/lib/Socket/messages-send.d.ts +137 -0
  56. package/lib/Socket/messages-send.js +818 -0
  57. package/lib/Socket/mex.d.ts +2 -0
  58. package/lib/Socket/mex.js +46 -0
  59. package/lib/Socket/newsletter.d.ts +143 -0
  60. package/lib/Socket/newsletter.js +183 -0
  61. package/lib/Socket/socket.d.ts +42 -0
  62. package/lib/Socket/socket.js +616 -0
  63. package/lib/Socket/usync.d.ts +36 -0
  64. package/lib/Socket/usync.js +65 -0
  65. package/lib/Types/Auth.d.ts +103 -0
  66. package/lib/Types/Auth.js +2 -0
  67. package/lib/Types/Call.d.ts +13 -0
  68. package/lib/Types/Call.js +2 -0
  69. package/lib/Types/Chat.d.ts +115 -0
  70. package/lib/Types/Chat.js +10 -0
  71. package/lib/Types/Contact.d.ts +23 -0
  72. package/lib/Types/Contact.js +2 -0
  73. package/lib/Types/Events.d.ts +198 -0
  74. package/lib/Types/Events.js +2 -0
  75. package/lib/Types/GroupMetadata.d.ts +63 -0
  76. package/lib/Types/GroupMetadata.js +2 -0
  77. package/lib/Types/Label.d.ts +46 -0
  78. package/lib/Types/Label.js +27 -0
  79. package/lib/Types/LabelAssociation.d.ts +29 -0
  80. package/lib/Types/LabelAssociation.js +9 -0
  81. package/lib/Types/Message.d.ts +279 -0
  82. package/lib/Types/Message.js +7 -0
  83. package/lib/Types/Newsletter.d.ts +134 -0
  84. package/lib/Types/Newsletter.js +33 -0
  85. package/lib/Types/Product.d.ts +78 -0
  86. package/lib/Types/Product.js +2 -0
  87. package/lib/Types/Signal.d.ts +57 -0
  88. package/lib/Types/Signal.js +2 -0
  89. package/lib/Types/Socket.d.ts +121 -0
  90. package/lib/Types/Socket.js +2 -0
  91. package/lib/Types/State.d.ts +27 -0
  92. package/lib/Types/State.js +2 -0
  93. package/lib/Types/USync.d.ts +25 -0
  94. package/lib/Types/USync.js +2 -0
  95. package/lib/Types/index.d.ts +64 -0
  96. package/lib/Types/index.js +42 -0
  97. package/lib/Utils/auth-utils.d.ts +18 -0
  98. package/lib/Utils/auth-utils.js +199 -0
  99. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  100. package/lib/Utils/baileys-event-stream.js +63 -0
  101. package/lib/Utils/business.d.ts +22 -0
  102. package/lib/Utils/business.js +240 -0
  103. package/lib/Utils/chat-utils.d.ts +70 -0
  104. package/lib/Utils/chat-utils.js +753 -0
  105. package/lib/Utils/crypto.d.ts +40 -0
  106. package/lib/Utils/crypto.js +187 -0
  107. package/lib/Utils/decode-wa-message.d.ts +36 -0
  108. package/lib/Utils/decode-wa-message.js +236 -0
  109. package/lib/Utils/event-buffer.d.ts +35 -0
  110. package/lib/Utils/event-buffer.js +516 -0
  111. package/lib/Utils/generics.d.ts +91 -0
  112. package/lib/Utils/generics.js +397 -0
  113. package/lib/Utils/history.d.ts +19 -0
  114. package/lib/Utils/history.js +93 -0
  115. package/lib/Utils/index.d.ts +17 -0
  116. package/lib/Utils/index.js +33 -0
  117. package/lib/Utils/link-preview.d.ts +21 -0
  118. package/lib/Utils/link-preview.js +122 -0
  119. package/lib/Utils/logger.d.ts +11 -0
  120. package/lib/Utils/logger.js +7 -0
  121. package/lib/Utils/lt-hash.d.ts +12 -0
  122. package/lib/Utils/lt-hash.js +51 -0
  123. package/lib/Utils/make-mutex.d.ts +7 -0
  124. package/lib/Utils/make-mutex.js +44 -0
  125. package/lib/Utils/messages-media.d.ts +111 -0
  126. package/lib/Utils/messages-media.js +701 -0
  127. package/lib/Utils/messages.d.ts +76 -0
  128. package/lib/Utils/messages.js +816 -0
  129. package/lib/Utils/noise-handler.d.ts +19 -0
  130. package/lib/Utils/noise-handler.js +150 -0
  131. package/lib/Utils/process-message.d.ts +41 -0
  132. package/lib/Utils/process-message.js +377 -0
  133. package/lib/Utils/signal.d.ts +33 -0
  134. package/lib/Utils/signal.js +159 -0
  135. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
  136. package/lib/Utils/use-multi-file-auth-state.js +124 -0
  137. package/lib/Utils/validate-connection.d.ts +10 -0
  138. package/lib/Utils/validate-connection.js +170 -0
  139. package/lib/WABinary/constants.d.ts +27 -0
  140. package/lib/WABinary/constants.js +1303 -0
  141. package/lib/WABinary/decode.d.ts +6 -0
  142. package/lib/WABinary/decode.js +266 -0
  143. package/lib/WABinary/encode.d.ts +2 -0
  144. package/lib/WABinary/encode.js +252 -0
  145. package/lib/WABinary/generic-utils.d.ts +14 -0
  146. package/lib/WABinary/generic-utils.js +110 -0
  147. package/lib/WABinary/index.d.ts +5 -0
  148. package/lib/WABinary/index.js +21 -0
  149. package/lib/WABinary/jid-utils.d.ts +36 -0
  150. package/lib/WABinary/jid-utils.js +66 -0
  151. package/lib/WABinary/types.d.ts +18 -0
  152. package/lib/WABinary/types.js +2 -0
  153. package/lib/WAM/BinaryInfo.d.ts +8 -0
  154. package/lib/WAM/BinaryInfo.js +13 -0
  155. package/lib/WAM/constants.d.ts +39 -0
  156. package/lib/WAM/constants.js +15243 -0
  157. package/lib/WAM/encode.d.ts +2 -0
  158. package/lib/WAM/encode.js +153 -0
  159. package/lib/WAM/index.d.ts +3 -0
  160. package/lib/WAM/index.js +19 -0
  161. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  162. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  163. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  164. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  165. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  166. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  167. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  168. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  169. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  170. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  171. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  172. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  173. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  174. package/lib/WAUSync/Protocols/index.js +20 -0
  175. package/lib/WAUSync/USyncQuery.d.ts +28 -0
  176. package/lib/WAUSync/USyncQuery.js +93 -0
  177. package/lib/WAUSync/USyncUser.d.ts +12 -0
  178. package/lib/WAUSync/USyncUser.js +26 -0
  179. package/lib/WAUSync/index.d.ts +3 -0
  180. package/lib/WAUSync/index.js +19 -0
  181. package/lib/index.d.ts +11 -0
  182. package/lib/index.js +30 -0
  183. package/package.json +102 -0
@@ -0,0 +1,818 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeMessagesSocket = void 0;
7
+ const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
8
+ const boom_1 = require("@hapi/boom");
9
+ const WAProto_1 = require("../../WAProto");
10
+ const Defaults_1 = require("../Defaults");
11
+ var ListType = WAProto_1.proto.Message.ListMessage.ListType;
12
+ const Utils_1 = require("../Utils");
13
+ const link_preview_1 = require("../Utils/link-preview");
14
+ const WABinary_1 = require("../WABinary");
15
+ const WAUSync_1 = require("../WAUSync");
16
+ const groups_1 = require("./groups");
17
+ const makeMessagesSocket = (config) => {
18
+ const { logger, linkPreviewImageThumbnailWidth, generateHighQualityLinkPreview, options: axiosOptions, patchMessageBeforeSending, cachedGroupMetadata } = config;
19
+ const sock = (0, groups_1.makeGroupsSocket)(config);
20
+ const { ev, authState, processingMutex, signalRepository, upsertMessage, query, fetchPrivacySettings, sendNode, groupMetadata, groupToggleEphemeral } = sock;
21
+ const userDevicesCache = config.userDevicesCache ||
22
+ new node_cache_1.default({
23
+ stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.USER_DEVICES,
24
+ useClones: false
25
+ });
26
+ const lidCache = new node_cache_1.default({
27
+ stdTTL: 3600, // 1 hour
28
+ useClones: false
29
+ });
30
+ let mediaConn;
31
+ const refreshMediaConn = async (forceGet = false) => {
32
+ const media = await mediaConn;
33
+ if (!media || forceGet || new Date().getTime() - media.fetchDate.getTime() > media.ttl * 1000) {
34
+ mediaConn = (async () => {
35
+ const result = await query({
36
+ tag: 'iq',
37
+ attrs: {
38
+ type: 'set',
39
+ xmlns: 'w:m',
40
+ to: WABinary_1.S_WHATSAPP_NET
41
+ },
42
+ content: [{ tag: 'media_conn', attrs: {} }]
43
+ });
44
+ const mediaConnNode = (0, WABinary_1.getBinaryNodeChild)(result, 'media_conn');
45
+ const node = {
46
+ hosts: (0, WABinary_1.getBinaryNodeChildren)(mediaConnNode, 'host').map(({ attrs }) => ({
47
+ hostname: attrs.hostname,
48
+ maxContentLengthBytes: +attrs.maxContentLengthBytes
49
+ })),
50
+ auth: mediaConnNode.attrs.auth,
51
+ ttl: +mediaConnNode.attrs.ttl,
52
+ fetchDate: new Date()
53
+ };
54
+ logger.debug('fetched media conn');
55
+ return node;
56
+ })();
57
+ }
58
+ return mediaConn;
59
+ };
60
+ /**
61
+ * generic send receipt function
62
+ * used for receipts of phone call, read, delivery etc.
63
+ * */
64
+ const sendReceipt = async (jid, participant, messageIds, type) => {
65
+ const node = {
66
+ tag: 'receipt',
67
+ attrs: {
68
+ id: messageIds[0]
69
+ }
70
+ };
71
+ const isReadReceipt = type === 'read' || type === 'read-self';
72
+ if (isReadReceipt) {
73
+ node.attrs.t = (0, Utils_1.unixTimestampSeconds)().toString();
74
+ }
75
+ if (type === 'sender' && (0, WABinary_1.isJidUser)(jid)) {
76
+ node.attrs.recipient = jid;
77
+ node.attrs.to = participant;
78
+ }
79
+ else {
80
+ node.attrs.to = jid;
81
+ if (participant) {
82
+ node.attrs.participant = participant;
83
+ }
84
+ }
85
+ if (type) {
86
+ node.attrs.type = type;
87
+ }
88
+ const remainingMessageIds = messageIds.slice(1);
89
+ if (remainingMessageIds.length) {
90
+ node.content = [
91
+ {
92
+ tag: 'list',
93
+ attrs: {},
94
+ content: remainingMessageIds.map(id => ({
95
+ tag: 'item',
96
+ attrs: { id }
97
+ }))
98
+ }
99
+ ];
100
+ }
101
+ logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages');
102
+ await sendNode(node);
103
+ };
104
+ /** Correctly bulk send receipts to multiple chats, participants */
105
+ const sendReceipts = async (keys, type) => {
106
+ const recps = (0, Utils_1.aggregateMessageKeysNotFromMe)(keys);
107
+ for (const { jid, participant, messageIds } of recps) {
108
+ await sendReceipt(jid, participant, messageIds, type);
109
+ }
110
+ };
111
+ /** Bulk read messages. Keys can be from different chats & participants */
112
+ const readMessages = async (keys) => {
113
+ const privacySettings = await fetchPrivacySettings();
114
+ // based on privacy settings, we have to change the read type
115
+ const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self';
116
+ await sendReceipts(keys, readType);
117
+ };
118
+ /** Fetch all the devices we've to send a message to */
119
+ const getUSyncDevices = async (jids, useCache, ignoreZeroDevices) => {
120
+ var _a, _b;
121
+ const deviceResults = [];
122
+ if (!useCache) {
123
+ logger.debug('not using cache for devices');
124
+ }
125
+ const toFetch = [];
126
+ jids = Array.from(new Set(jids));
127
+ for (let jid of jids) {
128
+ const user = (_a = (0, WABinary_1.jidDecode)(jid)) === null || _a === void 0 ? void 0 : _a.user;
129
+ jid = (0, WABinary_1.jidNormalizedUser)(jid);
130
+ if (useCache) {
131
+ const devices = userDevicesCache.get(user);
132
+ if (devices) {
133
+ deviceResults.push(...devices);
134
+ logger.trace({ user }, 'using cache for devices');
135
+ }
136
+ else {
137
+ toFetch.push(jid);
138
+ }
139
+ }
140
+ else {
141
+ toFetch.push(jid);
142
+ }
143
+ }
144
+ if (!toFetch.length) {
145
+ return deviceResults;
146
+ }
147
+ const query = new WAUSync_1.USyncQuery().withContext('message').withDeviceProtocol();
148
+ for (const jid of toFetch) {
149
+ query.withUser(new WAUSync_1.USyncUser().withId(jid));
150
+ }
151
+ const result = await sock.executeUSyncQuery(query);
152
+ if (result) {
153
+ const extracted = (0, Utils_1.extractDeviceJids)(result === null || result === void 0 ? void 0 : result.list, authState.creds.me.id, ignoreZeroDevices, (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.lid);
154
+ const deviceMap = {};
155
+ for (const item of extracted) {
156
+ deviceMap[item.user] = deviceMap[item.user] || [];
157
+ deviceMap[item.user].push(item);
158
+ deviceResults.push(item);
159
+ }
160
+ for (const key in deviceMap) {
161
+ userDevicesCache.set(key, deviceMap[key]);
162
+ }
163
+ }
164
+ return deviceResults;
165
+ };
166
+ const assertSessions = async (jids, force, lids) => {
167
+ var _a, _b;
168
+ let didFetchNewSession = false;
169
+ const melid = (0, WABinary_1.jidNormalizedUser)((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid);
170
+ const meid = (0, WABinary_1.jidNormalizedUser)((_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.id);
171
+ let jidsRequiringFetch = [];
172
+ if (force) {
173
+ jidsRequiringFetch = jids;
174
+ }
175
+ else {
176
+ const addrs = jids.map(jid => signalRepository.jidToSignalProtocolAddress((0, Utils_1.convertlidDevice)(jid, lids, meid, melid)));
177
+ const sessions = await authState.keys.get('session', addrs);
178
+ for (const jid of jids) {
179
+ const signalId = signalRepository.jidToSignalProtocolAddress((0, Utils_1.convertlidDevice)(jid, lids, meid, melid));
180
+ if (!sessions[signalId]) {
181
+ jidsRequiringFetch.push(jid);
182
+ }
183
+ }
184
+ }
185
+ if (jidsRequiringFetch.length) {
186
+ logger.debug({ jidsRequiringFetch }, 'fetching sessions');
187
+ const result = await query({
188
+ tag: 'iq',
189
+ attrs: {
190
+ xmlns: 'encrypt',
191
+ type: 'get',
192
+ to: WABinary_1.S_WHATSAPP_NET
193
+ },
194
+ content: [
195
+ {
196
+ tag: 'key',
197
+ attrs: {},
198
+ content: jidsRequiringFetch.map(jid => ({
199
+ tag: 'user',
200
+ attrs: { jid }
201
+ }))
202
+ }
203
+ ]
204
+ });
205
+ await (0, Utils_1.parseAndInjectE2ESessions)(result, signalRepository, lids, meid, melid);
206
+ didFetchNewSession = true;
207
+ }
208
+ return didFetchNewSession;
209
+ };
210
+ const sendPeerDataOperationMessage = async (pdoMessage) => {
211
+ var _a;
212
+ //TODO: for later, abstract the logic to send a Peer Message instead of just PDO - useful for App State Key Resync with phone
213
+ if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
214
+ throw new boom_1.Boom('Not authenticated');
215
+ }
216
+ const protocolMessage = {
217
+ protocolMessage: {
218
+ peerDataOperationRequestMessage: pdoMessage,
219
+ type: WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_MESSAGE
220
+ }
221
+ };
222
+ const meJid = (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id);
223
+ const msgId = await relayMessage(meJid, protocolMessage, {
224
+ additionalAttributes: {
225
+ category: 'peer',
226
+ // eslint-disable-next-line camelcase
227
+ push_priority: 'high_force'
228
+ }
229
+ });
230
+ return msgId;
231
+ };
232
+ const createParticipantNodes = async (jids, message, extraAttrs, lid, meid, melid) => {
233
+ let patched = await patchMessageBeforeSending(message, jids);
234
+ if (!Array.isArray(patched)) {
235
+ patched = jids ? jids.map(jid => ({ recipientJid: jid, ...patched })) : [patched];
236
+ }
237
+ let shouldIncludeDeviceIdentity = false;
238
+ const nodes = await Promise.all(patched.map(async (patchedMessageWithJid) => {
239
+ const { recipientJid: jid, ...patchedMessage } = patchedMessageWithJid;
240
+ if (!jid) {
241
+ return {};
242
+ }
243
+ const bytes = (0, Utils_1.encodeWAMessage)(patchedMessage);
244
+ const { type, ciphertext } = await signalRepository.encryptMessage({ jid: (0, Utils_1.convertlidDevice)(jid, lid, meid, melid), data: bytes });
245
+ if (type === 'pkmsg') {
246
+ shouldIncludeDeviceIdentity = true;
247
+ }
248
+ const node = {
249
+ tag: 'to',
250
+ attrs: { jid },
251
+ content: [
252
+ {
253
+ tag: 'enc',
254
+ attrs: {
255
+ v: '2',
256
+ type,
257
+ ...(extraAttrs || {})
258
+ },
259
+ content: ciphertext
260
+ }
261
+ ]
262
+ };
263
+ return node;
264
+ }));
265
+ return { nodes, shouldIncludeDeviceIdentity };
266
+ };
267
+ const getLid = async (jid) => {
268
+ var _a;
269
+ const cachedLid = lidCache.get(jid);
270
+ if (cachedLid) {
271
+ return cachedLid;
272
+ }
273
+ const usyncQuery = new WAUSync_1.USyncQuery()
274
+ .withContactProtocol()
275
+ .withLIDProtocol()
276
+ .withUser(new WAUSync_1.USyncUser().withPhone(jid.split('@')[0]));
277
+ const results = await sock.executeUSyncQuery(usyncQuery);
278
+ if (results === null || results === void 0 ? void 0 : results.list) {
279
+ const maybeLid = (_a = results.list[0]) === null || _a === void 0 ? void 0 : _a.lid;
280
+ if (typeof maybeLid === 'string') {
281
+ lidCache.set(jid, maybeLid);
282
+ return maybeLid;
283
+ }
284
+ }
285
+ return null;
286
+ };
287
+ const relayMessage = async (jid, message, { messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, useCachedGroupMetadata, statusJidList, isretry }) => {
288
+ var _a, _b, _c;
289
+ const meId = authState.creds.me.id;
290
+ const meLid = authState.creds.me.lid || authState.creds.me.id;
291
+ const lidattrs = (0, WABinary_1.jidDecode)((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid);
292
+ const jlidUser = lidattrs === null || lidattrs === void 0 ? void 0 : lidattrs.user;
293
+ let lids;
294
+ if ((0, WABinary_1.isJidUser)(jid) || (0, WABinary_1.isJidUser)(participant === null || participant === void 0 ? void 0 : participant.jid)) {
295
+ const userQuery = (0, WABinary_1.jidNormalizedUser)((participant === null || participant === void 0 ? void 0 : participant.jid) || jid);
296
+ if (!(0, WABinary_1.isLidUser)(userQuery)) {
297
+ const verify = lidCache.get(userQuery);
298
+ if (verify) {
299
+ lids = verify;
300
+ }
301
+ else {
302
+ const usyncQuery = new WAUSync_1.USyncQuery().withContactProtocol().withLIDProtocol();
303
+ usyncQuery.withUser(new WAUSync_1.USyncUser().withPhone(userQuery.split('@')[0]));
304
+ const results = await sock.executeUSyncQuery(usyncQuery);
305
+ if (results === null || results === void 0 ? void 0 : results.list) {
306
+ const maybeLid = (_b = results.list[0]) === null || _b === void 0 ? void 0 : _b.lid;
307
+ if (typeof maybeLid === 'string') {
308
+ lidCache.set(userQuery, maybeLid);
309
+ lids = maybeLid;
310
+ }
311
+ }
312
+ }
313
+ }
314
+ }
315
+ const { user, server } = (0, WABinary_1.jidDecode)(jid);
316
+ const statusJid = 'status@broadcast';
317
+ const isGroup = server === 'g.us';
318
+ const isStatus = jid === statusJid;
319
+ const isLid = server === 'lid';
320
+ let shouldIncludeDeviceIdentity = false;
321
+ msgId = msgId || (0, Utils_1.generateMessageIDV2)((_c = sock.user) === null || _c === void 0 ? void 0 : _c.id);
322
+ useUserDevicesCache = useUserDevicesCache !== false;
323
+ useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus;
324
+ const participants = [];
325
+ const destinationJid = !isStatus ? (0, WABinary_1.jidEncode)(user, isLid ? 'lid' : isGroup ? 'g.us' : 's.whatsapp.net') : statusJid;
326
+ const binaryNodeContent = [];
327
+ const devices = [];
328
+ const meMsg = {
329
+ deviceSentMessage: {
330
+ destinationJid,
331
+ message
332
+ }
333
+ };
334
+ const extraAttrs = {};
335
+ if (participant) {
336
+ if (!isGroup && !isStatus) {
337
+ additionalAttributes = { ...additionalAttributes, device_fanout: 'false' };
338
+ }
339
+ const { user, device, } = (0, WABinary_1.jidDecode)(participant.jid);
340
+ devices.push({ user, device, jid: (0, WABinary_1.jidNormalizedUser)(participant.jid) });
341
+ }
342
+ await authState.keys.transaction(async () => {
343
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
344
+ const mediaType = getMediaType(message);
345
+ if (mediaType) {
346
+ extraAttrs['mediatype'] = mediaType;
347
+ }
348
+ if ((_a = (0, Utils_1.normalizeMessageContent)(message)) === null || _a === void 0 ? void 0 : _a.pinInChatMessage) {
349
+ extraAttrs['decrypt-fail'] = 'hide';
350
+ }
351
+ if (isGroup || isStatus) {
352
+ const [groupData, senderKeyMap] = await Promise.all([
353
+ (async () => {
354
+ let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined;
355
+ if (groupData && Array.isArray(groupData === null || groupData === void 0 ? void 0 : groupData.participants)) {
356
+ logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata');
357
+ }
358
+ else if (!isStatus) {
359
+ groupData = await groupMetadata(jid);
360
+ }
361
+ return groupData;
362
+ })(),
363
+ (async () => {
364
+ if (!participant && !isStatus) {
365
+ const result = await authState.keys.get('sender-key-memory', [jid]);
366
+ return result[jid] || {};
367
+ }
368
+ return {};
369
+ })()
370
+ ]);
371
+ if (!participant) {
372
+ const participantsList = (groupData && !isStatus) ? groupData.participants.map(p => p.lid || p.id) : [];
373
+ if (isStatus && statusJidList) {
374
+ participantsList.push(...statusJidList);
375
+ }
376
+ if (!isStatus) {
377
+ additionalAttributes = {
378
+ ...additionalAttributes,
379
+ addressing_mode: (groupData === null || groupData === void 0 ? void 0 : groupData.addressingMode) || 'pn'
380
+ };
381
+ }
382
+ const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false);
383
+ devices.push(...additionalDevices);
384
+ const Mephone = additionalDevices.some(d => d.user === jlidUser && d.device === 0);
385
+ if (!Mephone) {
386
+ devices.push({ user: jlidUser, device: 0, jid: (0, WABinary_1.jidNormalizedUser)(meLid) });
387
+ }
388
+ }
389
+ const patched = await patchMessageBeforeSending(message);
390
+ if (Array.isArray(patched)) {
391
+ throw new boom_1.Boom('Per-jid patching is not supported in groups');
392
+ }
393
+ const bytes = (0, Utils_1.encodeWAMessage)(patched);
394
+ const { ciphertext, senderKeyDistributionMessage } = await signalRepository.encryptGroupMessage({
395
+ group: destinationJid,
396
+ data: bytes,
397
+ meId: meLid
398
+ });
399
+ const senderKeyJids = [];
400
+ for (const { user, device, jid } of devices) {
401
+ const server = ((_b = (0, WABinary_1.jidDecode)(jid)) === null || _b === void 0 ? void 0 : _b.server) || 'lid';
402
+ const senderId = (0, WABinary_1.jidEncode)(user, server, device);
403
+ senderKeyJids.push(senderId);
404
+ senderKeyMap[senderId] = true;
405
+ }
406
+ // if there are some participants with whom the session has not been established
407
+ // if there are, we re-send the senderkey
408
+ if (senderKeyJids.length) {
409
+ logger.debug({ senderKeyJids }, 'sending new sender key');
410
+ const senderKeyMsg = {
411
+ senderKeyDistributionMessage: {
412
+ axolotlSenderKeyDistributionMessage: senderKeyDistributionMessage,
413
+ groupId: destinationJid
414
+ }
415
+ };
416
+ await assertSessions(senderKeyJids, isretry ? true : false, lids);
417
+ const result = await createParticipantNodes(senderKeyJids, senderKeyMsg, extraAttrs, lids, meId, meLid);
418
+ shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity;
419
+ participants.push(...result.nodes);
420
+ }
421
+ binaryNodeContent.push({
422
+ tag: 'enc',
423
+ attrs: { v: '2', type: 'skmsg' },
424
+ content: ciphertext
425
+ });
426
+ await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } });
427
+ }
428
+ else {
429
+ const { user: meUser, device: meDevice } = (0, WABinary_1.jidDecode)(meId);
430
+ if (!participant) {
431
+ devices.push({ user, device: 0, jid });
432
+ if (meDevice !== undefined && meDevice !== 0) {
433
+ if ((0, WABinary_1.isLidUser)(jid) && jlidUser) {
434
+ devices.push({ user: jlidUser, device: 0, jid: (0, WABinary_1.jidNormalizedUser)(meLid) });
435
+ const additionalDevices = await getUSyncDevices([jid, meLid], !!useUserDevicesCache, true);
436
+ devices.push(...additionalDevices);
437
+ }
438
+ else {
439
+ devices.push({ user: meUser, device: 0, jid: (0, WABinary_1.jidNormalizedUser)(meId) });
440
+ const additionalDevices = await getUSyncDevices([jid, meId], !!useUserDevicesCache, true);
441
+ devices.push(...additionalDevices);
442
+ }
443
+ }
444
+ }
445
+ const allJids = [];
446
+ const meJids = [];
447
+ const otherJids = [];
448
+ for (const { user, device, jid } of devices) {
449
+ const isMe = user === meUser;
450
+ const ismeLid = user === jlidUser;
451
+ const server = ((_c = (0, WABinary_1.jidDecode)(jid)) === null || _c === void 0 ? void 0 : _c.server) || 'lid';
452
+ const senderId = (0, WABinary_1.jidEncode)(user, server, device);
453
+ if (isMe || ismeLid) {
454
+ meJids.push(senderId);
455
+ }
456
+ else {
457
+ otherJids.push(senderId);
458
+ }
459
+ allJids.push(senderId);
460
+ }
461
+ await assertSessions(allJids, isretry ? true : false, lids);
462
+ const [{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }] = await Promise.all([
463
+ createParticipantNodes(meJids, meMsg, extraAttrs, lids, meId, meLid),
464
+ createParticipantNodes(otherJids, message, extraAttrs, lids, meId, meLid)
465
+ ]);
466
+ participants.push(...meNodes);
467
+ participants.push(...otherNodes);
468
+ shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2;
469
+ }
470
+ if (participants.length) {
471
+ if ((additionalAttributes === null || additionalAttributes === void 0 ? void 0 : additionalAttributes['category']) === 'peer') {
472
+ const peerNode = (_e = (_d = participants[0]) === null || _d === void 0 ? void 0 : _d.content) === null || _e === void 0 ? void 0 : _e[0];
473
+ if (peerNode) {
474
+ binaryNodeContent.push(peerNode); // push only enc
475
+ }
476
+ }
477
+ else {
478
+ binaryNodeContent.push({
479
+ tag: 'participants',
480
+ attrs: {},
481
+ content: participants
482
+ });
483
+ }
484
+ }
485
+ const stanza = {
486
+ tag: 'message',
487
+ attrs: {
488
+ id: msgId,
489
+ type: getMessageType(message),
490
+ ...(additionalAttributes || {})
491
+ },
492
+ content: binaryNodeContent
493
+ };
494
+ // if the participant to send to is explicitly specified (generally retry recp)
495
+ // ensure the message is only sent to that person
496
+ // if a retry receipt is sent to everyone -- it'll fail decryption for everyone else who received the msg
497
+ if (participant) {
498
+ if ((0, WABinary_1.isJidGroup)(destinationJid)) {
499
+ stanza.attrs.to = destinationJid;
500
+ stanza.attrs.participant = participant.jid;
501
+ }
502
+ else if ((0, WABinary_1.areJidsSameUser)(participant.jid, meId)) {
503
+ stanza.attrs.to = participant.jid;
504
+ stanza.attrs.recipient = destinationJid;
505
+ }
506
+ else {
507
+ stanza.attrs.to = participant.jid;
508
+ }
509
+ }
510
+ else {
511
+ stanza.attrs.to = destinationJid;
512
+ }
513
+ if (shouldIncludeDeviceIdentity) {
514
+ ;
515
+ stanza.content.push({
516
+ tag: 'device-identity',
517
+ attrs: {},
518
+ content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
519
+ });
520
+ logger.debug({ jid }, 'adding device identity');
521
+ }
522
+ if (additionalNodes && additionalNodes.length > 0) {
523
+ ;
524
+ stanza.content.push(...additionalNodes);
525
+ }
526
+ const content = (0, Utils_1.normalizeMessageContent)(message);
527
+ const contentType = (0, Utils_1.getContentType)(content);
528
+ if (((0, WABinary_1.isJidGroup)(jid) || (0, WABinary_1.isJidUser)(jid)) || (0, WABinary_1.isLidUser)(jid) && (contentType === 'interactiveMessage' ||
529
+ contentType === 'buttonsMessage' ||
530
+ contentType === 'listMessage')) {
531
+ const bizNode = { tag: 'biz', attrs: {} };
532
+ if ((((_g = (_f = message === null || message === void 0 ? void 0 : message.viewOnceMessage) === null || _f === void 0 ? void 0 : _f.message) === null || _g === void 0 ? void 0 : _g.interactiveMessage) || ((_j = (_h = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2) === null || _h === void 0 ? void 0 : _h.message) === null || _j === void 0 ? void 0 : _j.interactiveMessage) || ((_l = (_k = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension) === null || _k === void 0 ? void 0 : _k.message) === null || _l === void 0 ? void 0 : _l.interactiveMessage) || (message === null || message === void 0 ? void 0 : message.interactiveMessage)) || (((_o = (_m = message === null || message === void 0 ? void 0 : message.viewOnceMessage) === null || _m === void 0 ? void 0 : _m.message) === null || _o === void 0 ? void 0 : _o.buttonsMessage) || ((_q = (_p = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2) === null || _p === void 0 ? void 0 : _p.message) === null || _q === void 0 ? void 0 : _q.buttonsMessage) || ((_s = (_r = message === null || message === void 0 ? void 0 : message.viewOnceMessageV2Extension) === null || _r === void 0 ? void 0 : _r.message) === null || _s === void 0 ? void 0 : _s.buttonsMessage) || (message === null || message === void 0 ? void 0 : message.buttonsMessage))) {
533
+ bizNode.content = [{
534
+ tag: 'interactive',
535
+ attrs: {
536
+ type: 'native_flow',
537
+ v: '1'
538
+ },
539
+ content: [{
540
+ tag: 'native_flow',
541
+ attrs: { v: '9', name: 'mixed' }
542
+ }]
543
+ }];
544
+ }
545
+ else if (message === null || message === void 0 ? void 0 : message.listMessage) {
546
+ // list message only support in private chat
547
+ bizNode.content = [{
548
+ tag: 'list',
549
+ attrs: {
550
+ type: 'product_list',
551
+ v: '2'
552
+ }
553
+ }];
554
+ }
555
+ stanza.content.push(bizNode);
556
+ }
557
+ logger.debug({ msgId }, `sending message to ${participants.length} devices`);
558
+ await sendNode(stanza);
559
+ });
560
+ return msgId;
561
+ };
562
+ const getMessageType = (message) => {
563
+ if (message.pollCreationMessage || message.pollCreationMessageV2 || message.pollCreationMessageV3) {
564
+ return 'poll';
565
+ }
566
+ return 'text';
567
+ };
568
+ const getMediaType = (message) => {
569
+ if (message.imageMessage) {
570
+ return 'image';
571
+ }
572
+ else if (message.videoMessage) {
573
+ return message.videoMessage.gifPlayback ? 'gif' : 'video';
574
+ }
575
+ else if (message.audioMessage) {
576
+ return message.audioMessage.ptt ? 'ptt' : 'audio';
577
+ }
578
+ else if (message.contactMessage) {
579
+ return 'vcard';
580
+ }
581
+ else if (message.documentMessage) {
582
+ return 'document';
583
+ }
584
+ else if (message.contactsArrayMessage) {
585
+ return 'contact_array';
586
+ }
587
+ else if (message.liveLocationMessage) {
588
+ return 'livelocation';
589
+ }
590
+ else if (message.stickerMessage) {
591
+ return 'sticker';
592
+ }
593
+ else if (message.listMessage) {
594
+ return 'list';
595
+ }
596
+ else if (message.listResponseMessage) {
597
+ return 'list_response';
598
+ }
599
+ else if (message.buttonsResponseMessage) {
600
+ return 'buttons_response';
601
+ }
602
+ else if (message.orderMessage) {
603
+ return 'order';
604
+ }
605
+ else if (message.productMessage) {
606
+ return 'product';
607
+ }
608
+ else if (message.interactiveResponseMessage) {
609
+ return 'native_flow_response';
610
+ }
611
+ else if (message.groupInviteMessage) {
612
+ return 'url';
613
+ }
614
+ };
615
+ const getButtonType = (message) => {
616
+ if (message.buttonsMessage) {
617
+ return 'buttons';
618
+ }
619
+ else if (message.buttonsResponseMessage) {
620
+ return 'buttons_response';
621
+ }
622
+ else if (message.interactiveResponseMessage) {
623
+ return 'interactive_response';
624
+ }
625
+ else if (message.listMessage) {
626
+ return 'list';
627
+ }
628
+ else if (message.listResponseMessage) {
629
+ return 'list_response';
630
+ }
631
+ };
632
+ const getButtonArgs = (message) => {
633
+ if (message.templateMessage) {
634
+ // TODO: Add attributes
635
+ return {};
636
+ }
637
+ else if (message.listMessage) {
638
+ const type = message.listMessage.listType;
639
+ if (!type) {
640
+ throw new boom_1.Boom('Expected list type inside message');
641
+ }
642
+ return { v: '2', type: ListType[type].toLowerCase() };
643
+ }
644
+ else {
645
+ return {};
646
+ }
647
+ };
648
+ const getPrivacyTokens = async (jids) => {
649
+ const t = (0, Utils_1.unixTimestampSeconds)().toString();
650
+ const result = await query({
651
+ tag: 'iq',
652
+ attrs: {
653
+ to: WABinary_1.S_WHATSAPP_NET,
654
+ type: 'set',
655
+ xmlns: 'privacy'
656
+ },
657
+ content: [
658
+ {
659
+ tag: 'tokens',
660
+ attrs: {},
661
+ content: jids.map(jid => ({
662
+ tag: 'token',
663
+ attrs: {
664
+ jid: (0, WABinary_1.jidNormalizedUser)(jid),
665
+ t,
666
+ type: 'trusted_contact'
667
+ }
668
+ }))
669
+ }
670
+ ]
671
+ });
672
+ return result;
673
+ };
674
+ const waUploadToServer = (0, Utils_1.getWAUploadToServer)(config, refreshMediaConn);
675
+ const waitForMsgMediaUpdate = (0, Utils_1.bindWaitForEvent)(ev, 'messages.media-update');
676
+ return {
677
+ ...sock,
678
+ getPrivacyTokens,
679
+ assertSessions,
680
+ relayMessage,
681
+ sendReceipt,
682
+ sendReceipts,
683
+ readMessages,
684
+ refreshMediaConn,
685
+ waUploadToServer,
686
+ fetchPrivacySettings,
687
+ sendPeerDataOperationMessage,
688
+ createParticipantNodes,
689
+ getUSyncDevices,
690
+ updateMediaMessage: async (message) => {
691
+ const content = (0, Utils_1.assertMediaContent)(message.message);
692
+ const mediaKey = content.mediaKey;
693
+ const meId = authState.creds.me.id;
694
+ const node = await (0, Utils_1.encryptMediaRetryRequest)(message.key, mediaKey, meId);
695
+ let error = undefined;
696
+ await Promise.all([
697
+ sendNode(node),
698
+ waitForMsgMediaUpdate(async (update) => {
699
+ const result = update.find(c => c.key.id === message.key.id);
700
+ if (result) {
701
+ if (result.error) {
702
+ error = result.error;
703
+ }
704
+ else {
705
+ try {
706
+ const media = await (0, Utils_1.decryptMediaRetryData)(result.media, mediaKey, result.key.id);
707
+ if (media.result !== WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS) {
708
+ const resultStr = WAProto_1.proto.MediaRetryNotification.ResultType[media.result];
709
+ throw new boom_1.Boom(`Media re-upload failed by device (${resultStr})`, {
710
+ data: media,
711
+ statusCode: (0, Utils_1.getStatusCodeForMediaRetry)(media.result) || 404
712
+ });
713
+ }
714
+ content.directPath = media.directPath;
715
+ content.url = (0, Utils_1.getUrlFromDirectPath)(content.directPath);
716
+ logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful');
717
+ }
718
+ catch (err) {
719
+ error = err;
720
+ }
721
+ }
722
+ return true;
723
+ }
724
+ })
725
+ ]);
726
+ if (error) {
727
+ throw error;
728
+ }
729
+ ev.emit('messages.update', [{ key: message.key, update: { message: message.message } }]);
730
+ return message;
731
+ },
732
+ sendMessage: async (jid, content, options = {}) => {
733
+ var _a, _b, _c;
734
+ const userJid = authState.creds.me.id;
735
+ if (typeof content === 'object' &&
736
+ 'disappearingMessagesInChat' in content &&
737
+ typeof content['disappearingMessagesInChat'] !== 'undefined' &&
738
+ (0, WABinary_1.isJidGroup)(jid)) {
739
+ const { disappearingMessagesInChat } = content;
740
+ const value = typeof disappearingMessagesInChat === 'boolean'
741
+ ? disappearingMessagesInChat
742
+ ? Defaults_1.WA_DEFAULT_EPHEMERAL
743
+ : 0
744
+ : disappearingMessagesInChat;
745
+ await groupToggleEphemeral(jid, value);
746
+ }
747
+ else {
748
+ const fullMsg = await (0, Utils_1.generateWAMessage)(jid, content, {
749
+ logger,
750
+ userJid,
751
+ getUrlInfo: text => (0, link_preview_1.getUrlInfo)(text, {
752
+ thumbnailWidth: linkPreviewImageThumbnailWidth,
753
+ fetchOpts: {
754
+ timeout: 3000,
755
+ ...(axiosOptions || {})
756
+ },
757
+ logger,
758
+ uploadImage: generateHighQualityLinkPreview ? waUploadToServer : undefined
759
+ }),
760
+ //TODO: CACHE
761
+ getProfilePicUrl: sock.profilePictureUrl,
762
+ upload: waUploadToServer,
763
+ mediaCache: config.mediaCache,
764
+ options: config.options,
765
+ messageId: (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id),
766
+ ...options
767
+ });
768
+ const isDeleteMsg = 'delete' in content && !!content.delete;
769
+ const isEditMsg = 'edit' in content && !!content.edit;
770
+ const isPinMsg = 'pin' in content && !!content.pin;
771
+ const isPollMessage = 'poll' in content && !!content.poll;
772
+ const additionalAttributes = {};
773
+ const additionalNodes = [];
774
+ // required for delete
775
+ if (isDeleteMsg) {
776
+ // if the chat is a group, and I am not the author, then delete the message as an admin
777
+ if ((0, WABinary_1.isJidGroup)((_b = content.delete) === null || _b === void 0 ? void 0 : _b.remoteJid) && !((_c = content.delete) === null || _c === void 0 ? void 0 : _c.fromMe)) {
778
+ additionalAttributes.edit = '8';
779
+ }
780
+ else {
781
+ additionalAttributes.edit = '7';
782
+ }
783
+ }
784
+ else if (isEditMsg) {
785
+ additionalAttributes.edit = '1';
786
+ }
787
+ else if (isPinMsg) {
788
+ additionalAttributes.edit = '2';
789
+ }
790
+ else if (isPollMessage) {
791
+ additionalNodes.push({
792
+ tag: 'meta',
793
+ attrs: {
794
+ polltype: 'creation'
795
+ }
796
+ });
797
+ }
798
+ if ('cachedGroupMetadata' in options) {
799
+ console.warn('cachedGroupMetadata in sendMessage are deprecated, now cachedGroupMetadata is part of the socket config.');
800
+ }
801
+ await relayMessage(jid, fullMsg.message, {
802
+ messageId: fullMsg.key.id,
803
+ useCachedGroupMetadata: options.useCachedGroupMetadata,
804
+ additionalAttributes,
805
+ statusJidList: options.statusJidList,
806
+ additionalNodes
807
+ });
808
+ if (config.emitOwnEvents) {
809
+ process.nextTick(() => {
810
+ processingMutex.mutex(() => upsertMessage(fullMsg, 'append'));
811
+ });
812
+ }
813
+ return fullMsg;
814
+ }
815
+ }
816
+ };
817
+ };
818
+ exports.makeMessagesSocket = makeMessagesSocket;