@lazyneoaz/testfca 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/COOKIE_LOGIN.md +208 -0
  3. package/LICENSE +3 -0
  4. package/README.md +492 -0
  5. package/index.js +2 -0
  6. package/package.json +120 -0
  7. package/scripts/build-go.mjs +54 -0
  8. package/scripts/detect-platform.mjs +36 -0
  9. package/scripts/download-prebuilt.mjs +119 -0
  10. package/scripts/package.mjs +6 -0
  11. package/scripts/postinstall.mjs +113 -0
  12. package/src/apis/addExternalModule.js +24 -0
  13. package/src/apis/addUserToGroup.js +108 -0
  14. package/src/apis/changeAdminStatus.js +148 -0
  15. package/src/apis/changeArchivedStatus.js +61 -0
  16. package/src/apis/changeAvatar.js +103 -0
  17. package/src/apis/changeBio.js +69 -0
  18. package/src/apis/changeBlockedStatus.js +54 -0
  19. package/src/apis/changeGroupImage.js +136 -0
  20. package/src/apis/changeThreadColor.js +116 -0
  21. package/src/apis/changeThreadEmoji.js +53 -0
  22. package/src/apis/comment.js +207 -0
  23. package/src/apis/createAITheme.js +129 -0
  24. package/src/apis/createNewGroup.js +79 -0
  25. package/src/apis/createPoll.js +73 -0
  26. package/src/apis/deleteMessage.js +52 -0
  27. package/src/apis/deleteThread.js +52 -0
  28. package/src/apis/e2ee.js +170 -0
  29. package/src/apis/editMessage.js +78 -0
  30. package/src/apis/emoji.js +124 -0
  31. package/src/apis/fetchThemeData.js +82 -0
  32. package/src/apis/follow.js +81 -0
  33. package/src/apis/forwardMessage.js +52 -0
  34. package/src/apis/friend.js +243 -0
  35. package/src/apis/gcmember.js +122 -0
  36. package/src/apis/gcname.js +123 -0
  37. package/src/apis/gcrule.js +119 -0
  38. package/src/apis/getAccess.js +111 -0
  39. package/src/apis/getBotInfo.js +88 -0
  40. package/src/apis/getBotInitialData.js +43 -0
  41. package/src/apis/getFriendsList.js +79 -0
  42. package/src/apis/getMessage.js +423 -0
  43. package/src/apis/getTheme.js +95 -0
  44. package/src/apis/getThemeInfo.js +116 -0
  45. package/src/apis/getThreadHistory.js +239 -0
  46. package/src/apis/getThreadInfo.js +267 -0
  47. package/src/apis/getThreadList.js +232 -0
  48. package/src/apis/getThreadPictures.js +58 -0
  49. package/src/apis/getUserID.js +117 -0
  50. package/src/apis/getUserInfo.js +513 -0
  51. package/src/apis/getUserInfoV2.js +146 -0
  52. package/src/apis/handleMessageRequest.js +50 -0
  53. package/src/apis/httpGet.js +63 -0
  54. package/src/apis/httpPost.js +89 -0
  55. package/src/apis/httpPostFormData.js +69 -0
  56. package/src/apis/listenMqtt.js +1236 -0
  57. package/src/apis/listenSpeed.js +179 -0
  58. package/src/apis/logout.js +93 -0
  59. package/src/apis/markAsDelivered.js +47 -0
  60. package/src/apis/markAsRead.js +115 -0
  61. package/src/apis/markAsReadAll.js +40 -0
  62. package/src/apis/markAsSeen.js +70 -0
  63. package/src/apis/mqttDeltaValue.js +250 -0
  64. package/src/apis/muteThread.js +45 -0
  65. package/src/apis/nickname.js +132 -0
  66. package/src/apis/notes.js +163 -0
  67. package/src/apis/pinMessage.js +150 -0
  68. package/src/apis/produceMetaTheme.js +180 -0
  69. package/src/apis/realtime.js +182 -0
  70. package/src/apis/removeUserFromGroup.js +117 -0
  71. package/src/apis/resolvePhotoUrl.js +58 -0
  72. package/src/apis/searchForThread.js +154 -0
  73. package/src/apis/sendMessage.js +346 -0
  74. package/src/apis/sendMessageMqtt.js +248 -0
  75. package/src/apis/sendTypingIndicator.js +105 -0
  76. package/src/apis/setMessageReaction.js +38 -0
  77. package/src/apis/setMessageReactionMqtt.js +61 -0
  78. package/src/apis/setThreadTheme.js +260 -0
  79. package/src/apis/setThreadThemeMqtt.js +94 -0
  80. package/src/apis/share.js +107 -0
  81. package/src/apis/shareContact.js +66 -0
  82. package/src/apis/stickers.js +257 -0
  83. package/src/apis/story.js +181 -0
  84. package/src/apis/theme.js +233 -0
  85. package/src/apis/unfriend.js +47 -0
  86. package/src/apis/unsendMessage.js +25 -0
  87. package/src/database/appStateBackup.js +298 -0
  88. package/src/database/models/index.js +56 -0
  89. package/src/database/models/thread.js +31 -0
  90. package/src/database/models/user.js +32 -0
  91. package/src/database/threadData.js +101 -0
  92. package/src/database/userData.js +90 -0
  93. package/src/e2ee/bridge.js +275 -0
  94. package/src/e2ee/index.js +60 -0
  95. package/src/engine/client.js +95 -0
  96. package/src/engine/models/buildAPI.js +152 -0
  97. package/src/engine/models/loginHelper.js +574 -0
  98. package/src/engine/models/setOptions.js +88 -0
  99. package/src/types/index.d.ts +574 -0
  100. package/src/utils/antiSuspension.js +529 -0
  101. package/src/utils/auth-helpers.js +149 -0
  102. package/src/utils/autoReLogin.js +336 -0
  103. package/src/utils/axios.js +436 -0
  104. package/src/utils/cache.js +54 -0
  105. package/src/utils/clients.js +282 -0
  106. package/src/utils/constants.js +410 -0
  107. package/src/utils/formatters/data/formatAttachment.js +370 -0
  108. package/src/utils/formatters/data/formatDelta.js +109 -0
  109. package/src/utils/formatters/index.js +159 -0
  110. package/src/utils/formatters/value/formatCookie.js +91 -0
  111. package/src/utils/formatters/value/formatDate.js +36 -0
  112. package/src/utils/formatters/value/formatID.js +16 -0
  113. package/src/utils/formatters.js +1373 -0
  114. package/src/utils/headers.js +235 -0
  115. package/src/utils/index.js +153 -0
  116. package/src/utils/monitoring.js +333 -0
  117. package/src/utils/rateLimiter.js +319 -0
  118. package/src/utils/tokenRefresh.js +680 -0
  119. package/src/utils/user-agents.js +238 -0
  120. package/src/utils/validation.js +157 -0
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+ const { globalAntiSuspension } = require('../utils/antiSuspension');
5
+
6
+ module.exports = (defaultFuncs, api, ctx) => {
7
+ function detectAttachmentType(attachment) {
8
+ const p = attachment.path || "";
9
+ const ext = p.toLowerCase().split(".").pop();
10
+ const audio = ["mp3", "wav", "aac", "m4a", "ogg", "opus", "flac"];
11
+ const video = ["mp4", "mov", "avi", "mkv", "webm", "wmv", "flv"];
12
+ const image = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"];
13
+ if (audio.includes(ext)) return { voice_clip: "true" };
14
+ if (video.includes(ext)) return { video: "true" };
15
+ if (image.includes(ext)) return { image: "true" };
16
+ return { file: "true" };
17
+ }
18
+
19
+ async function uploadAttachment(attachments, callback) {
20
+ callback = callback || function () {};
21
+ var uploads = [];
22
+ try {
23
+ for (var i = 0; i < attachments.length; i++) {
24
+ if (!utils.isReadableStream(attachments[i])) {
25
+ throw { error: "Attachment should be a readable stream and not " + utils.getType(attachments[i]) + "." };
26
+ }
27
+
28
+ if (i > 0) {
29
+ await globalAntiSuspension.addSmartDelay();
30
+ }
31
+
32
+ var form = {
33
+ upload_1024: attachments[i],
34
+ ...detectAttachmentType(attachments[i]),
35
+ };
36
+
37
+ const upload = await defaultFuncs
38
+ .postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {}, { ...ctx, requestThreadID: String(ctx._lastThreadHint || "") })
39
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
40
+ .then(resData => {
41
+ if (resData.error) throw resData;
42
+ return resData.payload.metadata[0];
43
+ });
44
+
45
+ uploads.push(upload);
46
+ }
47
+ callback(null, uploads);
48
+ } catch (err) {
49
+ utils.error("uploadAttachment", err);
50
+ return callback(err);
51
+ }
52
+ }
53
+
54
+ function getSendPayload(threadID, msg, otid) {
55
+ const isString = typeof msg === 'string';
56
+ const body = isString ? msg : msg.body || "";
57
+ otid = otid.toString() || utils.generateOfflineThreadingID().toString();
58
+ let payload = {
59
+ thread_id: threadID.toString(),
60
+ otid,
61
+ source: 0,
62
+ send_type: 1,
63
+ sync_group: 1,
64
+ text: body,
65
+ initiating_source: 1,
66
+ skip_url_preview_gen: 0,
67
+ };
68
+ if (typeof msg === 'object') {
69
+ if (msg.sticker) {
70
+ payload.send_type = 2;
71
+ payload.sticker_id = msg.sticker;
72
+ payload.text = null;
73
+ }
74
+ if (msg.attachment) {
75
+ payload.send_type = 3;
76
+ payload.attachment_fbids = Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment];
77
+ }
78
+ }
79
+ return payload;
80
+ }
81
+
82
+ function extractIdsFromPayload(payload) {
83
+ let messageID = null;
84
+ let threadID = null;
85
+ function walk(n) {
86
+ if (Array.isArray(n)) {
87
+ if (n[0] === 5 && (n[1] === "replaceOptimsiticMessage" || n[1] === "replaceOptimisticMessage")) {
88
+ messageID = String(n[3]);
89
+ }
90
+ if (n[0] === 5 && n[1] === "writeCTAIdToThreadsTable") {
91
+ const a = n[2];
92
+ if (Array.isArray(a) && a[0] === 19) threadID = String(a[1]);
93
+ }
94
+ for (const x of n) walk(x);
95
+ }
96
+ }
97
+ walk(payload?.step);
98
+ return { threadID, messageID };
99
+ }
100
+
101
+ function publishWithAck(content, reqID, callback) {
102
+ return new Promise((resolve, reject) => {
103
+ if (!ctx.mqttClient || typeof ctx.mqttClient.on !== "function" || typeof ctx.mqttClient.publish !== "function") {
104
+ const err = new Error("MQTT client is not initialized");
105
+ utils.error("sendMessageMqtt", err);
106
+ callback && callback(err);
107
+ return reject(err);
108
+ }
109
+
110
+ if (typeof ctx.mqttClient.setMaxListeners === "function") {
111
+ ctx.mqttClient.setMaxListeners(0);
112
+ }
113
+
114
+ let done = false;
115
+ const cleanup = () => {
116
+ if (done) return;
117
+ done = true;
118
+ ctx.mqttClient.removeListener("message", handleRes);
119
+ };
120
+ const handleRes = (topic, message) => {
121
+ if (topic !== "/ls_resp") return;
122
+ let jsonMsg;
123
+ try {
124
+ jsonMsg = JSON.parse(message.toString());
125
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
126
+ } catch {
127
+ return;
128
+ }
129
+ if (jsonMsg.request_id !== reqID) return;
130
+ const { threadID, messageID } = extractIdsFromPayload(jsonMsg.payload);
131
+ const result = { messageID, threadID };
132
+ cleanup();
133
+ callback && callback(undefined, result);
134
+ resolve(result);
135
+ };
136
+ ctx.mqttClient.on("message", handleRes);
137
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(content), { qos: 1, retain: false }, err => {
138
+ if (err) {
139
+ cleanup();
140
+ callback && callback(err);
141
+ reject(err);
142
+ }
143
+ });
144
+ setTimeout(() => {
145
+ if (done) return;
146
+ cleanup();
147
+ const err = { error: "Timeout waiting for ACK" };
148
+ callback && callback(err);
149
+ reject(err);
150
+ }, 15000);
151
+ });
152
+ }
153
+
154
+ return async (msg, threadID, replyToMessage, callback) => {
155
+ if (typeof msg !== 'string' && typeof msg !== 'object') {
156
+ throw new Error("Message should be of type string or object, not " + utils.getType(msg) + ".");
157
+ }
158
+
159
+ if (typeof threadID !== 'string' && typeof threadID !== 'number') {
160
+ throw new Error("threadID must be a string or number.");
161
+ }
162
+
163
+ if (!callback && typeof threadID === "function") {
164
+ throw new Error("Pass a threadID as a second argument.");
165
+ }
166
+
167
+ if (!callback && typeof replyToMessage === "function") {
168
+ callback = replyToMessage;
169
+ replyToMessage = null;
170
+ }
171
+
172
+ try {
173
+ await globalAntiSuspension.prepareBeforeMessage(String(threadID), typeof msg === 'string' ? msg : (msg.body || ''));
174
+ } catch (suspErr) {
175
+ utils.warn("sendMessageMqtt", "Anti-suspension check raised:", suspErr && suspErr.message ? suspErr.message : suspErr);
176
+ }
177
+
178
+ const timestamp = Date.now();
179
+ const otid = utils.generateOfflineThreadingID();
180
+ const epoch_id = utils.generateOfflineThreadingID();
181
+ const payload = getSendPayload(threadID, msg, otid);
182
+
183
+ const tasks = [{
184
+ label: "46",
185
+ payload,
186
+ queue_name: threadID.toString(),
187
+ task_id: 0,
188
+ failure_count: null,
189
+ }, {
190
+ label: "21",
191
+ payload: {
192
+ thread_id: threadID.toString(),
193
+ last_read_watermark_ts: timestamp,
194
+ sync_group: 1,
195
+ },
196
+ queue_name: threadID.toString(),
197
+ task_id: 1,
198
+ failure_count: null,
199
+ }];
200
+
201
+ if (replyToMessage) {
202
+ tasks[0].payload.reply_metadata = {
203
+ reply_source_id: replyToMessage,
204
+ reply_source_type: 1,
205
+ reply_type: 0,
206
+ };
207
+ }
208
+
209
+ const request_id = ++ctx.wsReqNumber;
210
+ const form = {
211
+ app_id: "2220391788200892",
212
+ payload: {
213
+ tasks,
214
+ epoch_id,
215
+ version_id: "6120284488008082",
216
+ data_trace_id: null,
217
+ },
218
+ request_id,
219
+ type: 3,
220
+ };
221
+
222
+ if (msg.attachment) {
223
+ try {
224
+ ctx._lastThreadHint = threadID;
225
+ const files = await new Promise((resolve, reject) => {
226
+ uploadAttachment(
227
+ Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment],
228
+ (err, files) => {
229
+ if (err) return reject(err);
230
+ return resolve(files);
231
+ }
232
+ );
233
+ });
234
+ form.payload.tasks[0].payload.attachment_fbids = files.map(file => Object.values(file)[0]);
235
+ } catch (err) {
236
+ utils.error("Attachment upload failed:", err);
237
+ throw err;
238
+ }
239
+ }
240
+
241
+ form.payload.tasks.forEach(task => {
242
+ task.payload = JSON.stringify(task.payload);
243
+ });
244
+ form.payload = JSON.stringify(form.payload);
245
+
246
+ return publishWithAck(form, request_id, callback);
247
+ };
248
+ };
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ /**
6
+ * @param {Object} defaultFuncs
7
+ * @param {Object} api
8
+ * @param {Object} ctx
9
+ */
10
+ module.exports = function (defaultFuncs, api, ctx) {
11
+ /**
12
+ * Thread-type cache so we only look up group status once per thread.
13
+ * Shared with sendMessage via ctx.threadTypeCache.
14
+ */
15
+ function getThreadCache() {
16
+ if (!ctx.threadTypeCache) ctx.threadTypeCache = Object.create(null);
17
+ return ctx.threadTypeCache;
18
+ }
19
+
20
+ /**
21
+ * Reliably determines whether a thread is a group by querying the API
22
+ * and caching the result. Falls back to string-length heuristic only on error.
23
+ */
24
+ async function isGroupThread(threadID) {
25
+ const tid = threadID.toString();
26
+ const cache = getThreadCache();
27
+ if (Object.prototype.hasOwnProperty.call(cache, tid)) return !!cache[tid];
28
+ try {
29
+ const info = await api.getThreadInfo(tid);
30
+ cache[tid] = !!info.isGroup;
31
+ return !!info.isGroup;
32
+ } catch (_) {
33
+ const fallback = tid.length >= 16;
34
+ cache[tid] = fallback;
35
+ return fallback;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Sends a typing indicator to a specific thread.
41
+ * @param {boolean} sendTyping - True to show typing indicator, false to hide.
42
+ * @param {string} threadID - The ID of the thread to send the typing indicator to.
43
+ * @param {Function} [callback] - An optional callback function.
44
+ * @returns {Promise<void>}
45
+ */
46
+ return async function sendTypingIndicatorV2(sendTyping, threadID, callback) {
47
+ // E2EE routing: if threadID looks like a JID (contains @msgr)
48
+ const threadStr = threadID ? threadID.toString() : '';
49
+ if (threadStr.includes('@msgr') || threadStr.includes('@facebook')) {
50
+ if (api.e2ee && api.e2ee.isAvailable() && api.e2ee.isConnected().e2eeConnected) {
51
+ try {
52
+ await api.e2ee.sendE2EETyping(threadStr, !!sendTyping);
53
+ if (callback) callback();
54
+ } catch (err) {
55
+ if (callback) callback(err);
56
+ }
57
+ }
58
+ return;
59
+ }
60
+
61
+ if (!ctx.mqttClient) {
62
+ const err = new Error("You can only use sendTypingIndicator after you start listening.");
63
+ if (callback) callback(err);
64
+ else throw err;
65
+ return;
66
+ }
67
+
68
+ let count_req = 0;
69
+
70
+ let isGroup;
71
+ try {
72
+ isGroup = await isGroupThread(threadID);
73
+ } catch (_) {
74
+ isGroup = threadID.toString().length >= 16;
75
+ }
76
+
77
+ const wsContent = {
78
+ app_id: 2220391788200892,
79
+ payload: JSON.stringify({
80
+ label: 3,
81
+ payload: JSON.stringify({
82
+ thread_key: threadID.toString(),
83
+ is_group_thread: +isGroup,
84
+ is_typing: +sendTyping,
85
+ attribution: 0
86
+ }),
87
+ version: 5849951561777440
88
+ }),
89
+ request_id: ++count_req,
90
+ type: 4
91
+ };
92
+
93
+ // Wrap publish in a timeout so a stale MQTT connection never hangs this call.
94
+ await new Promise((resolve, reject) => {
95
+ const timer = setTimeout(() => reject(new Error("sendTypingIndicator: MQTT publish timed out")), 8000);
96
+ ctx.mqttClient.publish('/ls_req', JSON.stringify(wsContent), {}, (err, _packet) => {
97
+ clearTimeout(timer);
98
+ if (err) reject(err);
99
+ else resolve();
100
+ });
101
+ });
102
+
103
+ if (callback) callback();
104
+ };
105
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ module.exports = function (defaultFuncs, api, ctx) {
6
+ return async (reaction, messageID) => {
7
+ if (!reaction && reaction !== "") throw new Error("Please enter a valid emoji.");
8
+
9
+ // E2EE routing: setMessageReaction(emoji, { chatJid, messageId, senderJid })
10
+ if (utils.getType(messageID) === "Object" && messageID.chatJid && messageID.messageId) {
11
+ if (!api.e2ee || !api.e2ee.isAvailable()) throw new Error("E2EE bridge not available");
12
+ return api.e2ee.sendE2EEReaction(
13
+ messageID.chatJid,
14
+ messageID.messageId,
15
+ messageID.senderJid || '',
16
+ reaction
17
+ );
18
+ }
19
+
20
+ const defData = await defaultFuncs.postFormData("https://www.facebook.com/webgraphql/mutation/", ctx.jar, {}, {
21
+ doc_id: "1491398900900362",
22
+ variables: JSON.stringify({
23
+ data: {
24
+ client_mutation_id: ctx.clientMutationId++,
25
+ actor_id: ctx.userID,
26
+ action: reaction === "" ? "REMOVE_REACTION" : "ADD_REACTION",
27
+ message_id: messageID,
28
+ reaction
29
+ }
30
+ }),
31
+ dpr: 1
32
+ });
33
+ const resData = await utils.parseAndCheckLogin(ctx, defaultFuncs)(defData);
34
+ if (!resData) {
35
+ throw new Error("setMessageReactionLegacy returned empty object.");
36
+ }
37
+ };
38
+ };
@@ -0,0 +1,61 @@
1
+
2
+ 'use strict';
3
+
4
+ const utils = require('../utils');
5
+
6
+ function isCallable(func) {
7
+   try {
8
+     Reflect.apply(func, null, []);
9
+     return true;
10
+   } catch (error) {
11
+     return false;
12
+   }
13
+ }
14
+
15
+ module.exports = function (defaultFuncs, api, ctx) {
16
+   return function setMessageReactionMqtt(reaction, messageID, threadID) {
17
+     if (!ctx.mqttClient) {
18
+       throw new Error('Not connected to MQTT');
19
+     }
20
+
21
+     ctx.wsReqNumber += 1;
22
+     ctx.wsTaskNumber += 1;
23
+
24
+     const taskPayload = {
25
+       thread_key: threadID,
26
+       timestamp_ms: Date.now(),
27
+       message_id: messageID,
28
+       reaction,
29
+       actor_id: ctx.userID,
30
+       reaction_style: null,
31
+       sync_group: 1,
32
+       send_attribution: Math.random() < 0.5 ? 65537 : 524289
33
+     };
34
+
35
+     const task = {
36
+       failure_count: null,
37
+       label: '29',
38
+       payload: JSON.stringify(taskPayload),
39
+       queue_name: JSON.stringify(['reaction', messageID]),
40
+       task_id: ctx.wsTaskNumber
41
+     };
42
+
43
+     const content = {
44
+       app_id: '2220391788200892',
45
+       payload: JSON.stringify({
46
+         data_trace_id: null,
47
+         epoch_id: parseInt(utils.generateOfflineThreadingID()),
48
+         tasks: [task],
49
+         version_id: '7158486590867448',
50
+       }),
51
+       request_id: ctx.wsReqNumber,
52
+       type: 3,
53
+     };
54
+
55
+     /*if (isCallable(callback)) {
56
+       ctx.reqCallbacks[ctx.wsReqNumber] = callback;
57
+     }*/
58
+
59
+     ctx.mqttClient.publish('/ls_req', JSON.stringify(content), { qos: 1, retain: false });
60
+   };
61
+ };