@dongdev/fca-unofficial 0.0.1 → 0.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.
Files changed (3) hide show
  1. package/index.js +31 -19
  2. package/package.json +81 -84
  3. package/src/listenMqtt.js +59 -50
package/index.js CHANGED
@@ -451,26 +451,38 @@ function loginHelper(appState, email, password, globalOptions, callback, prCallb
451
451
  });
452
452
  }
453
453
 
454
- var ctx = null;
455
- var _defaultFuncs = null;
456
- var api = null;
457
-
458
- mainPromise = mainPromise
459
- .then(function (res) {
460
- // Hacky check for the redirection that happens on some ISPs, which doesn't return statusCode 3xx
461
- var reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
462
- var redirect = reg.exec(res.body);
463
- if (redirect && redirect[1]) return utils.get(redirect[1], jar, null, globalOptions).then(utils.saveCookies(jar));
464
- return res;
465
- })
466
- .then(function (res) {
467
- var html = res.body;
468
- var stuff = buildAPI(globalOptions, html, jar);
469
- ctx = stuff[0];
470
- _defaultFuncs = stuff[1];
471
- api = stuff[2];
454
+ var redirect = [1, "https://m.facebook.com/"], bypass_region_err = false, ctx, _defaultFuncs, api;
455
+ function CheckAndFixErr(res) {
456
+ if (/This browser is not supported/gs.test(res.body)) {
457
+ let fid = (res.body.split('2Fhome.php&amp;gfid=')[1] || '').split("\\")[0];
458
+ if (!fid) return res;
459
+ bypass_region_err = true;
460
+ let redirectlink = `${redirect[1]}a/preferences.php?basic_site_devices=m_basic&uri=${encodeURIComponent("https://m.facebook.com/home.php")}&gfid=${fid}`;
461
+ return utils.get(redirectlink, jar, null, globalOptions).then(utils.saveCookies(jar));
462
+ }
472
463
  return res;
473
- });
464
+ }
465
+ function Redirect(res) {
466
+ let match = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/.exec(res.body);
467
+ return match ? utils.get(match[1], jar, null, globalOptions).then(utils.saveCookies(jar)) : res;
468
+ }
469
+ mainPromise = mainPromise
470
+ .then(Redirect)
471
+ .then(CheckAndFixErr)
472
+ .then(res => {
473
+ if (!/MPageLoadClientMetrics/gs.test(res.body)) {
474
+ globalOptions.userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X)";
475
+ return utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
476
+ }
477
+ return res;
478
+ })
479
+ .then(Redirect)
480
+ .then(CheckAndFixErr)
481
+ .then(res => {
482
+ let stuff = buildAPI(globalOptions, res.body, jar, bypass_region_err);
483
+ [ctx, _defaultFuncs, api] = stuff;
484
+ return res;
485
+ });
474
486
 
475
487
  // given a pageID we log in as a page
