From 584b5e9822911970a1cd321bed25e7bcd7d608bf Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 8 Mar 2020 22:05:08 +0800 Subject: [PATCH] xml and Json messages support --- .../mamoe/mirai/qqandroid/message/messages.kt | 28 ++++++-------- .../net.mamoe.mirai/message/data/Json.kt | 24 ++++++++++++ .../message/data/MessageChain.kt | 9 +++-- .../message/data/RichMessage.kt | 29 +++++++++++++++ .../net.mamoe.mirai/message/data/XML.kt | 37 +++++++++++-------- 5 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Json.kt create mode 100644 mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt index 51a8e7451..bed9a11b5 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt @@ -273,9 +273,9 @@ internal fun MessageChain.toRichTextElems(forGroup: Boolean): MutableList()){ - elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "78 00 F8 01 00 C8 02 00".hexToBytes()))) - // } + if (this.any()) { + elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "08 09 78 00 C8 01 00 F0 01 00 F8 01 00 90 02 00 C8 02 00 98 03 00 A0 03 20 B0 03 00 C0 03 00 D0 03 00 E8 03 00 8A 04 02 08 03 90 04 80 80 80 10 B8 04 00 C0 04 00".hexToBytes()))) + } else elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "78 00 F8 01 00 C8 02 00".hexToBytes()))) return elements } @@ -343,6 +343,10 @@ internal class OnlineFriendImageImpl( internal fun MsgComm.Msg.toMessageChain(): MessageChain { val elements = this.msgBody.richText.elems + if (this.msgHead.fromUin == 1040400290L){ + println(this._miraiContentToString()) + } + return buildMessageChain(elements.size + 1) { +MessageSourceFromMsg(delegate = this@toMessageChain) elements.joinToMessageChain(this) @@ -405,27 +409,19 @@ internal fun List.joinToMessageChain(message: MessageChainBuilde } } it.richMsg != null -> { + println(this._miraiContentToString()) + val content = MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString() when (it.richMsg.serviceId) { - 60 -> message.add( - XMLMessage( - content = MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString() - ) - ) + 1 -> message.add(JsonMessage(content)) + 60 -> message.add(XmlMessage(content)) else -> { @Suppress("DEPRECATION") MiraiLogger.debug { - "unknown richMsg.serviceId: ${it.richMsg.serviceId}, content=${it.richMsg.template1.contentToString()}, \ntryUnzip=${ - kotlin.runCatching { - MiraiPlatformUtils.unzip(it.richMsg.template1, 1).encodeToString() - }.getOrElse { "" } - }" + "unknown richMsg.serviceId: ${it.richMsg.serviceId}, content=${it.richMsg.template1.contentToString()}, \ntryUnzip=${content}" } } } } - else -> { - println(it._miraiContentToString()) - } } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Json.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Json.kt new file mode 100644 index 000000000..98407774c --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Json.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.message.data + +import net.mamoe.mirai.utils.MiraiExperimentalAPI +import net.mamoe.mirai.utils.SinceMirai + +@SinceMirai("0.27.0") +@OptIn(MiraiExperimentalAPI::class) +class JsonMessage(override val content: String) : RichMessage { + companion object Key : Message.Key + + override val serviceId: Int + get() = 1 + + override fun toString(): String = content +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt index 8426a0aef..ea61fa3ed 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt @@ -61,7 +61,7 @@ interface MessageChain : Message, Iterable { fun getOrNull(key: Message.Key): M? = firstOrNull(key) /** - * 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply]. + * 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply]. * 仅供 `Java` 使用 */ @Suppress("FunctionName", "INAPPLICABLE_JVM_NAME") @@ -73,7 +73,7 @@ interface MessageChain : Message, Iterable { } /** - * 遍历每一个消息, 即 [MessageSource] [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply]. + * 遍历每一个消息, 即 [MessageSource] [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply]. * 仅供 `Java` 使用 */ @Suppress("FunctionName", "INAPPLICABLE_JVM_NAME") @@ -88,7 +88,7 @@ interface MessageChain : Message, Iterable { // region accessors /** - * 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XMLMessage], [QuoteReply] + * 遍历每一个有内容的消息, 即 [At], [AtAll], [PlainText], [Image], [Face], [XmlMessage], [QuoteReply] */ @JvmSynthetic inline fun MessageChain.foreachContent(block: (Message) -> Unit) { @@ -130,6 +130,9 @@ fun MessageChain.firstOrNull(key: Message.Key): M? = when (key) Face -> first() QuoteReply -> first() MessageSource -> first() + XmlMessage -> first() + JsonMessage -> first() + RichMessage -> first() else -> null } as M? diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt new file mode 100644 index 000000000..27601e147 --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/RichMessage.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.message.data + +import net.mamoe.mirai.utils.MiraiExperimentalAPI +import net.mamoe.mirai.utils.SinceMirai + +/** + * XML 消息等富文本消息 + * + * @see XmlMessage + */ +@SinceMirai("0.27.0") +interface RichMessage : MessageContent { + companion object Key : Message.Key + + @MiraiExperimentalAPI + val serviceId: Int + + @MiraiExperimentalAPI + val content: String +} \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt index fb01e7c6c..55a55303c 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/XML.kt @@ -15,6 +15,7 @@ package net.mamoe.mirai.message.data import net.mamoe.mirai.utils.MiraiExperimentalAPI +import net.mamoe.mirai.utils.SinceMirai import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -23,20 +24,25 @@ import kotlin.jvm.JvmName * * @see buildXMLMessage */ -@MiraiExperimentalAPI -inline class XMLMessage(val stringValue: String) : Message, MessageContent { - override fun followedBy(tail: Message): Nothing = error("XMLMessage Message cannot be followed") - override fun toString(): String = stringValue +@SinceMirai("0.27.0") +@OptIn(MiraiExperimentalAPI::class) +class XmlMessage constructor(override val content: String) : RichMessage { + companion object Key : Message.Key + + override val serviceId: Int get() = 60 + + override fun toString(): String = content } /** * 构造一条 XML 消息 */ -@MiraiExperimentalAPI("还未支持") -inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XMLMessage = - XMLMessage(XMLMessageBuilder().apply(block).text) +@SinceMirai("0.27.0") +@MiraiExperimentalAPI +inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XmlMessage = + XmlMessage(XMLMessageBuilder().apply(block).text) -@Suppress("NOTHING_TO_INLINE") +@SinceMirai("0.27.0") @XMLDsl class ItemBuilder( var bg: Int = 0, @@ -46,21 +52,20 @@ class ItemBuilder( internal val builder: StringBuilder = StringBuilder() val text: String get() = "$builder" - inline fun summary(text: String, color: String = "#FFFFFF") { + fun summary(text: String, color: String = "#000000") { this.builder.append("$text") } - inline fun title(text: String, size: Int = 18, color: String = "#FFFFFF") { + fun title(text: String, size: Int = 25, color: String = "#000000") { this.builder.append("$text") } - inline fun picture(coverUrl: String) { + fun picture(coverUrl: String) { this.builder.append("") } } @XMLDsl -@Suppress("NOTHING_TO_INLINE") class XMLMessageBuilder( var templateId: Int = 1, var serviceId: Int = 1, @@ -70,7 +75,7 @@ class XMLMessageBuilder( */ var actionData: String = "", /** - * 摘要 + * 摘要, 在官方客户端内消息列表中显示 */ var brief: String = "", var flag: Int = 3, @@ -89,11 +94,11 @@ class XMLMessageBuilder( "" @XMLDsl - inline fun item(block: @XMLDsl ItemBuilder.() -> Unit) { + fun item(block: @XMLDsl ItemBuilder.() -> Unit) { builder.append(ItemBuilder().apply(block).text) } - inline fun source(name: String, iconURL: String = "") { + fun source(name: String, iconURL: String = "") { sourceName = name sourceIconURL = iconURL } @@ -101,4 +106,4 @@ class XMLMessageBuilder( @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPE) @DslMarker -internal annotation class XMLDsl \ No newline at end of file +annotation class XMLDsl \ No newline at end of file