From b1ad60fc1142e9b626fc2f115fa5c60530e7ba2f Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 2 Oct 2021 19:02:05 +0800 Subject: [PATCH] Fix `ForwardMessage` length check; fix #1590 --- .../commonMain/kotlin/contact/SendMessageHandler.kt | 10 +++++++++- mirai-core/src/commonMain/kotlin/contact/util.kt | 8 ++++++-- .../commonMain/kotlin/message/LongMessageInternal.kt | 4 ++-- mirai-core/src/commonMain/kotlin/utils/type.kt | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt index 3a208b534..1cfe4d919 100644 --- a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt +++ b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt @@ -100,7 +100,7 @@ internal abstract class SendMessageHandler { } if (!contains(IgnoreLengthCheck)) { - verityLength(this, contact) + verifyLength(this, contact) } this @@ -274,6 +274,14 @@ internal suspend fun SendMessageHandler.transformSpecialMessage "ForwardMessage allows up to 200 nodes, but found ${forward.nodeList.size}" ) } + val tmp = ArrayList( + forward.nodeList.sumOf { it.messageChain.size } + ) + forward.nodeList.forEach { tmp.addAll(it.messageChain) } + + // toMessageChain will lose some element + @Suppress("INVISIBLE_MEMBER") + createMessageChainImplOptimized(tmp).verifyLength(forward, contact) } val resId = getMiraiImpl().uploadMessageHighway( diff --git a/mirai-core/src/commonMain/kotlin/contact/util.kt b/mirai-core/src/commonMain/kotlin/contact/util.kt index 7900a35ac..2f9a8793c 100644 --- a/mirai-core/src/commonMain/kotlin/contact/util.kt +++ b/mirai-core/src/commonMain/kotlin/contact/util.kt @@ -33,7 +33,7 @@ internal fun Contact.logMessageSent(message: Message) { internal fun MessageChain.countImages(): Int = this.count { it is Image } -internal fun MessageChain.verityLength( +internal fun MessageChain.verifyLength( originalMessage: Message, target: Contact, ): Int { val chain = this @@ -42,7 +42,11 @@ internal fun MessageChain.verityLength( throw MessageTooLargeException( target, originalMessage, this, "message(${ - chain.joinToString("", limit = 10) + chain.joinToString("", limit = 10).let { rsp -> + if (rsp.length > 100) { + rsp.take(100) + "..." + } else rsp + } }) is too large. Allow up to 50 images or 5000 chars" ) } diff --git a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt index 768bdfe4f..5888dadac 100644 --- a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt +++ b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt @@ -177,12 +177,12 @@ internal fun RichMessage.Key.forwardMessage( when { preview.size > 4 -> { preview.take(3).joinToString("") { - """${it.xmlEnc()}""" + """${it.take(50).xmlEnc()}""" } + """...""" } else -> { preview.joinToString("") { - """${it.xmlEnc()}""" + """${it.take(50).xmlEnc()}""" } } } diff --git a/mirai-core/src/commonMain/kotlin/utils/type.kt b/mirai-core/src/commonMain/kotlin/utils/type.kt index 77ccfe7eb..17e2fb86a 100644 --- a/mirai-core/src/commonMain/kotlin/utils/type.kt +++ b/mirai-core/src/commonMain/kotlin/utils/type.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.internal.utils import net.mamoe.mirai.contact.ContactOrBot +import net.mamoe.mirai.internal.message.ForwardMessageInternal import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.toInt import net.mamoe.mirai.utils.toLongUnsigned @@ -65,6 +66,7 @@ internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int is PlainText -> content.chineseLength(upTo) is At -> 60 //Magic number is AtAll -> 60 //Magic number + is ForwardMessageInternal -> 0 // verified in SendMessageHandler.transformSpecialMessages(message: Message) else -> this.toString().chineseLength(upTo) } }