diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt index 687f4ef0b..f6b46ddbc 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/QQAndroidBot.kt @@ -402,7 +402,12 @@ internal abstract class QQAndroidBotBase constructor( val resId: String when (response) { is MultiMsg.ApplyUp.Response.MessageTooLarge -> - error("message is too large") + error( + "Internal error: message is too large, but this should be handled before sending. Message content:" + + message.joinToString { + "${it::class.simpleName}(l=${it.toString().length})" + } + ) is MultiMsg.ApplyUp.Response.RequireUpload -> { resId = response.proto.msgResid diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 43af7cae5..cd874bae5 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -30,7 +30,6 @@ import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc -import net.mamoe.mirai.qqandroid.utils.estimateLength import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.utils.* import kotlin.contracts.ExperimentalContracts @@ -285,7 +284,7 @@ internal class GroupImpl( throw EventCancelledException("cancelled by GroupMessageSendEvent") } - val length = event.message.estimateLength(5001) + val length = event.message.toString().length if (!(length <= 5000 && event.message.count { it is Image } <= 50)) { throw MessageTooLargeException( this, @@ -294,10 +293,17 @@ internal class GroupImpl( "message(${event.message.joinToString( "", limit = 10 - )}) is too large. Allow up to 5000 in weight (Chinese char=4, English char=1, Quote=700, Image=800, others are estimated in String length.)" + )}) is too large. Allow up to 50 images or 5000 chars" ) } - if (length >= 800) { + + val imageCount = event.message.count { it is Image } + + if (length >= 800 + || imageCount >= 4 + || (event.message.any() + && (imageCount != 0 || length > 100)) + ) { return bot.lowLevelSendLongGroupMessage(this.id, event.message) } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/MultiMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/MultiMsg.kt index 35f1daa37..4ef576578 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/MultiMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/MultiMsg.kt @@ -49,7 +49,7 @@ internal fun MessageChain.calculateValidationDataForGroup( groupCode: Long, botMemberNameCard: String ): MessageValidationData { - val richTextElems = this.toRichTextElems(true, false) + val richTextElems = this.toRichTextElems(forGroup = true, withGeneralFlags = false) val msgTransmit = MsgTransmit.PbMultiMsgTransmit( msg = listOf( diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt index 23df48254..854f69944 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/type.kt @@ -33,7 +33,7 @@ internal fun Int.toIpV4AddressString(): String { } internal fun String.chineseLength(upTo: Int): Int { - return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 4 else 1 } + return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 } } internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int = @@ -47,7 +47,7 @@ internal fun SingleMessage.estimateLength(upTo: Int = Int.MAX_VALUE): Int { is QuoteReply -> { 700 + source.originalMessage.estimateLength(upTo) } - is Image -> 300 + // is Image -> 300 is PlainText -> stringValue.chineseLength(upTo) is At -> display.chineseLength(upTo) is AtAll -> display.chineseLength(upTo)