Browse Source

Fix `ForwardMessage` length check; fix #1590

Karlatemp 4 years ago
parent
commit
b1ad60fc11

+ 9 - 1
mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt

@@ -100,7 +100,7 @@ internal abstract class SendMessageHandler<C : Contact> {
                 }
 
                 if (!contains(IgnoreLengthCheck)) {
-                    verityLength(this, contact)
+                    verifyLength(this, contact)
                 }
 
                 this
@@ -274,6 +274,14 @@ internal suspend fun <C : Contact> SendMessageHandler<C>.transformSpecialMessage
                     "ForwardMessage allows up to 200 nodes, but found ${forward.nodeList.size}"
                 )
             }
+            val tmp = ArrayList<SingleMessage>(
+                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(

+ 6 - 2
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"
         )
     }

+ 2 - 2
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("") {
-                    """<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.xmlEnc()}</title>"""
+                    """<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.take(50).xmlEnc()}</title>"""
                 } + """<title size="26" color="#777777" maxLines="2" lineSpace="12">...</title>"""
             }
             else -> {
                 preview.joinToString("") {
-                    """<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.xmlEnc()}</title>"""
+                    """<title size="26" color="#777777" maxLines="2" lineSpace="12">${it.take(50).xmlEnc()}</title>"""
                 }
             }
         }

+ 2 - 0
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<C>.transformSpecialMessages(message: Message)
         else -> this.toString().chineseLength(upTo)
     }
 }