@cexy/hoonfca 1.0.0 → 1.0.2
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/.travis.yml +6 -0
- package/LICENSE-MIT +21 -0
- package/index.d.ts +620 -0
- package/index.js +745 -433
- package/package.json +45 -6
- package/src/addExternalModule.js +14 -16
- package/src/addUserToGroup.js +45 -30
- package/src/changeAdminStatus.js +39 -42
- package/src/changeApprovalMode.js +79 -0
- package/src/changeArchivedStatus.js +19 -15
- package/src/changeBio.js +18 -16
- package/src/changeBlockedStatus.js +14 -12
- package/src/changeBlockedStatusMqtt.js +38 -17
- package/src/changeGroupImage.js +100 -77
- package/src/changeNickname.js +12 -11
- package/src/changeThreadColor.js +25 -16
- package/src/changeThreadEmoji.js +11 -10
- package/src/createNewGroup.js +21 -14
- package/src/createPoll.js +87 -21
- package/src/deleteMessage.js +18 -17
- package/src/deleteThread.js +18 -17
- package/src/editMessage.js +28 -31
- package/src/forwardAttachment.js +22 -19
- package/src/forwardMessage.js +63 -0
- package/src/getCurrentUserID.js +4 -2
- package/src/getEmojiUrl.js +20 -9
- package/src/getFriendsList.js +21 -18
- package/src/getGroupsList.js +83 -0
- package/src/getMarketplace.js +84 -0
- package/src/getNotifications.js +86 -0
- package/src/getPagesList.js +83 -0
- package/src/getStories.js +88 -0
- package/src/getThreadHistory.js +277 -126
- package/src/getThreadInfo.js +143 -115
- package/src/getThreadList.js +216 -143
- package/src/getThreadPictures.js +26 -32
- package/src/getUserID.js +13 -14
- package/src/getUserInfo.js +107 -68
- package/src/handleFriendRequest.js +16 -15
- package/src/handleMessageRequest.js +15 -15
- package/src/httpGet.js +36 -31
- package/src/httpPost.js +36 -31
- package/src/listenMqtt.js +705 -370
- package/src/logout.js +26 -28
- package/src/markAsDelivered.js +22 -17
- package/src/markAsRead.js +63 -49
- package/src/markAsReadAll.js +17 -13
- package/src/markAsSeen.js +23 -19
- package/src/muteThread.js +17 -14
- package/src/pinMessage.js +23 -22
- package/src/removeUserFromGroup.js +42 -22
- package/src/resolvePhotoUrl.js +15 -17
- package/src/searchForThread.js +18 -20
- package/src/searchUsers.js +89 -0
- package/src/sendComment.js +161 -0
- package/src/sendMessage.js +65 -52
- package/src/sendTypingIndicator.js +173 -48
- package/src/setBio.js +86 -0
- package/src/setMessageReaction.js +76 -16
- package/src/setPostReaction.js +29 -61
- package/src/setTheme.js +313 -0
- package/src/setTitle.js +27 -37
- package/src/shareContact.js +34 -82
- package/src/shareLink.js +56 -99
- package/src/threadColors.js +35 -97
- package/src/unfriend.js +15 -20
- package/src/unsendMessage.js +71 -18
- package/utils.js +1000 -472
- package/README.md +0 -83
- package/replit.nix +0 -3
- package/src/changeAvatar.js +0 -89
- package/src/changeCover.js +0 -92
- package/src/changeName.js +0 -106
- package/src/changeUsername.js +0 -80
- package/src/createCommentPost.js +0 -196
- package/src/createPost.js +0 -285
- package/src/data/getThreadInfo.json +0 -1
- package/src/follow.js +0 -71
- package/src/getAccess.js +0 -130
- package/src/getAvatarUser.js +0 -88
- package/src/getBotInitialData.js +0 -59
- package/src/getCtx.js +0 -5
- package/src/getMessage.js +0 -827
- package/src/getOptions.js +0 -5
- package/src/getRegion.js +0 -7
- package/src/getThreadHistoryDeprecated.js +0 -95
- package/src/getThreadInfoDeprecated.js +0 -77
- package/src/getThreadListDeprecated.js +0 -86
- package/src/httpPostFormData.js +0 -58
- package/src/listenNotification.js +0 -96
- package/src/refreshFb_dtsg.js +0 -75
- package/src/searchStickers.js +0 -59
- package/src/sendMessageMqtt.js +0 -326
- package/src/setMessageReactionMqtt.js +0 -74
- package/src/setProfileGuard.js +0 -66
- package/src/setStoryReaction.js +0 -97
- package/src/stopListenMqtt.js +0 -47
- package/src/uploadAttachment.js +0 -98
package/index.js
CHANGED
|
@@ -1,467 +1,779 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
var utils = require("./utils");
|
|
4
|
+
var cheerio = require("cheerio");
|
|
5
|
+
var log = require("npmlog");
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
var checkVerified = null;
|
|
8
|
+
|
|
9
|
+
var defaultLogRecordSize = 100;
|
|
10
|
+
log.maxRecordSize = defaultLogRecordSize;
|
|
10
11
|
|
|
11
12
|
function setOptions(globalOptions, options) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
13
|
+
Object.keys(options).map(function (key) {
|
|
14
|
+
switch (key) {
|
|
15
|
+
case "pauseLog":
|
|
16
|
+
if (options.pauseLog) log.pause();
|
|
17
|
+
break;
|
|
18
|
+
case "online":
|
|
19
|
+
globalOptions.online = Boolean(options.online);
|
|
20
|
+
break;
|
|
21
|
+
case "logLevel":
|
|
22
|
+
log.level = options.logLevel;
|
|
23
|
+
globalOptions.logLevel = options.logLevel;
|
|
24
|
+
break;
|
|
25
|
+
case "logRecordSize":
|
|
26
|
+
log.maxRecordSize = options.logRecordSize;
|
|
27
|
+
globalOptions.logRecordSize = options.logRecordSize;
|
|
28
|
+
break;
|
|
29
|
+
case "selfListen":
|
|
30
|
+
globalOptions.selfListen = Boolean(options.selfListen);
|
|
31
|
+
break;
|
|
32
|
+
case "listenEvents":
|
|
33
|
+
globalOptions.listenEvents = Boolean(options.listenEvents);
|
|
34
|
+
break;
|
|
35
|
+
case "pageID":
|
|
36
|
+
globalOptions.pageID = options.pageID.toString();
|
|
37
|
+
break;
|
|
38
|
+
case "updatePresence":
|
|
39
|
+
globalOptions.updatePresence = Boolean(options.updatePresence);
|
|
40
|
+
break;
|
|
41
|
+
case "forceLogin":
|
|
42
|
+
globalOptions.forceLogin = Boolean(options.forceLogin);
|
|
43
|
+
break;
|
|
44
|
+
case "userAgent":
|
|
45
|
+
globalOptions.userAgent = options.userAgent;
|
|
46
|
+
break;
|
|
47
|
+
case "autoMarkDelivery":
|
|
48
|
+
globalOptions.autoMarkDelivery = Boolean(options.autoMarkDelivery);
|
|
49
|
+
break;
|
|
50
|
+
case "autoMarkRead":
|
|
51
|
+
globalOptions.autoMarkRead = Boolean(options.autoMarkRead);
|
|
52
|
+
break;
|
|
53
|
+
case "listenTyping":
|
|
54
|
+
globalOptions.listenTyping = Boolean(options.listenTyping);
|
|
55
|
+
break;
|
|
56
|
+
case "proxy":
|
|
57
|
+
if (typeof options.proxy != "string") {
|
|
58
|
+
delete globalOptions.proxy;
|
|
59
|
+
utils.setProxy();
|
|
60
|
+
} else {
|
|
61
|
+
globalOptions.proxy = options.proxy;
|
|
62
|
+
utils.setProxy(globalOptions.proxy);
|
|
60
63
|
}
|
|
61
|
-
|
|
64
|
+
break;
|
|
65
|
+
case "autoReconnect":
|
|
66
|
+
globalOptions.autoReconnect = Boolean(options.autoReconnect);
|
|
67
|
+
break;
|
|
68
|
+
case "emitReady":
|
|
69
|
+
globalOptions.emitReady = Boolean(options.emitReady);
|
|
70
|
+
break;
|
|
71
|
+
default:
|
|
72
|
+
log.warn(
|
|
73
|
+
"setOptions",
|
|
74
|
+
"Unrecognized option given to setOptions: " + key
|
|
75
|
+
);
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
62
79
|
}
|
|
63
80
|
|
|
64
81
|
function buildAPI(globalOptions, html, jar) {
|
|
65
|
-
|
|
66
|
-
let irisSeqID = null;
|
|
67
|
-
|
|
68
|
-
function extractFromHTML() {
|
|
69
|
-
try {
|
|
70
|
-
const $ = cheerio.load(html);
|
|
71
|
-
const patterns = [
|
|
72
|
-
/\["DTSGInitialData",\[\],{"token":"([^"]+)"}]/,
|
|
73
|
-
/\["DTSGInitData",\[\],{"token":"([^"]+)"/,
|
|
74
|
-
/"token":"([^"]+)"/,
|
|
75
|
-
/{\\"token\\":\\"([^\\]+)\\"/,
|
|
76
|
-
/,\{"token":"([^"]+)"\},\d+\]/,
|
|
77
|
-
/"async_get_token":"([^"]+)"/,
|
|
78
|
-
/"dtsg":\{"token":"([^"]+)"/,
|
|
79
|
-
/DTSGInitialData[^>]+>([^<]+)/
|
|
80
|
-
];
|
|
81
|
-
|
|
82
|
-
const scripts = $('script').get();
|
|
83
|
-
for (const script of scripts) {
|
|
84
|
-
const scriptText = $(script).html() || '';
|
|
85
|
-
for (const pattern of patterns) {
|
|
86
|
-
const match = scriptText.match(pattern);
|
|
87
|
-
if (match && match[1]) {
|
|
88
|
-
fb_dtsg = match[1].includes('\\"') ? match[1].replace(/\\"/g, '"') : match[1];
|
|
89
|
-
if (fb_dtsg.startsWith('{')) {
|
|
90
|
-
try { fb_dtsg = JSON.parse(fb_dtsg).token || fb_dtsg; } catch(e){}
|
|
91
|
-
}
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (fb_dtsg) break;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!fb_dtsg) {
|
|
99
|
-
const dtsgInput = $('input[name="fb_dtsg"]').val();
|
|
100
|
-
if (dtsgInput) fb_dtsg = dtsgInput;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const seqMatches = html.match(/irisSeqID":"([^"]+)"/);
|
|
104
|
-
if (seqMatches && seqMatches[1]) {
|
|
105
|
-
irisSeqID = seqMatches[1];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (!fb_dtsg) {
|
|
109
|
-
const jsonMatches = html.match(/\{"dtsg":({[^}]+})/);
|
|
110
|
-
if (jsonMatches && jsonMatches[1]) {
|
|
111
|
-
try {
|
|
112
|
-
const dtsgData = JSON.parse(jsonMatches[1]);
|
|
113
|
-
if (dtsgData.token) fb_dtsg = dtsgData.token;
|
|
114
|
-
} catch(e){}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (fb_dtsg) {
|
|
119
|
-
console.log("Đã tìm thấy fb_dtsg");
|
|
120
|
-
}
|
|
121
|
-
} catch (e) {
|
|
122
|
-
console.log("Lỗi khi tìm fb_dtsg:", e);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
extractFromHTML();
|
|
127
|
-
let userID;
|
|
128
|
-
const cookies = jar.getCookies("https://www.facebook.com");
|
|
129
|
-
const userCookie = cookies.find(cookie => cookie.cookieString().startsWith("c_user="));
|
|
130
|
-
const tiktikCookie = cookies.find(cookie => cookie.cookieString().startsWith("i_user="));
|
|
131
|
-
|
|
132
|
-
if (!userCookie && !tiktikCookie) {
|
|
133
|
-
return log.error('login', "Không tìm thấy cookie cho người dùng, vui lòng kiểm tra lại thông tin đăng nhập");
|
|
134
|
-
}
|
|
135
|
-
if (html.includes("/checkpoint/block/?next")) {
|
|
136
|
-
return log.error('login', "Appstate die, vui lòng thay cái mới!", 'error');
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
userID = (tiktikCookie || userCookie).cookieString().split("=")[1];
|
|
140
|
-
const clientID = (Math.random() * 2147483648 | 0).toString(16);
|
|
141
|
-
let mqttEndpoint = `wss://edge-chat.facebook.com/chat?region=prn&sid=${userID}`;
|
|
142
|
-
let region = "PRN";
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
if (html.includes("601051028565049")) {
|
|
146
|
-
console.log(`lỗi login vì dính tài khoản tự động`);
|
|
147
|
-
global.ditconmemay = true;
|
|
148
|
-
}
|
|
149
|
-
const endpointMatch = html.match(/"endpoint":"([^"]+)"/);
|
|
150
|
-
if (endpointMatch && endpointMatch[1]) {
|
|
151
|
-
mqttEndpoint = endpointMatch[1].replace(/\\\//g, '/');
|
|
152
|
-
const url = new URL(mqttEndpoint);
|
|
153
|
-
region = url.searchParams.get('region')?.toUpperCase() || "PRN";
|
|
154
|
-
}
|
|
155
|
-
} catch (e) {
|
|
156
|
-
console.log('Using default MQTT endpoint');
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
log.info('login', 'Fix fca by DongDev x Satoru, published By Team Calyx');
|
|
160
|
-
|
|
161
|
-
const ctx = {
|
|
162
|
-
userID: userID,
|
|
163
|
-
jar: jar,
|
|
164
|
-
clientID: clientID,
|
|
165
|
-
globalOptions: globalOptions,
|
|
166
|
-
loggedIn: true,
|
|
167
|
-
access_token: 'NONE',
|
|
168
|
-
clientMutationId: 0,
|
|
169
|
-
mqttClient: undefined,
|
|
170
|
-
lastSeqId: irisSeqID,
|
|
171
|
-
syncToken: undefined,
|
|
172
|
-
mqttEndpoint: mqttEndpoint,
|
|
173
|
-
region: region,
|
|
174
|
-
firstListen: true,
|
|
175
|
-
fb_dtsg: fb_dtsg,
|
|
176
|
-
req_ID: 0,
|
|
177
|
-
callback_Task: {},
|
|
178
|
-
wsReqNumber: 0,
|
|
179
|
-
wsTaskNumber: 0,
|
|
180
|
-
reqCallbacks: {}
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const defaultFuncs = utils.makeDefaults(html, userID, ctx);
|
|
184
|
-
const api = {
|
|
185
|
-
setOptions: setOptions.bind(null, globalOptions),
|
|
186
|
-
getAppState: () => utils.getAppState(jar),
|
|
187
|
-
postFormData: function (url, body) {
|
|
188
|
-
return defaultFuncs.postFormData(url, ctx.jar, body);
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
api.getFreshDtsg = async function () {
|
|
193
|
-
try {
|
|
194
|
-
const res = await defaultFuncs.get('https://www.facebook.com/', jar, null, globalOptions);
|
|
195
|
-
const $ = cheerio.load(res.body);
|
|
196
|
-
let newDtsg;
|
|
197
|
-
const patterns = [
|
|
198
|
-
/\["DTSGInitialData",\[\],{"token":"([^"]+)"}]/,
|
|
199
|
-
/\["DTSGInitData",\[\],{"token":"([^"]+)"/,
|
|
200
|
-
/"token":"([^"]+)"/,
|
|
201
|
-
/name="fb_dtsg" value="([^"]+)"/
|
|
202
|
-
];
|
|
203
|
-
|
|
204
|
-
const scripts = $('script').get();
|
|
205
|
-
for (const script of scripts) {
|
|
206
|
-
const scriptText = $(script).html() || '';
|
|
207
|
-
for (const pattern of patterns) {
|
|
208
|
-
const match = scriptText.match(pattern);
|
|
209
|
-
if (match && match[1]) {
|
|
210
|
-
newDtsg = match[1];
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (newDtsg) break;
|
|
215
|
-
}
|
|
82
|
+
var maybeCookie = jar.getCookies("https://www.facebook.com");
|
|
216
83
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
84
|
+
utils.usersCache.resetModel();
|
|
85
|
+
utils.groupsCache.resetModel();
|
|
220
86
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
87
|
+
if (
|
|
88
|
+
maybeCookie.some((val) => val.cookieString().split("=")[0] === "i_user")
|
|
89
|
+
) {
|
|
90
|
+
maybeCookie = [
|
|
91
|
+
maybeCookie.find((val) => val.cookieString().split("=")[0] === "i_user"),
|
|
92
|
+
];
|
|
93
|
+
} else {
|
|
94
|
+
maybeCookie = maybeCookie.filter(
|
|
95
|
+
(val) => val.cookieString().split("=")[0] === "c_user"
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (maybeCookie.length === 0) {
|
|
100
|
+
throw {
|
|
101
|
+
error:
|
|
102
|
+
"Error retrieving userID. This can be caused by a lot of things, including getting blocked by Facebook for logging in from an unknown location. Try logging in with a browser to verify.",
|
|
226
103
|
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (html.indexOf("/checkpoint/block/?next") > -1) {
|
|
107
|
+
log.warn(
|
|
108
|
+
"login",
|
|
109
|
+
"Checkpoint detected. Please log in with a browser to verify."
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var userID = maybeCookie[0].cookieString().split("=")[1].toString();
|
|
114
|
+
log.info("login", `Logged in as ${userID}`);
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
clearInterval(checkVerified);
|
|
118
|
+
} catch (_) {}
|
|
119
|
+
|
|
120
|
+
var clientID = ((Math.random() * 2147483648) | 0).toString(16);
|
|
121
|
+
|
|
122
|
+
let oldFBMQTTMatch = html.match(
|
|
123
|
+
/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/
|
|
124
|
+
);
|
|
125
|
+
let mqttEndpoint = null;
|
|
126
|
+
let region = null;
|
|
127
|
+
let irisSeqID = null;
|
|
128
|
+
var noMqttData = null;
|
|
129
|
+
|
|
130
|
+
if (oldFBMQTTMatch) {
|
|
131
|
+
irisSeqID = oldFBMQTTMatch[1];
|
|
132
|
+
mqttEndpoint = oldFBMQTTMatch[2];
|
|
133
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
134
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
135
|
+
} else {
|
|
136
|
+
let newFBMQTTMatch = html.match(
|
|
137
|
+
/{"app_id":"219994525426954","endpoint":"(.+?)","iris_seq_id":"(.+?)"}/
|
|
138
|
+
);
|
|
139
|
+
if (newFBMQTTMatch) {
|
|
140
|
+
irisSeqID = newFBMQTTMatch[2];
|
|
141
|
+
mqttEndpoint = newFBMQTTMatch[1].replace(/\\\//g, "/");
|
|
142
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
143
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
144
|
+
} else {
|
|
145
|
+
let legacyFBMQTTMatch = html.match(
|
|
146
|
+
/(\["MqttWebConfig",\[\],{fbid:")(.+?)(",appID:219994525426954,endpoint:")(.+?)(",pollingEndpoint:")(.+?)(3790])/
|
|
147
|
+
);
|
|
148
|
+
if (legacyFBMQTTMatch) {
|
|
149
|
+
mqttEndpoint = legacyFBMQTTMatch[4];
|
|
150
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
151
|
+
log.warn(
|
|
152
|
+
"login",
|
|
153
|
+
`Cannot get sequence ID with new RegExp. Fallback to old RegExp (without seqID)...`
|
|
154
|
+
);
|
|
155
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
156
|
+
log.info("login", `[Unused] Polling endpoint: ${legacyFBMQTTMatch[6]}`);
|
|
157
|
+
} else {
|
|
158
|
+
log.warn("login", "Cannot get MQTT region & sequence ID.");
|
|
159
|
+
noMqttData = html;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// All data available to api functions
|
|
165
|
+
var ctx = {
|
|
166
|
+
userID: userID,
|
|
167
|
+
jar: jar,
|
|
168
|
+
clientID: clientID,
|
|
169
|
+
globalOptions: globalOptions,
|
|
170
|
+
loggedIn: true,
|
|
171
|
+
access_token: "NONE",
|
|
172
|
+
clientMutationId: 0,
|
|
173
|
+
mqttClient: undefined,
|
|
174
|
+
lastSeqId: irisSeqID,
|
|
175
|
+
syncToken: undefined,
|
|
176
|
+
mqttEndpoint,
|
|
177
|
+
wsReqNumber: 0,
|
|
178
|
+
wsTaskNumber: 0,
|
|
179
|
+
region,
|
|
180
|
+
firstListen: true,
|
|
181
|
+
tasks: new Map(),
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
var api = {
|
|
185
|
+
setOptions: setOptions.bind(null, globalOptions),
|
|
186
|
+
getAppState: function getAppState() {
|
|
187
|
+
return utils.getAppState(jar);
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
if (noMqttData) {
|
|
192
|
+
api["htmlData"] = noMqttData;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const apiFuncNames = [
|
|
196
|
+
"addExternalModule",
|
|
197
|
+
"addUserToGroup",
|
|
198
|
+
"changeAdminStatus",
|
|
199
|
+
"changeApprovalMode",
|
|
200
|
+
"changeArchivedStatus",
|
|
201
|
+
"changeBio",
|
|
202
|
+
"changeBlockedStatus",
|
|
203
|
+
"changeBlockedStatusMqtt",
|
|
204
|
+
"changeGroupImage",
|
|
205
|
+
"changeNickname",
|
|
206
|
+
"changeThreadColor",
|
|
207
|
+
"changeThreadEmoji",
|
|
208
|
+
"createNewGroup",
|
|
209
|
+
"createPoll",
|
|
210
|
+
"deleteMessage",
|
|
211
|
+
"deleteThread",
|
|
212
|
+
"editMessage",
|
|
213
|
+
"forwardAttachment",
|
|
214
|
+
"forwardMessage",
|
|
215
|
+
"getCurrentUserID",
|
|
216
|
+
"getEmojiUrl",
|
|
217
|
+
"getFriendsList",
|
|
218
|
+
"getGroupsList",
|
|
219
|
+
"getMarketplace",
|
|
220
|
+
"getNotifications",
|
|
221
|
+
"getPagesList",
|
|
222
|
+
"getStories",
|
|
223
|
+
"getThreadHistory",
|
|
224
|
+
"getThreadInfo",
|
|
225
|
+
"getThreadList",
|
|
226
|
+
"getThreadPictures",
|
|
227
|
+
"getUserID",
|
|
228
|
+
"getUserInfo",
|
|
229
|
+
"handleMessageRequest",
|
|
230
|
+
"listenMqtt",
|
|
231
|
+
"logout",
|
|
232
|
+
"markAsDelivered",
|
|
233
|
+
"markAsRead",
|
|
234
|
+
"markAsReadAll",
|
|
235
|
+
"markAsSeen",
|
|
236
|
+
"muteThread",
|
|
237
|
+
"pinMessage",
|
|
238
|
+
"removeUserFromGroup",
|
|
239
|
+
"resolvePhotoUrl",
|
|
240
|
+
"searchForThread",
|
|
241
|
+
"searchUsers",
|
|
242
|
+
"sendMessage",
|
|
243
|
+
"sendTypingIndicator",
|
|
244
|
+
"setBio",
|
|
245
|
+
"setMessageReaction",
|
|
246
|
+
"setTitle",
|
|
247
|
+
"shareContact",
|
|
248
|
+
"shareLink",
|
|
249
|
+
"setTheme",
|
|
250
|
+
"threadColors",
|
|
251
|
+
"unsendMessage",
|
|
252
|
+
"handleFriendRequest",
|
|
253
|
+
"sendComment",
|
|
254
|
+
"setPostReaction",
|
|
255
|
+
"unfriend",
|
|
256
|
+
|
|
257
|
+
// HTTP
|
|
258
|
+
"httpGet",
|
|
259
|
+
"httpPost",
|
|
260
|
+
];
|
|
261
|
+
|
|
262
|
+
var defaultFuncs = utils.makeDefaults(html, userID, ctx);
|
|
263
|
+
|
|
264
|
+
// Load all api functions in a loop
|
|
265
|
+
apiFuncNames.map(function (v) {
|
|
266
|
+
api[v] = require("./src/" + v)(defaultFuncs, api, ctx);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
//Removing original `listen` that uses pull.
|
|
270
|
+
//Map it to listenMqtt instead for backward compatibly.
|
|
271
|
+
api.listen = api.listenMqtt;
|
|
272
|
+
|
|
273
|
+
return [ctx, defaultFuncs, api];
|
|
236
274
|
}
|
|
237
275
|
|
|
238
276
|
function makeLogin(jar, email, password, loginOptions, callback, prCallback) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
277
|
+
return function (res) {
|
|
278
|
+
var html = res.body;
|
|
279
|
+
var $ = cheerio.load(html);
|
|
280
|
+
var arr = [];
|
|
281
|
+
|
|
282
|
+
// This will be empty, but just to be sure we leave it
|
|
283
|
+
$("#login_form input").map(function (i, v) {
|
|
284
|
+
arr.push({ val: $(v).val(), name: $(v).attr("name") });
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
arr = arr.filter(function (v) {
|
|
288
|
+
return v.val && v.val.length;
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
var form = utils.arrToForm(arr);
|
|
292
|
+
form.lsd = utils.getFrom(html, '["LSD",[],{"token":"', '"}');
|
|
293
|
+
form.lgndim = Buffer.from(
|
|
294
|
+
'{"w":1440,"h":900,"aw":1440,"ah":834,"c":24}'
|
|
295
|
+
).toString("base64");
|
|
296
|
+
form.email = email;
|
|
297
|
+
form.pass = password;
|
|
298
|
+
form.default_persistent = "0";
|
|
299
|
+
form.lgnrnd = utils.getFrom(html, 'name="lgnrnd" value="', '"');
|
|
300
|
+
form.locale = "en_US";
|
|
301
|
+
form.timezone = "240";
|
|
302
|
+
form.lgnjs = ~~(Date.now() / 1000);
|
|
303
|
+
|
|
304
|
+
// Getting cookies from the HTML page... (kill me now plz)
|
|
305
|
+
// we used to get a bunch of cookies in the headers of the response of the
|
|
306
|
+
// request, but FB changed and they now send those cookies inside the JS.
|
|
307
|
+
// They run the JS which then injects the cookies in the page.
|
|
308
|
+
// The "solution" is to parse through the html and find those cookies
|
|
309
|
+
// which happen to be conveniently indicated with a _js_ in front of their
|
|
310
|
+
// variable name.
|
|
311
|
+
//
|
|
312
|
+
// ---------- Very Hacky Part Starts -----------------
|
|
313
|
+
var willBeCookies = html.split('"_js_');
|
|
314
|
+
willBeCookies.slice(1).map(function (val) {
|
|
315
|
+
var cookieData = JSON.parse('["' + utils.getFrom(val, "", "]") + "]");
|
|
316
|
+
jar.setCookie(
|
|
317
|
+
utils.formatCookie(cookieData, "facebook"),
|
|
318
|
+
"https://www.facebook.com"
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
// ---------- Very Hacky Part Ends -----------------
|
|
322
|
+
|
|
323
|
+
log.info("login", "Logging in...");
|
|
324
|
+
return utils
|
|
325
|
+
.post(
|
|
326
|
+
"https://mbasic.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110",
|
|
327
|
+
jar,
|
|
328
|
+
form,
|
|
329
|
+
loginOptions
|
|
330
|
+
)
|
|
331
|
+
.then(utils.saveCookies(jar))
|
|
332
|
+
.then(function (res) {
|
|
333
|
+
var headers = res.headers;
|
|
334
|
+
if (!headers.location) {
|
|
335
|
+
throw { error: "Wrong username/password." };
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// This means the account has login approvals turned on.
|
|
339
|
+
if (
|
|
340
|
+
headers.location.indexOf("https://mbasic.facebook.com/checkpoint/") > -1
|
|
341
|
+
) {
|
|
342
|
+
log.info("login", "You have login approvals turned on.");
|
|
343
|
+
var nextURL =
|
|
344
|
+
"https://mbasic.facebook.com/checkpoint/?next=https%3A%2F%2Fmbasic.facebook.com%2Fhome.php";
|
|
345
|
+
|
|
346
|
+
return utils
|
|
347
|
+
.get(headers.location, jar, null, loginOptions)
|
|
348
|
+
.then(utils.saveCookies(jar))
|
|
349
|
+
.then(function (res) {
|
|
350
|
+
var html = res.body;
|
|
351
|
+
// Make the form in advance which will contain the fb_dtsg and nh
|
|
352
|
+
var $ = cheerio.load(html);
|
|
353
|
+
var arr = [];
|
|
354
|
+
$("form input").map(function (i, v) {
|
|
355
|
+
arr.push({
|
|
356
|
+
val: $(v).val(),
|
|
357
|
+
name: $(v).attr("name"),
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
arr = arr.filter(function (v) {
|
|
362
|
+
return v.val && v.val.length;
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
var form = utils.arrToForm(arr);
|
|
366
|
+
if (html.indexOf("checkpoint/?next") > -1) {
|
|
367
|
+
setTimeout(() => {
|
|
368
|
+
checkVerified = setInterval(
|
|
369
|
+
(_form) => {
|
|
370
|
+
/* utils
|
|
371
|
+
.post("https://www.facebook.com/login/approvals/approved_machine_check/", jar, form, loginOptions, null, {
|
|
372
|
+
"Referer": "https://www.facebook.com/checkpoint/?next"
|
|
373
|
+
})
|
|
374
|
+
.then(utils.saveCookies(jar))
|
|
375
|
+
.then(res => {
|
|
376
|
+
try {
|
|
377
|
+
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*()/, ""));
|
|
378
|
+
} catch (ex) {
|
|
379
|
+
clearInterval(checkVerified);
|
|
380
|
+
log.info("login", "Verified from browser. Logging in...");
|
|
381
|
+
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
.catch(ex => {
|
|
385
|
+
log.error("login", ex);
|
|
386
|
+
}); */
|
|
387
|
+
},
|
|
388
|
+
5000,
|
|
389
|
+
{
|
|
390
|
+
fb_dtsg: form.fb_dtsg,
|
|
391
|
+
jazoest: form.jazoest,
|
|
392
|
+
dpr: 1,
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
}, 2500);
|
|
396
|
+
throw {
|
|
397
|
+
error: "login-approval",
|
|
398
|
+
continue: function submit2FA(code) {
|
|
399
|
+
form.approvals_code = code;
|
|
400
|
+
form["submit[Continue]"] = $(
|
|
401
|
+
"#checkpointSubmitButton"
|
|
402
|
+
).html(); //'Continue';
|
|
403
|
+
var prResolve = null;
|
|
404
|
+
var prReject = null;
|
|
405
|
+
var rtPromise = new Promise(function (resolve, reject) {
|
|
406
|
+
prResolve = resolve;
|
|
407
|
+
prReject = reject;
|
|
322
408
|
});
|
|
409
|
+
if (typeof code == "string") {
|
|
410
|
+
utils
|
|
411
|
+
.post(nextURL, jar, form, loginOptions)
|
|
412
|
+
.then(utils.saveCookies(jar))
|
|
413
|
+
.then(function (res) {
|
|
414
|
+
var $ = cheerio.load(res.body);
|
|
415
|
+
var error = $("#approvals_code")
|
|
416
|
+
.parent()
|
|
417
|
+
.attr("data-xui-error");
|
|
418
|
+
if (error) {
|
|
419
|
+
throw {
|
|
420
|
+
error: "login-approval",
|
|
421
|
+
errordesc: "Invalid 2FA code.",
|
|
422
|
+
lerror: error,
|
|
423
|
+
continue: submit2FA,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
})
|
|
427
|
+
.then(function () {
|
|
428
|
+
// Use the same form (safe I hope)
|
|
429
|
+
delete form.no_fido;
|
|
430
|
+
delete form.approvals_code;
|
|
431
|
+
form.name_action_selected = "dont_save"; //'save_device';
|
|
432
|
+
|
|
433
|
+
return utils
|
|
434
|
+
.post(nextURL, jar, form, loginOptions)
|
|
435
|
+
.then(utils.saveCookies(jar));
|
|
436
|
+
})
|
|
437
|
+
.then(function (res) {
|
|
438
|
+
var headers = res.headers;
|
|
439
|
+
if (
|
|
440
|
+
!headers.location &&
|
|
441
|
+
res.body.indexOf("Review Recent Login") > -1
|
|
442
|
+
) {
|
|
443
|
+
throw {
|
|
444
|
+
error:
|
|
445
|
+
"Something went wrong with login approvals.",
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
var appState = utils.getAppState(jar);
|
|
450
|
+
|
|
451
|
+
if (callback === prCallback) {
|
|
452
|
+
callback = function (err, api) {
|
|
453
|
+
if (err) {
|
|
454
|
+
return prReject(err);
|
|
455
|
+
}
|
|
456
|
+
return prResolve(api);
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// Simply call loginHelper because all it needs is the jar
|
|
461
|
+
// and will then complete the login process
|
|
462
|
+
return loginHelper(
|
|
463
|
+
appState,
|
|
464
|
+
email,
|
|
465
|
+
password,
|
|
466
|
+
loginOptions,
|
|
467
|
+
callback
|
|
468
|
+
);
|
|
469
|
+
})
|
|
470
|
+
.catch(function (err) {
|
|
471
|
+
// Check if using Promise instead of callback
|
|
472
|
+
if (callback === prCallback) {
|
|
473
|
+
prReject(err);
|
|
474
|
+
} else {
|
|
475
|
+
callback(err);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
} else {
|
|
479
|
+
utils
|
|
480
|
+
.post(
|
|
481
|
+
"https://mbasic.facebook.com/checkpoint/?next=https%3A%2F%2Fmbasic.facebook.com%2Fhome.php",
|
|
482
|
+
jar,
|
|
483
|
+
form,
|
|
484
|
+
loginOptions,
|
|
485
|
+
null,
|
|
486
|
+
{
|
|
487
|
+
Referer:
|
|
488
|
+
"https://mbasic.facebook.com/checkpoint/?next",
|
|
489
|
+
}
|
|
490
|
+
)
|
|
491
|
+
.then(utils.saveCookies(jar))
|
|
492
|
+
.then((res) => {
|
|
493
|
+
try {
|
|
494
|
+
JSON.parse(
|
|
495
|
+
res.body.replace(
|
|
496
|
+
/for\s*\(\s*;\s*;\s*\)\s*;\s*/,
|
|
497
|
+
""
|
|
498
|
+
)
|
|
499
|
+
);
|
|
500
|
+
} catch (ex) {
|
|
501
|
+
clearInterval(checkVerified);
|
|
502
|
+
log.info(
|
|
503
|
+
"login",
|
|
504
|
+
"Verified from browser. Logging in..."
|
|
505
|
+
);
|
|
506
|
+
if (callback === prCallback) {
|
|
507
|
+
callback = function (err, api) {
|
|
508
|
+
if (err) {
|
|
509
|
+
return prReject(err);
|
|
510
|
+
}
|
|
511
|
+
return prResolve(api);
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
return loginHelper(
|
|
515
|
+
utils.getAppState(jar),
|
|
516
|
+
email,
|
|
517
|
+
password,
|
|
518
|
+
loginOptions,
|
|
519
|
+
callback
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
})
|
|
523
|
+
.catch((ex) => {
|
|
524
|
+
log.error("login", ex);
|
|
525
|
+
if (callback === prCallback) {
|
|
526
|
+
prReject(ex);
|
|
527
|
+
} else {
|
|
528
|
+
callback(ex);
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
return rtPromise;
|
|
533
|
+
},
|
|
534
|
+
};
|
|
535
|
+
} else {
|
|
536
|
+
if (!loginOptions.forceLogin) {
|
|
537
|
+
throw {
|
|
538
|
+
error:
|
|
539
|
+
"Couldn't login. Facebook might have blocked this account. Please login with a browser or enable the option 'forceLogin' and try again.",
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
if (html.indexOf("Suspicious Login Attempt") > -1) {
|
|
543
|
+
form["submit[This was me]"] = "This was me";
|
|
544
|
+
} else {
|
|
545
|
+
form["submit[This Is Okay]"] = "This Is Okay";
|
|
323
546
|
}
|
|
324
|
-
if (!loginOptions.forceLogin) throw new Error("Couldn't login. Facebook might have blocked this account.");
|
|
325
|
-
form2fa['submit[This was me]'] = checkpointHtml.includes("Suspicious Login Attempt") ? "This was me" : "This Is Okay";
|
|
326
|
-
await utils.post("https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php", jar, form2fa, loginOptions);
|
|
327
|
-
form2fa.name_action_selected = 'save_device';
|
|
328
|
-
await utils.post("https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php", jar, form2fa, loginOptions);
|
|
329
|
-
const appState = utils.getAppState(jar);
|
|
330
|
-
return await loginHelper(appState, email, password, loginOptions, callback, prCallback);
|
|
331
|
-
}
|
|
332
|
-
await utils.get('https://www.facebook.com/', jar, null, loginOptions);
|
|
333
|
-
return await utils.saveCookies(jar)(loginRes);
|
|
334
|
-
} catch (error) {
|
|
335
|
-
callback(error);
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
547
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
548
|
+
return utils
|
|
549
|
+
.post(nextURL, jar, form, loginOptions)
|
|
550
|
+
.then(utils.saveCookies(jar))
|
|
551
|
+
.then(function () {
|
|
552
|
+
// Use the same form (safe I hope)
|
|
553
|
+
form.name_action_selected = "save_device";
|
|
554
|
+
|
|
555
|
+
return utils
|
|
556
|
+
.post(nextURL, jar, form, loginOptions)
|
|
557
|
+
.then(utils.saveCookies(jar));
|
|
558
|
+
})
|
|
559
|
+
.then(function (res) {
|
|
560
|
+
var headers = res.headers;
|
|
351
561
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
562
|
+
if (
|
|
563
|
+
!headers.location &&
|
|
564
|
+
res.body.indexOf("Review Recent Login") > -1
|
|
565
|
+
) {
|
|
566
|
+
throw {
|
|
567
|
+
error: "Something went wrong with review recent login.",
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
var appState = utils.getAppState(jar);
|
|
572
|
+
|
|
573
|
+
// Simply call loginHelper because all it needs is the jar
|
|
574
|
+
// and will then complete the login process
|
|
575
|
+
return loginHelper(
|
|
576
|
+
appState,
|
|
577
|
+
email,
|
|
578
|
+
password,
|
|
579
|
+
loginOptions,
|
|
580
|
+
callback
|
|
581
|
+
);
|
|
582
|
+
})
|
|
583
|
+
.catch(function (e) {
|
|
584
|
+
callback(e);
|
|
585
|
+
});
|
|
586
|
+
}
|
|
356
587
|
});
|
|
357
|
-
mainPromise = utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
|
|
358
|
-
.then(utils.saveCookies(jar));
|
|
359
|
-
} catch (e) {
|
|
360
|
-
return callback(new Error("Failed to load appState cookies: " + e.message));
|
|
361
588
|
}
|
|
362
|
-
} else {
|
|
363
|
-
mainPromise = utils
|
|
364
|
-
.get("https://www.facebook.com/", null, null, globalOptions, { noRef: true })
|
|
365
|
-
.then(utils.saveCookies(jar))
|
|
366
|
-
.then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
|
|
367
|
-
.then(() => utils.get('https://www.facebook.com/', jar, null, globalOptions).then(utils.saveCookies(jar)));
|
|
368
|
-
}
|
|
369
589
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
590
|
+
return utils
|
|
591
|
+
.get("https://mbasic.facebook.com/", jar, null, loginOptions)
|
|
592
|
+
.then(utils.saveCookies(jar));
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// Helps the login
|
|
598
|
+
function loginHelper(
|
|
599
|
+
appState,
|
|
600
|
+
email,
|
|
601
|
+
password,
|
|
602
|
+
globalOptions,
|
|
603
|
+
callback,
|
|
604
|
+
prCallback
|
|
605
|
+
) {
|
|
606
|
+
var mainPromise = null;
|
|
607
|
+
var jar = utils.getJar();
|
|
608
|
+
|
|
609
|
+
// If we're given an appState we loop through it and save each cookie
|
|
610
|
+
// back into the jar.
|
|
611
|
+
if (appState) {
|
|
612
|
+
appState.map(function (c) {
|
|
613
|
+
var str =
|
|
614
|
+
c.key +
|
|
615
|
+
"=" +
|
|
616
|
+
c.value +
|
|
617
|
+
"; expires=" +
|
|
618
|
+
c.expires +
|
|
619
|
+
"; domain=" +
|
|
620
|
+
c.domain +
|
|
621
|
+
"; path=" +
|
|
622
|
+
c.path +
|
|
623
|
+
";";
|
|
624
|
+
jar.setCookie(str, "http://" + c.domain);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// Load the main page.
|
|
628
|
+
mainPromise = utils
|
|
629
|
+
.get("https://www.facebook.com/", jar, null, globalOptions, {
|
|
630
|
+
noRef: true,
|
|
631
|
+
})
|
|
632
|
+
.then(utils.saveCookies(jar));
|
|
633
|
+
} else {
|
|
634
|
+
// Open the main page, then we login with the given credentials and finally
|
|
635
|
+
// load the main page again (it'll give us some IDs that we need)
|
|
636
|
+
mainPromise = utils
|
|
637
|
+
.get("https://www.facebook.com/", null, null, globalOptions, {
|
|
638
|
+
noRef: true,
|
|
639
|
+
})
|
|
640
|
+
.then(utils.saveCookies(jar))
|
|
641
|
+
.then(
|
|
642
|
+
makeLogin(jar, email, password, globalOptions, callback, prCallback)
|
|
643
|
+
)
|
|
644
|
+
.then(function () {
|
|
645
|
+
return utils
|
|
646
|
+
.get("https://www.facebook.com/", jar, null, globalOptions)
|
|
647
|
+
.then(utils.saveCookies(jar));
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
var ctx = null;
|
|
652
|
+
var _defaultFuncs = null;
|
|
653
|
+
var api = null;
|
|
378
654
|
|
|
655
|
+
mainPromise = mainPromise
|
|
656
|
+
.then(function (res) {
|
|
657
|
+
// Hacky check for the redirection that happens on some ISPs, which doesn't return statusCode 3xx
|
|
658
|
+
var reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
|
|
659
|
+
var redirect = reg.exec(res.body);
|
|
660
|
+
if (redirect && redirect[1]) {
|
|
661
|
+
return utils
|
|
662
|
+
.get(redirect[1], jar, null, globalOptions)
|
|
663
|
+
.then(utils.saveCookies(jar));
|
|
664
|
+
}
|
|
665
|
+
return res;
|
|
666
|
+
})
|
|
667
|
+
.then(function (res) {
|
|
668
|
+
var html = res.body;
|
|
669
|
+
var stuff = buildAPI(globalOptions, html, jar);
|
|
670
|
+
ctx = stuff[0];
|
|
671
|
+
_defaultFuncs = stuff[1];
|
|
672
|
+
api = stuff[2];
|
|
673
|
+
return res;
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
// given a pageID we log in as a page
|
|
677
|
+
if (globalOptions.pageID) {
|
|
379
678
|
mainPromise = mainPromise
|
|
380
|
-
|
|
381
|
-
.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
679
|
+
.then(function () {
|
|
680
|
+
return utils.get(
|
|
681
|
+
"https://www.facebook.com/" +
|
|
682
|
+
ctx.globalOptions.pageID +
|
|
683
|
+
"/messages/?section=messages&subsection=inbox",
|
|
684
|
+
ctx.jar,
|
|
685
|
+
null,
|
|
686
|
+
globalOptions
|
|
687
|
+
);
|
|
688
|
+
})
|
|
689
|
+
.then(function (resData) {
|
|
690
|
+
var url = utils
|
|
691
|
+
.getFrom(
|
|
692
|
+
resData.body,
|
|
693
|
+
'window.location.replace("https:\\/\\/www.facebook.com\\',
|
|
694
|
+
'");'
|
|
695
|
+
)
|
|
696
|
+
.split("\\")
|
|
697
|
+
.join("");
|
|
698
|
+
url = url.substring(0, url.length - 1);
|
|
699
|
+
|
|
700
|
+
return utils.get(
|
|
701
|
+
"https://www.facebook.com" + url,
|
|
702
|
+
ctx.jar,
|
|
703
|
+
null,
|
|
704
|
+
globalOptions
|
|
705
|
+
);
|
|
706
|
+
});
|
|
707
|
+
}
|
|
403
708
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
709
|
+
// At the end we call the callback or catch an exception
|
|
710
|
+
mainPromise
|
|
711
|
+
.then(function () {
|
|
712
|
+
log.info("login", "Done logging in.");
|
|
713
|
+
return callback(null, api);
|
|
714
|
+
})
|
|
715
|
+
.catch(function (e) {
|
|
716
|
+
log.error("login", e.error || e);
|
|
717
|
+
callback(e);
|
|
718
|
+
});
|
|
414
719
|
}
|
|
415
720
|
|
|
416
721
|
function login(loginData, options, callback) {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
722
|
+
if (
|
|
723
|
+
utils.getType(options) === "Function" ||
|
|
724
|
+
utils.getType(options) === "AsyncFunction"
|
|
725
|
+
) {
|
|
726
|
+
callback = options;
|
|
727
|
+
options = {};
|
|
728
|
+
}
|
|
421
729
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
730
|
+
var globalOptions = {
|
|
731
|
+
selfListen: false,
|
|
732
|
+
listenEvents: true,
|
|
733
|
+
listenTyping: false,
|
|
734
|
+
updatePresence: false,
|
|
735
|
+
forceLogin: false,
|
|
736
|
+
autoMarkDelivery: true,
|
|
737
|
+
autoMarkRead: false,
|
|
738
|
+
autoReconnect: true,
|
|
739
|
+
logRecordSize: defaultLogRecordSize,
|
|
740
|
+
online: true,
|
|
741
|
+
emitReady: false,
|
|
742
|
+
cacheTime: 5 * 60 * 1000,
|
|
743
|
+
userAgent:
|
|
744
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18",
|
|
745
|
+
};
|
|
436
746
|
|
|
437
|
-
|
|
438
|
-
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
|
439
|
-
let rejectFunc = null;
|
|
440
|
-
let resolveFunc = null;
|
|
441
|
-
const returnPromise = new Promise(function (resolve, reject) {
|
|
442
|
-
resolveFunc = resolve;
|
|
443
|
-
rejectFunc = reject;
|
|
444
|
-
});
|
|
445
|
-
prCallback = function (error, api) {
|
|
446
|
-
if (error) return rejectFunc(error);
|
|
447
|
-
return resolveFunc(api);
|
|
448
|
-
};
|
|
449
|
-
callback = prCallback;
|
|
450
|
-
}
|
|
747
|
+
setOptions(globalOptions, options || {});
|
|
451
748
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
|
|
749
|
+
var prCallback = null;
|
|
750
|
+
if (
|
|
751
|
+
utils.getType(callback) !== "Function" &&
|
|
752
|
+
utils.getType(callback) !== "AsyncFunction"
|
|
753
|
+
) {
|
|
754
|
+
var rejectFunc = null;
|
|
755
|
+
var resolveFunc = null;
|
|
756
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
757
|
+
resolveFunc = resolve;
|
|
758
|
+
rejectFunc = reject;
|
|
759
|
+
});
|
|
760
|
+
prCallback = function (error, api) {
|
|
761
|
+
if (error) {
|
|
762
|
+
return rejectFunc(error);
|
|
763
|
+
}
|
|
764
|
+
return resolveFunc(api);
|
|
765
|
+
};
|
|
766
|
+
callback = prCallback;
|
|
767
|
+
}
|
|
768
|
+
loginHelper(
|
|
769
|
+
loginData.appState,
|
|
770
|
+
loginData.email,
|
|
771
|
+
loginData.password,
|
|
772
|
+
globalOptions,
|
|
773
|
+
callback,
|
|
774
|
+
prCallback
|
|
775
|
+
);
|
|
776
|
+
return returnPromise;
|
|
465
777
|
}
|
|
466
778
|
|
|
467
779
|
module.exports = login;
|