Fix ForwardMessage length check; fix #1590

This commit is contained in:
Karlatemp 2021-10-02 19:02:05 +08:00
parent c1932559a7
commit b1ad60fc11
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
4 changed files with 19 additions and 5 deletions

View File

@ -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(

View File

@ -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"
)
}

View File

@ -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>"""
}
}
}

View File

@ -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)
}
}