@japofc/baileys 2.1.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/NOTICE.md +111 -0
- package/README.md +1254 -0
- package/WAProto/index.d.ts +9 -0
- package/WAProto/index.js +142276 -0
- package/engine-requirements.js +13 -0
- package/lib/Builders/A2UI.d.ts +242 -0
- package/lib/Builders/A2UI.js +308 -0
- package/lib/Builders/AIRich.d.ts +105 -0
- package/lib/Builders/AIRich.js +2286 -0
- package/lib/Builders/Button.d.ts +103 -0
- package/lib/Builders/Button.js +883 -0
- package/lib/Builders/ButtonV2.d.ts +28 -0
- package/lib/Builders/ButtonV2.js +144 -0
- package/lib/Builders/ButtonV3.d.ts +25 -0
- package/lib/Builders/ButtonV3.js +184 -0
- package/lib/Builders/Carousel.d.ts +22 -0
- package/lib/Builders/Carousel.js +111 -0
- package/lib/Builders/JapBaileys.d.ts +34 -0
- package/lib/Builders/JapBaileys.js +108 -0
- package/lib/Builders/Poll.d.ts +25 -0
- package/lib/Builders/Poll.js +128 -0
- package/lib/Builders/index.d.ts +17 -0
- package/lib/Builders/index.js +11 -0
- package/lib/Builders/shared.d.ts +67 -0
- package/lib/Builders/shared.js +652 -0
- package/lib/Defaults/index.d.ts +154 -0
- package/lib/Defaults/index.js +178 -0
- package/lib/Framework/Bot.d.ts +61 -0
- package/lib/Framework/Bot.js +248 -0
- package/lib/Framework/Context.d.ts +21 -0
- package/lib/Framework/Context.js +89 -0
- package/lib/Framework/MediaManager.d.ts +17 -0
- package/lib/Framework/MediaManager.js +151 -0
- package/lib/Framework/SessionManager.d.ts +13 -0
- package/lib/Framework/SessionManager.js +33 -0
- package/lib/Framework/StatsManager.d.ts +26 -0
- package/lib/Framework/StatsManager.js +119 -0
- package/lib/Framework/Store/SQLiteStore.d.ts +11 -0
- package/lib/Framework/Store/SQLiteStore.js +73 -0
- package/lib/Framework/index.d.ts +9 -0
- package/lib/Framework/index.js +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +430 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +276 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/chats.d.ts +136 -0
- package/lib/Socket/chats.js +1327 -0
- package/lib/Socket/communities.d.ts +562 -0
- package/lib/Socket/communities.js +439 -0
- package/lib/Socket/dugong.d.ts +117 -0
- package/lib/Socket/dugong.js +798 -0
- package/lib/Socket/groups.d.ts +285 -0
- package/lib/Socket/groups.js +354 -0
- package/lib/Socket/index.d.ts +585 -0
- package/lib/Socket/index.js +52 -0
- package/lib/Socket/messages-recv.d.ts +333 -0
- package/lib/Socket/messages-recv.js +1781 -0
- package/lib/Socket/messages-send.d.ts +353 -0
- package/lib/Socket/messages-send.js +1524 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.d.ts +286 -0
- package/lib/Socket/newsletter.js +364 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1012 -0
- package/lib/Socket/username.d.ts +82 -0
- package/lib/Socket/username.js +159 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +428 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +5 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +6 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +62 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +138 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +8 -0
- package/lib/Types/State.js +55 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/index.d.ts +68 -0
- package/lib/Types/index.js +27 -0
- package/lib/Utils/MessageBuilder.d.ts +2 -0
- package/lib/Utils/MessageBuilder.js +2 -0
- package/lib/Utils/PersistentStore.d.ts +60 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/anti-delete.d.ts +74 -0
- package/lib/Utils/anti-delete.js +221 -0
- package/lib/Utils/auth-secure.d.ts +86 -0
- package/lib/Utils/auth-secure.js +764 -0
- package/lib/Utils/auth-utils.d.ts +57 -0
- package/lib/Utils/auth-utils.js +306 -0
- package/lib/Utils/auto-reply.d.ts +47 -0
- package/lib/Utils/auto-reply.js +157 -0
- package/lib/Utils/baileys-event-stream.d.ts +8 -0
- package/lib/Utils/baileys-event-stream.js +62 -0
- package/lib/Utils/browser-utils.d.ts +8 -0
- package/lib/Utils/browser-utils.js +27 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +239 -0
- package/lib/Utils/button-helper-utils.d.ts +53 -0
- package/lib/Utils/button-helper-utils.js +312 -0
- package/lib/Utils/button-sender.d.ts +54 -0
- package/lib/Utils/button-sender.js +822 -0
- package/lib/Utils/chat-control.d.ts +159 -0
- package/lib/Utils/chat-control.js +230 -0
- package/lib/Utils/chat-history-helpers.d.ts +21 -0
- package/lib/Utils/chat-history-helpers.js +70 -0
- package/lib/Utils/chat-utils.d.ts +407 -0
- package/lib/Utils/chat-utils.js +885 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +39 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +147 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +316 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +635 -0
- package/lib/Utils/generics.d.ts +69 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history-share.d.ts +11 -0
- package/lib/Utils/history-share.js +70 -0
- package/lib/Utils/history.d.ts +90 -0
- package/lib/Utils/history.js +146 -0
- package/lib/Utils/humanizer.d.ts +12 -0
- package/lib/Utils/humanizer.js +73 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/index.d.ts +57 -0
- package/lib/Utils/index.js +56 -0
- package/lib/Utils/jid-tools.d.ts +6 -0
- package/lib/Utils/jid-tools.js +77 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +26 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/media-messages.d.ts +18 -0
- package/lib/Utils/media-messages.js +70 -0
- package/lib/Utils/media-set.d.ts +13 -0
- package/lib/Utils/media-set.js +171 -0
- package/lib/Utils/message-kind.d.ts +32 -0
- package/lib/Utils/message-kind.js +136 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +264 -0
- package/lib/Utils/message-search.d.ts +44 -0
- package/lib/Utils/message-search.js +188 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.js +868 -0
- package/lib/Utils/messages.d.ts +44 -0
- package/lib/Utils/messages.js +2326 -0
- package/lib/Utils/meta-ai.d.ts +13 -0
- package/lib/Utils/meta-ai.js +74 -0
- package/lib/Utils/mini-app.d.ts +35 -0
- package/lib/Utils/mini-app.js +213 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/past-participants.d.ts +14 -0
- package/lib/Utils/past-participants.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +25 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.d.ts +82 -0
- package/lib/Utils/process-message.js +808 -0
- package/lib/Utils/qr-render.d.ts +59 -0
- package/lib/Utils/qr-render.js +179 -0
- package/lib/Utils/qrcodegen.d.ts +79 -0
- package/lib/Utils/qrcodegen.js +848 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.d.ts +48 -0
- package/lib/Utils/rich-message-utils.js +444 -0
- package/lib/Utils/router.d.ts +37 -0
- package/lib/Utils/router.js +165 -0
- package/lib/Utils/scheduled-posts.d.ts +30 -0
- package/lib/Utils/scheduled-posts.js +110 -0
- package/lib/Utils/scheduling.d.ts +42 -0
- package/lib/Utils/scheduling.js +139 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +200 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/status.d.ts +50 -0
- package/lib/Utils/status.js +107 -0
- package/lib/Utils/stickerpack.d.ts +51 -0
- package/lib/Utils/stickerpack.js +274 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/tag.d.ts +12 -0
- package/lib/Utils/tag.js +44 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/templates.d.ts +76 -0
- package/lib/Utils/templates.js +150 -0
- package/lib/Utils/transcribe.d.ts +12 -0
- package/lib/Utils/transcribe.js +107 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
- package/lib/Utils/use-cache-manager-auth-state.js +81 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-multi-file-auth-state.js +166 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +113 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +169 -0
- package/lib/Utils/validate-connection.d.ts +42 -0
- package/lib/Utils/validate-connection.js +198 -0
- package/lib/Utils/vcard.d.ts +58 -0
- package/lib/Utils/vcard.js +104 -0
- package/lib/Utils/voice-note.d.ts +15 -0
- package/lib/Utils/voice-note.js +63 -0
- package/lib/VoIP/audio-feeder.d.ts +18 -0
- package/lib/VoIP/audio-feeder.js +160 -0
- package/lib/VoIP/call-recorder.d.ts +13 -0
- package/lib/VoIP/call-recorder.js +73 -0
- package/lib/VoIP/index.d.ts +86 -0
- package/lib/VoIP/index.js +611 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +563 -0
- package/lib/VoIP/signaling.d.ts +26 -0
- package/lib/VoIP/signaling.js +662 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +120 -0
- package/lib/VoIP/wasm-engine.js +1358 -0
- package/lib/VoIP/worker-bootstrap.d.ts +4 -0
- package/lib/VoIP/worker-bootstrap.js +1046 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1466 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.d.ts +74 -0
- package/lib/WABinary/generic-utils.js +276 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.d.ts +7 -0
- package/lib/WAUSync/Protocols/index.js +7 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +121 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +30 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/assets/wasm/loader.d.ts +5 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.d.ts +5 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +23 -0
- package/package.json +180 -0
- package/postinstall-banner.js +82 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds caching capability to a SignalKeyStore
|
|
3
|
+
* @param store the store to add caching to
|
|
4
|
+
* @param logger to log trace events
|
|
5
|
+
* @param _cache cache store to use
|
|
6
|
+
*/
|
|
7
|
+
export function makeCacheableSignalKeyStore(store: any, logger: any, _cache: any): {
|
|
8
|
+
get(type: any, ids: any): Promise<any>;
|
|
9
|
+
set(data: any): Promise<any>;
|
|
10
|
+
clear(): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export function addTransactionCapability(state: any, logger: any, { maxCommitRetries, delayBetweenTriesMs }: {
|
|
13
|
+
maxCommitRetries: any;
|
|
14
|
+
delayBetweenTriesMs: any;
|
|
15
|
+
}): {
|
|
16
|
+
get: (type: any, ids: any) => Promise<any>;
|
|
17
|
+
set: (data: any) => Promise<void>;
|
|
18
|
+
isInTransaction: () => boolean;
|
|
19
|
+
transaction: (work: any, key: any) => Promise<any>;
|
|
20
|
+
};
|
|
21
|
+
export function assertMeId(creds: any): any;
|
|
22
|
+
export function initAuthCreds(): {
|
|
23
|
+
noiseKey: {
|
|
24
|
+
private: any;
|
|
25
|
+
public: any;
|
|
26
|
+
};
|
|
27
|
+
pairingEphemeralKeyPair: {
|
|
28
|
+
private: any;
|
|
29
|
+
public: any;
|
|
30
|
+
};
|
|
31
|
+
signedIdentityKey: {
|
|
32
|
+
private: any;
|
|
33
|
+
public: any;
|
|
34
|
+
};
|
|
35
|
+
signedPreKey: {
|
|
36
|
+
keyPair: {
|
|
37
|
+
private: any;
|
|
38
|
+
public: any;
|
|
39
|
+
};
|
|
40
|
+
signature: any;
|
|
41
|
+
keyId: any;
|
|
42
|
+
};
|
|
43
|
+
registrationId: number;
|
|
44
|
+
advSecretKey: any;
|
|
45
|
+
processedHistoryMessages: never[];
|
|
46
|
+
nextPreKeyId: number;
|
|
47
|
+
firstUnuploadedPreKeyId: number;
|
|
48
|
+
accountSyncCounter: number;
|
|
49
|
+
accountSettings: {
|
|
50
|
+
unarchiveChats: boolean;
|
|
51
|
+
};
|
|
52
|
+
registered: boolean;
|
|
53
|
+
pairingCode: undefined;
|
|
54
|
+
lastPropHash: undefined;
|
|
55
|
+
routingInfo: undefined;
|
|
56
|
+
additionalData: undefined;
|
|
57
|
+
};
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import NodeCache from '@cacheable/node-cache';
|
|
2
|
+
import { Boom } from '@hapi/boom';
|
|
3
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
4
|
+
import { Mutex } from 'async-mutex';
|
|
5
|
+
import { randomBytes } from 'crypto';
|
|
6
|
+
import PQueue from 'p-queue';
|
|
7
|
+
import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
|
|
8
|
+
import { Curve, signedKeyPair } from './crypto.js';
|
|
9
|
+
import { delay, generateRegistrationId } from './generics.js';
|
|
10
|
+
import { PreKeyManager } from './pre-key-manager.js';
|
|
11
|
+
/**
|
|
12
|
+
* Adds caching capability to a SignalKeyStore
|
|
13
|
+
* @param store the store to add caching to
|
|
14
|
+
* @param logger to log trace events
|
|
15
|
+
* @param _cache cache store to use
|
|
16
|
+
*/
|
|
17
|
+
export function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
18
|
+
const cache = _cache ||
|
|
19
|
+
new NodeCache({
|
|
20
|
+
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
|
|
21
|
+
useClones: false,
|
|
22
|
+
deleteOnExpire: true
|
|
23
|
+
});
|
|
24
|
+
// Mutex for protecting cache operations
|
|
25
|
+
const cacheMutex = new Mutex();
|
|
26
|
+
function getUniqueId(type, id) {
|
|
27
|
+
return `${type}.${id}`;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
async get(type, ids) {
|
|
31
|
+
return cacheMutex.runExclusive(async () => {
|
|
32
|
+
const data = {};
|
|
33
|
+
const idsToFetch = [];
|
|
34
|
+
for (const id of ids) {
|
|
35
|
+
const item = (await cache.get(getUniqueId(type, id)));
|
|
36
|
+
if (typeof item !== 'undefined') {
|
|
37
|
+
data[id] = item;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
idsToFetch.push(id);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (idsToFetch.length) {
|
|
44
|
+
logger?.trace({ items: idsToFetch.length }, 'loading from store');
|
|
45
|
+
const fetched = await store.get(type, idsToFetch);
|
|
46
|
+
for (const id of idsToFetch) {
|
|
47
|
+
const item = fetched[id];
|
|
48
|
+
if (item) {
|
|
49
|
+
data[id] = item;
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
51
|
+
await cache.set(getUniqueId(type, id), item);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return data;
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
async set(data) {
|
|
59
|
+
return cacheMutex.runExclusive(async () => {
|
|
60
|
+
let keys = 0;
|
|
61
|
+
for (const type in data) {
|
|
62
|
+
for (const id in data[type]) {
|
|
63
|
+
await cache.set(getUniqueId(type, id), data[type][id]);
|
|
64
|
+
keys += 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
logger?.trace({ keys }, 'updated cache');
|
|
68
|
+
await store.set(data);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
async clear() {
|
|
72
|
+
await cache.flushAll();
|
|
73
|
+
await store.clear?.();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds DB-like transaction capability to the SignalKeyStore
|
|
79
|
+
* Uses AsyncLocalStorage for automatic context management
|
|
80
|
+
* @param state the key store to apply this capability to
|
|
81
|
+
* @param logger logger to log events
|
|
82
|
+
* @returns SignalKeyStore with transaction capability
|
|
83
|
+
*/
|
|
84
|
+
export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
|
|
85
|
+
const txStorage = new AsyncLocalStorage();
|
|
86
|
+
// Queues for concurrency control (keyed by signal data type - bounded set)
|
|
87
|
+
const keyQueues = new Map();
|
|
88
|
+
// Transaction mutexes with reference counting for cleanup
|
|
89
|
+
const txMutexes = new Map();
|
|
90
|
+
const txMutexRefCounts = new Map();
|
|
91
|
+
// Pre-key manager for specialized operations
|
|
92
|
+
const preKeyManager = new PreKeyManager(state, logger);
|
|
93
|
+
/**
|
|
94
|
+
* Get or create a queue for a specific key type
|
|
95
|
+
*/
|
|
96
|
+
function getQueue(key) {
|
|
97
|
+
if (!keyQueues.has(key)) {
|
|
98
|
+
keyQueues.set(key, new PQueue({ concurrency: 1 }));
|
|
99
|
+
}
|
|
100
|
+
return keyQueues.get(key);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get or create a transaction mutex
|
|
104
|
+
*/
|
|
105
|
+
function getTxMutex(key) {
|
|
106
|
+
if (!txMutexes.has(key)) {
|
|
107
|
+
txMutexes.set(key, new Mutex());
|
|
108
|
+
txMutexRefCounts.set(key, 0);
|
|
109
|
+
}
|
|
110
|
+
return txMutexes.get(key);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Acquire a reference to a transaction mutex
|
|
114
|
+
*/
|
|
115
|
+
function acquireTxMutexRef(key) {
|
|
116
|
+
const count = txMutexRefCounts.get(key) ?? 0;
|
|
117
|
+
txMutexRefCounts.set(key, count + 1);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Release a reference to a transaction mutex and cleanup if no longer needed
|
|
121
|
+
*/
|
|
122
|
+
function releaseTxMutexRef(key) {
|
|
123
|
+
const count = (txMutexRefCounts.get(key) ?? 1) - 1;
|
|
124
|
+
txMutexRefCounts.set(key, count);
|
|
125
|
+
// Cleanup if no more references and mutex is not locked
|
|
126
|
+
if (count <= 0) {
|
|
127
|
+
const mutex = txMutexes.get(key);
|
|
128
|
+
if (mutex && !mutex.isLocked()) {
|
|
129
|
+
txMutexes.delete(key);
|
|
130
|
+
txMutexRefCounts.delete(key);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check if currently in a transaction
|
|
136
|
+
*/
|
|
137
|
+
function isInTransaction() {
|
|
138
|
+
return !!txStorage.getStore();
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Commit transaction with retries
|
|
142
|
+
*/
|
|
143
|
+
async function commitWithRetry(mutations) {
|
|
144
|
+
if (Object.keys(mutations).length === 0) {
|
|
145
|
+
logger.trace('no mutations in transaction');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
logger.trace('committing transaction');
|
|
149
|
+
for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
|
|
150
|
+
try {
|
|
151
|
+
await state.set(mutations);
|
|
152
|
+
logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
const retriesLeft = maxCommitRetries - attempt - 1;
|
|
157
|
+
logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
|
|
158
|
+
if (retriesLeft === 0) {
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
await delay(delayBetweenTriesMs);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
get: async (type, ids) => {
|
|
167
|
+
const ctx = txStorage.getStore();
|
|
168
|
+
if (!ctx) {
|
|
169
|
+
// No transaction - direct read without exclusive lock for concurrency
|
|
170
|
+
return state.get(type, ids);
|
|
171
|
+
}
|
|
172
|
+
// In transaction - check cache first
|
|
173
|
+
const cached = ctx.cache[type] || {};
|
|
174
|
+
const missing = ids.filter(id => !(id in cached));
|
|
175
|
+
if (missing.length > 0) {
|
|
176
|
+
ctx.dbQueries++;
|
|
177
|
+
logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
|
|
178
|
+
const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
|
|
179
|
+
// Update cache
|
|
180
|
+
ctx.cache[type] = ctx.cache[type] || {};
|
|
181
|
+
Object.assign(ctx.cache[type], fetched);
|
|
182
|
+
}
|
|
183
|
+
// Return requested ids from cache
|
|
184
|
+
const result = {};
|
|
185
|
+
for (const id of ids) {
|
|
186
|
+
const value = ctx.cache[type]?.[id];
|
|
187
|
+
if (value !== undefined && value !== null) {
|
|
188
|
+
result[id] = value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
},
|
|
193
|
+
set: async (data) => {
|
|
194
|
+
const ctx = txStorage.getStore();
|
|
195
|
+
if (!ctx) {
|
|
196
|
+
// No transaction - direct write with queue protection
|
|
197
|
+
const types = Object.keys(data);
|
|
198
|
+
// JAP@Fix (bug 66): validateDeletions() used to run *before* this,
|
|
199
|
+
// serialized only against PreKeyManager's own internal queue —
|
|
200
|
+
// a separate PQueue instance from getQueue(type) below, both keyed
|
|
201
|
+
// 'pre-key' but never synchronized with each other. Two concurrent
|
|
202
|
+
// set() calls for pre-key data could validate against a store state
|
|
203
|
+
// that a still-pending write from the other call hadn't applied yet,
|
|
204
|
+
// letting a stale deletion go through or a just-deleted key get
|
|
205
|
+
// resurrected by data racing back in. Validation now happens inside
|
|
206
|
+
// the same per-type queue job as the write, so it's ordered against
|
|
207
|
+
// any other set() for that type instead of racing it.
|
|
208
|
+
await Promise.all(types.map(type => getQueue(type).add(async () => {
|
|
209
|
+
if (type === 'pre-key') {
|
|
210
|
+
await preKeyManager.validateDeletions(data, type);
|
|
211
|
+
}
|
|
212
|
+
const typeData = { [type]: data[type] };
|
|
213
|
+
await state.set(typeData);
|
|
214
|
+
})));
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// In transaction - update cache and mutations
|
|
218
|
+
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
|
|
219
|
+
for (const key_ in data) {
|
|
220
|
+
const key = key_;
|
|
221
|
+
// Ensure structures exist
|
|
222
|
+
ctx.cache[key] = ctx.cache[key] || {};
|
|
223
|
+
ctx.mutations[key] = ctx.mutations[key] || {};
|
|
224
|
+
// Special handling for pre-keys
|
|
225
|
+
if (key === 'pre-key') {
|
|
226
|
+
await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
// Normal key types
|
|
230
|
+
Object.assign(ctx.cache[key], data[key]);
|
|
231
|
+
Object.assign(ctx.mutations[key], data[key]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
isInTransaction,
|
|
236
|
+
transaction: async (work, key) => {
|
|
237
|
+
const existing = txStorage.getStore();
|
|
238
|
+
// Nested transaction - reuse existing context
|
|
239
|
+
if (existing) {
|
|
240
|
+
logger.trace('reusing existing transaction context');
|
|
241
|
+
return work();
|
|
242
|
+
}
|
|
243
|
+
// New transaction - acquire mutex and create context
|
|
244
|
+
const mutex = getTxMutex(key);
|
|
245
|
+
acquireTxMutexRef(key);
|
|
246
|
+
try {
|
|
247
|
+
return await mutex.runExclusive(async () => {
|
|
248
|
+
const ctx = {
|
|
249
|
+
cache: {},
|
|
250
|
+
mutations: {},
|
|
251
|
+
dbQueries: 0
|
|
252
|
+
};
|
|
253
|
+
logger.trace('entering transaction');
|
|
254
|
+
try {
|
|
255
|
+
const result = await txStorage.run(ctx, work);
|
|
256
|
+
// Commit mutations
|
|
257
|
+
await commitWithRetry(ctx.mutations);
|
|
258
|
+
logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
logger.error({ error }, 'transaction failed, rolling back');
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
releaseTxMutexRef(key);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* Returns the authenticated user's JID, or throws a Boom-401 if creds are not yet authenticated.
|
|
275
|
+
* Use this anywhere we'd otherwise reach for `creds.me!.id` to fail fast with a descriptive error.
|
|
276
|
+
*/
|
|
277
|
+
export const assertMeId = (creds) => {
|
|
278
|
+
const id = creds.me?.id;
|
|
279
|
+
if (!id) {
|
|
280
|
+
throw new Boom('Cannot proceed: socket is not authenticated yet (creds.me.id is missing)', { statusCode: 401 });
|
|
281
|
+
}
|
|
282
|
+
return id;
|
|
283
|
+
};
|
|
284
|
+
export const initAuthCreds = () => {
|
|
285
|
+
const identityKey = Curve.generateKeyPair();
|
|
286
|
+
return {
|
|
287
|
+
noiseKey: Curve.generateKeyPair(),
|
|
288
|
+
pairingEphemeralKeyPair: Curve.generateKeyPair(),
|
|
289
|
+
signedIdentityKey: identityKey,
|
|
290
|
+
signedPreKey: signedKeyPair(identityKey, 1),
|
|
291
|
+
registrationId: generateRegistrationId(),
|
|
292
|
+
advSecretKey: randomBytes(32).toString('base64'),
|
|
293
|
+
processedHistoryMessages: [],
|
|
294
|
+
nextPreKeyId: 1,
|
|
295
|
+
firstUnuploadedPreKeyId: 1,
|
|
296
|
+
accountSyncCounter: 0,
|
|
297
|
+
accountSettings: {
|
|
298
|
+
unarchiveChats: false
|
|
299
|
+
},
|
|
300
|
+
registered: false,
|
|
301
|
+
pairingCode: undefined,
|
|
302
|
+
lastPropHash: undefined,
|
|
303
|
+
routingInfo: undefined,
|
|
304
|
+
additionalData: undefined
|
|
305
|
+
};
|
|
306
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { WAMessage, AnyMessageContent } from '../Types/index.js';
|
|
2
|
+
export interface AutoReplyRule {
|
|
3
|
+
id?: string;
|
|
4
|
+
keywords?: string[];
|
|
5
|
+
pattern?: RegExp;
|
|
6
|
+
exactMatch?: string;
|
|
7
|
+
response: AnyMessageContent | ((message: WAMessage, match: RegExpMatchArray | string[]) => Promise<AnyMessageContent> | AnyMessageContent);
|
|
8
|
+
cooldown?: number;
|
|
9
|
+
priority?: number;
|
|
10
|
+
active?: boolean;
|
|
11
|
+
groupsOnly?: boolean;
|
|
12
|
+
privateOnly?: boolean;
|
|
13
|
+
allowedJids?: string[];
|
|
14
|
+
blockedJids?: string[];
|
|
15
|
+
quoted?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface AutoReplyOptions {
|
|
18
|
+
globalCooldown?: number;
|
|
19
|
+
simulateTyping?: boolean;
|
|
20
|
+
typingDuration?: number;
|
|
21
|
+
multiMatch?: boolean;
|
|
22
|
+
onReply?: (rule: AutoReplyRule, message: WAMessage, response: AnyMessageContent) => void;
|
|
23
|
+
onError?: (error: Error, rule: AutoReplyRule, message: WAMessage) => void;
|
|
24
|
+
}
|
|
25
|
+
export declare class AutoReplyHandler {
|
|
26
|
+
private rules;
|
|
27
|
+
private cooldowns;
|
|
28
|
+
private globalCooldown;
|
|
29
|
+
private sendMessage;
|
|
30
|
+
private sendPresence?;
|
|
31
|
+
private options;
|
|
32
|
+
constructor(sendMessage: (jid: string, content: AnyMessageContent, options?: any) => Promise<any>, sendPresence?: (jid: string, presence: string) => Promise<void>, options?: AutoReplyOptions);
|
|
33
|
+
addRule(rule: AutoReplyRule): AutoReplyRule & {
|
|
34
|
+
id: string;
|
|
35
|
+
};
|
|
36
|
+
removeRule(id: string): boolean;
|
|
37
|
+
getRules(): (AutoReplyRule & {
|
|
38
|
+
id: string;
|
|
39
|
+
})[];
|
|
40
|
+
getRule(id: string): (AutoReplyRule & {
|
|
41
|
+
id: string;
|
|
42
|
+
}) | undefined;
|
|
43
|
+
setRuleActive(id: string, active: boolean): boolean;
|
|
44
|
+
clearRules(): void;
|
|
45
|
+
processMessage(message: WAMessage): Promise<boolean>;
|
|
46
|
+
}
|
|
47
|
+
export declare const createAutoReply: (sendMessage: (jid: string, content: AnyMessageContent, options?: any) => Promise<any>, sendPresence?: (jid: string, presence: string) => Promise<void>, options?: AutoReplyOptions) => AutoReplyHandler;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// JAP@Add --- part of the @japofc/baileys utils (auto-reply.ts) (type-only
|
|
2
|
+
// annotations dropped; behavior unchanged).
|
|
3
|
+
export class AutoReplyHandler {
|
|
4
|
+
rules = new Map();
|
|
5
|
+
cooldowns = new Map();
|
|
6
|
+
globalCooldown = new Map();
|
|
7
|
+
sendMessage;
|
|
8
|
+
sendPresence;
|
|
9
|
+
options;
|
|
10
|
+
constructor(sendMessage, sendPresence, options = {}) {
|
|
11
|
+
this.sendMessage = sendMessage;
|
|
12
|
+
this.sendPresence = sendPresence;
|
|
13
|
+
this.options = {
|
|
14
|
+
globalCooldown: options.globalCooldown ?? 1000,
|
|
15
|
+
simulateTyping: options.simulateTyping ?? false,
|
|
16
|
+
typingDuration: options.typingDuration ?? 1000,
|
|
17
|
+
multiMatch: options.multiMatch ?? false,
|
|
18
|
+
onReply: options.onReply ?? (() => { }),
|
|
19
|
+
onError: options.onError ?? (() => { })
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
generateId() {
|
|
23
|
+
return `ar_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
24
|
+
}
|
|
25
|
+
/** Add a rule keyed by keywords, a regex pattern, or an exact-match string. */
|
|
26
|
+
addRule(rule) {
|
|
27
|
+
const fullRule = {
|
|
28
|
+
...rule,
|
|
29
|
+
id: rule.id ?? this.generateId(),
|
|
30
|
+
active: rule.active ?? true,
|
|
31
|
+
priority: rule.priority ?? 0
|
|
32
|
+
};
|
|
33
|
+
if (!fullRule.keywords && !fullRule.pattern && !fullRule.exactMatch)
|
|
34
|
+
throw new Error('Rule must have keywords, pattern, or exactMatch');
|
|
35
|
+
this.rules.set(fullRule.id, fullRule);
|
|
36
|
+
return fullRule;
|
|
37
|
+
}
|
|
38
|
+
removeRule(id) {
|
|
39
|
+
return this.rules.delete(id);
|
|
40
|
+
}
|
|
41
|
+
getRules() {
|
|
42
|
+
return Array.from(this.rules.values());
|
|
43
|
+
}
|
|
44
|
+
getRule(id) {
|
|
45
|
+
return this.rules.get(id);
|
|
46
|
+
}
|
|
47
|
+
setRuleActive(id, active) {
|
|
48
|
+
const r = this.rules.get(id);
|
|
49
|
+
if (r) {
|
|
50
|
+
r.active = active;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
clearRules() {
|
|
56
|
+
this.rules.clear();
|
|
57
|
+
}
|
|
58
|
+
checkCooldown(ruleId, jid) {
|
|
59
|
+
return Date.now() - (this.cooldowns.get(`${ruleId}:${jid}`) ?? 0) > 0;
|
|
60
|
+
}
|
|
61
|
+
checkGlobalCooldown(jid) {
|
|
62
|
+
return Date.now() - (this.globalCooldown.get(jid) ?? 0) > this.options.globalCooldown;
|
|
63
|
+
}
|
|
64
|
+
setCooldown(ruleId, jid, cooldown) {
|
|
65
|
+
this.cooldowns.set(`${ruleId}:${jid}`, Date.now() + cooldown);
|
|
66
|
+
}
|
|
67
|
+
matchRule(text, rule) {
|
|
68
|
+
if (!rule.active)
|
|
69
|
+
return null;
|
|
70
|
+
if (text.toLowerCase() === rule.exactMatch?.toLowerCase())
|
|
71
|
+
return [text];
|
|
72
|
+
if (rule.keywords?.length) {
|
|
73
|
+
const lower = text.toLowerCase();
|
|
74
|
+
for (const kw of rule.keywords)
|
|
75
|
+
if (lower.includes(kw.toLowerCase()))
|
|
76
|
+
return [kw];
|
|
77
|
+
}
|
|
78
|
+
if (rule.pattern)
|
|
79
|
+
return text.match(rule.pattern);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
isJidAllowed(jid, rule) {
|
|
83
|
+
const isGroup = jid.endsWith('@g.us');
|
|
84
|
+
if (jid.endsWith('@newsletter'))
|
|
85
|
+
return false;
|
|
86
|
+
if (rule.groupsOnly && !isGroup)
|
|
87
|
+
return false;
|
|
88
|
+
if (rule.privateOnly && isGroup)
|
|
89
|
+
return false;
|
|
90
|
+
if (rule.allowedJids?.length && !rule.allowedJids.includes(jid))
|
|
91
|
+
return false;
|
|
92
|
+
if (rule.blockedJids?.includes(jid))
|
|
93
|
+
return false;
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Feed each incoming message through here (e.g. from `messages.upsert`).
|
|
98
|
+
* Returns true if a rule matched and a reply was sent.
|
|
99
|
+
*/
|
|
100
|
+
async processMessage(message) {
|
|
101
|
+
const content = message.message;
|
|
102
|
+
if (!content)
|
|
103
|
+
return false;
|
|
104
|
+
const text = content.conversation ||
|
|
105
|
+
content.extendedTextMessage?.text ||
|
|
106
|
+
content.imageMessage?.caption ||
|
|
107
|
+
content.videoMessage?.caption ||
|
|
108
|
+
content.documentMessage?.caption ||
|
|
109
|
+
'';
|
|
110
|
+
if (!text)
|
|
111
|
+
return false;
|
|
112
|
+
const jid = message.key.remoteJid;
|
|
113
|
+
if (!jid || !this.checkGlobalCooldown(jid))
|
|
114
|
+
return false;
|
|
115
|
+
const sortedRules = Array.from(this.rules.values())
|
|
116
|
+
.filter((r) => r.active)
|
|
117
|
+
.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
118
|
+
let matched = false;
|
|
119
|
+
for (const rule of sortedRules) {
|
|
120
|
+
if (!this.isJidAllowed(jid, rule))
|
|
121
|
+
continue;
|
|
122
|
+
if (rule.cooldown && !this.checkCooldown(rule.id, jid))
|
|
123
|
+
continue;
|
|
124
|
+
const match = this.matchRule(text, rule);
|
|
125
|
+
if (!match)
|
|
126
|
+
continue;
|
|
127
|
+
try {
|
|
128
|
+
let response;
|
|
129
|
+
if (typeof rule.response === 'function')
|
|
130
|
+
response = await rule.response(message, match);
|
|
131
|
+
else
|
|
132
|
+
response = rule.response;
|
|
133
|
+
if (this.options.simulateTyping && this.sendPresence) {
|
|
134
|
+
await this.sendPresence(jid, 'composing');
|
|
135
|
+
await new Promise((r) => setTimeout(r, this.options.typingDuration));
|
|
136
|
+
await this.sendPresence(jid, 'paused');
|
|
137
|
+
}
|
|
138
|
+
await this.sendMessage(jid, response, rule.quoted ? { quoted: message } : undefined);
|
|
139
|
+
// JAP@Fix (bug 60): global cooldown used to only get stamped inside
|
|
140
|
+
// setCooldown(), which only ran when the matched rule had its own
|
|
141
|
+
// cooldown — so rules without one never fed the global anti-spam gate.
|
|
142
|
+
this.globalCooldown.set(jid, Date.now());
|
|
143
|
+
if (rule.cooldown)
|
|
144
|
+
this.setCooldown(rule.id, jid, rule.cooldown);
|
|
145
|
+
this.options.onReply(rule, message, response);
|
|
146
|
+
matched = true;
|
|
147
|
+
if (!this.options.multiMatch)
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
this.options.onError(error, rule, message);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return matched;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export const createAutoReply = (sendMessage, sendPresence, options) => new AutoReplyHandler(sendMessage, sendPresence, options);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BaileysEventEmitter } from '../Types/index.js';
|
|
2
|
+
/** Monkey-patches ev.emit to append every event as an NDJSON line to `filename`. */
|
|
3
|
+
export declare const captureEventStream: (ev: BaileysEventEmitter, filename: string) => void;
|
|
4
|
+
/** Reads an NDJSON file written by captureEventStream and replays each event on a new EventEmitter. */
|
|
5
|
+
export declare const readAndEmitEventStream: (filename: string, delayIntervalMs?: number) => {
|
|
6
|
+
ev: BaileysEventEmitter;
|
|
7
|
+
task: Promise<void>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// JAP@Utils --- durable event stream (append-only JSONL log + replay).
|
|
2
|
+
// Uses this package's own
|
|
3
|
+
// `generics.js` (delay) and `make-mutex.js` (makeMutex) — both already
|
|
4
|
+
// present, no logic changes otherwise.
|
|
5
|
+
import EventEmitter from 'events';
|
|
6
|
+
import { createReadStream } from 'fs';
|
|
7
|
+
import { writeFile } from 'fs/promises';
|
|
8
|
+
import { createInterface } from 'readline';
|
|
9
|
+
import { delay } from './generics.js';
|
|
10
|
+
import { makeMutex } from './make-mutex.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Monkey-patches `ev.emit` to append every Baileys event as a JSON line
|
|
14
|
+
* (NDJSON) to `filename`. Useful for debugging or replaying sessions.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* captureEventStream(sock.ev, './events.ndjson')
|
|
18
|
+
*/
|
|
19
|
+
export const captureEventStream = (ev, filename) => {
|
|
20
|
+
const originalEmit = ev.emit.bind(ev);
|
|
21
|
+
const writeMutex = makeMutex();
|
|
22
|
+
const patchedEmit = (event, ...rest) => {
|
|
23
|
+
const line = JSON.stringify({ timestamp: Date.now(), event, data: rest[0] }) + '\n';
|
|
24
|
+
const result = originalEmit(event, ...rest);
|
|
25
|
+
void writeMutex.mutex(async () => {
|
|
26
|
+
await writeFile(filename, line, { flag: 'a' });
|
|
27
|
+
});
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
ev.emit = patchedEmit;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reads an NDJSON file written by {@link captureEventStream} and replays
|
|
35
|
+
* each event on a new EventEmitter.
|
|
36
|
+
*
|
|
37
|
+
* @param filename Path to the NDJSON file.
|
|
38
|
+
* @param delayIntervalMs Milliseconds to wait between events (default 0).
|
|
39
|
+
* @returns `{ ev, task }` — ev is the emitter, task resolves when done.
|
|
40
|
+
*/
|
|
41
|
+
export const readAndEmitEventStream = (filename, delayIntervalMs = 0) => {
|
|
42
|
+
const ev = new EventEmitter();
|
|
43
|
+
const fireEvents = async () => {
|
|
44
|
+
const fileStream = createReadStream(filename);
|
|
45
|
+
const rl = createInterface({ input: fileStream, crlfDelay: Infinity });
|
|
46
|
+
for await (const line of rl) {
|
|
47
|
+
if (!line.trim())
|
|
48
|
+
continue;
|
|
49
|
+
try {
|
|
50
|
+
const { event, data } = JSON.parse(line);
|
|
51
|
+
ev.emit(event, data);
|
|
52
|
+
if (delayIntervalMs)
|
|
53
|
+
await delay(delayIntervalMs);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// skip malformed lines
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
fileStream.destroy();
|
|
60
|
+
};
|
|
61
|
+
return { ev, task: fireEvents() };
|
|
62
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export namespace Browsers {
|
|
2
|
+
function ubuntu(browser: any): any[];
|
|
3
|
+
function macOS(browser: any): any[];
|
|
4
|
+
function baileys(browser: any): any[];
|
|
5
|
+
function windows(browser: any): any[];
|
|
6
|
+
function appropriate(browser: any): any[];
|
|
7
|
+
}
|
|
8
|
+
export function getPlatformId(browser: any): any;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { platform, release } from 'os';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
const PLATFORM_MAP = {
|
|
4
|
+
aix: 'AIX',
|
|
5
|
+
darwin: 'Mac OS',
|
|
6
|
+
win32: 'Windows',
|
|
7
|
+
android: 'Android',
|
|
8
|
+
freebsd: 'FreeBSD',
|
|
9
|
+
openbsd: 'OpenBSD',
|
|
10
|
+
sunos: 'Solaris',
|
|
11
|
+
linux: undefined,
|
|
12
|
+
haiku: undefined,
|
|
13
|
+
cygwin: undefined,
|
|
14
|
+
netbsd: undefined
|
|
15
|
+
};
|
|
16
|
+
export const Browsers = {
|
|
17
|
+
ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
|
|
18
|
+
macOS: browser => ['Mac OS', browser, '14.4.1'],
|
|
19
|
+
baileys: browser => ['Baileys', browser, '6.5.0'],
|
|
20
|
+
windows: browser => ['Windows', browser, '10.0.22631'],
|
|
21
|
+
/** The appropriate browser based on your OS & release */
|
|
22
|
+
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
|
|
23
|
+
};
|
|
24
|
+
export const getPlatformId = (browser) => {
|
|
25
|
+
const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
|
26
|
+
return platformType ? platformType.toString() : '1'; //chrome
|
|
27
|
+
};
|