From cd146e597664f6207b0109808fd024af54cadd42 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 2 Feb 2021 18:07:18 +0800 Subject: [PATCH] Fix forward and long detect --- mirai-core/src/commonMain/kotlin/MiraiImpl.kt | 2 +- .../src/commonMain/kotlin/QQAndroidBot.kt | 2 +- .../kotlin/message/LongMessageInternal.kt | 2 +- .../kotlin/message/ReceiveMessageHandler.kt | 25 +++++++++++-------- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt index 36e959254..6b6aac805 100644 --- a/mirai-core/src/commonMain/kotlin/MiraiImpl.kt +++ b/mirai-core/src/commonMain/kotlin/MiraiImpl.kt @@ -966,7 +966,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor { override suspend fun downloadLongMessage(bot: Bot, resourceId: String): MessageChain { bot.asQQAndroidBot() - when (val resp = MultiMsg.ApplyDown(bot.client, 1, resourceId, 1).sendAndExpect(bot)) { + when (val resp = MultiMsg.ApplyDown(bot.client, 2, resourceId, 1).sendAndExpect(bot)) { is MultiMsg.ApplyDown.Response.RequireDownload -> { val http = Mirai.Http val origin = resp.origin diff --git a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt index ec53ede39..3f6d1012a 100644 --- a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt +++ b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt @@ -202,5 +202,5 @@ internal fun RichMessage.Key.forwardMessage( """.trimIndent().replace("\n", " ") - return ForwardMessageInternal(template) + return ForwardMessageInternal(template, resId) } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt index 3c6cc9b40..183f59d16 100644 --- a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt +++ b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt @@ -32,7 +32,7 @@ internal data class LongMessageInternal internal constructor(override val conten } // internal runtime value, not serializable -internal data class ForwardMessageInternal(override val content: String) : AbstractServiceMessage(), RefinableMessage { +internal data class ForwardMessageInternal(override val content: String, val resId: String) : AbstractServiceMessage(), RefinableMessage { override val serviceId: Int get() = 35 override suspend fun refine(contact: Contact, context: MessageChain): Message { diff --git a/mirai-core/src/commonMain/kotlin/message/ReceiveMessageHandler.kt b/mirai-core/src/commonMain/kotlin/message/ReceiveMessageHandler.kt index 1b1e68f7d..ca4dc75e3 100644 --- a/mirai-core/src/commonMain/kotlin/message/ReceiveMessageHandler.kt +++ b/mirai-core/src/commonMain/kotlin/message/ReceiveMessageHandler.kt @@ -118,12 +118,10 @@ private object ReceiveMessageTransformer { builder: MessageChainBuilder ) { // (this._miraiContentToString().soutv()) - val generalFlags = elements.find { it.generalFlags != null }?.generalFlags - for (element in elements) { transformElement(element, groupIdOrZero, messageSourceKind, botId, builder) when { - element.richMsg != null -> decodeRichMessage(generalFlags, element.richMsg, builder) + element.richMsg != null -> decodeRichMessage(element.richMsg, builder) } } } @@ -359,7 +357,6 @@ private object ReceiveMessageTransformer { } private fun decodeRichMessage( - generalFlags: ImMsgBody.GeneralFlags?, richMsg: ImMsgBody.RichMsg, builder: MessageChainBuilder ) { @@ -388,13 +385,21 @@ private object ReceiveMessageTransformer { * [LongMessageInternal], [ForwardMessage] */ 35 -> { - val resId = generalFlags?.longTextResid - - if (resId != null) { - builder.add(LongMessageInternal(content, resId)) - } else { - builder.add(ForwardMessageInternal(content)) + fun findStringProperty(name: String): String { + return content.substringAfter("$name=\"", "").substringBefore("\"", "") } + + val resId = findStringProperty("m_resid") + + val msg = when(findStringProperty("multiMsgFlag").toIntOrNull()) { + 1 -> LongMessageInternal(content, resId) + 0 -> ForwardMessageInternal(content, resId) + else -> { + SimpleServiceMessage(35, content) + } + } + + builder.add(msg) } // 104 新群员入群的消息