476
488
  if (globalOptions.pageID) {
package/package.json CHANGED
@@ -1,84 +1,81 @@
1
- {
2
- "name": "@dongdev/fca-unofficial",
3
- "version": "0.0.1",
4
- "description": "A Facebook chat API that doesn't rely on XMPP. Will NOT be deprecated after April 30th 2015.",
5
- "scripts": {
6
- "test": "mocha",
7
- "lint": "eslint **.js",
8
- "prettier": "prettier utils.js src/* --write"
9
- },
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git://github.com/DongDevVN/fca-unofficial.git"
16
- },
17
- "keywords": [
18
- "facebook",
19
- "chat",
20
- "api",
21
- "fca"
22
- ],
23
- "bugs": {
24
- "url": "https://github.com/DongDevVN/fca-unofficial/issues"
25
- },
26
- "author": "Avery, David, Maude, Benjamin, UIRI, MiraiTeam",
27
- "license": "MIT",
28
- "dependencies": {
29
- "bluebird": "^2.11.0",
30
- "cheerio": "^1.0.0-rc.10",
31
- "https-proxy-agent": "^4.0.0",
32
- "mqtt": "^4.2.8",
33
- "npmlog": "^1.2.0",
34
- "request": "^2.53.0",
35
- "websocket-stream": "^5.5.0"
36
- },
37
- "engines": {
38
- "node": ">=10.x"
39
- },
40
- "devDependencies": {
41
- "eslint": "^7.5.0",
42
- "mocha": "^7.0.1",
43
- "prettier": "^1.11.1"
44
- },
45
- "eslintConfig": {
46
- "env": {
47
- "es6": true,
48
- "es2017": true,
49
- "node": true
50
- },
51
- "extends": "eslint:recommended",
52
- "parserOptions": {
53
- "sourceType": "module"
54
- },
55
- "rules": {
56
- "linebreak-style": [
57
- "error",
58
- "unix"
59
- ],
60
- "semi": [
61
- "error",
62
- "always"
63
- ],
64
- "no-unused-vars": [
65
- 1,
66
- {
67
- "argsIgnorePattern": "^_",
68
- "varsIgnorePattern": "^_"
69
- }
70
- ],
71
- "no-empty": [
72
- "error",
73
- {
74
- "allowEmptyCatch": true
75
- }
76
- ]
77
- }
78
- },
79
- "homepage": "https://github.com/DongDevVN/fca-unofficial#readme",
80
- "main": "index.js",
81
- "directories": {
82
- "test": "test"
83
- }
84
- }
1
+ {
2
+ "name": "@dongdev/fca-unofficial",
3
+ "version": "0.0.2",
4
+ "description": "A Facebook chat API that doesn't rely on XMPP. Will NOT be deprecated after April 30th 2015.",
5
+ "scripts": {
6
+ "test": "mocha",
7
+ "lint": "eslint **.js",
8
+ "prettier": "prettier utils.js src/* --write"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git://github.com/DongDevVN/fca-unofficial.git"
13
+ },
14
+ "keywords": [
15
+ "facebook",
16
+ "chat",
17
+ "api",
18
+ "fca"
19
+ ],
20
+ "bugs": {
21
+ "url": "https://github.com/DongDevVN/fca-unofficial/issues"
22
+ },
23
+ "author": "Avery, David, Maude, Benjamin, UIRI, MiraiTeam",
24
+ "license": "MIT",
25
+ "dependencies": {
26
+ "bluebird": "^2.11.0",
27
+ "cheerio": "^1.0.0-rc.10",
28
+ "https-proxy-agent": "^4.0.0",
29
+ "mqtt": "^4.2.8",
30
+ "npmlog": "^1.2.0",
31
+ "request": "^2.53.0",
32
+ "websocket-stream": "^5.5.0"
33
+ },
34
+ "engines": {
35
+ "node": ">=10.x"
36
+ },
37
+ "devDependencies": {
38
+ "eslint": "^7.5.0",
39
+ "mocha": "^7.0.1",
40
+ "prettier": "^1.11.1"
41
+ },
42
+ "eslintConfig": {
43
+ "env": {
44
+ "es6": true,
45
+ "es2017": true,
46
+ "node": true
47
+ },
48
+ "extends": "eslint:recommended",
49
+ "parserOptions": {
50
+ "sourceType": "module"
51
+ },
52
+ "rules": {
53
+ "linebreak-style": [
54
+ "error",
55
+ "unix"
56
+ ],
57
+ "semi": [
58
+ "error",
59
+ "always"
60
+ ],
61
+ "no-unused-vars": [
62
+ 1,
63
+ {
64
+ "argsIgnorePattern": "^_",
65
+ "varsIgnorePattern": "^_"
66
+ }
67
+ ],
68
+ "no-empty": [
69
+ "error",
70
+ {
71
+ "allowEmptyCatch": true
72
+ }
73
+ ]
74
+ }
75
+ },
76
+ "homepage": "https://github.com/DongDevVN/fca-unofficial#readme",
77
+ "main": "index.js",
78
+ "directories": {
79
+ "test": "test"
80
+ }
81
+ }
package/src/listenMqtt.js CHANGED
@@ -42,52 +42,57 @@ function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
42
42
  var foreground = false;
43
43
 
44
44
  var sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
45
- var username = {
46
- u: ctx.userID,
47
- s: sessionID,
48
- chat_on: chatOn,
49
- fg: foreground,
50
- d: utils.getGUID(),
51
- ct: "websocket",
52
- //App id from facebook
53
- aid: "219994525426954",
54
- mqtt_sid: "",
55
- cp: 3,
56
- ecp: 10,
57
- st: [],
58
- pm: [],
59
- dc: "",
60
- no_auto_fg: true,
61
- gas: null,
62
- pack: []
63
- };
45
+ var GUID = utils.getGUID();
46
+ const username = {
47
+ u: ctx.userID,
48
+ s: sessionID,
49
+ chat_on: chatOn,
50
+ fg: foreground,
51
+ d: GUID,
52
+ ct: 'websocket',
53
+ aid: '219994525426954',
54
+ aids: null,
55
+ mqtt_sid: '',
56
+ cp: 3,
57
+ ecp: 10,
58
+ st: [],
59
+ pm: [],
60
+ dc: '',
61
+ no_auto_fg: true,
62
+ gas: null,
63
+ pack: [],
64
+ p: null,
65
+ php_override: ""
66
+ };
64
67
  var cookies = ctx.jar.getCookies("https://www.facebook.com").join("; ");
