From 837bae41fcb205f3ac45896c83e50ec24318e605 Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Wed, 13 Jan 2021 18:53:00 +0800 Subject: [PATCH 1/3] Fix Long message error (#857) * Fix #195 again, also fix #539 * Let AtAll consistent with At Co-authored-by: Him188 * Change Member to NormalMember for capable events * Make StrangerRelationChangeEvent sealed * 2.0-RC * Fix dokka * Fix #195 again Co-authored-by: Him188 --- mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index 8e28bcae2..24ed7075d 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt @@ -162,7 +162,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, From 72ca12a347301130b88a47852f0bd0ca7f1c64d3 Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Wed, 13 Jan 2021 21:45:35 +0800 Subject: [PATCH 2/3] Fix long message error again (#858) * Fix #195 again, also fix #539 * Let AtAll consistent with At Co-authored-by: Him188 * Change Member to NormalMember for capable events * Make StrangerRelationChangeEvent sealed * 2.0-RC * Fix dokka * Fix #195 again * Revert Image length to 260 and increase limit to 15000 * Add fallback measure to group message * Remove unnecessary log Co-authored-by: Him188 Co-authored-by: Him188 --- .../commonMain/kotlin/contact/GroupImpl.kt | 20 +++++++++++++++++-- .../src/commonMain/kotlin/contact/util.kt | 4 ++-- .../chat/receive/MessageSvc.PbSendMsg.kt | 13 ++++++++---- .../src/commonMain/kotlin/utils/type.kt | 6 +++--- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupImpl.kt index 24ed7075d..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 { @@ -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.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/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) } } From 3cc3f52e6961151d242d17118ccaa53291f1f80f Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Wed, 13 Jan 2021 21:46:16 +0800 Subject: [PATCH 3/3] Fix #856, also fix wrong comment in PbPushTransMsg.kt (#859) --- .../network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt | 2 +- .../protocol/packet/chat/receive/OnlinePush.PbPushTransMsg.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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()