fca-nx 6.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +21 -0
- package/README.md +446 -0
- package/e2ee/config.json +0 -0
- package/e2ee/crypto.js +173 -0
- package/e2ee/index.js +1032 -0
- package/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/e2ee/proto/ConsumerApplication.proto +232 -0
- package/e2ee/proto/MessageApplication.proto +82 -0
- package/e2ee/proto/MessageTransport.proto +77 -0
- package/e2ee/proto/WACommon.proto +66 -0
- package/e2ee/proto/WAMediaTransport.proto +176 -0
- package/e2ee/protocol.js +196 -0
- package/e2ee/ratchet.js +219 -0
- package/e2ee/store.js +182 -0
- package/e2ee/vendor/fb-e2ee.cjs +9159 -0
- package/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/e2ee/vendor/proto/WACommon.proto +66 -0
- package/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/func/checkUpdate.js +7 -0
- package/func/logAdapter.js +33 -0
- package/func/logger.js +134 -0
- package/index.js +22 -0
- package/module/config.js +45 -0
- package/module/login.js +183 -0
- package/module/loginHelper.js +1382 -0
- package/module/options.js +52 -0
- package/package.json +101 -0
- package/src/api/action/addExternalModule.js +25 -0
- package/src/api/action/addFriend.js +145 -0
- package/src/api/action/changeAvatar.js +137 -0
- package/src/api/action/changeBio.js +75 -0
- package/src/api/action/changeCover.js +277 -0
- package/src/api/action/enableAutoSaveAppState.js +73 -0
- package/src/api/action/getCurrentUserID.js +7 -0
- package/src/api/action/handleFriendRequest.js +57 -0
- package/src/api/action/logout.js +76 -0
- package/src/api/action/refreshFb_dtsg.js +48 -0
- package/src/api/action/setPostReaction.js +106 -0
- package/src/api/action/unfriend.js +54 -0
- package/src/api/http/httpGet.js +46 -0
- package/src/api/http/httpPost.js +52 -0
- package/src/api/http/postFormData.js +47 -0
- package/src/api/messaging/addUserToGroup.js +68 -0
- package/src/api/messaging/changeAdminStatus.js +126 -0
- package/src/api/messaging/changeArchivedStatus.js +55 -0
- package/src/api/messaging/changeBlockedStatus.js +48 -0
- package/src/api/messaging/changeGroupImage.js +91 -0
- package/src/api/messaging/changeNickname.js +70 -0
- package/src/api/messaging/changeThreadColor.js +79 -0
- package/src/api/messaging/changeThreadEmoji.js +111 -0
- package/src/api/messaging/createNewGroup.js +88 -0
- package/src/api/messaging/createPoll.js +46 -0
- package/src/api/messaging/createThemeAI.js +98 -0
- package/src/api/messaging/deleteMessage.js +136 -0
- package/src/api/messaging/deleteThread.js +56 -0
- package/src/api/messaging/editMessage.js +90 -0
- package/src/api/messaging/forwardAttachment.js +57 -0
- package/src/api/messaging/forwardMessage.js +134 -0
- package/src/api/messaging/getEmojiUrl.js +29 -0
- package/src/api/messaging/getFriendsList.js +82 -0
- package/src/api/messaging/getMessage.js +829 -0
- package/src/api/messaging/getThemePictures.js +62 -0
- package/src/api/messaging/handleMessageRequest.js +65 -0
- package/src/api/messaging/markAsDelivered.js +57 -0
- package/src/api/messaging/markAsRead.js +88 -0
- package/src/api/messaging/markAsReadAll.js +49 -0
- package/src/api/messaging/markAsSeen.js +61 -0
- package/src/api/messaging/muteThread.js +50 -0
- package/src/api/messaging/pinMessage.js +115 -0
- package/src/api/messaging/removeUserFromGroup.js +62 -0
- package/src/api/messaging/resolvePhotoUrl.js +43 -0
- package/src/api/messaging/scheduler.js +264 -0
- package/src/api/messaging/searchForThread.js +53 -0
- package/src/api/messaging/sendBroadcast.js +93 -0
- package/src/api/messaging/sendMessage.js +269 -0
- package/src/api/messaging/sendTypingIndicator.js +74 -0
- package/src/api/messaging/sessionGuard.js +208 -0
- package/src/api/messaging/setMessageReaction.js +95 -0
- package/src/api/messaging/setTitle.js +124 -0
- package/src/api/messaging/shareContact.js +49 -0
- package/src/api/messaging/threadColors.js +128 -0
- package/src/api/messaging/unsendMessage.js +81 -0
- package/src/api/messaging/uploadAttachment.js +523 -0
- package/src/api/socket/OldMessage.js +186 -0
- package/src/api/socket/core/connectMqtt.js +320 -0
- package/src/api/socket/core/emitAuth.js +103 -0
- package/src/api/socket/core/getSeqID.js +338 -0
- package/src/api/socket/core/getTaskResponseData.js +25 -0
- package/src/api/socket/core/parseDelta.js +387 -0
- package/src/api/socket/detail/buildStream.js +215 -0
- package/src/api/socket/detail/constants.js +28 -0
- package/src/api/socket/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/index.js +421 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/index.js +437 -0
- package/src/api/socket/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/listenE2EE.js +75 -0
- package/src/api/socket/listenMqtt.js +551 -0
- package/src/api/socket/middleware/index.js +216 -0
- package/src/api/socket/sendMessage.js +309 -0
- package/src/api/socket/sendMessageMqtt.js +69 -0
- package/src/api/threads/getThreadHistory.js +664 -0
- package/src/api/threads/getThreadInfo.js +296 -0
- package/src/api/threads/getThreadList.js +293 -0
- package/src/api/threads/getThreadPictures.js +78 -0
- package/src/api/users/getUserID.js +65 -0
- package/src/api/users/getUserInfo.js +402 -0
- package/src/api/users/getUserInfoV2.js +911 -0
- package/src/app/MessengerBot.js +209 -0
- package/src/app/MessengerContext.js +32 -0
- package/src/app/createFcaClient.js +139 -0
- package/src/app/threadInfoRealtimeSync.js +284 -0
- package/src/core/sendReqMqtt.js +96 -0
- package/src/database/helpers.js +53 -0
- package/src/database/models/index.js +88 -0
- package/src/database/models/thread.js +50 -0
- package/src/database/models/user.js +46 -0
- package/src/database/threadData.js +94 -0
- package/src/database/userData.js +98 -0
- package/src/remote/remoteClient.js +123 -0
- package/src/utils/broadcast.js +51 -0
- package/src/utils/client.js +10 -0
- package/src/utils/clientIdentity.js +118 -0
- package/src/utils/constants.js +23 -0
- package/src/utils/cookies.js +68 -0
- package/src/utils/format/attachment.js +357 -0
- package/src/utils/format/cookie.js +9 -0
- package/src/utils/format/date.js +50 -0
- package/src/utils/format/decode.js +44 -0
- package/src/utils/format/delta.js +194 -0
- package/src/utils/format/ids.js +64 -0
- package/src/utils/format/index.js +64 -0
- package/src/utils/format/message.js +88 -0
- package/src/utils/format/presence.js +132 -0
- package/src/utils/format/readTyp.js +44 -0
- package/src/utils/format/thread.js +42 -0
- package/src/utils/format/utils.js +141 -0
- package/src/utils/headers.js +124 -0
- package/src/utils/logger.js +144 -0
- package/src/utils/loginParser/autoLogin.js +125 -0
- package/src/utils/loginParser/helpers.js +43 -0
- package/src/utils/loginParser/index.js +10 -0
- package/src/utils/loginParser/parseAndCheckLogin.js +220 -0
- package/src/utils/loginParser/textUtils.js +28 -0
- package/src/utils/request/client.js +26 -0
- package/src/utils/request/config.js +29 -0
- package/src/utils/request/defaults.js +46 -0
- package/src/utils/request/helpers.js +46 -0
- package/src/utils/request/index.js +17 -0
- package/src/utils/request/methods.js +163 -0
- package/src/utils/request/proxy.js +46 -0
- package/src/utils/request/retry.js +77 -0
- package/src/utils/request/sanitize.js +49 -0
- package/src/utils/utils.js +686 -0
- package/src/utils/versionCheck.js +47 -0
- package/src/vendor/fca-unofficial/index.js +11 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const logger = require("../../../../func/logger");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Middleware system for filtering and processing events before they are emitted
|
|
6
|
+
*
|
|
7
|
+
* Middleware functions receive (event, next) where:
|
|
8
|
+
* - event: The event object (can be modified)
|
|
9
|
+
* - next: Function to call to continue to next middleware
|
|
10
|
+
* - next() - continue to next middleware
|
|
11
|
+
* - next(false) or next(null) - stop processing, don't emit event
|
|
12
|
+
* - next(error) - emit error instead
|
|
13
|
+
*/
|
|
14
|
+
module.exports = function createMiddlewareSystem() {
|
|
15
|
+
const middlewareStack = [];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Add middleware to the stack
|
|
19
|
+
* @param {Function|string} middleware - Middleware function or name for named middleware
|
|
20
|
+
* @param {Function} [fn] - Middleware function (if first param is name)
|
|
21
|
+
* @returns {Function} Unsubscribe function
|
|
22
|
+
*/
|
|
23
|
+
function use(middleware, fn) {
|
|
24
|
+
let middlewareFn, name;
|
|
25
|
+
|
|
26
|
+
if (typeof middleware === "string" && typeof fn === "function") {
|
|
27
|
+
name = middleware;
|
|
28
|
+
middlewareFn = fn;
|
|
29
|
+
} else if (typeof middleware === "function") {
|
|
30
|
+
middlewareFn = middleware;
|
|
31
|
+
name = `middleware_${middlewareStack.length}`;
|
|
32
|
+
} else {
|
|
33
|
+
throw new Error("Middleware must be a function or (name, function)");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const wrapped = {
|
|
37
|
+
name,
|
|
38
|
+
fn: middlewareFn,
|
|
39
|
+
enabled: true
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
middlewareStack.push(wrapped);
|
|
43
|
+
logger(`Middleware "${name}" added`, "info");
|
|
44
|
+
|
|
45
|
+
// Return unsubscribe function
|
|
46
|
+
return function remove() {
|
|
47
|
+
const index = middlewareStack.indexOf(wrapped);
|
|
48
|
+
if (index !== -1) {
|
|
49
|
+
middlewareStack.splice(index, 1);
|
|
50
|
+
logger(`Middleware "${name}" removed`, "info");
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Remove middleware by name or function
|
|
57
|
+
* @param {string|Function} identifier - Name or function to remove
|
|
58
|
+
* @returns {boolean} True if removed
|
|
59
|
+
*/
|
|
60
|
+
function remove(identifier) {
|
|
61
|
+
if (typeof identifier === "string") {
|
|
62
|
+
const index = middlewareStack.findIndex(m => m.name === identifier);
|
|
63
|
+
if (index !== -1) {
|
|
64
|
+
const removed = middlewareStack.splice(index, 1)[0];
|
|
65
|
+
logger(`Middleware "${removed.name}" removed`, "info");
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
} else if (typeof identifier === "function") {
|
|
70
|
+
const index = middlewareStack.findIndex(m => m.fn === identifier);
|
|
71
|
+
if (index !== -1) {
|
|
72
|
+
const removed = middlewareStack.splice(index, 1)[0];
|
|
73
|
+
logger(`Middleware "${removed.name}" removed`, "info");
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Remove all middleware
|
|
83
|
+
*/
|
|
84
|
+
function clear() {
|
|
85
|
+
const count = middlewareStack.length;
|
|
86
|
+
middlewareStack.length = 0;
|
|
87
|
+
logger(`All middleware cleared (${count} removed)`, "info");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get list of middleware names
|
|
92
|
+
* @returns {string[]} Array of middleware names
|
|
93
|
+
*/
|
|
94
|
+
function list() {
|
|
95
|
+
return middlewareStack.filter(m => m.enabled).map(m => m.name);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Enable/disable middleware by name
|
|
100
|
+
* @param {string} name - Middleware name
|
|
101
|
+
* @param {boolean} enabled - Enable or disable
|
|
102
|
+
* @returns {boolean} True if found and updated
|
|
103
|
+
*/
|
|
104
|
+
function setEnabled(name, enabled) {
|
|
105
|
+
const middleware = middlewareStack.find(m => m.name === name);
|
|
106
|
+
if (middleware) {
|
|
107
|
+
middleware.enabled = enabled;
|
|
108
|
+
logger(`Middleware "${name}" ${enabled ? "enabled" : "disabled"}`, "info");
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Process event through middleware stack
|
|
116
|
+
* @param {*} event - Event object
|
|
117
|
+
* @param {Function} finalCallback - Callback to call after all middleware
|
|
118
|
+
* @returns {Promise} Promise that resolves when processing is complete
|
|
119
|
+
*/
|
|
120
|
+
function process(event, finalCallback) {
|
|
121
|
+
if (!middlewareStack.length) {
|
|
122
|
+
return finalCallback(null, event);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let index = 0;
|
|
126
|
+
const enabledMiddleware = middlewareStack.filter(m => m.enabled);
|
|
127
|
+
|
|
128
|
+
function next(err) {
|
|
129
|
+
// Error occurred, stop processing
|
|
130
|
+
if (err && err !== false && err !== null) {
|
|
131
|
+
return finalCallback(err, null);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Explicitly stopped (next(false) or next(null))
|
|
135
|
+
if (err === false || err === null) {
|
|
136
|
+
return finalCallback(null, null); // null event means don't emit
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// No more middleware, call final callback
|
|
140
|
+
if (index >= enabledMiddleware.length) {
|
|
141
|
+
return finalCallback(null, event);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Get next middleware
|
|
145
|
+
const middleware = enabledMiddleware[index++];
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
// Call middleware with event and next
|
|
149
|
+
const result = middleware.fn(event, next);
|
|
150
|
+
|
|
151
|
+
// If middleware returns a promise, handle it
|
|
152
|
+
if (result && typeof result.then === "function") {
|
|
153
|
+
result
|
|
154
|
+
.then(() => next())
|
|
155
|
+
.catch(err => next(err));
|
|
156
|
+
} else if (result === false || result === null) {
|
|
157
|
+
// Middleware returned false/null, stop processing
|
|
158
|
+
finalCallback(null, null);
|
|
159
|
+
}
|
|
160
|
+
// If middleware called next() synchronously, it will continue
|
|
161
|
+
} catch (err) {
|
|
162
|
+
logger(`Middleware "${middleware.name}" error: ${err && err.message ? err.message : String(err)}`, "error");
|
|
163
|
+
next(err);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Start processing
|
|
168
|
+
next();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Wrap a callback with middleware processing
|
|
173
|
+
* @param {Function} callback - Original callback (err, event)
|
|
174
|
+
* @returns {Function} Wrapped callback
|
|
175
|
+
*/
|
|
176
|
+
function wrapCallback(callback) {
|
|
177
|
+
return function wrappedCallback(err, event) {
|
|
178
|
+
if (err) {
|
|
179
|
+
// Errors bypass middleware
|
|
180
|
+
return callback(err, null);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (!event) {
|
|
184
|
+
return callback(null, null);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Process event through middleware
|
|
188
|
+
process(event, (middlewareErr, processedEvent) => {
|
|
189
|
+
if (middlewareErr) {
|
|
190
|
+
return callback(middlewareErr, null);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// If processedEvent is null, middleware blocked the event
|
|
194
|
+
if (processedEvent === null) {
|
|
195
|
+
return; // Don't emit
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Emit processed event
|
|
199
|
+
callback(null, processedEvent);
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
use,
|
|
206
|
+
remove,
|
|
207
|
+
clear,
|
|
208
|
+
list,
|
|
209
|
+
setEnabled,
|
|
210
|
+
process,
|
|
211
|
+
wrapCallback,
|
|
212
|
+
get count() {
|
|
213
|
+
return middlewareStack.filter(m => m.enabled).length;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
};
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../../utils/utils");
|
|
4
|
+
var logger = require("../../utils/logger");
|
|
5
|
+
|
|
6
|
+
var ALLOWED = {
|
|
7
|
+
attachment: true, url: true, sticker: true, emoji: true,
|
|
8
|
+
emojiSize: true, body: true, mentions: true, location: true,
|
|
9
|
+
replyToMessage: true, forwardAttachmentIds: true
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
var EMOJI_SIZES = { small: 1, medium: 2, large: 3 };
|
|
13
|
+
|
|
14
|
+
function toEmojiSize(size) {
|
|
15
|
+
if (typeof size === "number" && !isNaN(size)) return Math.min(3, Math.max(1, size));
|
|
16
|
+
if (typeof size === "string" && size in EMOJI_SIZES) return EMOJI_SIZES[size];
|
|
17
|
+
return 1;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function hasLinks(text) {
|
|
21
|
+
return /(https?:\/\/|www\.|t\.me\/|fb\.me\/|youtu\.be\/|facebook\.com\/|youtube\.com\/)/i.test(text);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function buildMentionData(msg, baseBody) {
|
|
25
|
+
if (!Array.isArray(msg.mentions) || !msg.mentions.length) return null;
|
|
26
|
+
var ids = [], offsets = [], lengths = [], types = [];
|
|
27
|
+
for (var i = 0; i < msg.mentions.length; i++) {
|
|
28
|
+
var mention = msg.mentions[i];
|
|
29
|
+
// Ensure tag always has @ prefix — Facebook counts @ in both offset and length
|
|
30
|
+
var tag = String(mention.tag || "");
|
|
31
|
+
if (tag && !tag.startsWith("@")) tag = "@" + tag;
|
|
32
|
+
var fromIndex = Number.isInteger(mention.fromIndex) ? mention.fromIndex : 0;
|
|
33
|
+
var offset = baseBody.indexOf(tag, fromIndex);
|
|
34
|
+
if (offset === -1) {
|
|
35
|
+
// Fallback: search for bare name without @
|
|
36
|
+
offset = baseBody.indexOf(tag.slice(1), fromIndex);
|
|
37
|
+
}
|
|
38
|
+
if (offset < 0) offset = 0;
|
|
39
|
+
ids.push(String(mention.id || 0));
|
|
40
|
+
offsets.push(offset);
|
|
41
|
+
lengths.push(tag.length); // includes @ — matches Facebook's expectation
|
|
42
|
+
types.push("p");
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
mention_ids: ids.join(","),
|
|
46
|
+
mention_offsets: offsets.join(","),
|
|
47
|
+
mention_lengths: lengths.join(","),
|
|
48
|
+
mention_types: types.join(",")
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function extractIdsFromPayload(payload) {
|
|
53
|
+
var messageID = null, threadID = null;
|
|
54
|
+
function walk(node) {
|
|
55
|
+
if (!Array.isArray(node)) return;
|
|
56
|
+
if (node[0] === 5 && (node[1] === "replaceOptimsiticMessage" || node[1] === "replaceOptimisticMessage")) {
|
|
57
|
+
messageID = String(node[3]);
|
|
58
|
+
}
|
|
59
|
+
if (node[0] === 5 && node[1] === "writeCTAIdToThreadsTable") {
|
|
60
|
+
var candidate = node[2];
|
|
61
|
+
if (Array.isArray(candidate) && candidate[0] === 19) threadID = String(candidate[1]);
|
|
62
|
+
}
|
|
63
|
+
for (var i = 0; i < node.length; i++) walk(node[i]);
|
|
64
|
+
}
|
|
65
|
+
try { walk(payload && payload.step); } catch (_) { }
|
|
66
|
+
return { threadID, messageID };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function publishLsRequestWithAck(mqttClient, content, requestId, timeout) {
|
|
70
|
+
timeout = timeout || 15000;
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
var timer = setTimeout(() => {
|
|
73
|
+
mqttClient.removeListener('message', onMessage);
|
|
74
|
+
reject(new Error('MQTT sendMessage timed out after ' + timeout + 'ms'));
|
|
75
|
+
}, timeout);
|
|
76
|
+
|
|
77
|
+
function onMessage(topic, message) {
|
|
78
|
+
if (topic !== '/ls_resp') return;
|
|
79
|
+
try {
|
|
80
|
+
var data = JSON.parse(message.toString());
|
|
81
|
+
if (String(data.request_id) === String(requestId)) {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
mqttClient.removeListener('message', onMessage);
|
|
84
|
+
var extracted = extractIdsFromPayload(data.payload ? JSON.parse(data.payload) : {});
|
|
85
|
+
resolve({ threadID: extracted.threadID, messageID: extracted.messageID });
|
|
86
|
+
}
|
|
87
|
+
} catch (_) { }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
mqttClient.on('message', onMessage);
|
|
91
|
+
mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1 }, err => {
|
|
92
|
+
if (err) {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
mqttClient.removeListener('message', onMessage);
|
|
95
|
+
reject(err);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
102
|
+
var uploadAttachmentFn = require('../messaging/uploadAttachment')(defaultFuncs, api, ctx);
|
|
103
|
+
|
|
104
|
+
async function uploadAttachments(attachments) {
|
|
105
|
+
if (!Array.isArray(attachments)) attachments = [attachments];
|
|
106
|
+
return uploadAttachmentFn(attachments);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function sendViaMqtt(msg, threadID, replyToMessage) {
|
|
110
|
+
var mqttClient = ctx.mqttClient || global.mqttClient;
|
|
111
|
+
if (!mqttClient) throw new Error('MQTT client not available');
|
|
112
|
+
|
|
113
|
+
var baseBody = msg.body != null ? String(msg.body) : "";
|
|
114
|
+
var requestId = Math.floor(100 + Math.random() * 900);
|
|
115
|
+
var epoch = (BigInt(Date.now()) << 22n).toString();
|
|
116
|
+
|
|
117
|
+
var payload0 = {
|
|
118
|
+
thread_id: String(threadID),
|
|
119
|
+
otid: utils.generateOfflineThreadingID(),
|
|
120
|
+
source: 2097153,
|
|
121
|
+
send_type: 1,
|
|
122
|
+
sync_group: 1,
|
|
123
|
+
mark_thread_read: 1,
|
|
124
|
+
text: baseBody === "" ? null : baseBody,
|
|
125
|
+
initiating_source: 0,
|
|
126
|
+
skip_url_preview_gen: 1,
|
|
127
|
+
text_has_links: hasLinks(baseBody) ? 1 : 0,
|
|
128
|
+
multitab_env: 0,
|
|
129
|
+
metadata_dataclass: JSON.stringify({ media_accessibility_metadata: { alt_text: null } })
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
var mentionData = buildMentionData(msg, baseBody);
|
|
133
|
+
if (mentionData) payload0.mention_data = mentionData;
|
|
134
|
+
|
|
135
|
+
if (msg.sticker) { payload0.send_type = 2; payload0.sticker_id = msg.sticker; }
|
|
136
|
+
if (msg.emoji) { payload0.send_type = 1; payload0.text = msg.emoji; payload0.hot_emoji_size = toEmojiSize(msg.emojiSize); }
|
|
137
|
+
|
|
138
|
+
if (msg.location && msg.location.latitude != null && msg.location.longitude != null) {
|
|
139
|
+
payload0.send_type = 1;
|
|
140
|
+
payload0.location_data = {
|
|
141
|
+
coordinates: { latitude: msg.location.latitude, longitude: msg.location.longitude },
|
|
142
|
+
is_current_location: Boolean(msg.location.current),
|
|
143
|
+
is_live_location: Boolean(msg.location.live)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
var effectiveReplyTo = replyToMessage || msg.replyToMessage;
|
|
148
|
+
if (effectiveReplyTo) {
|
|
149
|
+
payload0.reply_metadata = {
|
|
150
|
+
reply_source_id: effectiveReplyTo,
|
|
151
|
+
reply_source_type: 1,
|
|
152
|
+
reply_type: 0
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (msg.attachment) {
|
|
157
|
+
payload0.send_type = 3;
|
|
158
|
+
if (payload0.text === "") payload0.text = null;
|
|
159
|
+
payload0.attachment_fbids = [];
|
|
160
|
+
var list = Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment];
|
|
161
|
+
var preuploaded = [], toUpload = [];
|
|
162
|
+
for (var item of list) {
|
|
163
|
+
if (Array.isArray(item) && item.length >= 2 && typeof item[0] === "string") {
|
|
164
|
+
preuploaded.push(String(item[1]));
|
|
165
|
+
} else if (utils.isReadableStream(item)) {
|
|
166
|
+
toUpload.push(item);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (preuploaded.length) payload0.attachment_fbids = payload0.attachment_fbids.concat(preuploaded);
|
|
170
|
+
if (msg.forwardAttachmentIds && msg.forwardAttachmentIds.length) {
|
|
171
|
+
payload0.attachment_fbids = payload0.attachment_fbids.concat(msg.forwardAttachmentIds.map(String));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (toUpload.length) {
|
|
175
|
+
var uploaded = await uploadAttachments(toUpload);
|
|
176
|
+
for (var u of uploaded) {
|
|
177
|
+
if (!u) continue;
|
|
178
|
+
var fbid = u.image_id || u.video_id || u.audio_id || u.file_id || u.sticker_id || u.gif_id;
|
|
179
|
+
if (!fbid) {
|
|
180
|
+
var firstKey = Object.keys(u).find(function(k) { return u[k] && /^\d+$/.test(String(u[k])); });
|
|
181
|
+
if (firstKey) fbid = u[firstKey];
|
|
182
|
+
}
|
|
183
|
+
if (fbid) payload0.attachment_fbids.push(String(fbid));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (!payload0.attachment_fbids.length) delete payload0.attachment_fbids;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
var tasks = [
|
|
190
|
+
{
|
|
191
|
+
label: '46',
|
|
192
|
+
payload: JSON.stringify(payload0),
|
|
193
|
+
queue_name: String(threadID),
|
|
194
|
+
task_id: 400,
|
|
195
|
+
failure_count: null
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
label: '21',
|
|
199
|
+
payload: JSON.stringify({
|
|
200
|
+
thread_id: String(threadID),
|
|
201
|
+
last_read_watermark_ts: Date.now(),
|
|
202
|
+
sync_group: 1
|
|
203
|
+
}),
|
|
204
|
+
queue_name: String(threadID),
|
|
205
|
+
task_id: 401,
|
|
206
|
+
failure_count: null
|
|
207
|
+
}
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
var content = {
|
|
211
|
+
app_id: '2220391788200892',
|
|
212
|
+
payload: JSON.stringify({
|
|
213
|
+
tasks: tasks,
|
|
214
|
+
epoch_id: epoch,
|
|
215
|
+
version_id: '24804310205905615',
|
|
216
|
+
data_trace_id: '#' + Buffer.from(String(Math.random())).toString('base64').replace(/=+$/, '')
|
|
217
|
+
}),
|
|
218
|
+
request_id: requestId,
|
|
219
|
+
type: 3
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
return publishLsRequestWithAck(mqttClient, content, requestId, 15000);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return async function sendMessage(msg, threadID, callback, replyToMessage, isSingleUser) {
|
|
226
|
+
if (typeof msg === "string") msg = { body: msg };
|
|
227
|
+
if (typeof callback !== "function") { callback = null; }
|
|
228
|
+
|
|
229
|
+
var resolve, reject;
|
|
230
|
+
var promise = new Promise((res, rej) => { resolve = res; reject = rej; });
|
|
231
|
+
|
|
232
|
+
for (var key in msg) {
|
|
233
|
+
if (!ALLOWED[key]) {
|
|
234
|
+
var err = { error: "sendMessage: Unknown property '" + key + "'" };
|
|
235
|
+
if (callback) callback(err);
|
|
236
|
+
else reject(err);
|
|
237
|
+
return promise;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Auto-detect isSingleUser from ctx.threadTypes if not explicitly provided.
|
|
242
|
+
// parseDelta in listenMqtt.js populates ctx.threadTypes[senderID] = 'dm' | 'group'
|
|
243
|
+
if (isSingleUser === undefined && ctx.threadTypes) {
|
|
244
|
+
isSingleUser = ctx.threadTypes[String(threadID)] === 'dm';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// DM attachment sends — skip MQTT entirely.
|
|
248
|
+
// For E2EE DMs: route through the E2EE bridge (Noise WebSocket, Signal Protocol).
|
|
249
|
+
// Facebook strips attachment_fbids from MQTT messages in E2EE threads
|
|
250
|
+
// (can't re-encrypt CDN attachments on the fly), so MQTT silently drops them.
|
|
251
|
+
// The vendor's client.sendImage/sendVideo/sendAudio encrypts the file data
|
|
252
|
+
// and sends via the Noise WebSocket — the only path that actually delivers
|
|
253
|
+
// attachments in E2EE threads.
|
|
254
|
+
// For non-E2EE DMs: use OldMessage.
|
|
255
|
+
// /messaging/send/ with other_user_fbid routing works for plain DMs.
|
|
256
|
+
// (For E2EE DMs it returns 404 because the endpoint is deprecated for those.)
|
|
257
|
+
if (isSingleUser && msg.attachment) {
|
|
258
|
+
var useE2EE = api.e2ee && typeof api.e2ee.isConnected === "function" && api.e2ee.isConnected();
|
|
259
|
+
if (useE2EE) {
|
|
260
|
+
try {
|
|
261
|
+
var e2eeResult = await api.e2ee.sendMessage(String(threadID), msg, replyToMessage);
|
|
262
|
+
var wrapped = e2eeResult && e2eeResult.messageId
|
|
263
|
+
? { threadID: String(threadID), messageID: String(e2eeResult.messageId) }
|
|
264
|
+
: e2eeResult;
|
|
265
|
+
if (callback) callback(null, wrapped);
|
|
266
|
+
else resolve(wrapped);
|
|
267
|
+
} catch (e2eeErr) {
|
|
268
|
+
logger.error("sendMessage", "E2EE DM attachment send failed: " + (e2eeErr.message || e2eeErr));
|
|
269
|
+
if (callback) callback(e2eeErr);
|
|
270
|
+
else reject(e2eeErr);
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
try {
|
|
274
|
+
var omResult = await new Promise((res2, rej2) => {
|
|
275
|
+
api.OldMessage(msg, threadID, (err2, data2) => err2 ? rej2(err2) : res2(data2), replyToMessage, true);
|
|
276
|
+
});
|
|
277
|
+
if (callback) callback(null, omResult);
|
|
278
|
+
else resolve(omResult);
|
|
279
|
+
} catch (omErr) {
|
|
280
|
+
logger.error("sendMessage", "DM attachment via OldMessage failed: " + (omErr.error || omErr.message || omErr));
|
|
281
|
+
if (callback) callback(omErr);
|
|
282
|
+
else reject(omErr);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return promise;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
var result = await sendViaMqtt(msg, threadID, replyToMessage);
|
|
290
|
+
if (callback) callback(null, result);
|
|
291
|
+
else resolve(result);
|
|
292
|
+
} catch (mqttErr) {
|
|
293
|
+
logger.warn("sendMessage", "MQTT failed, falling back to OldMessage: " + (mqttErr.message || mqttErr));
|
|
294
|
+
try {
|
|
295
|
+
var fbResult = await new Promise((res2, rej2) => {
|
|
296
|
+
api.OldMessage(msg, threadID, (err2, data2) => err2 ? rej2(err2) : res2(data2), replyToMessage, isSingleUser);
|
|
297
|
+
});
|
|
298
|
+
if (callback) callback(null, fbResult);
|
|
299
|
+
else resolve(fbResult);
|
|
300
|
+
} catch (fbErr) {
|
|
301
|
+
logger.error("sendMessage", fbErr.error || fbErr.message || fbErr);
|
|
302
|
+
if (callback) callback(fbErr);
|
|
303
|
+
else reject(fbErr);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return promise;
|
|
308
|
+
};
|
|
309
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../../utils/utils");
|
|
4
|
+
|
|
5
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
6
|
+
return function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
|
|
7
|
+
if (typeof msg === "string") msg = { body: msg };
|
|
8
|
+
var mqttClient = ctx.mqttClient || global.mqttClient;
|
|
9
|
+
if (!mqttClient) {
|
|
10
|
+
var err = { error: "MQTT not connected. Call listenMqtt first." };
|
|
11
|
+
if (typeof callback === "function") return callback(err);
|
|
12
|
+
return Promise.reject(err);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
ctx.wsReqNumber = (ctx.wsReqNumber || 0) + 1;
|
|
16
|
+
ctx.wsTaskNumber = (ctx.wsTaskNumber || 0) + 1;
|
|
17
|
+
|
|
18
|
+
var baseBody = msg.body != null ? String(msg.body) : "";
|
|
19
|
+
var hasLinks = typeof baseBody === "string" && /(https?:\/\/|www\.|t\.me\/|fb\.me\/|youtu\.be\/|facebook\.com\/|youtube\.com\/)/i.test(baseBody);
|
|
20
|
+
var requestId = ctx.wsReqNumber;
|
|
21
|
+
|
|
22
|
+
var payload0 = {
|
|
23
|
+
thread_id: String(threadID),
|
|
24
|
+
otid: utils.generateOfflineThreadingID(),
|
|
25
|
+
source: 2097153,
|
|
26
|
+
send_type: 1,
|
|
27
|
+
sync_group: 1,
|
|
28
|
+
mark_thread_read: 1,
|
|
29
|
+
text: baseBody === "" ? null : baseBody,
|
|
30
|
+
initiating_source: 0,
|
|
31
|
+
skip_url_preview_gen: 1,
|
|
32
|
+
text_has_links: hasLinks ? 1 : 0,
|
|
33
|
+
multitab_env: 0
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (msg.sticker) { payload0.send_type = 2; payload0.sticker_id = msg.sticker; }
|
|
37
|
+
if (replyToMessage || msg.replyToMessage) {
|
|
38
|
+
payload0.reply_metadata = {
|
|
39
|
+
reply_source_id: replyToMessage || msg.replyToMessage,
|
|
40
|
+
reply_source_type: 1,
|
|
41
|
+
reply_type: 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
var content = {
|
|
46
|
+
app_id: '2220391788200892',
|
|
47
|
+
payload: JSON.stringify({
|
|
48
|
+
tasks: [{
|
|
49
|
+
failure_count: null,
|
|
50
|
+
label: '46',
|
|
51
|
+
payload: JSON.stringify(payload0),
|
|
52
|
+
queue_name: String(threadID),
|
|
53
|
+
task_id: ctx.wsTaskNumber
|
|
54
|
+
}],
|
|
55
|
+
epoch_id: utils.generateOfflineThreadingID(),
|
|
56
|
+
version_id: '7214102258676893'
|
|
57
|
+
}),
|
|
58
|
+
request_id: requestId,
|
|
59
|
+
type: 3
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (typeof callback === "function") {
|
|
63
|
+
ctx.reqCallbacks[requestId] = callback;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1 });
|
|
67
|
+
return Promise.resolve({ requestId });
|
|
68
|
+
};
|
|
69
|
+
};
|