mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
* feat(feature/pdo-sync): initial commit * feat(feature/pdo-sync): Moved to conventional send functions, exported, patched some errors * fix(feature/pdo-sync): Linting and more bugsquatting * chore(feature/pdo-sync): linting done * feat/fix(feat/pdo-sync): Newsletter decrypt + ack * merge (#946) * fix: profilePictureUrl (#901) * Update module to latest version (#926) * Update package.json Update the module to the latest * Add files via upload * Fix: Readme use upsert events (#908) * Fix: getUSyncDevices (#862) * Update messages-send.ts * Update messages-send.ts * Update messages-send.ts * Fix lint * Fix lint * fix(master): update linting workflow to node 20 (current LTS) --------- Co-authored-by: Akhlaqul Muhammad Fadwa <75623219+zennn08@users.noreply.github.com> Co-authored-by: Rizz2Dev <muhamad.rizki27483@smp.belajar.id> Co-authored-by: Oscar Guindzberg <oscar.guindzberg@gmail.com> Co-authored-by: Bob <115008575+bobslavtriev@users.noreply.github.com> * chore(feature/pdo-sync): final linting * fix(feature/pdo-sync): make replies optional * feat(feat/pdo-sync): add <unavailable> handle * feat(feature/pdo-sync): Fixed the issues with peer messages and implemented some more logic * fix(feature/pdo-sync): Make progress optional * fix(feature/pdo-sync): Nullify and defeat Message absent from node if it is resolved immediately * feat(feature/pdo-sync): Export message absent from node and export PDO request ID with it --------- Co-authored-by: Akhlaqul Muhammad Fadwa <75623219+zennn08@users.noreply.github.com> Co-authored-by: Rizz2Dev <muhamad.rizki27483@smp.belajar.id> Co-authored-by: Oscar Guindzberg <oscar.guindzberg@gmail.com> Co-authored-by: Bob <115008575+bobslavtriev@users.noreply.github.com>
214 lines
5.5 KiB
TypeScript
214 lines
5.5 KiB
TypeScript
import { Boom } from '@hapi/boom'
|
|
import { Logger } from 'pino'
|
|
import { proto } from '../../WAProto'
|
|
import { SignalRepository, WAMessageKey } from '../Types'
|
|
import { areJidsSameUser, BinaryNode, isJidBroadcast, isJidGroup, isJidNewsletter, isJidStatusBroadcast, isJidUser, isLidUser } from '../WABinary'
|
|
import { unpadRandomMax16 } from './generics'
|
|
|
|
export const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node'
|
|
|
|
type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'direct_peer_status' | 'other_status' | 'newsletter'
|
|
|
|
/**
|
|
* Decode the received node as a message.
|
|
* @note this will only parse the message, not decrypt it
|
|
*/
|
|
export function decodeMessageNode(
|
|
stanza: BinaryNode,
|
|
meId: string,
|
|
meLid: string
|
|
) {
|
|
let msgType: MessageType
|
|
let chatId: string
|
|
let author: string
|
|
|
|
const msgId = stanza.attrs.id
|
|
const from = stanza.attrs.from
|
|
const participant: string | undefined = stanza.attrs.participant
|
|
const recipient: string | undefined = stanza.attrs.recipient
|
|
|
|
const isMe = (jid: string) => areJidsSameUser(jid, meId)
|
|
const isMeLid = (jid: string) => areJidsSameUser(jid, meLid)
|
|
|
|
if(isJidUser(from)) {
|
|
if(recipient) {
|
|
if(!isMe(from)) {
|
|
throw new Boom('receipient present, but msg not from me', { data: stanza })
|
|
}
|
|
|
|
chatId = recipient
|
|
} else {
|
|
chatId = from
|
|
}
|
|
|
|
msgType = 'chat'
|
|
author = from
|
|
} else if(isLidUser(from)) {
|
|
if(recipient) {
|
|
if(!isMeLid(from)) {
|
|
throw new Boom('receipient present, but msg not from me', { data: stanza })
|
|
}
|
|
|
|
chatId = recipient
|
|
} else {
|
|
chatId = from
|
|
}
|
|
|
|
msgType = 'chat'
|
|
author = from
|
|
} else if(isJidGroup(from)) {
|
|
if(!participant) {
|
|
throw new Boom('No participant in group message')
|
|
}
|
|
|
|
msgType = 'group'
|
|
author = participant
|
|
chatId = from
|
|
} else if(isJidBroadcast(from)) {
|
|
if(!participant) {
|
|
throw new Boom('No participant in group message')
|
|
}
|
|
|
|
const isParticipantMe = isMe(participant)
|
|
if(isJidStatusBroadcast(from)) {
|
|
msgType = isParticipantMe ? 'direct_peer_status' : 'other_status'
|
|
} else {
|
|
msgType = isParticipantMe ? 'peer_broadcast' : 'other_broadcast'
|
|
}
|
|
|
|
chatId = from
|
|
author = participant
|
|
} else if(isJidNewsletter(from)) {
|
|
msgType = 'newsletter'
|
|
chatId = from
|
|
author = from
|
|
} else {
|
|
throw new Boom('Unknown message type', { data: stanza })
|
|
}
|
|
|
|
const fromMe = (isLidUser(from) ? isMeLid : isMe)(stanza.attrs.participant || stanza.attrs.from)
|
|
const pushname = stanza?.attrs?.notify
|
|
|
|
const key: WAMessageKey = {
|
|
remoteJid: chatId,
|
|
fromMe,
|
|
id: msgId,
|
|
participant
|
|
}
|
|
|
|
const fullMessage: proto.IWebMessageInfo = {
|
|
key,
|
|
messageTimestamp: +stanza.attrs.t,
|
|
pushName: pushname,
|
|
broadcast: isJidBroadcast(from)
|
|
}
|
|
|
|
if(key.fromMe) {
|
|
fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK
|
|
}
|
|
|
|
return {
|
|
fullMessage,
|
|
author,
|
|
sender: msgType === 'chat' ? author : chatId
|
|
}
|
|
}
|
|
|
|
export const decryptMessageNode = (
|
|
stanza: BinaryNode,
|
|
meId: string,
|
|
meLid: string,
|
|
repository: SignalRepository,
|
|
logger: Logger
|
|
) => {
|
|
const { fullMessage, author, sender } = decodeMessageNode(stanza, meId, meLid)
|
|
return {
|
|
fullMessage,
|
|
category: stanza.attrs.category,
|
|
author,
|
|
async decrypt() {
|
|
let decryptables = 0
|
|
if(Array.isArray(stanza.content)) {
|
|
for(const { tag, attrs, content } of stanza.content) {
|
|
if(tag === 'verified_name' && content instanceof Uint8Array) {
|
|
const cert = proto.VerifiedNameCertificate.decode(content)
|
|
const details = proto.VerifiedNameCertificate.Details.decode(cert.details)
|
|
fullMessage.verifiedBizName = details.verifiedName
|
|
}
|
|
|
|
if(tag !== 'enc' && tag !== 'plaintext') {
|
|
continue
|
|
}
|
|
|
|
if(!(content instanceof Uint8Array)) {
|
|
continue
|
|
}
|
|
|
|
decryptables += 1
|
|
|
|
let msgBuffer: Uint8Array
|
|
|
|
try {
|
|
const e2eType = tag === 'plaintext' ? 'plaintext' : attrs.type
|
|
switch (e2eType) {
|
|
case 'skmsg':
|
|
msgBuffer = await repository.decryptGroupMessage({
|
|
group: sender,
|
|
authorJid: author,
|
|
msg: content
|
|
})
|
|
break
|
|
case 'pkmsg':
|
|
case 'msg':
|
|
const user = isJidUser(sender) ? sender : author
|
|
msgBuffer = await repository.decryptMessage({
|
|
jid: user,
|
|
type: e2eType,
|
|
ciphertext: content
|
|
})
|
|
break
|
|
case 'plaintext':
|
|
msgBuffer = content
|
|
break
|
|
default:
|
|
throw new Error(`Unknown e2e type: ${e2eType}`)
|
|
}
|
|
|
|
let msg: proto.IMessage = proto.Message.decode(e2eType !== 'plaintext' ? unpadRandomMax16(msgBuffer) : msgBuffer)
|
|
msg = msg.deviceSentMessage?.message || msg
|
|
if(msg.senderKeyDistributionMessage) {
|
|
try {
|
|
await repository.processSenderKeyDistributionMessage({
|
|
authorJid: author,
|
|
item: msg.senderKeyDistributionMessage
|
|
})
|
|
} catch(err) {
|
|
logger.error({ key: fullMessage.key, err }, 'failed to decrypt message')
|
|
}
|
|
}
|
|
|
|
if(fullMessage.message) {
|
|
Object.assign(fullMessage.message, msg)
|
|
} else {
|
|
fullMessage.message = msg
|
|
}
|
|
} catch(err) {
|
|
logger.error(
|
|
{ key: fullMessage.key, err },
|
|
'failed to decrypt message'
|
|
)
|
|
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
fullMessage.messageStubParameters = [err.message]
|
|
}
|
|
}
|
|
}
|
|
|
|
// if nothing was found to decrypt
|
|
if(!decryptables) {
|
|
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
|
|
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
|
|
}
|
|
}
|
|
}
|
|
}
|