diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index 8e28bcae2..1af75e985 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt @@ -140,7 +140,8 @@ internal class GroupImpl( return MiraiImpl.lowLevelSendGroupLongOrForwardMessage(bot, this.id, message.nodeList, false, message) } - val msg: MessageChain = if (message !is LongMessage && message !is ForwardMessageInternal) { + val isLongOrForward = message is LongMessage || message is ForwardMessageInternal + val msg: MessageChain = if (!isLongOrForward) { val chain = kotlin.runCatching { GroupMessagePreSendEvent(this, message).broadcast() }.onSuccess { @@ -162,7 +163,7 @@ internal class GroupImpl( imageCnt = it }) - if (length > 702 || imageCnt > 2) { // 阈值为700左右,限制到3的倍数 + if (length > 702 || imageCnt > 1) { // 阈值为700左右,限制到3的倍数 return MiraiImpl.lowLevelSendGroupLongOrForwardMessage( bot, this.id, @@ -194,7 +195,7 @@ internal class GroupImpl( } } - val result = bot.network.runCatching { + val result = bot.network.runCatching sendMsg@{ val source: OnlineMessageSourceToGroupImpl MessageSvcPbSendMsg.createToGroup( bot.client, @@ -204,6 +205,21 @@ internal class GroupImpl( ) { source = it }.sendAndExpect().let { + if (!isLongOrForward && it is MessageSvcPbSendMsg.Response.MessageTooLarge) { + return@sendMsg MiraiImpl.lowLevelSendGroupLongOrForwardMessage( + bot, + this@GroupImpl.id, + listOf( + ForwardMessage.Node( + senderId = bot.id, + time = currentTimeSeconds().toInt(), + messageChain = msg, + senderName = bot.nick + ) + ), + true, null + ) + } check(it is MessageSvcPbSendMsg.Response.SUCCESS) { "Send group message failed: $it" } diff --git a/mirai-core/src/commonMain/kotlin/contact/util.kt b/mirai-core/src/commonMain/kotlin/contact/util.kt index 3a8e893a8..a583ce536 100644 --- a/mirai-core/src/commonMain/kotlin/contact/util.kt +++ b/mirai-core/src/commonMain/kotlin/contact/util.kt @@ -154,9 +154,9 @@ internal inline fun MessageChain.verityLength( } val chain = this - val length = estimateLength(target, 5001) + val length = estimateLength(target, 15001) lengthCallback(length) - if (length > 5000 || count { it is Image }.apply { imageCntCallback(this) } > 50) { + if (length > 15000 || count { it is Image }.apply { imageCntCallback(this) } > 50) { throw MessageTooLargeException( target, message, this, "message(${ diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index 4eb21d1d3..8cef406df 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -244,7 +244,7 @@ internal suspend fun MsgComm.Msg.transform(bot: QQAndroidBot, fromSync: Boolean when (msgHead.msgType) { 33 -> bot.groupListModifyLock.withLock { msgBody.msgContent.read { - val groupUin = readUInt().toLong() + val groupUin = Mirai.calculateGroupUinByGroupCode(readUInt().toLong()) val group = bot.getGroupByUinOrNull(groupUin) ?: bot.createGroupForBot(groupUin) ?: return null discardExact(1) val joinedMemberUin = readUInt().toLong() diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt index 628ed11e7..13dc4af7c 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbSendMsg.kt @@ -46,6 +46,10 @@ internal object MessageSvcPbSendMsg : OutgoingPacketFactory Response.SUCCESS + 10 -> Response.MessageTooLarge + else -> Response.Failed( response.result, response.errtype, response.errmsg ) + } } } diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.PbPushTransMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.PbPushTransMsg.kt index b5f391b80..817fae3d9 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.PbPushTransMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.PbPushTransMsg.kt @@ -209,7 +209,7 @@ internal object OnlinePushPbPushTransMsg : A8 32 51 A1 83 3E 03 3F A2 06 B4 B4 BD A8 D5 DF 00 30 39 32 46 45 30 36 31 41 33 37 36 43 44 35 37 35 37 39 45 37 32 34 44 37 37 30 36 46 39 39 43 35 35 33 33 31 34 44 32 44 46 35 45 42 43 31 31 36 */ - readUInt().toLong() // groupUin + readUInt().toLong() // groupCode readByte().toInt() // follow type val target = readUInt().toLong() val type = readUByte().toInt() diff --git a/mirai-core/src/commonMain/kotlin/utils/type.kt b/mirai-core/src/commonMain/kotlin/utils/type.kt index fcec4e708..90b1a4f3f 100644 --- a/mirai-core/src/commonMain/kotlin/utils/type.kt +++ b/mirai-core/src/commonMain/kotlin/utils/type.kt @@ -46,10 +46,10 @@ internal fun MessageChain.estimateLength(target: ContactOrBot, upTo: Int): Int = internal fun SingleMessage.estimateLength(target: ContactOrBot, upTo: Int): Int { return when (this) { is QuoteReply -> 444 + this.source.originalMessage.estimateLength(target, upTo) // Magic number - is Image -> 40 //magic number + is Image -> 260 //Magic number is PlainText -> content.chineseLength(upTo) - is At -> 60 //magic number - is AtAll -> 60 //magic number + is At -> 60 //Magic number + is AtAll -> 60 //Magic number else -> this.toString().chineseLength(upTo) } }