@itznomo/baileys 0.0.0-stage → 0.0.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.
Files changed (124) hide show
  1. package/BUTTONS.md +153 -0
  2. package/Example/call-bot.mjs +38 -0
  3. package/Example/example.mjs +36 -0
  4. package/Example/full-bot-commands.mjs +319 -0
  5. package/Example/full-bot.mjs +77 -0
  6. package/LICENSE +21 -0
  7. package/README.md +543 -2
  8. package/WAProto/@dnuzi +1 -0
  9. package/WAProto/index.d.ts +15932 -0
  10. package/WAProto/index.js +113300 -0
  11. package/assets/banner.svg +17 -0
  12. package/assets/devider.svg +1 -0
  13. package/assets/features.svg +21 -0
  14. package/docs/CALLS.md +106 -0
  15. package/docs/FEATURES.md +1152 -0
  16. package/engine-requirements.js +19 -0
  17. package/index.d.ts +815 -0
  18. package/package.json +51 -4
  19. package/src/binary/wa-binary-constants.js +1300 -0
  20. package/src/binary/wa-binary.js +627 -0
  21. package/src/constants.js +147 -0
  22. package/src/defaults.js +106 -0
  23. package/src/foundation/ai-rich.js +654 -0
  24. package/src/foundation/audio-duration.js +470 -0
  25. package/src/foundation/audio-samples.js +250 -0
  26. package/src/foundation/boom.js +39 -0
  27. package/src/foundation/builders.js +851 -0
  28. package/src/foundation/cache.js +134 -0
  29. package/src/foundation/concurrency.js +69 -0
  30. package/src/foundation/curve25519.js +189 -0
  31. package/src/foundation/image-thumb.js +65 -0
  32. package/src/foundation/link-preview.js +278 -0
  33. package/src/foundation/logger.js +78 -0
  34. package/src/foundation/long.js +130 -0
  35. package/src/foundation/performance.js +385 -0
  36. package/src/foundation/protobuf-minimal.js +587 -0
  37. package/src/foundation/qrcode-terminal.js +273 -0
  38. package/src/foundation/sql-database.js +716 -0
  39. package/src/foundation/wa-crypto.js +47 -0
  40. package/src/foundation/zip.js +108 -0
  41. package/src/index.js +67 -0
  42. package/src/signal/libsignal.js +996 -0
  43. package/src/signal/signal-group.js +455 -0
  44. package/src/signal/signal-repository.js +635 -0
  45. package/src/socket/business-communities.js +811 -0
  46. package/src/socket/chats.js +702 -0
  47. package/src/socket/groups-newsletter.js +524 -0
  48. package/src/socket/messages-recv.js +1815 -0
  49. package/src/socket/messages-send.js +1526 -0
  50. package/src/socket/socket-builder.js +131 -0
  51. package/src/socket/socket-core.js +913 -0
  52. package/src/socket/usync.js +287 -0
  53. package/src/socket-client/native-websocket.js +344 -0
  54. package/src/socket-client/websocket-client.js +170 -0
  55. package/src/utils/auth-state-core.js +731 -0
  56. package/src/utils/auth-state-storage.js +594 -0
  57. package/src/utils/chat-sync.js +1046 -0
  58. package/src/utils/check-status.js +159 -0
  59. package/src/utils/cstoken.js +17 -0
  60. package/src/utils/media.js +807 -0
  61. package/src/utils/message-compose.js +1747 -0
  62. package/src/utils/message-processing.js +912 -0
  63. package/src/utils/modern-messages.js +435 -0
  64. package/src/utils/nomo-buttons.js +138 -0
  65. package/src/utils/nomo-commands.js +168 -0
  66. package/src/utils/spam-report-utils.js +136 -0
  67. package/src/utils/use-keyv-auth-state.js +307 -0
  68. package/src/utils/username.js +19 -0
  69. package/src/utils/wa-protocol-core.js +1227 -0
  70. package/src/voip/WaVoipCoordinator.js +158 -0
  71. package/src/voip/app-data/WaAppDataStream.js +193 -0
  72. package/src/voip/app-data/protocol.js +172 -0
  73. package/src/voip/auto.js +139 -0
  74. package/src/voip/bytes.js +63 -0
  75. package/src/voip/call/WaCallManager.js +442 -0
  76. package/src/voip/call/WaCallMediaSession.js +1727 -0
  77. package/src/voip/call/call-state.js +203 -0
  78. package/src/voip/crypto/encryption.js +13 -0
  79. package/src/voip/crypto/primitives.js +21 -0
  80. package/src/voip/crypto/rtcp.js +66 -0
  81. package/src/voip/crypto/srtp.js +180 -0
  82. package/src/voip/crypto/ssrc.js +15 -0
  83. package/src/voip/index.js +730 -0
  84. package/src/voip/media/WaAudioEngine.js +486 -0
  85. package/src/voip/media/WaVideoEngine.js +275 -0
  86. package/src/voip/media/audio-codec.js +0 -0
  87. package/src/voip/media/audio-reorder.js +107 -0
  88. package/src/voip/media/h264.js +246 -0
  89. package/src/voip/media/mlow-codec.js +465 -0
  90. package/src/voip/media/rtcp.js +68 -0
  91. package/src/voip/media/rtp.js +242 -0
  92. package/src/voip/protobuf.js +30 -0
  93. package/src/voip/relay/WaManualRelay.js +609 -0
  94. package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
  95. package/src/voip/relay/dtls/aead.js +46 -0
  96. package/src/voip/relay/dtls/cert-builder.js +102 -0
  97. package/src/voip/relay/dtls/der.js +98 -0
  98. package/src/voip/relay/dtls/ecdhe.js +37 -0
  99. package/src/voip/relay/dtls/handshake-framing.js +100 -0
  100. package/src/voip/relay/dtls/handshake-messages.js +158 -0
  101. package/src/voip/relay/dtls/handshake.js +582 -0
  102. package/src/voip/relay/dtls/prf.js +39 -0
  103. package/src/voip/relay/dtls/record.js +56 -0
  104. package/src/voip/relay/relay-ack.js +141 -0
  105. package/src/voip/relay/sctp/association.js +434 -0
  106. package/src/voip/relay/sctp/crc32c.js +21 -0
  107. package/src/voip/relay/sctp/wire.js +226 -0
  108. package/src/voip/relay/stun.js +463 -0
  109. package/src/voip/shim/baileys-resolve.js +7 -0
  110. package/src/voip/shim/core.js +66 -0
  111. package/src/voip/shim/crypto.js +6 -0
  112. package/src/voip/shim/proto.js +10 -0
  113. package/src/voip/shim/protocol.js +130 -0
  114. package/src/voip/shim/transport.js +82 -0
  115. package/src/voip/shim/util.js +207 -0
  116. package/src/voip/signaling/bridge.js +102 -0
  117. package/src/voip/signaling/screen-share.js +60 -0
  118. package/src/voip/signaling/signaling.js +491 -0
  119. package/src/voip/types.js +69 -0
  120. package/src/voip/voip-deps.js +243 -0
  121. package/src/voip/voipClient.js +314 -0
  122. package/src/wam/wam-constants.js +22852 -0
  123. package/src/wam/wam.js +146 -0
  124. package/voip.d.ts +144 -0
