Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppProtocolBuilders2.kt
WhatsAppProtocolBuilders2.kt422 lines · 13.9 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol.LinkPreview |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol.Node |
| 5 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol.QuotedContext |
| 6 | import com.vayunmathur.communicate.data.whatsapp.proto.WhatsAppE2EProto |
| 7 | |
| 8 | // -- Rich content, presence/ack, poll/location/contact protos, group IQs -- |
| 9 | |
| 10 | /** |
| 11 | * Build a [ContextInfo] from optional mentions + quoted-reply. Returns null when neither is |
| 12 | * present so callers can send a bare `conversation` message. Ref whatsmeow ContextInfo usage. |
| 13 | */ |
| 14 | fun WhatsAppProtocol.buildContextInfo( |
| 15 | mentionedJids: List<String> = emptyList(), |
| 16 | quoted: QuotedContext? = null, |
| 17 | ): WhatsAppE2EProto.ContextInfo? { |
| 18 | if (mentionedJids.isEmpty() && quoted == null) return null |
| 19 | val ctx = WhatsAppE2EProto.ContextInfo.newBuilder() |
| 20 | for (jid in mentionedJids) ctx.addMentionedJid(jid) |
| 21 | if (quoted != null) { |
| 22 | ctx.stanzaId = quoted.stanzaId |
| 23 | if (quoted.participant.isNotEmpty()) ctx.participant = quoted.participant |
| 24 | quoted.quotedMessage?.let { ctx.quotedMessage = it } |
| 25 | } |
| 26 | return ctx.build() |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Build a text message proto with optional rich content. When there are no mentions, no quoted |
| 31 | * reply and no link preview, a plain `conversation` message is produced (matching |
| 32 | * [buildConversationMessage]); otherwise the text is carried in an [ExtendedTextMessage] with |
| 33 | * the accompanying [ContextInfo] and link-preview fields so the peer renders them. Ref |
| 34 | * whatsmeow send.go ExtendedTextMessage. |
| 35 | */ |
| 36 | fun WhatsAppProtocol.buildTextProto( |
| 37 | text: String, |
| 38 | mentionedJids: List<String> = emptyList(), |
| 39 | quoted: QuotedContext? = null, |
| 40 | linkPreview: LinkPreview? = null, |
| 41 | ): WhatsAppE2EProto.Message { |
| 42 | val ctx = buildContextInfo(mentionedJids, quoted) |
| 43 | if (ctx == null && linkPreview == null) { |
| 44 | return buildConversationMessage(text) |
| 45 | } |
| 46 | val ext = WhatsAppE2EProto.ExtendedTextMessage.newBuilder() |
| 47 | .setText(text) |
| 48 | if (ctx != null) ext.contextInfo = ctx |
| 49 | if (linkPreview != null) { |
| 50 | ext.matchedText = linkPreview.matchedText |
| 51 | ext.canonicalUrl = linkPreview.canonicalUrl |
| 52 | linkPreview.title?.let { ext.title = it } |
| 53 | linkPreview.description?.let { ext.description = it } |
| 54 | linkPreview.jpegThumbnail?.let { ext.jpegThumbnail = com.google.protobuf.ByteString.copyFrom(it) } |
| 55 | } |
| 56 | return WhatsAppE2EProto.Message.newBuilder() |
| 57 | .setExtendedTextMessage(ext.build()) |
| 58 | .build() |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Build an edit as an E2E [Message] proto (ProtocolMessage type MESSAGE_EDIT). Returned as a |
| 63 | * proto — NOT a wire node — so the caller sends it through the normal Signal encryption + |
| 64 | * multi-device fan-out (buildEncryptedMessageNode / buildEncryptedGroupMessageNode) with a |
| 65 | * message-level `edit="1"` attribute. Previously this emitted a plaintext `<enc>` node that |
| 66 | * bypassed Signal and was therefore undeliverable. Ref whatsmeow send.go BuildEdit. |
| 67 | */ |
| 68 | fun WhatsAppProtocol.buildEditProto( |
| 69 | chatJid: String, |
| 70 | targetMessageId: String, |
| 71 | newText: String, |
| 72 | ): WhatsAppE2EProto.Message { |
| 73 | val messageKey = WhatsAppE2EProto.MessageKey.newBuilder() |
| 74 | .setFromMe(true) |
| 75 | .setId(targetMessageId) |
| 76 | .setRemoteJid(chatJid) |
| 77 | .build() |
| 78 | |
| 79 | val newContent = WhatsAppE2EProto.Message.newBuilder() |
| 80 | .setConversation(newText) |
| 81 | .build() |
| 82 | |
| 83 | val protocolMessage = WhatsAppE2EProto.ProtocolMessage.newBuilder() |
| 84 | .setType(WhatsAppE2EProto.ProtocolMessage.Type.MESSAGE_EDIT) |
| 85 | .setKey(messageKey) |
| 86 | .setEditedMessage(newContent) |
| 87 | .setTimestampMs(System.currentTimeMillis()) |
| 88 | .build() |
| 89 | |
| 90 | return WhatsAppE2EProto.Message.newBuilder() |
| 91 | .setProtocolMessage(protocolMessage) |
| 92 | .build() |
| 93 | } |
| 94 | |
| 95 | /** Whether a revoke targets a message that the local user sent (empty/own sender = self). */ |
| 96 | fun WhatsAppProtocol.isRevokeFromMe(senderJid: String, ownJid: String): Boolean = |
| 97 | senderJid.isEmpty() || senderJid == ownJid || |
| 98 | senderJid.substringBefore("@") == ownJid.substringBefore("@") |
| 99 | |
| 100 | /** |
| 101 | * Build a revoke (delete-for-everyone) as an E2E [Message] proto (ProtocolMessage type REVOKE). |
| 102 | * Returned as a proto for the normal fan-out path; the caller sets the message-level `edit` |
| 103 | * attribute ("7" for own messages, "8" for others'). Ref whatsmeow send.go BuildRevoke. |
| 104 | */ |
| 105 | fun WhatsAppProtocol.buildRevokeProto( |
| 106 | chatJid: String, |
| 107 | senderJid: String, |
| 108 | targetMessageId: String, |
| 109 | ownJid: String, |
| 110 | ): WhatsAppE2EProto.Message { |
| 111 | val isFromMe = isRevokeFromMe(senderJid, ownJid) |
| 112 | val messageKey = WhatsAppE2EProto.MessageKey.newBuilder() |
| 113 | .setFromMe(isFromMe) |
| 114 | .setId(targetMessageId) |
| 115 | .setRemoteJid(chatJid) |
| 116 | if (!isFromMe && chatJid.contains("@g.us")) { |
| 117 | messageKey.setParticipant(senderJid) |
| 118 | } |
| 119 | |
| 120 | val protocolMessage = WhatsAppE2EProto.ProtocolMessage.newBuilder() |
| 121 | .setType(WhatsAppE2EProto.ProtocolMessage.Type.REVOKE) |
| 122 | .setKey(messageKey.build()) |
| 123 | .build() |
| 124 | |
| 125 | return WhatsAppE2EProto.Message.newBuilder() |
| 126 | .setProtocolMessage(protocolMessage) |
| 127 | .build() |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Build a chat presence (typing indicator) node. |
| 132 | * From whatsmeow/send.go SendChatPresence() |
| 133 | */ |
| 134 | fun WhatsAppProtocol.buildChatPresence( |
| 135 | chatJid: String, |
| 136 | isComposing: Boolean, |
| 137 | isAudio: Boolean = false, |
| 138 | ownJid: String = "", |
| 139 | ): Node { |
| 140 | val state = if (isComposing) "composing" else "paused" |
| 141 | val childAttrs = if (isComposing && isAudio) mapOf("media" to "audio") else emptyMap() |
| 142 | val attrs = mutableMapOf("to" to chatJid) |
| 143 | if (ownJid.isNotEmpty()) attrs["from"] = ownJid |
| 144 | return Node( |
| 145 | tag = "chatstate", |
| 146 | attrs = attrs, |
| 147 | content = listOf( |
| 148 | Node(tag = state, attrs = childAttrs) |
| 149 | ) |
| 150 | ) |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Build a keepalive (ping) IQ node. |
| 155 | * From whatsmeow/keepalive.go |
| 156 | */ |
| 157 | fun WhatsAppProtocol.buildKeepalive(id: String): Node { |
| 158 | return Node( |
| 159 | tag = "iq", |
| 160 | attrs = mapOf( |
| 161 | "id" to id, |
| 162 | "xmlns" to "w:p", |
| 163 | "type" to "get", |
| 164 | "to" to "s.whatsapp.net" |
| 165 | ) |
| 166 | ) |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Build an ack node for acknowledging received messages. |
| 171 | * From whatsmeow/receipt.go sendAck() |
| 172 | */ |
| 173 | fun WhatsAppProtocol.buildAck( |
| 174 | nodeClass: String, |
| 175 | nodeId: String, |
| 176 | from: String, |
| 177 | participant: String? = null, |
| 178 | recipient: String? = null, |
| 179 | type: String? = null, |
| 180 | ): Node { |
| 181 | val attrs = mutableMapOf( |
| 182 | "class" to nodeClass, |
| 183 | "id" to nodeId, |
| 184 | "to" to from |
| 185 | ) |
| 186 | if (participant != null) attrs["participant"] = participant |
| 187 | if (recipient != null) attrs["recipient"] = recipient |
| 188 | if (type != null && nodeClass != "message") attrs["type"] = type |
| 189 | return Node(tag = "ack", attrs = attrs) |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Build a native PollCreationMessage proto. Mirrors whatsmeow BuildPollCreation: only |
| 194 | * optionName is set per option (option hashes are computed later at vote time), and the poll's |
| 195 | * shared secret is carried in the sibling MessageContextInfo.messageSecret (encKey is left |
| 196 | * unset). [selectableCount] clamps to 0 (unlimited) when negative or greater than the option |
| 197 | * count. Returned as a Message proto so the caller routes it through the normal fan-out. |
| 198 | */ |
| 199 | fun WhatsAppProtocol.buildPollCreationProto( |
| 200 | name: String, |
| 201 | options: List<String>, |
| 202 | selectableCount: Int, |
| 203 | messageSecret: ByteArray, |
| 204 | ): WhatsAppE2EProto.Message { |
| 205 | val poll = WhatsAppE2EProto.PollCreationMessage.newBuilder() |
| 206 | .setName(name) |
| 207 | .setSelectableOptionsCount( |
| 208 | if (selectableCount < 0 || selectableCount > options.size) 0 else selectableCount |
| 209 | ) |
| 210 | for (opt in options) { |
| 211 | poll.addOptions( |
| 212 | WhatsAppE2EProto.PollCreationMessage.Option.newBuilder() |
| 213 | .setOptionName(opt) |
| 214 | ) |
| 215 | } |
| 216 | val ctx = WhatsAppE2EProto.MessageContextInfo.newBuilder() |
| 217 | .setMessageSecret(com.google.protobuf.ByteString.copyFrom(messageSecret)) |
| 218 | return WhatsAppE2EProto.Message.newBuilder() |
| 219 | .setPollCreationMessageV3(poll.build()) |
| 220 | .setMessageContextInfo(ctx.build()) |
| 221 | .build() |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Build a LocationMessage proto (lat/long + optional name/address). Returned as a Message |
| 226 | * proto so the caller can route it through the normal multi-device Signal fan-out |
| 227 | * (buildEncryptedMessageNode), matching whatsmeow from-matrix.go location handling. |
| 228 | */ |
| 229 | fun WhatsAppProtocol.buildLocationProto( |
| 230 | latitude: Double, |
| 231 | longitude: Double, |
| 232 | name: String? = null, |
| 233 | address: String? = null, |
| 234 | ): WhatsAppE2EProto.Message { |
| 235 | val locBuilder = WhatsAppE2EProto.LocationMessage.newBuilder() |
| 236 | .setDegreesLatitude(latitude) |
| 237 | .setDegreesLongitude(longitude) |
| 238 | if (!name.isNullOrEmpty()) locBuilder.setName(name) |
| 239 | if (!address.isNullOrEmpty()) locBuilder.setAddress(address) |
| 240 | |
| 241 | return WhatsAppE2EProto.Message.newBuilder() |
| 242 | .setLocationMessage(locBuilder.build()) |
| 243 | .build() |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Build a contact/vCard message as an E2E [Message] proto. Returned as a proto for the normal |
| 248 | * Signal fan-out path (previously emitted a plaintext `<enc>` node that was undeliverable). |
| 249 | * Ref whatsmeow wa-contact.go. |
| 250 | */ |
| 251 | fun WhatsAppProtocol.buildContactProto( |
| 252 | displayName: String, |
| 253 | vcard: String, |
| 254 | ): WhatsAppE2EProto.Message { |
| 255 | val contactMsg = WhatsAppE2EProto.ContactMessage.newBuilder() |
| 256 | .setDisplayName(displayName) |
| 257 | .setVcard(vcard) |
| 258 | .build() |
| 259 | |
| 260 | return WhatsAppE2EProto.Message.newBuilder() |
| 261 | .setContactMessage(contactMsg) |
| 262 | .build() |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Build a disappearing-timer change as an E2E [Message] proto (ProtocolMessage type |
| 267 | * EPHEMERAL_SETTING). Returned as a proto for the normal Signal fan-out path. Allowed values: |
| 268 | * 0 (off), 86400 (24h), 604800 (7d), 7776000 (90d). Ref whatsmeow HandleMatrixDisappearingTimer. |
| 269 | */ |
| 270 | fun WhatsAppProtocol.buildDisappearingTimerProto( |
| 271 | timerSeconds: Long, |
| 272 | ): WhatsAppE2EProto.Message { |
| 273 | val protocolMsg = WhatsAppE2EProto.ProtocolMessage.newBuilder() |
| 274 | .setType(WhatsAppE2EProto.ProtocolMessage.Type.EPHEMERAL_SETTING) |
| 275 | .setEphemeralExpiration(timerSeconds.toInt()) |
| 276 | .build() |
| 277 | |
| 278 | return WhatsAppE2EProto.Message.newBuilder() |
| 279 | .setProtocolMessage(protocolMsg) |
| 280 | .build() |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Build a group info change IQ node. |
| 285 | * From whatsmeow group.go SetGroupName/SetGroupTopic |
| 286 | */ |
| 287 | fun WhatsAppProtocol.buildGroupInfoChange( |
| 288 | groupJid: String, |
| 289 | field: String, |
| 290 | value: String, |
| 291 | id: String, |
| 292 | extraAttrs: Map<String, String> = emptyMap(), |
| 293 | ): Node { |
| 294 | val childAttrs = mutableMapOf<String, String>() |
| 295 | childAttrs.putAll(extraAttrs) |
| 296 | return Node( |
| 297 | tag = "iq", |
| 298 | attrs = mapOf( |
| 299 | "id" to id, |
| 300 | "type" to "set", |
| 301 | "xmlns" to "w:g2", |
| 302 | "to" to groupJid, |
| 303 | ), |
| 304 | content = listOf( |
| 305 | Node(tag = field, attrs = childAttrs, content = listOf(), data = value.toByteArray(Charsets.UTF_8)) |
| 306 | ), |
| 307 | ) |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Build a "set group topic/description" IQ. |
| 312 | * From whatsmeow group.go SetGroupTopic: <description id=newID [prev=previousID] |
| 313 | * [delete=true]> with a <body> child carrying the topic (no body when empty). |
| 314 | */ |
| 315 | fun WhatsAppProtocol.buildSetGroupTopic( |
| 316 | groupJid: String, |
| 317 | topic: String, |
| 318 | newId: String, |
| 319 | previousId: String? = null, |
| 320 | ): Node { |
| 321 | val descAttrs = mutableMapOf("id" to newId) |
| 322 | if (!previousId.isNullOrEmpty()) descAttrs["prev"] = previousId |
| 323 | val content = if (topic.isEmpty()) { |
| 324 | descAttrs["delete"] = "true" |
| 325 | emptyList() |
| 326 | } else { |
| 327 | listOf(Node(tag = "body", data = topic.toByteArray(Charsets.UTF_8))) |
| 328 | } |
| 329 | return Node( |
| 330 | tag = "iq", |
| 331 | attrs = mapOf( |
| 332 | "id" to newId, |
| 333 | "type" to "set", |
| 334 | "xmlns" to "w:g2", |
| 335 | "to" to groupJid, |
| 336 | ), |
| 337 | content = listOf(Node(tag = "description", attrs = descAttrs, content = content)), |
| 338 | ) |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Build a "leave group" IQ. |
| 343 | * From whatsmeow group.go LeaveGroup: iq to the group server (g.us) with |
| 344 | * <leave><group id=<groupJid>/></leave>. |
| 345 | */ |
| 346 | fun WhatsAppProtocol.buildLeaveGroup(groupJid: String, id: String): Node { |
| 347 | return Node( |
| 348 | tag = "iq", |
| 349 | attrs = mapOf( |
| 350 | "id" to id, |
| 351 | "type" to "set", |
| 352 | "xmlns" to "w:g2", |
| 353 | "to" to "g.us", |
| 354 | ), |
| 355 | content = listOf( |
| 356 | Node( |
| 357 | tag = "leave", |
| 358 | content = listOf(Node(tag = "group", attrs = mapOf("id" to groupJid))), |
| 359 | ), |
| 360 | ), |
| 361 | ) |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Build a create-group IQ node (whatsmeow group.go CreateGroup): |
| 366 | * `<iq to="g.us" type="set" xmlns="w:g2" id=...><create subject=".." key=".."> |
| 367 | * <participant jid="..."/>…</create></iq>`. |
| 368 | * [key] is a client-chosen unique id echoed back in the response so we can correlate. |
| 369 | */ |
| 370 | fun WhatsAppProtocol.buildCreateGroup( |
| 371 | subject: String, |
| 372 | participantJids: List<String>, |
| 373 | id: String, |
| 374 | key: String, |
| 375 | ): Node { |
| 376 | val participants = participantJids.map { jid -> |
| 377 | Node(tag = "participant", attrs = mapOf("jid" to jid)) |
| 378 | } |
| 379 | return Node( |
| 380 | tag = "iq", |
| 381 | attrs = mapOf( |
| 382 | "id" to id, |
| 383 | "type" to "set", |
| 384 | "xmlns" to "w:g2", |
| 385 | "to" to "g.us", |
| 386 | ), |
| 387 | content = listOf( |
| 388 | Node( |
| 389 | tag = "create", |
| 390 | attrs = mapOf("subject" to subject, "key" to key), |
| 391 | content = participants, |
| 392 | ), |
| 393 | ), |
| 394 | ) |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Build a group participant change IQ node. |
| 399 | * From whatsmeow group.go UpdateGroupParticipants |
| 400 | */ |
| 401 | fun WhatsAppProtocol.buildGroupParticipantChange( |
| 402 | groupJid: String, |
| 403 | participantJids: List<String>, |
| 404 | action: String, |
| 405 | id: String, |
| 406 | ): Node { |
| 407 | val participants = participantJids.map { jid -> |
| 408 | Node(tag = "participant", attrs = mapOf("jid" to jid)) |
| 409 | } |
| 410 | return Node( |
| 411 | tag = "iq", |
| 412 | attrs = mapOf( |
| 413 | "id" to id, |
| 414 | "type" to "set", |
| 415 | "xmlns" to "w:g2", |
| 416 | "to" to groupJid, |
| 417 | ), |
| 418 | content = listOf( |
| 419 | Node(tag = action, content = participants) |
| 420 | ), |
| 421 | ) |
| 422 | } |