65
68
 
66
69
  var host;
67
- if (ctx.mqttEndpoint) host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
68
- else if (ctx.region) host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}`;
69
- else host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
70
+ if (ctx.mqttEndpoint) host = `${ctx.mqttEndpoint}&sid=${sessionID}&cid=${GUID}`;
71
+ else if (ctx.region) host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}&cid=${GUID}`;
72
+ else host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}&cid=${GUID}`;
70
73
 
71
- var options = {
72
- clientId: "mqttwsclient",
74
+ const options = {
75
+ clientId: 'mqttwsclient',
73
76
  protocolId: 'MQIsdp',
74
77
  protocolVersion: 3,
75
78
  username: JSON.stringify(username),
76
79
  clean: true,
77
80
  wsOptions: {
78
- headers: {
79
- 'Cookie': cookies,
80
- 'Origin': 'https://www.facebook.com',
81
- 'User-Agent': ctx.globalOptions.userAgent,
82
- 'Referer': 'https://www.facebook.com/',
83
- 'Host': new URL(host).hostname //'edge-chat.facebook.com'
84
- },
85
- origin: 'https://www.facebook.com',
86
- protocolVersion: 13
81
+ headers: {
82
+ Cookie: cookies,
83
+ Origin: 'https://www.facebook.com',
84
+ 'User-Agent': ctx.globalOptions.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
85
+ Referer: 'https://www.facebook.com/',
86
+ Host: new URL(host).hostname,
87
+ },
88
+ origin: 'https://www.facebook.com',
89
+ protocolVersion: 13,
90
+ binaryType: 'arraybuffer',
87
91
  },
88
- keepalive: 10,
89
- reschedulePings: false
90
- };
92
+ keepalive: 60,
93
+ reschedulePings: true,
94
+ reconnectPeriod: 3,
95
+ };
91
96
 
92
97
  if (typeof ctx.globalOptions.proxy != "undefined") {
93
98
  var agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
@@ -96,7 +101,7 @@ function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
96
101
 
97
102
  ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
98
103
 
99
- global. mqttClient = ctx.mqttClient;
104
+ global.mqttClient = ctx.mqttClient;
100
105
 
101
106
  mqttClient.on('error', function (err) {
102
107
  log.error("listenMqtt", err);
@@ -197,10 +202,7 @@ function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
197
202
 
198
203
  });
199
204
 
200
- mqttClient.on('close', function () {
201
- //(function () { globalCallback("Connection closed."); })();
202
- // client.end();
203
- });
205
+ mqttClient.on('close', function () {});
204
206
  }
205
207
 
206
208
  function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
@@ -445,15 +447,22 @@ function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
445
447
  return (function () { globalCallback(null, fmtMsg); })();
446
448
  case "AdminTextMessage":
447
449
  switch (v.delta.type) {
448
- case "change_thread_theme":
449
- case "change_thread_icon":
450
- case "change_thread_quick_reaction":
451
- case "change_thread_nickname":
452
- case "change_thread_admins":
453
- case "change_thread_approval_mode":
454
- case "group_poll":
455
- case "messenger_call_log":
456
- case "participant_joined_group_call":
450
+ case 'confirm_friend_request':
451
+ case 'shared_album_delete':
452
+ case 'shared_album_addition':
453
+ case 'pin_messages_v2':
454
+ case 'unpin_messages_v2':
455
+ case "change_thread_theme":
456
+ case "change_thread_nickname":
457
+ case "change_thread_icon":
458
+ case "change_thread_quick_reaction":
459
+ case "change_thread_admins":
460
+ case "group_poll":
461
+ case "joinable_group_link_mode_change":
462
+ case "magic_words":
463
+ case "change_thread_approval_mode":
464
+ case "messenger_call_log":
465
+ case "participant_joined_group_call":
457
466
  var fmtMsg;
458
467
  try {
459
468
  fmtMsg = utils.formatDeltaEvent(v.delta);