package/src/wam/wam.js ADDED
@@ -0,0 +1,146 @@
1
+ import { FLAG_BYTE, FLAG_EVENT, FLAG_EXTENDED, FLAG_FIELD, FLAG_GLOBAL, WEB_EVENTS, WEB_GLOBALS } from './wam-constants.js'
2
+
3
+ export class BinaryInfo {
4
+ constructor(options = {}) {
5
+ this.protocolVersion = 5
6
+ this.sequence = 0
7
+ this.events = []
8
+ this.buffer = []
9
+ Object.assign(this, options)
10
+ }
11
+ }
12
+
13
+ const getHeaderBitLength = key => (key < 256 ? 2 : 3)
14
+
15
+ export const encodeWAM = binaryInfo => {
16
+ binaryInfo.buffer = []
17
+ encodeWAMHeader(binaryInfo)
18
+ encodeEvents(binaryInfo)
19
+ const totalSize = binaryInfo.buffer.map(a => a.length).reduce((a, b) => a + b)
20
+ const buffer = Buffer.alloc(totalSize)
21
+ let offset = 0
22
+ for (const buffer_ of binaryInfo.buffer) {
23
+ buffer_.copy(buffer, offset)
24
+ offset += buffer_.length
25
+ }
26
+ return buffer
27
+ }
28
+
29
+ function encodeWAMHeader(binaryInfo) {
30
+ const headerBuffer = Buffer.alloc(8)
31
+ headerBuffer.write('WAM', 0, 'utf8')
32
+ headerBuffer.writeUInt8(binaryInfo.protocolVersion, 3)
33
+ headerBuffer.writeUInt8(1, 4)
34
+ headerBuffer.writeUInt16BE(binaryInfo.sequence, 5)
35
+ headerBuffer.writeUInt8(0, 7)
36
+ binaryInfo.buffer.push(headerBuffer)
37
+ }
38
+
39
+ function encodeGlobalAttributes(binaryInfo, globals) {
40
+ for (const [key, _value] of Object.entries(globals)) {
41
+ const id = WEB_GLOBALS.find(a => a?.name === key).id
42
+ let value = _value
43
+ if (typeof value === 'boolean') value = value ? 1 : 0
44
+ binaryInfo.buffer.push(serializeData(id, value, FLAG_GLOBAL))
45
+ }
46
+ }
47
+
48
+ function encodeEvents(binaryInfo) {
49
+ for (const [name, { props, globals }] of binaryInfo.events.map(a => Object.entries(a)[0])) {
50
+ encodeGlobalAttributes(binaryInfo, globals)
51
+ const event = WEB_EVENTS.find(a => a.name === name)
52
+ const props_ = Object.entries(props)
53
+ let extended = false
54
+ for (const [, value] of props_) extended || (extended = value !== null)
55
+ const eventFlag = extended ? FLAG_EVENT : FLAG_EVENT | FLAG_EXTENDED
56
+ binaryInfo.buffer.push(serializeData(event.id, -event.weight, eventFlag))
57
+ for (let i = 0; i < props_.length; i++) {
58
+ const [key, _value] = props_[i]
59
+ const id = event.props[key]?.[0]
60
+ extended = i < props_.length - 1
61
+ let value = _value
62
+ if (typeof value === 'boolean') value = value ? 1 : 0
63
+ const fieldFlag = extended ? FLAG_EVENT : FLAG_FIELD | FLAG_EXTENDED
64
+ binaryInfo.buffer.push(serializeData(id, value, fieldFlag))
65
+ }
66
+ }
67
+ }
68
+
69
+ function serializeData(key, value, flag) {
70
+ const bufferLength = getHeaderBitLength(key)
71
+ let buffer
72
+ let offset = 0
73
+ if (value === null) {
74
+ if (flag === FLAG_GLOBAL) {
75
+ buffer = Buffer.alloc(bufferLength)
76
+ serializeHeader(buffer, offset, key, flag)
77
+ return buffer
78
+ }
79
+ } else if (typeof value === 'number' && Number.isInteger(value)) {
80
+ if (value === 0 || value === 1) {
81
+ buffer = Buffer.alloc(bufferLength)
82
+ serializeHeader(buffer, offset, key, flag | ((value + 1) << 4))
83
+ return buffer
84
+ } else if (-128 <= value && value < 128) {
85
+ buffer = Buffer.alloc(bufferLength + 1)
86
+ offset = serializeHeader(buffer, offset, key, flag | (3 << 4))
87
+ buffer.writeInt8(value, offset)
88
+ return buffer
89
+ } else if (-32768 <= value && value < 32768) {
90
+ buffer = Buffer.alloc(bufferLength + 2)
91
+ offset = serializeHeader(buffer, offset, key, flag | (4 << 4))
92
+ buffer.writeInt16LE(value, offset)
93
+ return buffer
94
+ } else if (-2147483648 <= value && value < 2147483648) {
95
+ buffer = Buffer.alloc(bufferLength + 4)
96
+ offset = serializeHeader(buffer, offset, key, flag | (5 << 4))
97
+ buffer.writeInt32LE(value, offset)
98
+ return buffer
99
+ } else {
100
+ buffer = Buffer.alloc(bufferLength + 8)
101
+ offset = serializeHeader(buffer, offset, key, flag | (7 << 4))
102
+ buffer.writeDoubleLE(value, offset)
103
+ return buffer
104
+ }
105
+ } else if (typeof value === 'number') {
106
+ buffer = Buffer.alloc(bufferLength + 8)
107
+ offset = serializeHeader(buffer, offset, key, flag | (7 << 4))
108
+ buffer.writeDoubleLE(value, offset)
109
+ return buffer
110
+ } else if (typeof value === 'string') {
111
+ const utf8Bytes = Buffer.byteLength(value, 'utf8')
112
+ if (utf8Bytes < 256) {
113
+ buffer = Buffer.alloc(bufferLength + 1 + utf8Bytes)
114
+ offset = serializeHeader(buffer, offset, key, flag | (8 << 4))
115
+ buffer.writeUint8(utf8Bytes, offset++)
116
+ } else if (utf8Bytes < 65536) {
117
+ buffer = Buffer.alloc(bufferLength + 2 + utf8Bytes)
118
+ offset = serializeHeader(buffer, offset, key, flag | (9 << 4))
119
+ buffer.writeUInt16LE(utf8Bytes, offset)
120
+ offset += 2
121
+ } else {
122
+ buffer = Buffer.alloc(bufferLength + 4 + utf8Bytes)
123
+ offset = serializeHeader(buffer, offset, key, flag | (10 << 4))
124
+ buffer.writeUInt32LE(utf8Bytes, offset)
125
+ offset += 4
126
+ }
127
+ buffer.write(value, offset, 'utf8')
128
+ return buffer
129
+ }
130
+ throw new Error('missing')
131
+ }
132
+
133
+ function serializeHeader(buffer, offset, key, flag) {
134
+ if (key < 256) {
135
+ buffer.writeUInt8(flag, offset)
136
+ offset += 1
137
+ buffer.writeUInt8(key, offset)
138
+ offset += 1
139
+ } else {
140
+ buffer.writeUInt8(flag | FLAG_BYTE, offset)
141
+ offset += 1
142
+ buffer.writeUInt16LE(key, offset)
143
+ offset += 2
144
+ }
145
+ return offset
146
+ }
package/voip.d.ts ADDED
@@ -0,0 +1,144 @@
1
+ // TypeScript declarations for `@nomo/ballieys/voip` (calls). Hand written, see docs/CALLS.md and docs/FEATURES.md.
2
+ import { EventEmitter } from 'node:events'
3
+ import type { WASocket } from './index.js'
4
+
5
+ export type Resolution = '240p' | '360p' | '480p' | '720p' | '1080p' | { width: number; height: number; frameRate?: number }
6
+
7
+ /** file path, URL, `{ audio }` / `{ video }`, an item with options, or a playlist of those */
8
+ export type MediaItem = string | { audio: string } | { video: string; autoAdvance?: boolean }
9
+ export type Media = MediaItem | MediaItem[]
10
+
11
+ export interface PlayOptions {
12
+ /** repeat the playlist */
13
+ loop?: boolean
14
+ /** hang up when the playlist ends (false = stay on the line) */
15
+ autoEndCall?: boolean
16
+ /** drop to audio when the peer turns every camera off */
17
+ autoDowngrade?: boolean
18
+ }
19
+
20
+ export interface VoipOptions {
21
+ ffprobePath?: string
22
+ voipLogLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error'
23
+ tmpDir?: string
24
+ maxConcurrentCalls?: number
25
+ /** how many finished calls are kept in `voip.history` (default 100) */
26
+ historyLimit?: number
27
+ }
28
+
29
+ export interface IncomingCall {
30
+ callId: string
31
+ /** caller id (number jid or @lid) */
32
+ jid: string
33
+ creator?: string
34
+ isVideo: boolean
35
+ [key: string]: any
36
+ }
37
+
38
+ export interface CallHistoryEntry {
39
+ callId: string
40
+ peer: string
41
+ direction: 'incoming' | 'outgoing'
42
+ isVideo: boolean
43
+ startedAt: number
44
+ connectedAt?: number
45
+ endedAt?: number
46
+ durationMs?: number
47
+ reason?: string
48
+ }
49
+
50
+ export interface NowPlaying {
51
+ index: number
52
+ kind: 'audio' | 'video'
53
+ source: string
54
+ }
55
+
56
+ export class VoipCall extends EventEmitter {
57
+ readonly callId: string
58
+ readonly target: string
59
+ readonly isSilenced: boolean
60
+ readonly nowPlaying: NowPlaying | null
61
+ readonly queueLength: number
62
+ hangup(): Promise<void>
63
+ end(): Promise<void>
64
+ silent(value?: boolean): Promise<void>
65
+ mute(value?: boolean): void
66
+ raiseHand(value?: boolean): void
67
+ react(emoji: string): boolean
68
+ shareScreen(value?: boolean): Promise<void>
69
+ upgradeToVideo(): Promise<'accepted' | 'rejected' | 'rejected_by_timeout' | 'error' | 'timeout' | 'cancelled'>
70
+ /** add to the end of the playlist (starts it if it already finished) */
71
+ enqueue(media: Media): Promise<void>
72
+ /** play right now, the rest continues afterwards */
73
+ play(media: MediaItem): Promise<void>
74
+ /** next item */
75
+ skip(): Promise<void>
76
+ on(event: 'ringing' | 'connected' | 'playlist_looped' | 'playlist_ended' | 'downgraded' | 'audio_finished', listener: () => void): this
77
+ on(event: 'ended', listener: (reason: string) => void): this
78
+ on(event: 'error', listener: (error: Error) => void): this
79
+ on(event: 'item', listener: (item: NowPlaying) => void): this
80
+ on(event: 'silent' | 'peer_mute', listener: (state: boolean) => void): this
81
+ on(event: string, listener: (...args: any[]) => void): this
82
+ }
83
+
84
+ export interface AutoAnswerOptions extends PlayOptions {
85
+ /** path, URL, playlist, `{ audio }` / `{ video }`, or a function per caller. Leave out for a silent call */
86
+ media?: Media | ((incoming: IncomingCall) => Media | undefined | Promise<Media | undefined>)
87
+ resolution?: Resolution
88
+ /** ring time before answering, in ms */
89
+ delayMs?: number
90
+ /** only these numbers or JIDs are answered */
91
+ allow?: string[]
92
+ /** these numbers or JIDs are never answered (rejected) */
93
+ deny?: string[]
94
+ /** what to do with callers that are not in `allow` */
95
+ unmatched?: 'ignore' | 'reject'
96
+ audioCalls?: boolean
97
+ videoCalls?: boolean
98
+ onAnswer?: (call: VoipCall, incoming: IncomingCall) => void
99
+ onReject?: (incoming: IncomingCall, why: string) => void
100
+ onError?: (error: Error, incoming?: IncomingCall) => void
101
+ }
102
+
103
+ export interface AutoRejectOptions {
104
+ /** these numbers or JIDs are NOT rejected */
105
+ allow?: string[]
106
+ /** text sent to the caller after rejecting */
107
+ message?: string | ((incoming: IncomingCall) => string | undefined | Promise<string | undefined>)
108
+ delayMs?: number
109
+ onReject?: (incoming: IncomingCall) => void
110
+ onError?: (error: Error, incoming?: IncomingCall) => void
111
+ }
112
+
113
+ export interface AutoAnswerHandle {
114
+ stop(): void
115
+ readonly answered: number
116
+ }
117
+ export interface AutoRejectHandle {
118
+ stop(): void
119
+ readonly rejected: number
120
+ }
121
+
122
+ export default class Voip {
123
+ constructor(sock: WASocket, options?: VoipOptions)
124
+ readonly calls: VoipCall[]
125
+ readonly history: CallHistoryEntry[]
126
+ readonly socket: WASocket
127
+ readonly maxConcurrentCalls: number
128
+ call(jid: string, media?: Media, resolution?: Resolution, options?: PlayOptions): Promise<VoipCall>
129
+ callMany(targets: string[], media?: Media, resolution?: Resolution, options?: PlayOptions): Promise<{ jid: string; call?: VoipCall; error?: Error }[]>
130
+ autoAnswer(options?: AutoAnswerOptions): Promise<AutoAnswerHandle>
131
+ autoReject(options?: AutoRejectOptions): Promise<AutoRejectHandle>
132
+ listen(): Promise<void>
133
+ onIncoming(listener: (incoming: IncomingCall) => void | Promise<void>): () => void
134
+ accept(callId: string, media?: Media, resolution?: Resolution, options?: PlayOptions): Promise<VoipCall>
135
+ acceptCall(callId: string, ...rest: any[]): Promise<VoipCall>
136
+ reject(callId: string): Promise<void>
137
+ rejectCall(callId: string): Promise<void>
138
+ hangup(callId: string): Promise<void>
139
+ /** end all calls (`true` = only release the slots) */
140
+ end(releaseOnly?: boolean): Promise<void>
141
+ on(event: string, listener: (...args: any[]) => void): void
142
+ off(event: string, listener: (...args: any[]) => void): void
143
+ readonly coordinator: any
144
+ }