Fix message length estimate again, #195

This commit is contained in:
ryoii 2020-04-05 23:27:22 +08:00
parent 215e79622c
commit fcf2f5c84c
3 changed files with 27 additions and 20 deletions

View File

@ -32,6 +32,7 @@ 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
@ -286,8 +287,10 @@ internal class GroupImpl(
throw EventCancelledException("cancelled by GroupMessageSendEvent")
}
val length = event.message.toString().length
if (!(length <= 5000 && event.message.count { it is Image } <= 50)) {
val length = event.message.estimateLength(703) // 阈值为700左右限制到3的倍数
var imageCnt = 0 // 通过下方逻辑短路延迟计算
if (length > 5000 || event.message.count { it is Image }.apply { imageCnt = this } > 50) {
throw MessageTooLargeException(
this,
message,
@ -299,13 +302,8 @@ internal class GroupImpl(
)
}
val imageCount = event.message.count { it is Image }
if (length >= 800
|| imageCount >= 4
|| (event.message.any<QuoteReply>()
&& (imageCount != 0 || length > 100))
) return bot.lowLevelSendLongGroupMessage(this.id, event.message)
if (length > 702 || imageCnt > 2)
return bot.lowLevelSendLongGroupMessage(this.id, event.message)
msg = event.message
} else msg = message.asMessageChain()

View File

@ -12,6 +12,7 @@
package net.mamoe.mirai.qqandroid.utils
import net.mamoe.mirai.message.data.*
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
@ -30,9 +31,15 @@ internal fun Int.toIpV4AddressString(): String {
}
}
/*
internal fun String.chineseLength(upTo: Int): Int {
return this.sumUpTo(upTo) { if (it in '\u0391'..'\uFFE5') 3 else 1 }
return this.sumUpTo(upTo) {
when(it) {
in '\u0000'..'\u007F' -> 1
in '\u0080'..'\u07FF' -> 2
in '\u0800'..'\uFFFF' -> 3
else -> 4
}
}
}
internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
@ -42,10 +49,8 @@ internal fun MessageChain.estimateLength(upTo: Int = Int.MAX_VALUE): Int =
internal fun SingleMessage.estimateLength(upTo: Int = Int.MAX_VALUE): Int {
return when (this) {
is QuoteReply -> {
700
}
// is Image -> 300
is QuoteReply -> 444 // Magic number
is Image -> 260 // Magic number
is PlainText -> stringValue.chineseLength(upTo)
is At -> display.chineseLength(upTo)
is AtAll -> display.chineseLength(upTo)
@ -74,4 +79,3 @@ internal inline fun CharSequence.sumUpTo(upTo: Int, selector: (Char) -> Int): In
}
return sum
}
*/

View File

@ -2,6 +2,7 @@
package net.mamoe.mirai.qqandroid.io.serialization
/*
import kotlinx.io.core.buildPacket
import kotlinx.io.core.toByteArray
import kotlinx.io.core.writeFully
@ -31,6 +32,7 @@ internal const val STRUCT_BEGIN: Byte = 10
internal const val STRUCT_END: Byte = 11
internal const val ZERO_TYPE: Byte = 12
*/
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
@ -38,7 +40,8 @@ internal const val ZERO_TYPE: Byte = 12
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
*//*
@Suppress("INVISIBLE_MEMBER") // bug
internal class JceInputTest {
init {
@ -146,10 +149,12 @@ internal class JceInputTest {
assertEquals(
TestSerializableClassA(
/*mapOf(
*/
/*mapOf(
TestSerializableClassB(123, TestSerializableClassC(123123), 9)
to TestSerializableClassC(123123)
)*/
)*//*
"1"
),
Jce.UTF_8.load(TestSerializableClassA.serializer(), input)
@ -582,4 +587,4 @@ internal class JceInputTest {
assertEquals(123456.0, input.useHead { input.readJceDoubleValue(it) })
assertEquals(true, input.useHead { input.readJceBooleanValue(it) })
}
}
}*/