kage-baileys 3.0.6 → 3.2.3
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/Example/connection.cjs +4 -0
- package/README.MD +4726 -48
- package/WAProto/WAProto.proto +3395 -415
- package/WAProto/compat.cjs +12 -0
- package/WAProto/index.d.ts +47104 -19888
- package/WAProto/index.js +131979 -57361
- package/lib/Addon/http-error.js +43 -0
- package/lib/Addon/index.js +32 -0
- package/lib/Addon/keyed-db.js +250 -0
- package/lib/Addon/link-preview.js +82 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +110 -0
- package/lib/Defaults/constants.js +156 -0
- package/lib/Defaults/history.js +46 -0
- package/lib/Defaults/index.js +230 -0
- package/lib/Defaults/media.js +104 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +44 -0
- package/lib/Extensions/business-advanced.js +382 -0
- package/lib/Extensions/business.js +159 -0
- package/lib/Extensions/chats-advanced.js +1106 -0
- package/lib/Extensions/chats.js +349 -0
- package/lib/Extensions/communities-advanced.js +430 -0
- package/lib/Extensions/community.js +94 -0
- package/lib/Extensions/groups-advanced.js +343 -0
- package/lib/Extensions/index.js +30 -0
- package/lib/Extensions/messages-recv-advanced.js +1690 -0
- package/lib/Extensions/messages-send-advanced.js +622 -0
- package/lib/Extensions/messages-send.js +1380 -0
- package/lib/Extensions/newsletter-advanced.js +228 -0
- package/lib/Extensions/newsletter.js +72 -0
- package/lib/Extensions/socket.js +389 -0
- package/lib/Modded/message_builder.js +2084 -4
- package/lib/Signal/Group/ciphertext-message.js +54 -0
- package/lib/Signal/Group/group-session-builder.js +124 -0
- package/lib/Signal/Group/group_cipher.js +216 -0
- package/lib/Signal/Group/index.js +124 -0
- package/lib/Signal/Group/keyhelper.js +108 -0
- package/lib/Signal/Group/sender-chain-key.js +83 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +157 -0
- package/lib/Signal/Group/sender-key-message.js +166 -0
- package/lib/Signal/Group/sender-key-name.js +130 -0
- package/lib/Signal/Group/sender-key-record.js +113 -0
- package/lib/Signal/Group/sender-key-state.js +208 -0
- package/lib/Signal/Group/sender-message-key.js +82 -0
- package/lib/Signal/libsignal.js +935 -0
- package/lib/Signal/lid-mapping.js +545 -1
- package/lib/Socket/Client/index.js +60 -0
- package/lib/Socket/Client/types.js +51 -0
- package/lib/Socket/Client/websocket.js +152 -0
- package/lib/Socket/business.js +831 -0
- package/lib/Socket/chats.js +2535 -49
- package/lib/Socket/communities.js +450 -1
- package/lib/Socket/community.js +1022 -0
- package/lib/Socket/dugong.js +1 -1
- package/lib/Socket/groups.js +747 -0
- package/lib/Socket/index.js +60 -2
- package/lib/Socket/messages-recv.js +3450 -5
- package/lib/Socket/messages-send.js +3123 -0
- package/lib/Socket/mex.js +126 -1
- package/lib/Socket/newsletter.js +465 -0
- package/lib/Socket/socket.js +2217 -1
- package/lib/Store/index.js +79 -0
- package/lib/Store/make-cache-manager-store.js +186 -0
- package/lib/Store/make-in-memory-store.js +878 -0
- package/lib/Store/make-ordered-dictionary.js +187 -0
- package/lib/Store/object-repository.js +78 -0
- package/lib/Types/Auth.js +25 -0
- package/lib/Types/Bussines.js +25 -1
- package/lib/Types/Call.js +25 -0
- package/lib/Types/Chat.js +39 -0
- package/lib/Types/Contact.js +25 -0
- package/lib/Types/Events.js +25 -0
- package/lib/Types/GroupMetadata.js +25 -0
- package/lib/Types/Label.js +76 -0
- package/lib/Types/LabelAssociation.js +40 -0
- package/lib/Types/Message.js +57 -0
- package/lib/Types/Mex.js +56 -1
- package/lib/Types/MexUpdates.js +46 -0
- package/lib/Types/Newsletter.js +56 -0
- package/lib/Types/Product.js +25 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.js +25 -0
- package/lib/Types/Socket.js +25 -0
- package/lib/Types/State.js +92 -0
- package/lib/Types/USync.js +25 -0
- package/lib/Types/index.js +133 -0
- package/lib/Utils/auth-utils.js +703 -0
- package/lib/Utils/automation-tools.js +51 -16
- package/lib/Utils/baileys-event-stream.js +146 -0
- package/lib/Utils/browser-utils.js +95 -0
- package/lib/Utils/business.js +525 -0
- package/lib/Utils/chat-utils.js +1861 -0
- package/lib/Utils/companion-reg-client-utils.js +55 -1
- package/lib/Utils/crypto.js +355 -0
- package/lib/Utils/decode-wa-message.js +709 -0
- package/lib/Utils/event-buffer.js +1323 -8
- package/lib/Utils/generics.js +945 -1
- package/lib/Utils/history.js +266 -0
- package/lib/Utils/identity-change-handler.js +62 -1
- package/lib/Utils/index.js +114 -0
- package/lib/Utils/link-preview.js +179 -0
- package/lib/Utils/logger.js +42 -0
- package/lib/Utils/lt-hash.js +73 -0
- package/lib/Utils/make-mutex.js +104 -0
- package/lib/Utils/message-inspector.js +110 -0
- package/lib/Utils/message-retry-manager.js +507 -2
- package/lib/Utils/messages-media.js +1931 -1
- package/lib/Utils/messages.js +3748 -0
- package/lib/Utils/noise-handler.js +423 -0
- package/lib/Utils/offline-node-processor.js +53 -1
- package/lib/Utils/pre-key-manager.js +255 -1
- package/lib/Utils/process-message.js +1592 -0
- package/lib/Utils/reporting-utils.js +272 -1
- package/lib/Utils/rich-message-utils.js +377 -0
- package/lib/Utils/rich-messages.js +1 -1
- package/lib/Utils/signal.js +437 -0
- package/lib/Utils/stanza-ack.js +51 -1
- package/lib/Utils/status-refresh.js +37 -0
- package/lib/Utils/status-tools.js +4 -2
- package/lib/Utils/sync-action-utils.js +63 -1
- package/lib/Utils/tc-token-utils.js +185 -1
- package/lib/Utils/terminal-banner.js +31 -0
- package/lib/Utils/use-multi-file-auth-state.js +271 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +142 -0
- package/lib/Utils/validate-connection.js +497 -1
- package/lib/Utils/view-once-compat.js +13 -5
- package/lib/VoIP/audio-feeder.js +150 -0
- package/lib/VoIP/index.js +289 -0
- package/lib/VoIP/relay-transport.js +608 -0
- package/lib/VoIP/signaling.js +583 -0
- package/lib/VoIP/types.js +30 -0
- package/lib/VoIP/wasm-engine.js +1261 -2
- package/lib/VoIP/worker-bootstrap.js +1090 -2
- package/lib/WABinary/constants.js +2805 -0
- package/lib/WABinary/decode.js +649 -2
- package/lib/WABinary/encode.js +564 -2
- package/lib/WABinary/generic-utils.js +401 -0
- package/lib/WABinary/index.js +82 -0
- package/lib/WABinary/jid-utils.js +290 -0
- package/lib/WABinary/types.js +25 -0
- package/lib/WAM/BinaryInfo.js +50 -0
- package/lib/WAM/constants.js +45747 -0
- package/lib/WAM/encode.js +393 -0
- package/lib/WAM/index.js +78 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +135 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +110 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +142 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +85 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +69 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +107 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +38 -1
- package/lib/WAUSync/Protocols/index.js +83 -0
- package/lib/WAUSync/USyncQuery.js +241 -2
- package/lib/WAUSync/USyncUser.js +86 -0
- package/lib/WAUSync/index.js +78 -0
- package/lib/assets/wasm/loader.js +1875 -1
- package/lib/assets/wasm/worker-modules.js +11047 -1
- package/lib/index.d.ts +126 -0
- package/lib/index.js +53 -21
- package/lib/index.mjs +437 -0
- package/licenses/upstream-MIT.txt +22 -0
- package/maintenance/api-coverage.json +1642 -0
- package/maintenance/archivos-sha256.json +207 -0
- package/maintenance/consumer-validation.log +3 -0
- package/maintenance/dependency-audit.json +4 -4
- package/maintenance/extension-inventory.json +57 -0
- package/maintenance/protocol-update.json +1101 -0
- package/maintenance/protocol-wire-audit.json +4 -0
- package/maintenance/reference-socket-surface.json +209 -0
- package/maintenance/resumen-validacion.json +50 -0
- package/maintenance/validation.log +2725 -0
- package/maintenance/verification.json +566 -0
- package/package.json +107 -9
- package/scripts/check-imports.cjs +10 -0
- package/scripts/check-package.cjs +40 -0
- package/scripts/check-syntax.cjs +1 -1
- package/scripts/generate-protobuf.cjs +6 -1
- package/scripts/generate-public-types.cjs +9 -0
- package/scripts/kage-base64-fix.cjs +45 -0
- package/scripts/maintenance.cjs +10 -0
- package/test/advanced.test.cjs +24 -0
- package/test/base64-fix.test.cjs +49 -0
- package/test/maintenance.test.cjs +1 -1
- package/test/npm-publication.test.cjs +141 -0
- package/test/recepcion-reparada.test.cjs +98 -0
- package/test/resilience-binary.test.cjs +69 -0
- package/test/resilience-transport.test.cjs +34 -0
- package/test/types.test.ts +5 -0
- package/test/upgrade.test.cjs +104 -0
- package/tsconfig.json +20 -0
- package/WAProto/p.html +0 -1
- package/lib/KageExtra/Modded/message_builder.js +0 -2330
- package/lib/KageExtra/VoIP/audio-feeder.js +0 -136
- package/lib/KageExtra/VoIP/index.js +0 -282
- package/lib/KageExtra/VoIP/relay-transport.js +0 -559
- package/lib/KageExtra/VoIP/signaling.js +0 -603
- package/lib/KageExtra/VoIP/types.js +0 -17
- package/lib/KageExtra/VoIP/wasm-engine.js +0 -1214
- package/lib/KageExtra/VoIP/worker-bootstrap.js +0 -1042
- package/lib/KageExtra/assets/wasm/loader.js +0 -5
- package/lib/KageExtra/assets/wasm/whatsapp.wasm +0 -0
- package/lib/KageExtra/assets/wasm/worker-modules.js +0 -273
- package/lib/KageExtra/index.js +0 -3
- package/lib/KageExtra/package.json +0 -1
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -24
- package/maintenance/revision-full-check.txt +0 -1097
- package/maintenance/revision-library-tests.tap +0 -1088
- package/maintenance/revision-protobuf.txt +0 -5
- package/maintenance/revision-syntax.txt +0 -5
- package/maintenance/schema-coverage.json +0 -23
- package/maintenance/schema-merge.json +0 -30
- package/maintenance/validation-node20.txt +0 -191
- package/maintenance/validation-node22.txt +0 -237
package/lib/Utils/generics.js
CHANGED
|
@@ -450,4 +450,948 @@ function bytesToCrockford(buffer) {
|
|
|
450
450
|
return crockford.join('');
|
|
451
451
|
}
|
|
452
452
|
exports.trimUndefined = trimUndefined;
|
|
453
|
-
exports.bytesToCrockford = bytesToCrockford;
|
|
453
|
+
exports.bytesToCrockford = bytesToCrockford;
|
|
454
|
+
// Kage: compatibilidad de API.
|
|
455
|
+
const __kageModule65 = (() => {
|
|
456
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
457
|
+
"use strict"
|
|
458
|
+
|
|
459
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
460
|
+
|
|
461
|
+
const { Boom } = require("@hapi/boom")
|
|
462
|
+
const {
|
|
463
|
+
createHash,
|
|
464
|
+
randomBytes
|
|
465
|
+
} = require("crypto")
|
|
466
|
+
const {
|
|
467
|
+
jidDecode,
|
|
468
|
+
getAllBinaryNodeChildren
|
|
469
|
+
} = require("../WABinary")
|
|
470
|
+
const { sha256 } = require("./crypto")
|
|
471
|
+
const { proto } = require("../../WAProto")
|
|
472
|
+
const { version } = require("../Defaults/baileys-version.json")
|
|
473
|
+
const { DisconnectReason } = require("../Types")
|
|
474
|
+
|
|
475
|
+
const Itsuki = async () => {
|
|
476
|
+
try {
|
|
477
|
+
const response = await fetch('https://raw.githubusercontent.com/Itsukichann/database/refs/heads/main/itsuki.json', {
|
|
478
|
+
method: 'GET'
|
|
479
|
+
})
|
|
480
|
+
const data = await response.json()
|
|
481
|
+
if (Array.isArray(data)) {
|
|
482
|
+
const itsukichann = data[Math.floor(Math.random() * data.length)]
|
|
483
|
+
return itsukichann
|
|
484
|
+
} else {
|
|
485
|
+
throw new Boom('Data is not in array format.')
|
|
486
|
+
}
|
|
487
|
+
} catch (error) {
|
|
488
|
+
throw new Boom(error.message)
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const BufferJSON = {
|
|
493
|
+
replacer: (k, value) => {
|
|
494
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
495
|
+
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
|
|
496
|
+
}
|
|
497
|
+
return value
|
|
498
|
+
},
|
|
499
|
+
reviver: (_, value) => {
|
|
500
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
501
|
+
return Buffer.from(value.data, 'base64')
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
505
|
+
const keys = Object.keys(value)
|
|
506
|
+
|
|
507
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
508
|
+
const values = Object.values(value)
|
|
509
|
+
|
|
510
|
+
if (values.every(v => typeof v === 'number')) {
|
|
511
|
+
return Buffer.from(values)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return value
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || ''
|
|
521
|
+
|
|
522
|
+
const writeRandomPadMax16 = (msg) => {
|
|
523
|
+
const pad = randomBytes(1)
|
|
524
|
+
const padLength = (pad[0] & 0x0f) + 1
|
|
525
|
+
|
|
526
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const unpadRandomMax16 = (e) => {
|
|
530
|
+
const t = new Uint8Array(e)
|
|
531
|
+
if (0 === t.length) {
|
|
532
|
+
throw new Error('unpadPkcs7 given empty bytes')
|
|
533
|
+
}
|
|
534
|
+
var r = t[t.length - 1]
|
|
535
|
+
if (r > t.length) {
|
|
536
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
|
|
537
|
+
}
|
|
538
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const encodeWAMessage = (message) => {
|
|
542
|
+
return writeRandomPadMax16(proto.Message.encode(message).finish())
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const encodeNewsletterMessage = (message) => {
|
|
546
|
+
return proto.Message.encode(message).finish()
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const generateRegistrationId = () => {
|
|
550
|
+
return Uint16Array.from(randomBytes(2))[0] & 16383
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const encodeBigEndian = (e, t = 4) => {
|
|
554
|
+
let r = e
|
|
555
|
+
const a = new Uint8Array(t)
|
|
556
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
557
|
+
a[i] = 255 & r
|
|
558
|
+
r >>>= 8
|
|
559
|
+
}
|
|
560
|
+
return a
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const toNumber = (t) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0)
|
|
564
|
+
|
|
565
|
+
/** unix timestamp of a date in seconds */
|
|
566
|
+
const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000)
|
|
567
|
+
|
|
568
|
+
const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
569
|
+
let timeout
|
|
570
|
+
return {
|
|
571
|
+
start: (newIntervalMs, newTask) => {
|
|
572
|
+
task = newTask || task
|
|
573
|
+
intervalMs = newIntervalMs || intervalMs
|
|
574
|
+
timeout && clearTimeout(timeout)
|
|
575
|
+
timeout = setTimeout(() => task?.(), intervalMs)
|
|
576
|
+
},
|
|
577
|
+
cancel: () => {
|
|
578
|
+
timeout && clearTimeout(timeout)
|
|
579
|
+
timeout = undefined
|
|
580
|
+
},
|
|
581
|
+
setTask: (newTask) => task = newTask,
|
|
582
|
+
setInterval: (newInterval) => intervalMs = newInterval
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const delay = (ms) => delayCancellable(ms).delay
|
|
587
|
+
|
|
588
|
+
const delayCancellable = (ms) => {
|
|
589
|
+
const stack = new Error().stack
|
|
590
|
+
let timeout
|
|
591
|
+
let reject
|
|
592
|
+
const delay = new Promise((resolve, _reject) => {
|
|
593
|
+
timeout = setTimeout(resolve, ms)
|
|
594
|
+
reject = _reject
|
|
595
|
+
})
|
|
596
|
+
const cancel = () => {
|
|
597
|
+
clearTimeout(timeout)
|
|
598
|
+
reject(new Boom('Cancelled', {
|
|
599
|
+
statusCode: 500,
|
|
600
|
+
data: {
|
|
601
|
+
stack
|
|
602
|
+
}
|
|
603
|
+
}))
|
|
604
|
+
}
|
|
605
|
+
return { delay, cancel }
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
async function promiseTimeout(ms, promise) {
|
|
609
|
+
if (!ms) {
|
|
610
|
+
return new Promise(promise)
|
|
611
|
+
}
|
|
612
|
+
const stack = new Error().stack
|
|
613
|
+
// Create a promise that rejects in <ms> milliseconds
|
|
614
|
+
const { delay, cancel } = delayCancellable(ms)
|
|
615
|
+
const p = new Promise((resolve, reject) => {
|
|
616
|
+
delay.then(() => reject(new Boom('Timed Out', {
|
|
617
|
+
statusCode: DisconnectReason.timedOut,
|
|
618
|
+
data: {
|
|
619
|
+
stack
|
|
620
|
+
}
|
|
621
|
+
}))).catch(err => reject(err))
|
|
622
|
+
promise(resolve, reject)
|
|
623
|
+
}).finally(cancel)
|
|
624
|
+
return p
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const generateMessageID = (userId) => {
|
|
628
|
+
const data = Buffer.alloc(8 + 20 + 16)
|
|
629
|
+
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))
|
|
630
|
+
if (userId) {
|
|
631
|
+
const id = jidDecode(userId)
|
|
632
|
+
if (id?.user) {
|
|
633
|
+
data.write(id.user, 8)
|
|
634
|
+
data.write('@c.us', 8 + id.user.length)
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const random = randomBytes(20)
|
|
638
|
+
random.copy(data, 28)
|
|
639
|
+
const sha = asciiDecode([ 83, 85, 75, 73 ])
|
|
640
|
+
const hash = createHash('sha256').update(data).digest()
|
|
641
|
+
return sha + hash.toString('hex').toUpperCase().substring(0, 16)
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// code is inspired by whatsmeow
|
|
645
|
+
const generateParticipantHashV2 = (participants) => {
|
|
646
|
+
participants.sort()
|
|
647
|
+
const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64')
|
|
648
|
+
return '2:' + sha256Hash.slice(0, 6)
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function bindWaitForEvent(ev, event) {
|
|
652
|
+
return async (check, timeoutMs) => {
|
|
653
|
+
let listener
|
|
654
|
+
let closeListener
|
|
655
|
+
await (promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
656
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
657
|
+
if (connection === 'close') {
|
|
658
|
+
reject((lastDisconnect?.error)
|
|
659
|
+
|| new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }))
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
ev.on('connection.update', closeListener)
|
|
663
|
+
listener = async (update) => {
|
|
664
|
+
if (await check(update)) {
|
|
665
|
+
resolve()
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
ev.on(event, listener)
|
|
669
|
+
}).finally(() => {
|
|
670
|
+
ev.off(event, listener)
|
|
671
|
+
ev.off('connection.update', closeListener)
|
|
672
|
+
}))
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const bindWaitForConnectionUpdate = (ev) => {
|
|
677
|
+
return bindWaitForEvent(ev, 'connection.update')
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
const printQRIfNecessaryListener = (ev, logger) => {
|
|
681
|
+
ev.on('connection.update', async ({ qr }) => {
|
|
682
|
+
if (qr) {
|
|
683
|
+
const QR = await Promise.resolve().then(() => __importStar(require('qrcode-terminal'))).then(m => m.default || m)
|
|
684
|
+
.catch(() => {
|
|
685
|
+
logger.error('QR code terminal not added as dependency')
|
|
686
|
+
})
|
|
687
|
+
QR?.generate(qr, { small: true })
|
|
688
|
+
}
|
|
689
|
+
})
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* utility that fetches latest baileys version from the master branch.
|
|
694
|
+
* Use to ensure your WA connection is always on the latest version
|
|
695
|
+
*/
|
|
696
|
+
const fetchLatestBaileysVersion = async (options = {}) => {
|
|
697
|
+
const URL = 'https://raw.githubusercontent.com/Itsukichann/Baileys/refs/heads/master/lib/Defaults/baileys-version.json'
|
|
698
|
+
try {
|
|
699
|
+
const result = await fetch(URL, {
|
|
700
|
+
...options,
|
|
701
|
+
method: 'GET'
|
|
702
|
+
})
|
|
703
|
+
const json = await result.json()
|
|
704
|
+
return {
|
|
705
|
+
version: json.version,
|
|
706
|
+
isLatest: true
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
catch (error) {
|
|
710
|
+
return {
|
|
711
|
+
version: version,
|
|
712
|
+
isLatest: false,
|
|
713
|
+
error
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* A utility that fetches the latest web version of whatsapp.
|
|
720
|
+
* Use to ensure your WA connection is always on the latest version
|
|
721
|
+
*/
|
|
722
|
+
const fetchLatestWaWebVersion = async (options = {}) => {
|
|
723
|
+
try {
|
|
724
|
+
// Absolute minimal headers required to bypass anti-bot detection
|
|
725
|
+
const defaultHeaders = {
|
|
726
|
+
'sec-fetch-site': 'none',
|
|
727
|
+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
const headers = { ...defaultHeaders, ...options.headers }
|
|
731
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
|
732
|
+
...options,
|
|
733
|
+
method: 'GET',
|
|
734
|
+
headers
|
|
735
|
+
})
|
|
736
|
+
|
|
737
|
+
if (!response.ok) {
|
|
738
|
+
throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const data = await response.text()
|
|
742
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
|
743
|
+
const match = data.match(regex)
|
|
744
|
+
|
|
745
|
+
if (!match?.[1]) {
|
|
746
|
+
return {
|
|
747
|
+
version: version,
|
|
748
|
+
isLatest: false,
|
|
749
|
+
error: {
|
|
750
|
+
message: 'Could not find client revision in the fetched content'
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
const clientRevision = match[1]
|
|
756
|
+
return {
|
|
757
|
+
version: [2, 3000, +clientRevision],
|
|
758
|
+
isLatest: true
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
catch (error) {
|
|
762
|
+
return {
|
|
763
|
+
version: version,
|
|
764
|
+
isLatest: false,
|
|
765
|
+
error
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/** unique message tag prefix for MD clients */
|
|
771
|
+
const generateMdTagPrefix = () => {
|
|
772
|
+
const bytes = randomBytes(4)
|
|
773
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
const STATUS_MAP = {
|
|
777
|
+
'sender': proto.WebMessageInfo.Status.SERVER_ACK,
|
|
778
|
+
'played': proto.WebMessageInfo.Status.PLAYED,
|
|
779
|
+
'read': proto.WebMessageInfo.Status.READ,
|
|
780
|
+
'read-self': proto.WebMessageInfo.Status.READ
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* Given a type of receipt, returns what the new status of the message should be
|
|
785
|
+
* @param type type from receipt
|
|
786
|
+
*/
|
|
787
|
+
const getStatusFromReceiptType = (type) => {
|
|
788
|
+
const status = STATUS_MAP[type]
|
|
789
|
+
if (typeof type === 'undefined') {
|
|
790
|
+
return proto.WebMessageInfo.Status.DELIVERY_ACK
|
|
791
|
+
}
|
|
792
|
+
return status
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
const CODE_MAP = {
|
|
796
|
+
conflict: DisconnectReason.connectionReplaced
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
801
|
+
* @param reason the string reason given, eg. "conflict"
|
|
802
|
+
*/
|
|
803
|
+
const getErrorCodeFromStreamError = (node) => {
|
|
804
|
+
const [reasonNode] = getAllBinaryNodeChildren(node)
|
|
805
|
+
let reason = reasonNode?.tag || 'unknown'
|
|
806
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
|
|
807
|
+
if (statusCode === DisconnectReason.restartRequired) {
|
|
808
|
+
reason = 'restart required'
|
|
809
|
+
}
|
|
810
|
+
return {
|
|
811
|
+
reason,
|
|
812
|
+
statusCode
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
817
|
+
let status
|
|
818
|
+
switch (tag) {
|
|
819
|
+
case 'offer':
|
|
820
|
+
case 'offer_notice':
|
|
821
|
+
status = 'offer'
|
|
822
|
+
break
|
|
823
|
+
case 'terminate':
|
|
824
|
+
if (attrs.reason === 'timeout') {
|
|
825
|
+
status = 'timeout'
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
//fired when accepted/rejected/timeout/caller hangs up
|
|
829
|
+
status = 'terminate'
|
|
830
|
+
}
|
|
831
|
+
break
|
|
832
|
+
case 'reject':
|
|
833
|
+
status = 'reject'
|
|
834
|
+
break
|
|
835
|
+
case 'accept':
|
|
836
|
+
status = 'accept'
|
|
837
|
+
break
|
|
838
|
+
default:
|
|
839
|
+
status = 'ringing'
|
|
840
|
+
break
|
|
841
|
+
}
|
|
842
|
+
return status
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
|
|
846
|
+
|
|
847
|
+
const getCodeFromWSError = (error) => {
|
|
848
|
+
let statusCode = 500
|
|
849
|
+
|
|
850
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
851
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
|
|
852
|
+
|
|
853
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
854
|
+
statusCode = code
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
else if (
|
|
858
|
+
error?.code?.startsWith('E') ||
|
|
859
|
+
error?.message?.includes('timed out')) {
|
|
860
|
+
// handle ETIMEOUT, ENOTFOUND etc
|
|
861
|
+
statusCode = 408
|
|
862
|
+
}
|
|
863
|
+
return statusCode
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Is the given platform WA business
|
|
868
|
+
* @param platform AuthenticationCreds.platform
|
|
869
|
+
*/
|
|
870
|
+
const isWABusinessPlatform = (platform) => {
|
|
871
|
+
return platform === 'smbi' || platform === 'smba'
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
875
|
+
function trimUndefined(obj) {
|
|
876
|
+
for (const key in obj) {
|
|
877
|
+
if (typeof obj[key] === 'undefined') {
|
|
878
|
+
delete obj[key]
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return obj
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function bytesToCrockford(buffer) {
|
|
885
|
+
let value = 0
|
|
886
|
+
let bitCount = 0
|
|
887
|
+
const crockford = []
|
|
888
|
+
for (const element of buffer) {
|
|
889
|
+
value = (value << 8) | (element & 0xff)
|
|
890
|
+
bitCount += 8
|
|
891
|
+
while (bitCount >= 5) {
|
|
892
|
+
crockford.push('123456789ABCDEFGHJKLMNPQRSTVWXYZ'.charAt((value >>> (bitCount - 5)) & 31))
|
|
893
|
+
bitCount -= 5
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (bitCount > 0) {
|
|
897
|
+
crockford.push('123456789ABCDEFGHJKLMNPQRSTVWXYZ'.charAt((value << (5 - bitCount)) & 31))
|
|
898
|
+
}
|
|
899
|
+
return crockford.join('')
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const toUnicodeEscape = (text) => {
|
|
903
|
+
return text.split("").map(char => "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0")).join("")
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
const fromUnicodeEscape = (escapedText) => {
|
|
907
|
+
return escapedText.replace(/\\u[\dA-Fa-f]{4}/g, match => String.fromCharCode(parseInt(match.slice(2), 16)))
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
const asciiEncode = (text) => {
|
|
911
|
+
var encoded = text.split("").map(c => c.charCodeAt(0))
|
|
912
|
+
return encoded
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
const asciiDecode = (...codes) => {
|
|
916
|
+
var codeArray = Array.isArray(codes[0]) ? codes[0] : codes
|
|
917
|
+
return codeArray.map(c => String.fromCharCode(c)).join("")
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
module.exports = {
|
|
921
|
+
Itsuki,
|
|
922
|
+
BufferJSON,
|
|
923
|
+
getKeyAuthor,
|
|
924
|
+
writeRandomPadMax16,
|
|
925
|
+
unpadRandomMax16,
|
|
926
|
+
encodeWAMessage,
|
|
927
|
+
encodeNewsletterMessage,
|
|
928
|
+
generateRegistrationId,
|
|
929
|
+
encodeBigEndian,
|
|
930
|
+
toNumber,
|
|
931
|
+
unixTimestampSeconds,
|
|
932
|
+
debouncedTimeout,
|
|
933
|
+
delay,
|
|
934
|
+
delayCancellable,
|
|
935
|
+
promiseTimeout,
|
|
936
|
+
generateMessageID,
|
|
937
|
+
generateParticipantHashV2,
|
|
938
|
+
bindWaitForEvent,
|
|
939
|
+
bindWaitForConnectionUpdate,
|
|
940
|
+
printQRIfNecessaryListener,
|
|
941
|
+
fetchLatestBaileysVersion,
|
|
942
|
+
fetchLatestWaWebVersion,
|
|
943
|
+
generateMdTagPrefix,
|
|
944
|
+
getStatusFromReceiptType,
|
|
945
|
+
getErrorCodeFromStreamError,
|
|
946
|
+
getCallStatusFromNode,
|
|
947
|
+
getCodeFromWSError,
|
|
948
|
+
isWABusinessPlatform,
|
|
949
|
+
trimUndefined,
|
|
950
|
+
bytesToCrockford,
|
|
951
|
+
toUnicodeEscape,
|
|
952
|
+
fromUnicodeEscape,
|
|
953
|
+
asciiEncode,
|
|
954
|
+
asciiDecode
|
|
955
|
+
}
|
|
956
|
+
return module.exports;
|
|
957
|
+
})();
|
|
958
|
+
for (const key of Object.keys(__kageModule65)) {
|
|
959
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule65[key]});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
// Kage: funciones avanzadas.
|
|
963
|
+
const __kageModule169 = (() => {
|
|
964
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
965
|
+
"use strict";
|
|
966
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
967
|
+
exports.isWABusinessPlatform = exports.getCodeFromWSError = exports.getCallStatusFromNode = exports.getErrorCodeFromStreamError = exports.getStatusFromReceiptType = exports.generateMdTagPrefix = exports.fetchLatestWaWebVersion = exports.fetchLatestBaileysVersion = exports.bindWaitForConnectionUpdate = exports.generateMessageID = exports.generateMessageIDV2 = exports.delayCancellable = exports.delay = exports.debouncedTimeout = exports.unixTimestampSeconds = exports.toNumber = exports.encodeBigEndian = exports.generateRegistrationId = exports.encodeWAMessage = exports.generateParticipantHashV2 = exports.unpadRandomMax16 = exports.writeRandomPadMax16 = exports.isStringNullOrEmpty = exports.getKeyAuthor = exports.BufferJSON = void 0;
|
|
968
|
+
exports.promiseTimeout = promiseTimeout;
|
|
969
|
+
exports.bindWaitForEvent = bindWaitForEvent;
|
|
970
|
+
exports.trimUndefined = trimUndefined;
|
|
971
|
+
exports.bytesToCrockford = bytesToCrockford;
|
|
972
|
+
exports.encodeNewsletterMessage = encodeNewsletterMessage;
|
|
973
|
+
const node_crypto_1 = require("node:crypto");
|
|
974
|
+
const index_js_1 = require("../../WAProto/index.js");
|
|
975
|
+
const index_js_2 = require("../Addon/index.js");
|
|
976
|
+
const baileysVersion = [2, 3000, 1051417111];
|
|
977
|
+
const index_js_3 = require("../Types/index.js");
|
|
978
|
+
const index_js_4 = require("../WABinary/index.js");
|
|
979
|
+
const crypto_js_1 = require("./crypto.js");
|
|
980
|
+
exports.BufferJSON = {
|
|
981
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
982
|
+
replacer: (k, value) => {
|
|
983
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
984
|
+
return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') };
|
|
985
|
+
}
|
|
986
|
+
return value;
|
|
987
|
+
},
|
|
988
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
989
|
+
reviver: (_, value) => {
|
|
990
|
+
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
991
|
+
return Buffer.from(value.data, 'base64');
|
|
992
|
+
}
|
|
993
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
994
|
+
const keys = Object.keys(value);
|
|
995
|
+
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
|
|
996
|
+
const values = Object.values(value);
|
|
997
|
+
if (values.every(v => typeof v === 'number')) {
|
|
998
|
+
return Buffer.from(values);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
return value;
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
|
|
1006
|
+
exports.getKeyAuthor = getKeyAuthor;
|
|
1007
|
+
const isStringNullOrEmpty = (value) =>
|
|
1008
|
+
// eslint-disable-next-line eqeqeq
|
|
1009
|
+
value == null || value === '';
|
|
1010
|
+
exports.isStringNullOrEmpty = isStringNullOrEmpty;
|
|
1011
|
+
const writeRandomPadMax16 = (msg) => {
|
|
1012
|
+
const pad = (0, node_crypto_1.randomBytes)(1);
|
|
1013
|
+
const padLength = (pad[0] & 0x0f) + 1;
|
|
1014
|
+
return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
|
|
1015
|
+
};
|
|
1016
|
+
exports.writeRandomPadMax16 = writeRandomPadMax16;
|
|
1017
|
+
const unpadRandomMax16 = (e) => {
|
|
1018
|
+
const t = new Uint8Array(e);
|
|
1019
|
+
if (0 === t.length) {
|
|
1020
|
+
throw new Error('unpadPkcs7 given empty bytes');
|
|
1021
|
+
}
|
|
1022
|
+
var r = t[t.length - 1];
|
|
1023
|
+
if (r > t.length) {
|
|
1024
|
+
throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
|
|
1025
|
+
}
|
|
1026
|
+
return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
|
|
1027
|
+
};
|
|
1028
|
+
exports.unpadRandomMax16 = unpadRandomMax16;
|
|
1029
|
+
// code is inspired by whatsmeow
|
|
1030
|
+
const generateParticipantHashV2 = (participants) => {
|
|
1031
|
+
participants.sort();
|
|
1032
|
+
const sha256Hash = (0, crypto_js_1.sha256)(Buffer.from(participants.join(''))).toString('base64');
|
|
1033
|
+
return '2:' + sha256Hash.slice(0, 6);
|
|
1034
|
+
};
|
|
1035
|
+
exports.generateParticipantHashV2 = generateParticipantHashV2;
|
|
1036
|
+
const encodeWAMessage = (message) => (0, exports.writeRandomPadMax16)(index_js_1.proto.Message.encode(message).finish());
|
|
1037
|
+
exports.encodeWAMessage = encodeWAMessage;
|
|
1038
|
+
const generateRegistrationId = () => {
|
|
1039
|
+
return Uint16Array.from((0, node_crypto_1.randomBytes)(2))[0] & 16383;
|
|
1040
|
+
};
|
|
1041
|
+
exports.generateRegistrationId = generateRegistrationId;
|
|
1042
|
+
const encodeBigEndian = (e, t = 4) => {
|
|
1043
|
+
let r = e;
|
|
1044
|
+
const a = new Uint8Array(t);
|
|
1045
|
+
for (let i = t - 1; i >= 0; i--) {
|
|
1046
|
+
a[i] = 255 & r;
|
|
1047
|
+
r >>>= 8;
|
|
1048
|
+
}
|
|
1049
|
+
return a;
|
|
1050
|
+
};
|
|
1051
|
+
exports.encodeBigEndian = encodeBigEndian;
|
|
1052
|
+
const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0;
|
|
1053
|
+
exports.toNumber = toNumber;
|
|
1054
|
+
/** unix timestamp of a date in seconds */
|
|
1055
|
+
const unixTimestampSeconds = (date = new Date()) => date.getTime() / 1000 | 0;
|
|
1056
|
+
exports.unixTimestampSeconds = unixTimestampSeconds;
|
|
1057
|
+
const debouncedTimeout = (intervalMs = 1000, task) => {
|
|
1058
|
+
let timeout;
|
|
1059
|
+
return {
|
|
1060
|
+
start: (newIntervalMs, newTask) => {
|
|
1061
|
+
task = newTask || task;
|
|
1062
|
+
intervalMs = newIntervalMs || intervalMs;
|
|
1063
|
+
timeout && clearTimeout(timeout);
|
|
1064
|
+
timeout = setTimeout(() => task?.(), intervalMs);
|
|
1065
|
+
},
|
|
1066
|
+
cancel: () => {
|
|
1067
|
+
timeout && clearTimeout(timeout);
|
|
1068
|
+
timeout = undefined;
|
|
1069
|
+
},
|
|
1070
|
+
setTask: (newTask) => (task = newTask),
|
|
1071
|
+
setInterval: (newInterval) => (intervalMs = newInterval)
|
|
1072
|
+
};
|
|
1073
|
+
};
|
|
1074
|
+
exports.debouncedTimeout = debouncedTimeout;
|
|
1075
|
+
const delay = (ms) => (0, exports.delayCancellable)(ms).delay;
|
|
1076
|
+
exports.delay = delay;
|
|
1077
|
+
const delayCancellable = (ms) => {
|
|
1078
|
+
const stack = new Error().stack;
|
|
1079
|
+
let timeout;
|
|
1080
|
+
let reject;
|
|
1081
|
+
const delay = new Promise((resolve, _reject) => {
|
|
1082
|
+
timeout = setTimeout(resolve, ms);
|
|
1083
|
+
reject = _reject;
|
|
1084
|
+
});
|
|
1085
|
+
const cancel = () => {
|
|
1086
|
+
clearTimeout(timeout);
|
|
1087
|
+
reject(new index_js_2.HttpError('Cancelled', {
|
|
1088
|
+
statusCode: 500,
|
|
1089
|
+
data: {
|
|
1090
|
+
stack
|
|
1091
|
+
}
|
|
1092
|
+
}));
|
|
1093
|
+
};
|
|
1094
|
+
return { delay, cancel };
|
|
1095
|
+
};
|
|
1096
|
+
exports.delayCancellable = delayCancellable;
|
|
1097
|
+
async function promiseTimeout(ms, promise) {
|
|
1098
|
+
if (!ms) {
|
|
1099
|
+
return new Promise(promise);
|
|
1100
|
+
}
|
|
1101
|
+
const stack = new Error().stack;
|
|
1102
|
+
// Create a promise that rejects in <ms> milliseconds
|
|
1103
|
+
const { delay, cancel } = (0, exports.delayCancellable)(ms);
|
|
1104
|
+
const p = new Promise((resolve, reject) => {
|
|
1105
|
+
delay
|
|
1106
|
+
.then(() => reject(new index_js_2.HttpError('Timed Out', {
|
|
1107
|
+
statusCode: index_js_3.DisconnectReason.timedOut,
|
|
1108
|
+
data: {
|
|
1109
|
+
stack
|
|
1110
|
+
}
|
|
1111
|
+
})))
|
|
1112
|
+
.catch(err => reject(err));
|
|
1113
|
+
promise(resolve, reject);
|
|
1114
|
+
}).finally(cancel);
|
|
1115
|
+
return p;
|
|
1116
|
+
}
|
|
1117
|
+
// inspired from whatsmeow code
|
|
1118
|
+
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
|
|
1119
|
+
const generateMessageIDV2 = (userId) => {
|
|
1120
|
+
const data = Buffer.allocUnsafe(44);
|
|
1121
|
+
data.writeBigUInt64BE(BigInt(Date.now() / 1000 | 0), 0);
|
|
1122
|
+
if (userId) {
|
|
1123
|
+
const userStr = userId.split('@')[0].split(':')[0];
|
|
1124
|
+
if (userStr) {
|
|
1125
|
+
const len = data.write(userStr, 8);
|
|
1126
|
+
data.write('@c.us', 8 + len);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
(0, node_crypto_1.randomFillSync)(data, 28, 16);
|
|
1130
|
+
const hash = (0, node_crypto_1.createHash)('sha256').update(data).digest();
|
|
1131
|
+
const hex = hash.toString('hex', 0, 9).toUpperCase();
|
|
1132
|
+
const baseId = '3EB0' + hex;
|
|
1133
|
+
const pos = 4 + (hash[0] & 15);
|
|
1134
|
+
return baseId.slice(0, pos) + 'STARFALL' + baseId.slice(pos);
|
|
1135
|
+
};
|
|
1136
|
+
exports.generateMessageIDV2 = generateMessageIDV2;
|
|
1137
|
+
// generate a random ID to attach to a message
|
|
1138
|
+
const generateMessageID = () => '3EB0' + (0, node_crypto_1.randomBytes)(18).toString('hex').toUpperCase();
|
|
1139
|
+
exports.generateMessageID = generateMessageID;
|
|
1140
|
+
function bindWaitForEvent(ev, event) {
|
|
1141
|
+
return async (check, timeoutMs) => {
|
|
1142
|
+
let listener;
|
|
1143
|
+
let closeListener;
|
|
1144
|
+
await promiseTimeout(timeoutMs, (resolve, reject) => {
|
|
1145
|
+
closeListener = ({ connection, lastDisconnect }) => {
|
|
1146
|
+
if (connection === 'close') {
|
|
1147
|
+
reject(lastDisconnect?.error || new index_js_2.HttpError('Connection Closed', { statusCode: index_js_3.DisconnectReason.connectionClosed }));
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
ev.on('connection.update', closeListener);
|
|
1151
|
+
listener = async (update) => {
|
|
1152
|
+
if (await check(update)) {
|
|
1153
|
+
resolve();
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
ev.on(event, listener);
|
|
1157
|
+
}).finally(() => {
|
|
1158
|
+
ev.off(event, listener);
|
|
1159
|
+
ev.off('connection.update', closeListener);
|
|
1160
|
+
});
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
|
|
1164
|
+
exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
|
|
1165
|
+
/**
|
|
1166
|
+
* utility that fetches latest baileys version from the master branch.
|
|
1167
|
+
* Use to ensure your WA connection is always on the latest version
|
|
1168
|
+
*/
|
|
1169
|
+
const fetchLatestBaileysVersion = async (options = {}) => {
|
|
1170
|
+
const URL = 'https://raw.githubusercontent.com/kage-baileys/main/lib/Defaults/index.js';
|
|
1171
|
+
try {
|
|
1172
|
+
const response = await fetch(URL, {
|
|
1173
|
+
dispatcher: options.dispatcher,
|
|
1174
|
+
method: 'GET',
|
|
1175
|
+
headers: options.headers
|
|
1176
|
+
});
|
|
1177
|
+
if (!response.ok) {
|
|
1178
|
+
throw new index_js_2.HttpError(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
|
|
1179
|
+
}
|
|
1180
|
+
const text = await response.text();
|
|
1181
|
+
// Extract version from line 5 (const version = [...])
|
|
1182
|
+
const lines = text.split('\n');
|
|
1183
|
+
const versionLine = lines[4]; // Line 5 (0-indexed)
|
|
1184
|
+
const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
|
|
1185
|
+
if (versionMatch) {
|
|
1186
|
+
const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
|
|
1187
|
+
return {
|
|
1188
|
+
version,
|
|
1189
|
+
isLatest: true
|
|
1190
|
+
};
|
|
1191
|
+
}
|
|
1192
|
+
else {
|
|
1193
|
+
throw new Error('Could not parse version from Defaults/index.ts');
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
catch (error) {
|
|
1197
|
+
return {
|
|
1198
|
+
version: baileysVersion,
|
|
1199
|
+
isLatest: false,
|
|
1200
|
+
error
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
|
|
1205
|
+
/**
|
|
1206
|
+
* A utility that fetches the latest web version of whatsapp.
|
|
1207
|
+
* Use to ensure your WA connection is always on the latest version
|
|
1208
|
+
*/
|
|
1209
|
+
const fetchLatestWaWebVersion = async (options = {}) => {
|
|
1210
|
+
try {
|
|
1211
|
+
// Absolute minimal headers required to bypass anti-bot detection
|
|
1212
|
+
const defaultHeaders = {
|
|
1213
|
+
'sec-fetch-site': 'none',
|
|
1214
|
+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
1215
|
+
};
|
|
1216
|
+
const headers = { ...defaultHeaders, ...options.headers };
|
|
1217
|
+
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
|
1218
|
+
...options,
|
|
1219
|
+
method: 'GET',
|
|
1220
|
+
headers
|
|
1221
|
+
});
|
|
1222
|
+
if (!response.ok) {
|
|
1223
|
+
throw new index_js_2.HttpError(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
|
|
1224
|
+
}
|
|
1225
|
+
const data = await response.text();
|
|
1226
|
+
const regex = /\\?"client_revision\\?":\s*(\d+)/;
|
|
1227
|
+
const match = data.match(regex);
|
|
1228
|
+
if (!match?.[1]) {
|
|
1229
|
+
return {
|
|
1230
|
+
version: baileysVersion,
|
|
1231
|
+
isLatest: false,
|
|
1232
|
+
error: {
|
|
1233
|
+
message: 'Could not find client revision in the fetched content'
|
|
1234
|
+
}
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
const clientRevision = match[1];
|
|
1238
|
+
return {
|
|
1239
|
+
version: [2, 3000, +clientRevision],
|
|
1240
|
+
isLatest: true
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
catch (error) {
|
|
1244
|
+
return {
|
|
1245
|
+
version: baileysVersion,
|
|
1246
|
+
isLatest: false,
|
|
1247
|
+
error
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
};
|
|
1251
|
+
exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion;
|
|
1252
|
+
/** unique message tag prefix for MD clients */
|
|
1253
|
+
const generateMdTagPrefix = () => {
|
|
1254
|
+
const bytes = (0, node_crypto_1.randomBytes)(4);
|
|
1255
|
+
return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
|
|
1256
|
+
};
|
|
1257
|
+
exports.generateMdTagPrefix = generateMdTagPrefix;
|
|
1258
|
+
const STATUS_MAP = {
|
|
1259
|
+
sender: index_js_1.proto.WebMessageInfo.Status.SERVER_ACK,
|
|
1260
|
+
played: index_js_1.proto.WebMessageInfo.Status.PLAYED,
|
|
1261
|
+
read: index_js_1.proto.WebMessageInfo.Status.READ,
|
|
1262
|
+
'read-self': index_js_1.proto.WebMessageInfo.Status.READ
|
|
1263
|
+
};
|
|
1264
|
+
/**
|
|
1265
|
+
* Given a type of receipt, returns what the new status of the message should be
|
|
1266
|
+
* @param type type from receipt
|
|
1267
|
+
*/
|
|
1268
|
+
const getStatusFromReceiptType = (type) => {
|
|
1269
|
+
const status = STATUS_MAP[type];
|
|
1270
|
+
if (typeof type === 'undefined') {
|
|
1271
|
+
return index_js_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
|
|
1272
|
+
}
|
|
1273
|
+
return status;
|
|
1274
|
+
};
|
|
1275
|
+
exports.getStatusFromReceiptType = getStatusFromReceiptType;
|
|
1276
|
+
const CODE_MAP = {
|
|
1277
|
+
conflict: index_js_3.DisconnectReason.connectionReplaced
|
|
1278
|
+
};
|
|
1279
|
+
/**
|
|
1280
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
1281
|
+
* @param reason the string reason given, eg. "conflict"
|
|
1282
|
+
*/
|
|
1283
|
+
const getErrorCodeFromStreamError = (node) => {
|
|
1284
|
+
const [reasonNode] = (0, index_js_4.getAllBinaryNodeChildren)(node);
|
|
1285
|
+
let reason = reasonNode?.tag || 'unknown';
|
|
1286
|
+
const statusCode = +(node.attrs.code || CODE_MAP[reason] || index_js_3.DisconnectReason.badSession);
|
|
1287
|
+
if (statusCode === index_js_3.DisconnectReason.restartRequired) {
|
|
1288
|
+
reason = 'restart required';
|
|
1289
|
+
}
|
|
1290
|
+
return {
|
|
1291
|
+
reason,
|
|
1292
|
+
statusCode
|
|
1293
|
+
};
|
|
1294
|
+
};
|
|
1295
|
+
exports.getErrorCodeFromStreamError = getErrorCodeFromStreamError;
|
|
1296
|
+
const getCallStatusFromNode = ({ tag, attrs }) => {
|
|
1297
|
+
let status;
|
|
1298
|
+
switch (tag) {
|
|
1299
|
+
case 'offer':
|
|
1300
|
+
case 'offer_notice':
|
|
1301
|
+
status = 'offer';
|
|
1302
|
+
break;
|
|
1303
|
+
case 'terminate':
|
|
1304
|
+
if (attrs.reason === 'timeout') {
|
|
1305
|
+
status = 'timeout';
|
|
1306
|
+
}
|
|
1307
|
+
else {
|
|
1308
|
+
//fired when accepted/rejected/timeout/caller hangs up
|
|
1309
|
+
status = 'terminate';
|
|
1310
|
+
}
|
|
1311
|
+
break;
|
|
1312
|
+
case 'preaccept':
|
|
1313
|
+
status = 'preaccept';
|
|
1314
|
+
break;
|
|
1315
|
+
case 'transport':
|
|
1316
|
+
status = 'transport';
|
|
1317
|
+
break;
|
|
1318
|
+
case 'relaylatency':
|
|
1319
|
+
status = 'relaylatency';
|
|
1320
|
+
break;
|
|
1321
|
+
case 'reject':
|
|
1322
|
+
status = 'reject';
|
|
1323
|
+
break;
|
|
1324
|
+
case 'accept':
|
|
1325
|
+
status = 'accept';
|
|
1326
|
+
break;
|
|
1327
|
+
default:
|
|
1328
|
+
status = 'ringing';
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
return status;
|
|
1332
|
+
};
|
|
1333
|
+
exports.getCallStatusFromNode = getCallStatusFromNode;
|
|
1334
|
+
const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
|
|
1335
|
+
const getCodeFromWSError = (error) => {
|
|
1336
|
+
let statusCode = 500;
|
|
1337
|
+
if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
|
|
1338
|
+
const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
|
|
1339
|
+
if (!Number.isNaN(code) && code >= 400) {
|
|
1340
|
+
statusCode = code;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
else if (
|
|
1344
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1345
|
+
error?.code?.startsWith('E') ||
|
|
1346
|
+
error?.message?.includes('timed out')) {
|
|
1347
|
+
// handle ETIMEOUT, ENOTFOUND etc
|
|
1348
|
+
statusCode = 408;
|
|
1349
|
+
}
|
|
1350
|
+
return statusCode;
|
|
1351
|
+
};
|
|
1352
|
+
exports.getCodeFromWSError = getCodeFromWSError;
|
|
1353
|
+
/**
|
|
1354
|
+
* Is the given platform WA business
|
|
1355
|
+
* @param platform AuthenticationCreds.platform
|
|
1356
|
+
*/
|
|
1357
|
+
const isWABusinessPlatform = (platform) => {
|
|
1358
|
+
return platform === 'smbi' || platform === 'smba';
|
|
1359
|
+
};
|
|
1360
|
+
exports.isWABusinessPlatform = isWABusinessPlatform;
|
|
1361
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1362
|
+
function trimUndefined(obj) {
|
|
1363
|
+
for (const key in obj) {
|
|
1364
|
+
if (typeof obj[key] === 'undefined') {
|
|
1365
|
+
delete obj[key];
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
return obj;
|
|
1369
|
+
}
|
|
1370
|
+
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
|
|
1371
|
+
function bytesToCrockford(buffer) {
|
|
1372
|
+
let value = 0;
|
|
1373
|
+
let bitCount = 0;
|
|
1374
|
+
const crockford = [];
|
|
1375
|
+
for (const element of buffer) {
|
|
1376
|
+
value = (value << 8) | (element & 0xff);
|
|
1377
|
+
bitCount += 8;
|
|
1378
|
+
while (bitCount >= 5) {
|
|
1379
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
|
|
1380
|
+
bitCount -= 5;
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
if (bitCount > 0) {
|
|
1384
|
+
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
|
|
1385
|
+
}
|
|
1386
|
+
return crockford.join('');
|
|
1387
|
+
}
|
|
1388
|
+
function encodeNewsletterMessage(message) {
|
|
1389
|
+
return index_js_1.proto.Message.encode(message).finish();
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
return module.exports;
|
|
1393
|
+
})();
|
|
1394
|
+
for (const key of Object.keys(__kageModule169)) {
|
|
1395
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule169[key]});
|
|
1396
|
+
}
|
|
1397
|
+
Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule169, configurable:true});
|