whalibmob 5.9.2 → 5.10.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.
- package/README.md +118 -0
- package/cli.js +95 -0
- package/index.js +3 -0
- package/lib/Client.js +160 -0
- package/lib/ReachoutTimelock.js +133 -0
- package/lib/messages/MessageSender.js +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,6 +98,7 @@ npm install -g whalibmob
|
|
|
98
98
|
- [Archive / Unarchive](#archive--unarchive)
|
|
99
99
|
- [Star / Unstar a Message](#star--unstar-a-message-cli)
|
|
100
100
|
- [Sync App State (CLI)](#cli-app-state)
|
|
101
|
+
- [Account Restriction Countdown](#cli-restriction)
|
|
101
102
|
- [Disappearing Messages](#cli-disappearing-messages)
|
|
102
103
|
- [Default Disappearing Timer](#default-disappearing-timer)
|
|
103
104
|
- [Block / Unblock](#block--unblock)
|
|
@@ -169,6 +170,7 @@ npm install -g whalibmob
|
|
|
169
170
|
- [Persistent Files Written to Disk](#persistent-files-written-to-disk)
|
|
170
171
|
- [Reading the History Store](#reading-the-history-store)
|
|
171
172
|
- [tcToken — Error 463 Defense](#tctoken--error-463-defense)
|
|
173
|
+
- [When 463 Means the Account Is Restricted](#account-restriction)
|
|
172
174
|
- [What Is Automatic vs What You Need to Do](#what-is-automatic-vs-what-you-need-to-do)
|
|
173
175
|
- [Receiving Media](#receiving-media)
|
|
174
176
|
- [Sending Messages](#sending-messages)
|
|
@@ -1113,6 +1115,8 @@ connect()
|
|
|
1113
1115
|
| `app_state_mutation` | `{ collection, index, action, removed }` | An app-state change this library does not model |
|
|
1114
1116
|
| `app_state_key_missing` | `{ collection, keyId }` | App state cannot be read until your phone shares this key |
|
|
1115
1117
|
| `app_state_keys` | `{ keys }` | Your phone shared app-state sync keys; a sync starts automatically |
|
|
1118
|
+
| `account_restriction` | `{ active, remaining, remainingMs, endsAtDate, enforcementType, reason, source }` | The account was restricted or the restriction was lifted — see [When 463 Means the Account Is Restricted](#account-restriction) |
|
|
1119
|
+
| `mex_notification` | `{ opName, data }` | A server push over `w:mex` this library does not model |
|
|
1116
1120
|
|
|
1117
1121
|
`remote: true` on a chat event means the change was made on your phone or another
|
|
1118
1122
|
linked device rather than by this session. Your own calls carry `synced` instead,
|
|
@@ -1390,6 +1394,61 @@ whalibmob implements the full lifecycle to prevent this:
|
|
|
1390
1394
|
|
|
1391
1395
|
Token storage uses the **LID JID** of the contact (e.g. `112345678901234@lid`) as the key — never the phone-number JID — matching WhatsApp's internal convention.
|
|
1392
1396
|
|
|
1397
|
+
<a id="account-restriction"></a>
|
|
1398
|
+
|
|
1399
|
+
### When 463 Means the Account Is Restricted
|
|
1400
|
+
|
|
1401
|
+
A `463` has two causes that look identical on the wire, and telling them apart matters because only one of them is worth retrying.
|
|
1402
|
+
|
|
1403
|
+
- **No privacy token for that one contact.** The library issues one and re-sends. This is the common case and it resolves itself.
|
|
1404
|
+
- **The account is restricted.** WhatsApp has decided you are starting too many chats with people who never reply, and refuses *all* new conversations until the restriction expires. Existing chats keep working, which is why the account otherwise looks healthy. No retry helps — and sending more counts as further reach-outs, which makes it longer.
|
|
1405
|
+
|
|
1406
|
+
The expiry is never pushed to a client that has not asked. `fetchReachoutTimelock()` is the only way to learn it:
|
|
1407
|
+
|
|
1408
|
+
```js
|
|
1409
|
+
const r = await client.fetchReachoutTimelock()
|
|
1410
|
+
|
|
1411
|
+
if (r.active) {
|
|
1412
|
+
console.log('restricted:', r.reason)
|
|
1413
|
+
console.log('ends at: ', r.endsAtDate.toISOString())
|
|
1414
|
+
console.log('remaining: ', r.remaining) // '04:59:20'
|
|
1415
|
+
} else {
|
|
1416
|
+
console.log('not restricted')
|
|
1417
|
+
}
|
|
1418
|
+
```
|
|
1419
|
+
|
|
1420
|
+
```js
|
|
1421
|
+
{
|
|
1422
|
+
active: true,
|
|
1423
|
+
remaining: '04:59:20', // HH:MM:SS, hours not wrapped at 24
|
|
1424
|
+
remainingMs: 17960000,
|
|
1425
|
+
endsAt: 1800018000000,
|
|
1426
|
+
endsAtDate: Date,
|
|
1427
|
+
enforcementType: 'BIZ_QUALITY',
|
|
1428
|
+
reason: 'too many people you messaged blocked or reported you',
|
|
1429
|
+
expiryUnknown: false, // true when the server withheld the end time
|
|
1430
|
+
checkedAt: 1800000040000
|
|
1431
|
+
}
|
|
1432
|
+
```
|
|
1433
|
+
|
|
1434
|
+
`getReachoutTimelock()` returns the same shape from what was last learned, without asking again — the countdown is recomputed on every call, so it is what a once-a-second display reads.
|
|
1435
|
+
|
|
1436
|
+
**You rarely need to call either.** A send refused with 463 triggers a check on its own (rate-limited to once a minute), and the server pushes an update both when a restriction starts and when it is lifted:
|
|
1437
|
+
|
|
1438
|
+
```js
|
|
1439
|
+
client.on('account_restriction', (r) => {
|
|
1440
|
+
if (r.active) console.log('restricted for', r.remaining, '—', r.reason)
|
|
1441
|
+
else console.log('restriction lifted')
|
|
1442
|
+
})
|
|
1443
|
+
```
|
|
1444
|
+
|
|
1445
|
+
`r.source` is `'notification'` when the server announced it and `'query'` when we asked.
|
|
1446
|
+
|
|
1447
|
+
> [!NOTE]
|
|
1448
|
+
> A first restriction is usually around five hours. Repeats get longer. Nothing
|
|
1449
|
+
> client-side shortens it; the only thing that helps is not sending more
|
|
1450
|
+
> unanswered first messages while it is on.
|
|
1451
|
+
|
|
1393
1452
|
### What Is Automatic vs What You Need to Do
|
|
1394
1453
|
|
|
1395
1454
|
**Everything in the "Automatic" column requires zero code from you.**
|
|
@@ -1412,6 +1471,8 @@ Token storage uses the **LID JID** of the contact (e.g. `112345678901234@lid`) a
|
|
|
1412
1471
|
| Handle incoming `privacy_token` notifications | ✅ | |
|
|
1413
1472
|
| Re-issue tcToken after peer identity change | ✅ | |
|
|
1414
1473
|
| Recover from error 463 with automatic retry | ✅ | |
|
|
1474
|
+
| Check for an account restriction after a 463 | ✅ | Rate-limited to once a minute |
|
|
1475
|
+
| Track restriction start / lift pushed by the server | ✅ | Raised as `account_restriction` |
|
|
1415
1476
|
| Populate in-memory LID↔PN maps | ✅ | |
|
|
1416
1477
|
| Listen to `history_sync` event | 🔵 Optional | Only if your app needs to react to history data |
|
|
1417
1478
|
| Read `<phone>.history.json` | 🔵 Optional | Only if your app needs chat/contact data at rest |
|
|
@@ -3150,6 +3211,60 @@ Changes that arrive on their own are printed as they land:
|
|
|
3150
3211
|
If your phone has not yet shared a sync key with this session, the command says
|
|
3151
3212
|
so — leave WhatsApp open on the phone for a moment and try again.
|
|
3152
3213
|
|
|
3214
|
+
<a id="cli-restriction"></a>
|
|
3215
|
+
|
|
3216
|
+
#### Account Restriction Countdown
|
|
3217
|
+
|
|
3218
|
+
When WhatsApp restricts an account, new chats are refused with error 463 until
|
|
3219
|
+
it expires — usually about five hours the first time. `/restriction` asks the
|
|
3220
|
+
server how long is left and then counts it down, once a second, in place:
|
|
3221
|
+
|
|
3222
|
+
```sh
|
|
3223
|
+
wa> /restriction
|
|
3224
|
+
checking account restriction...
|
|
3225
|
+
──────────────────────────────────────────────────
|
|
3226
|
+
status RESTRICTED
|
|
3227
|
+
reason too many people you messaged blocked or reported you
|
|
3228
|
+
type BIZ_QUALITY
|
|
3229
|
+
ends at 2026-07-28 19:41:12 UTC
|
|
3230
|
+
remaining 04:59:22
|
|
3231
|
+
──────────────────────────────────────────────────
|
|
3232
|
+
New chats with people you have never messaged are refused with
|
|
3233
|
+
error 463 until this expires. Existing conversations keep working,
|
|
3234
|
+
and sending more only makes the restriction longer.
|
|
3235
|
+
|
|
3236
|
+
press any key to stop watching
|
|
3237
|
+
restricted — 04:59:20 remaining
|
|
3238
|
+
```
|
|
3239
|
+
|
|
3240
|
+
The last line rewrites itself every second — `04:59:20`, `04:59:19`, … — and
|
|
3241
|
+
stops on its own the moment the restriction lifts. Any key ends the watch; the
|
|
3242
|
+
restriction is unaffected either way. `/limit` is the same command.
|
|
3243
|
+
|
|
3244
|
+
For the numbers without the countdown:
|
|
3245
|
+
|
|
3246
|
+
```sh
|
|
3247
|
+
wa> /restriction --once
|
|
3248
|
+
```
|
|
3249
|
+
|
|
3250
|
+
An account that is fine says so and returns immediately:
|
|
3251
|
+
|
|
3252
|
+
```sh
|
|
3253
|
+
wa> /restriction
|
|
3254
|
+
checking account restriction...
|
|
3255
|
+
──────────────────────────────────────────────────
|
|
3256
|
+
status not restricted — you can start new chats
|
|
3257
|
+
──────────────────────────────────────────────────
|
|
3258
|
+
```
|
|
3259
|
+
|
|
3260
|
+
You do not have to run it to find out. A refused send checks by itself, and the
|
|
3261
|
+
shell prints the change as it happens:
|
|
3262
|
+
|
|
3263
|
+
```sh
|
|
3264
|
+
ACCOUNT RESTRICTED — too many people you messaged blocked or reported you, 04:59:58 remaining
|
|
3265
|
+
run /restriction to watch the countdown
|
|
3266
|
+
```
|
|
3267
|
+
|
|
3153
3268
|
#### CLI Disappearing Messages
|
|
3154
3269
|
|
|
3155
3270
|
| Duration | Seconds |
|
|
@@ -3647,6 +3762,9 @@ wa> /quit
|
|
|
3647
3762
|
| `/unstar <jid> <msgId> [me]` | Unstar a message |
|
|
3648
3763
|
| `/appstate [collection...]` | Pull pins/archives/mutes/stars from your phone |
|
|
3649
3764
|
| `/appstate --snapshot` | Re-read all app state from scratch |
|
|
3765
|
+
| `/restriction` | Account restriction status with a live countdown |
|
|
3766
|
+
| `/restriction --once` | Restriction status without the countdown |
|
|
3767
|
+
| `/limit` | Alias for `/restriction` |
|
|
3650
3768
|
| `/ephemeral <jid> <seconds>` | Set disappearing messages timer for a chat |
|
|
3651
3769
|
| `/ephemeral-default <seconds>` | Set global default ephemeral timer for new chats |
|
|
3652
3770
|
| `/block <jid>` | Block a contact |
|
package/cli.js
CHANGED
|
@@ -303,6 +303,55 @@ function participantLine(p) {
|
|
|
303
303
|
// A chat setting either reached app state — and so every other device — or it
|
|
304
304
|
// did not, and stayed here. Saying which costs one word and saves the user
|
|
305
305
|
// wondering why their phone did not follow.
|
|
306
|
+
// A live HH:MM:SS countdown, redrawn in place once a second.
|
|
307
|
+
//
|
|
308
|
+
// The restriction's end is a fixed moment, so every tick recomputes from it
|
|
309
|
+
// rather than decrementing a counter — a tick that arrives late, or a laptop
|
|
310
|
+
// that slept, then shows the truth instead of drifting further behind.
|
|
311
|
+
//
|
|
312
|
+
// Returns a promise that settles when the time runs out or the user stops it.
|
|
313
|
+
function countdown(client, initial) {
|
|
314
|
+
return new Promise((resolve) => {
|
|
315
|
+
const tty = process.stdout.isTTY;
|
|
316
|
+
let done = false;
|
|
317
|
+
|
|
318
|
+
const finish = (why) => {
|
|
319
|
+
if (done) return;
|
|
320
|
+
done = true;
|
|
321
|
+
clearInterval(timer);
|
|
322
|
+
process.stdin.removeListener('data', onKey);
|
|
323
|
+
if (tty) process.stdout.write('\n');
|
|
324
|
+
resolve(why);
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const draw = () => {
|
|
328
|
+
const s = client.getReachoutTimelock();
|
|
329
|
+
if (!s.active) {
|
|
330
|
+
if (tty) process.stdout.write('\r\x1b[2K');
|
|
331
|
+
out(' the restriction has been lifted — you can start new chats again');
|
|
332
|
+
return finish('lifted');
|
|
333
|
+
}
|
|
334
|
+
const line = ' restricted — ' + s.remaining + ' remaining' +
|
|
335
|
+
(s.expiryUnknown ? ' (server did not give an end time; re-checking)' : '');
|
|
336
|
+
if (tty) process.stdout.write('\r\x1b[2K' + line);
|
|
337
|
+
else out(line);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// Any keypress stops the countdown; the restriction is unaffected either
|
|
341
|
+
// way, so there is nothing to confirm.
|
|
342
|
+
const onKey = () => finish('stopped');
|
|
343
|
+
if (tty && process.stdin.isTTY) {
|
|
344
|
+
process.stdin.resume();
|
|
345
|
+
process.stdin.once('data', onKey);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (tty) out(' press any key to stop watching');
|
|
349
|
+
void initial;
|
|
350
|
+
draw();
|
|
351
|
+
const timer = setInterval(draw, 1000);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
306
355
|
function chatResult(label, synced) {
|
|
307
356
|
return label + (synced ? '' : ' (this session only — no app state key)');
|
|
308
357
|
}
|
|
@@ -427,6 +476,8 @@ const HELP = `
|
|
|
427
476
|
/unstar <jid> <msgId> [me] unstar a message
|
|
428
477
|
/appstate [collection...] pull pins/archives/mutes/stars from your phone
|
|
429
478
|
/appstate --snapshot re-read all of it from scratch
|
|
479
|
+
/restriction account restriction + live countdown
|
|
480
|
+
/restriction --once just the numbers, no countdown
|
|
430
481
|
/ephemeral <jid> <seconds> set disappearing timer for chat
|
|
431
482
|
/ephemeral-default <seconds> set default timer for ALL new chats
|
|
432
483
|
/block <jid> block contact
|
|
@@ -600,6 +651,20 @@ function attachEvents(client) {
|
|
|
600
651
|
_rl && (_rl.resume(), _rl.prompt(true));
|
|
601
652
|
});
|
|
602
653
|
|
|
654
|
+
// The server announces a restriction starting and being lifted, so a session
|
|
655
|
+
// that is just sitting there still finds out.
|
|
656
|
+
client.on('account_restriction', (r) => {
|
|
657
|
+
if (r.source !== 'notification') return;
|
|
658
|
+
_rl && _rl.pause();
|
|
659
|
+
if (r.active) {
|
|
660
|
+
out(' ACCOUNT RESTRICTED — ' + r.reason + ', ' + r.remaining + ' remaining');
|
|
661
|
+
out(' run /restriction to watch the countdown');
|
|
662
|
+
} else {
|
|
663
|
+
out(' account restriction lifted — you can start new chats again');
|
|
664
|
+
}
|
|
665
|
+
_rl && (_rl.resume(), _rl.prompt(true));
|
|
666
|
+
});
|
|
667
|
+
|
|
603
668
|
client.on('app_state_keys', (e) => {
|
|
604
669
|
_rl && _rl.pause();
|
|
605
670
|
out(' app state: received ' + e.keys.length + ' sync key(s) from your phone —' +
|
|
@@ -1412,6 +1477,36 @@ async function handleLine(line) {
|
|
|
1412
1477
|
out('marked read');
|
|
1413
1478
|
break;
|
|
1414
1479
|
|
|
1480
|
+
case '/restriction':
|
|
1481
|
+
case '/limit': {
|
|
1482
|
+
requireConn();
|
|
1483
|
+
const watch = !p.includes('--once');
|
|
1484
|
+
out('checking account restriction...');
|
|
1485
|
+
const st = await _client.fetchReachoutTimelock();
|
|
1486
|
+
hr();
|
|
1487
|
+
if (!st.active) {
|
|
1488
|
+
kv('status', 'not restricted — you can start new chats');
|
|
1489
|
+
hr();
|
|
1490
|
+
break;
|
|
1491
|
+
}
|
|
1492
|
+
kv('status', 'RESTRICTED');
|
|
1493
|
+
kv('reason', st.reason);
|
|
1494
|
+
kv('type', st.enforcementType);
|
|
1495
|
+
kv('ends at', st.endsAtDate ? st.endsAtDate.toISOString().replace('T', ' ').replace(/\.\d+Z$/, ' UTC') : 'unknown');
|
|
1496
|
+
kv('remaining', st.remaining);
|
|
1497
|
+
hr();
|
|
1498
|
+
out(' New chats with people you have never messaged are refused with');
|
|
1499
|
+
out(' error 463 until this expires. Existing conversations keep working,');
|
|
1500
|
+
out(' and sending more only makes the restriction longer.');
|
|
1501
|
+
out('');
|
|
1502
|
+
if (watch) {
|
|
1503
|
+
_rl && _rl.pause();
|
|
1504
|
+
await countdown(_client, st);
|
|
1505
|
+
_rl && _rl.resume();
|
|
1506
|
+
}
|
|
1507
|
+
break;
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1415
1510
|
case '/appstate': {
|
|
1416
1511
|
requireConn();
|
|
1417
1512
|
const snapshot = p.includes('--snapshot');
|
package/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const { encryptMedia, decryptMedia, uploadMedia, downloadMedia } = require('./li
|
|
|
13
13
|
const { MessageSender, makeJid, generateMessageId } = require('./lib/messages/MessageSender');
|
|
14
14
|
const { GroupParticipantResult, GroupJoinRequest } = require('./lib/GroupParticipant');
|
|
15
15
|
const { AppStateStore, COLLECTIONS: APP_STATE_COLLECTIONS } = require('./lib/appstate/AppStateStore');
|
|
16
|
+
const ReachoutTimelock = require('./lib/ReachoutTimelock');
|
|
16
17
|
const AppStateSync = require('./lib/appstate/AppStateSync');
|
|
17
18
|
const { PATCH_INTEGRITY: LT_HASH } = require('./lib/appstate/LTHash');
|
|
18
19
|
const {
|
|
@@ -110,6 +111,8 @@ module.exports = {
|
|
|
110
111
|
AppStateSync,
|
|
111
112
|
APP_STATE_COLLECTIONS,
|
|
112
113
|
LT_HASH,
|
|
114
|
+
// Account restriction
|
|
115
|
+
ReachoutTimelock,
|
|
113
116
|
// Auth utilities
|
|
114
117
|
makeCacheableSignalKeyStore,
|
|
115
118
|
addTransactionCapability,
|
package/lib/Client.js
CHANGED
|
@@ -13,6 +13,7 @@ const { getDeviceConfig } = require('./DeviceConfig');
|
|
|
13
13
|
const { createNewStore, saveStore, loadStore, toSixParts, fromSixParts } = require('./Store');
|
|
14
14
|
const { BinaryNode } = require('./BinaryNode');
|
|
15
15
|
const { AppStateStore, COLLECTIONS } = require('./appstate/AppStateStore');
|
|
16
|
+
const ReachoutTimelock = require('./ReachoutTimelock');
|
|
16
17
|
const AppStateSync = require('./appstate/AppStateSync');
|
|
17
18
|
const SyncdProto = require('./appstate/SyncdProto');
|
|
18
19
|
const AppMutations = require('./appstate/Mutations');
|
|
@@ -50,6 +51,12 @@ const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
|
|
|
50
51
|
// The one prefix group invite codes are wrapped in.
|
|
51
52
|
const INVITE_LINK_PREFIX = 'https://chat.whatsapp.com/';
|
|
52
53
|
|
|
54
|
+
// The w:mex query that reports whether this account is restricted and until
|
|
55
|
+
// when. The id is the server's handle for that stored GraphQL document; there
|
|
56
|
+
// is no way to derive it, and a wrong one is answered with an error rather than
|
|
57
|
+
// with a different query.
|
|
58
|
+
const MEX_QUERY_REACHOUT_TIMELOCK = '23983697327930364';
|
|
59
|
+
|
|
53
60
|
// The names privacy settings actually go by on the wire, and the friendly
|
|
54
61
|
// aliases this library has always accepted for them. The wire names are the
|
|
55
62
|
// right-hand column.
|
|
@@ -246,6 +253,9 @@ class WhalibmobClient extends EventEmitter {
|
|
|
246
253
|
this._tcTokenStore = null; // TcTokenStore — loaded in init()
|
|
247
254
|
this._inFlightTcTokenIssuance = new Set(); // dedupe concurrent proactive issuePrivacyTokens per JID
|
|
248
255
|
this._inFlight463Recoveries = new Set(); // dedupe concurrent 463-triggered token issuances per JID (separate from proactive)
|
|
256
|
+
// Last known account restriction, or null until one is fetched or pushed.
|
|
257
|
+
this._reachoutTimelock = null;
|
|
258
|
+
this._reachoutTimelockInFlight = null;
|
|
249
259
|
// Blue-tick every message as it arrives. Set to false to make read receipts
|
|
250
260
|
// explicit, sent only by a markRead() call.
|
|
251
261
|
this.autoRead = opts.autoRead !== false;
|
|
@@ -2596,6 +2606,10 @@ class WhalibmobClient extends EventEmitter {
|
|
|
2596
2606
|
this._processGroupUpdate(node);
|
|
2597
2607
|
}
|
|
2598
2608
|
|
|
2609
|
+
if (type === 'mex') {
|
|
2610
|
+
this._handleMexNotification(node);
|
|
2611
|
+
}
|
|
2612
|
+
|
|
2599
2613
|
if (type === 'privacy_token') {
|
|
2600
2614
|
// Incoming trusted-contact token from a conversation partner — store it
|
|
2601
2615
|
// so we can attach it on next DM send (prevents error 463).
|
|
@@ -4833,6 +4847,152 @@ class WhalibmobClient extends EventEmitter {
|
|
|
4833
4847
|
return this._sendIq(node);
|
|
4834
4848
|
}
|
|
4835
4849
|
|
|
4850
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
4851
|
+
// ACCOUNT RESTRICTION (reachout timelock)
|
|
4852
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
4853
|
+
|
|
4854
|
+
/**
|
|
4855
|
+
* Ask the server whether this account is currently restricted, and until when.
|
|
4856
|
+
*
|
|
4857
|
+
* A restriction is what error 463 means when it will not go away: WhatsApp
|
|
4858
|
+
* has decided the account is starting too many chats with people who never
|
|
4859
|
+
* reply, and refuses new ones until it expires. Existing chats keep working,
|
|
4860
|
+
* which is why the account otherwise looks fine.
|
|
4861
|
+
*
|
|
4862
|
+
* The expiry is not pushed to the client — this is the only way to learn it.
|
|
4863
|
+
*
|
|
4864
|
+
* @returns {Promise<object>} { active, remaining, remainingMs, endsAtDate,
|
|
4865
|
+
* enforcementType, reason, expiryUnknown }
|
|
4866
|
+
*/
|
|
4867
|
+
async fetchReachoutTimelock() {
|
|
4868
|
+
const payload = await this._mexQuery(MEX_QUERY_REACHOUT_TIMELOCK, {},
|
|
4869
|
+
'xwa2_fetch_account_reachout_timelock');
|
|
4870
|
+
return this._setReachoutTimelock(
|
|
4871
|
+
ReachoutTimelock.parseTimelockPayload(payload || {}), 'query');
|
|
4872
|
+
}
|
|
4873
|
+
|
|
4874
|
+
/**
|
|
4875
|
+
* What we last learned about the restriction, without asking again.
|
|
4876
|
+
*
|
|
4877
|
+
* The countdown is computed fresh on every call, so this is what a ticking
|
|
4878
|
+
* display reads once a second. Returns an inactive state when nothing has
|
|
4879
|
+
* been fetched yet — call fetchReachoutTimelock() for that.
|
|
4880
|
+
*/
|
|
4881
|
+
getReachoutTimelock() {
|
|
4882
|
+
return ReachoutTimelock.describe(this._reachoutTimelock);
|
|
4883
|
+
}
|
|
4884
|
+
|
|
4885
|
+
/**
|
|
4886
|
+
* Refresh the restriction after a send was refused, at most once a minute.
|
|
4887
|
+
*
|
|
4888
|
+
* A 463 has two causes that look identical on the wire: a contact we simply
|
|
4889
|
+
* hold no privacy token for, which the resend recovers from, and an account
|
|
4890
|
+
* restriction, which no resend will fix. Asking the server tells the two
|
|
4891
|
+
* apart, and it is the only way to learn when the restriction ends.
|
|
4892
|
+
*
|
|
4893
|
+
* Rate-limited because a burst of refused sends would otherwise fire one
|
|
4894
|
+
* query each, and a restricted account is exactly where a burst happens.
|
|
4895
|
+
*/
|
|
4896
|
+
_refreshReachoutTimelockAfter463() {
|
|
4897
|
+
if (!this._connected) return;
|
|
4898
|
+
const last = this._reachoutTimelock && this._reachoutTimelock.checkedAt;
|
|
4899
|
+
if (last && Date.now() - last < 60000) return;
|
|
4900
|
+
if (this._reachoutTimelockInFlight) return;
|
|
4901
|
+
|
|
4902
|
+
this._reachoutTimelockInFlight = this.fetchReachoutTimelock()
|
|
4903
|
+
.then(state => {
|
|
4904
|
+
if (state.active) {
|
|
4905
|
+
_whaDbg('[DBG] ACCOUNT_RESTRICTED for ' + state.remaining +
|
|
4906
|
+
' (' + state.enforcementType + ')');
|
|
4907
|
+
}
|
|
4908
|
+
})
|
|
4909
|
+
.catch(err => _whaDbg('[DBG] REACHOUT_TIMELOCK fetch failed: ' + (err && err.message)))
|
|
4910
|
+
.then(() => { this._reachoutTimelockInFlight = null; });
|
|
4911
|
+
}
|
|
4912
|
+
|
|
4913
|
+
_setReachoutTimelock(state, source) {
|
|
4914
|
+
this._reachoutTimelock = Object.assign({}, state, { checkedAt: Date.now() });
|
|
4915
|
+
const described = ReachoutTimelock.describe(this._reachoutTimelock);
|
|
4916
|
+
this.emit('account_restriction', Object.assign({ source }, described));
|
|
4917
|
+
return described;
|
|
4918
|
+
}
|
|
4919
|
+
|
|
4920
|
+
/**
|
|
4921
|
+
* One w:mex GraphQL call.
|
|
4922
|
+
*
|
|
4923
|
+
* The reply is JSON inside <result>. GraphQL reports failures in the body
|
|
4924
|
+
* with a 200-shaped envelope, so an errors array has to be checked for
|
|
4925
|
+
* separately — otherwise a refused query reads as an empty result and the
|
|
4926
|
+
* caller concludes the account is fine.
|
|
4927
|
+
*/
|
|
4928
|
+
async _mexQuery(queryId, variables, dataPath) {
|
|
4929
|
+
const resp = await this._sendIq(new BinaryNode('iq', {
|
|
4930
|
+
id: this._genMsgId(),
|
|
4931
|
+
to: 's.whatsapp.net',
|
|
4932
|
+
type: 'get',
|
|
4933
|
+
xmlns: 'w:mex'
|
|
4934
|
+
}, [
|
|
4935
|
+
new BinaryNode('query', { query_id: String(queryId) },
|
|
4936
|
+
Buffer.from(JSON.stringify({ variables: variables || {} }), 'utf8'))
|
|
4937
|
+
]));
|
|
4938
|
+
|
|
4939
|
+
if (!resp) throw new Error('mex query ' + queryId + ': no reply from server (IQ timed out)');
|
|
4940
|
+
if (resp.attrs && resp.attrs.type === 'error') {
|
|
4941
|
+
const errNode = findChild(resp, 'error');
|
|
4942
|
+
const code = errNode && errNode.attrs && errNode.attrs.code;
|
|
4943
|
+
throw new Error('mex query ' + queryId + ': server rejected the request' +
|
|
4944
|
+
(code ? ' (' + code + ')' : ''));
|
|
4945
|
+
}
|
|
4946
|
+
|
|
4947
|
+
const resultNode = findChild(resp, 'result');
|
|
4948
|
+
const buf = resultNode ? getNodeContent(resultNode) : null;
|
|
4949
|
+
if (!buf) throw new Error('mex query ' + queryId + ': reply carried no result');
|
|
4950
|
+
|
|
4951
|
+
let json;
|
|
4952
|
+
try { json = JSON.parse(buf.toString('utf8')); }
|
|
4953
|
+
catch (_) { throw new Error('mex query ' + queryId + ': reply was not JSON'); }
|
|
4954
|
+
|
|
4955
|
+
if (json.errors && json.errors.length) {
|
|
4956
|
+
throw new Error('mex query ' + queryId + ': ' +
|
|
4957
|
+
json.errors.map(e => (e && e.message) || 'unknown error').join(', '));
|
|
4958
|
+
}
|
|
4959
|
+
return dataPath ? (json.data && json.data[dataPath]) : json.data;
|
|
4960
|
+
}
|
|
4961
|
+
|
|
4962
|
+
/**
|
|
4963
|
+
* <notification type="mex"> — a GraphQL push rather than a reply.
|
|
4964
|
+
*
|
|
4965
|
+
* The restriction is the one that matters here: the server announces both
|
|
4966
|
+
* when it starts and when it is lifted, so a client that listens does not
|
|
4967
|
+
* have to poll to know it can send again.
|
|
4968
|
+
*/
|
|
4969
|
+
_handleMexNotification(node) {
|
|
4970
|
+
const update = findChild(node, 'update');
|
|
4971
|
+
if (!update) return;
|
|
4972
|
+
const opName = (update.attrs && update.attrs.op_name) || '';
|
|
4973
|
+
const buf = getNodeContent(update);
|
|
4974
|
+
if (!buf) return;
|
|
4975
|
+
|
|
4976
|
+
let json;
|
|
4977
|
+
try { json = JSON.parse(buf.toString('utf8')); }
|
|
4978
|
+
catch (_) { _whaDbg('[DBG] MEX_NOTIF ' + opName + ' — body was not JSON'); return; }
|
|
4979
|
+
if (json.errors && json.errors.length) return;
|
|
4980
|
+
const data = json.data;
|
|
4981
|
+
if (!data) return;
|
|
4982
|
+
|
|
4983
|
+
if (opName === 'NotificationUserReachoutTimelockUpdate') {
|
|
4984
|
+
const payload = data.xwa2_notify_account_reachout_timelock;
|
|
4985
|
+
if (!payload) return;
|
|
4986
|
+
const state = ReachoutTimelock.parseTimelockPayload(payload);
|
|
4987
|
+
_whaDbg('[DBG] MEX_NOTIF restriction ' + (state.active ? 'set' : 'lifted'));
|
|
4988
|
+
this._setReachoutTimelock(state, 'notification');
|
|
4989
|
+
return;
|
|
4990
|
+
}
|
|
4991
|
+
|
|
4992
|
+
// Anything else the server pushes this way, passed on rather than dropped.
|
|
4993
|
+
this.emit('mex_notification', { opName, data });
|
|
4994
|
+
}
|
|
4995
|
+
|
|
4836
4996
|
// Internal: parse newsletter response from w:mex IQ result
|
|
4837
4997
|
_parseNewsletterResponse(resp) {
|
|
4838
4998
|
try {
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// The "reachout timelock" — the temporary restriction WhatsApp puts on an
|
|
4
|
+
// account that has started too many chats with people who have never replied.
|
|
5
|
+
//
|
|
6
|
+
// While it is on, messages to a contact you have no history with are refused
|
|
7
|
+
// with error 463 and nothing you do client-side gets them through. The
|
|
8
|
+
// restriction is not permanent: the server holds an expiry, and once it passes
|
|
9
|
+
// the account goes back to normal on its own. Five hours is the usual first
|
|
10
|
+
// one; repeat offences get longer.
|
|
11
|
+
//
|
|
12
|
+
// The expiry is not on any stanza the client is sent by default — it has to be
|
|
13
|
+
// asked for, over the GraphQL-style w:mex transport.
|
|
14
|
+
|
|
15
|
+
// The reason a restriction was applied. DEFAULT is the one that means "no
|
|
16
|
+
// restriction", which is also what the server sends when it is lifting one.
|
|
17
|
+
const ENFORCEMENT_TYPES = new Set([
|
|
18
|
+
'DEFAULT',
|
|
19
|
+
'BIZ_QUALITY',
|
|
20
|
+
'WEB_COMPANION_ONLY',
|
|
21
|
+
'BIZ_COMMERCE_VIOLATION_ALCOHOL',
|
|
22
|
+
'BIZ_COMMERCE_VIOLATION_ADULT',
|
|
23
|
+
'BIZ_COMMERCE_VIOLATION_ANIMALS',
|
|
24
|
+
'BIZ_COMMERCE_VIOLATION_BODY_PARTS_FLUIDS',
|
|
25
|
+
'BIZ_COMMERCE_VIOLATION_DATING',
|
|
26
|
+
'BIZ_COMMERCE_VIOLATION_DIGITAL_SERVICES_PRODUCTS',
|
|
27
|
+
'BIZ_COMMERCE_VIOLATION_DRUGS',
|
|
28
|
+
'BIZ_COMMERCE_VIOLATION_DRUGS_ONLY_OTC',
|
|
29
|
+
'BIZ_COMMERCE_VIOLATION_GAMBLING',
|
|
30
|
+
'BIZ_COMMERCE_VIOLATION_HEALTHCARE',
|
|
31
|
+
'BIZ_COMMERCE_VIOLATION_REAL_FAKE_CURRENCY',
|
|
32
|
+
'BIZ_COMMERCE_VIOLATION_SUPPLEMENTS',
|
|
33
|
+
'BIZ_COMMERCE_VIOLATION_TOBACCO',
|
|
34
|
+
'BIZ_COMMERCE_VIOLATION_VIOLENT_CONTENT',
|
|
35
|
+
'BIZ_COMMERCE_VIOLATION_WEAPONS'
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
// Plain-language reasons for the ones a normal account actually meets.
|
|
39
|
+
const ENFORCEMENT_REASONS = {
|
|
40
|
+
DEFAULT: 'no restriction',
|
|
41
|
+
BIZ_QUALITY: 'too many people you messaged blocked or reported you',
|
|
42
|
+
WEB_COMPANION_ONLY: 'restricted to the linked-device app'
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// When the server says a restriction is on but does not say when it ends, it
|
|
46
|
+
// means "ask again shortly" rather than "forever". A minute is what WhatsApp
|
|
47
|
+
// Web assumes in the same spot.
|
|
48
|
+
const UNKNOWN_EXPIRY_MS = 60 * 1000;
|
|
49
|
+
|
|
50
|
+
function normalizeEnforcementType(value) {
|
|
51
|
+
return (typeof value === 'string' && ENFORCEMENT_TYPES.has(value)) ? value : 'DEFAULT';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Turn what the server sent into the shape the rest of the library uses.
|
|
56
|
+
*
|
|
57
|
+
* `time_enforcement_ends` is unix *seconds* as a string, and "0" is the
|
|
58
|
+
* server's way of saying there is no expiry to report — treating it as a date
|
|
59
|
+
* would put the restriction at 1970 and make it look long expired.
|
|
60
|
+
*/
|
|
61
|
+
function parseTimelockPayload(payload, opts) {
|
|
62
|
+
const p = payload || {};
|
|
63
|
+
const active = !!p.is_active;
|
|
64
|
+
|
|
65
|
+
if (!active) {
|
|
66
|
+
return { active: false, endsAt: null, enforcementType: 'DEFAULT', reason: ENFORCEMENT_REASONS.DEFAULT };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const raw = p.time_enforcement_ends;
|
|
70
|
+
const ts = (raw != null && String(raw) !== '0') ? parseInt(String(raw), 10) : NaN;
|
|
71
|
+
const endsAt = Number.isFinite(ts) && ts > 0
|
|
72
|
+
? ts * 1000
|
|
73
|
+
: ((opts && opts.now ? opts.now : Date.now()) + UNKNOWN_EXPIRY_MS);
|
|
74
|
+
|
|
75
|
+
const enforcementType = normalizeEnforcementType(p.enforcement_type);
|
|
76
|
+
return {
|
|
77
|
+
active: true,
|
|
78
|
+
endsAt,
|
|
79
|
+
enforcementType,
|
|
80
|
+
reason: ENFORCEMENT_REASONS[enforcementType] || 'account restricted',
|
|
81
|
+
// The server did not tell us when it ends, so this is a guess and the
|
|
82
|
+
// caller should ask again rather than trust the countdown.
|
|
83
|
+
expiryUnknown: !(Number.isFinite(ts) && ts > 0)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Milliseconds left, floored at zero. */
|
|
88
|
+
function remainingMs(state, now) {
|
|
89
|
+
if (!state || !state.active || !state.endsAt) return 0;
|
|
90
|
+
return Math.max(0, state.endsAt - (now == null ? Date.now() : now));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* HH:MM:SS, which is how long a restriction is worth showing to a person.
|
|
95
|
+
* Hours are not wrapped at 24 — a two-day restriction reads "48:00:00" rather
|
|
96
|
+
* than looking like a fresh one.
|
|
97
|
+
*/
|
|
98
|
+
function formatRemaining(ms) {
|
|
99
|
+
const total = Math.max(0, Math.ceil(ms / 1000));
|
|
100
|
+
const h = Math.floor(total / 3600);
|
|
101
|
+
const m = Math.floor((total % 3600) / 60);
|
|
102
|
+
const s = total % 60;
|
|
103
|
+
const pad = n => String(n).padStart(2, '0');
|
|
104
|
+
return pad(h) + ':' + pad(m) + ':' + pad(s);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A state object with the derived fields filled in, for handing to a caller. */
|
|
108
|
+
function describe(state, now) {
|
|
109
|
+
const base = state || { active: false, endsAt: null, enforcementType: 'DEFAULT' };
|
|
110
|
+
const left = remainingMs(base, now);
|
|
111
|
+
return {
|
|
112
|
+
active: !!base.active && left > 0,
|
|
113
|
+
endsAt: base.endsAt || null,
|
|
114
|
+
endsAtDate: base.endsAt ? new Date(base.endsAt) : null,
|
|
115
|
+
remainingMs: left,
|
|
116
|
+
remaining: formatRemaining(left),
|
|
117
|
+
enforcementType: base.enforcementType || 'DEFAULT',
|
|
118
|
+
reason: base.reason || ENFORCEMENT_REASONS.DEFAULT,
|
|
119
|
+
expiryUnknown: !!base.expiryUnknown,
|
|
120
|
+
checkedAt: base.checkedAt || null
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = {
|
|
125
|
+
parseTimelockPayload,
|
|
126
|
+
remainingMs,
|
|
127
|
+
formatRemaining,
|
|
128
|
+
describe,
|
|
129
|
+
normalizeEnforcementType,
|
|
130
|
+
ENFORCEMENT_TYPES,
|
|
131
|
+
ENFORCEMENT_REASONS,
|
|
132
|
+
UNKNOWN_EXPIRY_MS
|
|
133
|
+
};
|
|
@@ -1513,6 +1513,13 @@ class MessageSender {
|
|
|
1513
1513
|
// token attached. Uses a separate _inFlight463Recoveries Set so
|
|
1514
1514
|
// concurrent 463 recoveries and proactive issuances don't clash.
|
|
1515
1515
|
if (error === '463' && !_isRetry) {
|
|
1516
|
+
// A 463 is either a missing privacy token for this one contact,
|
|
1517
|
+
// which the resend below fixes, or an account-wide restriction,
|
|
1518
|
+
// which it cannot. Ask the server which it is — that answer also
|
|
1519
|
+
// carries when a restriction ends, which nothing else reports.
|
|
1520
|
+
if (typeof client._refreshReachoutTimelockAfter463 === 'function') {
|
|
1521
|
+
client._refreshReachoutTimelockAfter463();
|
|
1522
|
+
}
|
|
1516
1523
|
const ackFrom = ackAttrs.from ? String(ackAttrs.from) : '';
|
|
1517
1524
|
const tcJid = client._resolveTcTokenJid(
|
|
1518
1525
|
ackFrom || String(node.attrs && node.attrs.to || '')
|