xml and Json messages support

This commit is contained in:
Him188 2020-03-08 22:05:08 +08:00
parent 21abfe4a64
commit 584b5e9822
5 changed files with 92 additions and 35 deletions

View File

@ -273,9 +273,9 @@ internal fun MessageChain.toRichTextElems(forGroup: Boolean): MutableList<ImMsgB
}
this.forEach(::transformOneMessage)
// if(this.any<QuoteReply>()){
elements.add(ImMsgBody.Elem(generalFlags = ImMsgBody.GeneralFlags(pbReserve = "78 00 F8 01 00 C8 02 00".hexToBytes())))
// }
if (this.any<RichMessage>()) {
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<ImMsgBody.Elem>.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 { "<failed>" }
}"
"unknown richMsg.serviceId: ${it.richMsg.serviceId}, content=${it.richMsg.template1.contentToString()}, \ntryUnzip=${content}"
}
}
}
}
else -> {
println(it._miraiContentToString())
}
}
}

View File

@ -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<JsonMessage>
override val serviceId: Int
get() = 1
override fun toString(): String = content
}

View File

@ -61,7 +61,7 @@ interface MessageChain : Message, Iterable<SingleMessage> {
fun <M : Message> getOrNull(key: Message.Key<M>): 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<SingleMessage> {
}
/**
* 遍历每一个消息, [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<SingleMessage> {
// 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 <M : Message> MessageChain.firstOrNull(key: Message.Key<M>): M? = when (key)
Face -> first<Face>()
QuoteReply -> first<QuoteReply>()
MessageSource -> first<MessageSource>()
XmlMessage -> first<XmlMessage>()
JsonMessage -> first<JsonMessage>()
RichMessage -> first<RichMessage>()
else -> null
} as M?

View File

@ -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<RichMessage>
@MiraiExperimentalAPI
val serviceId: Int
@MiraiExperimentalAPI
val content: String
}

View File

@ -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<XmlMessage>
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() = "<item bg='$bg' layout='$layout'>$builder</item>"
inline fun summary(text: String, color: String = "#FFFFFF") {
fun summary(text: String, color: String = "#000000") {
this.builder.append("<summary color='$color'>$text</summary>")
}
inline fun title(text: String, size: Int = 18, color: String = "#FFFFFF") {
fun title(text: String, size: Int = 25, color: String = "#000000") {
this.builder.append("<title size='$size' color='$color'>$text</title>")
}
inline fun picture(coverUrl: String) {
fun picture(coverUrl: String) {
this.builder.append("<picture cover='$coverUrl'/>")
}
}
@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(
"</msg>"
@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
annotation class XMLDsl