From b0e25a5cb9a6874b7a095115655b0bc16bb376b2 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 30 Sep 2021 22:40:34 +0100 Subject: [PATCH] Fix FriendNickChangedEvent, fix #1536 --- .../notice/priv/FriendNoticeProcessor.kt | 2 +- .../network/protocol/data/jce/MsgType0x210.kt | 19 +++- .../notice/processors/FriendNickChangeTest.kt | 97 +++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 mirai-core/src/commonTest/kotlin/notice/processors/FriendNickChangeTest.kt diff --git a/mirai-core/src/commonMain/kotlin/network/notice/priv/FriendNoticeProcessor.kt b/mirai-core/src/commonMain/kotlin/network/notice/priv/FriendNoticeProcessor.kt index 613d740f2..e764c91fa 100644 --- a/mirai-core/src/commonMain/kotlin/network/notice/priv/FriendNoticeProcessor.kt +++ b/mirai-core/src/commonMain/kotlin/network/notice/priv/FriendNoticeProcessor.kt @@ -206,7 +206,7 @@ internal class FriendNoticeProcessor( bot.nick = to } else { val friend = bot.getFriend(body.uin)?.impl() ?: continue - val from = bot.nick + val from = friend.nick if (from == to) continue collect(FriendNickChangedEvent(friend, from, to)) friend.info.nick = to diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/data/jce/MsgType0x210.kt b/mirai-core/src/commonMain/kotlin/network/protocol/data/jce/MsgType0x210.kt index beee7702e..d076ecfc0 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/data/jce/MsgType0x210.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/data/jce/MsgType0x210.kt @@ -10,7 +10,12 @@ package net.mamoe.mirai.internal.network.protocol.data.jce import kotlinx.serialization.Serializable +import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27 import net.mamoe.mirai.internal.utils.io.JceStruct +import net.mamoe.mirai.internal.utils.io.NestedStructure +import net.mamoe.mirai.internal.utils.io.NestedStructureDesensitizer +import net.mamoe.mirai.internal.utils.io.ProtocolStruct +import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.tars.TarsId import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY @@ -72,8 +77,18 @@ internal class MsgType0x210( @TarsId(7) @JvmField val stMsgInfo0x20: MsgType0x210SubMsgType0x20? = null, @TarsId(8) @JvmField val stMsgInfo0x1d: MsgType0x210SubMsgType0x1d? = null, @TarsId(9) @JvmField val stMsgInfo0x24: MsgType0x210SubMsgType0x24? = null, - @TarsId(10) @JvmField val vProtobuf: ByteArray = EMPTY_BYTE_ARRAY, -) : JceStruct + @NestedStructure(Deserializer::class) @TarsId(10) @JvmField val vProtobuf: ByteArray = EMPTY_BYTE_ARRAY, +) : JceStruct { + object Deserializer : NestedStructureDesensitizer { + override fun deserialize(context: MsgType0x210, byteArray: ByteArray): ProtocolStruct? { + return when (context.uSubMsgType) { + 0x27L -> byteArray.loadAs(Submsgtype0x27.SubMsgType0x27.SubMsgType0x27MsgBody.serializer()) + else -> null + } + } + + } +} @Serializable internal class MsgType0x210SubMsgType0x13( diff --git a/mirai-core/src/commonTest/kotlin/notice/processors/FriendNickChangeTest.kt b/mirai-core/src/commonTest/kotlin/notice/processors/FriendNickChangeTest.kt new file mode 100644 index 000000000..1779d2095 --- /dev/null +++ b/mirai-core/src/commonTest/kotlin/notice/processors/FriendNickChangeTest.kt @@ -0,0 +1,97 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * 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/dev/LICENSE + */ + +@file:JvmBlockingBridge + +package net.mamoe.mirai.internal.notice.processors + +import net.mamoe.kjbb.JvmBlockingBridge +import net.mamoe.mirai.event.events.FriendNickChangedEvent +import net.mamoe.mirai.internal.network.protocol.data.jce.MsgInfo +import net.mamoe.mirai.internal.network.protocol.data.jce.MsgType0x210 +import net.mamoe.mirai.internal.network.protocol.data.jce.ShareData +import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27.SubMsgType0x27 +import net.mamoe.mirai.internal.utils.io.serialization.toByteArray +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs + +internal class FriendNickChangeTest : AbstractNoticeProcessorTest() { + + @Test + suspend fun `nick changed`() { + // FriendNickChangedEvent 内容异常 https://github.com/mamoe/mirai/issues/1356 + + suspend fun runTest() = use { + + net.mamoe.mirai.internal.network.protocol.data.jce.OnlinePushPack.SvcReqPushMsg( + uin = 1230002, + uMsgTime = 1633037660, + vMsgInfos = mutableListOf( + MsgInfo( + lFromUin = 1230002, + shMsgType = 528, + shMsgSeq = 142, + strMsg = "", + uRealMsgTime = 160, + vMsg = MsgType0x210( + uSubMsgType = 39, + vProtobuf = SubMsgType0x27.SubMsgType0x27MsgBody( + msgModInfos = mutableListOf( + SubMsgType0x27.ForwardBody( + opType = 20, + msgModProfile = SubMsgType0x27.ModProfile( + uin = 1230001, + msgProfileInfos = mutableListOf( + SubMsgType0x27.ProfileInfo( + field = 20002, + value = "ABC", + ) + ), + ), + ) + ), + ) + .toByteArray(SubMsgType0x27.SubMsgType0x27MsgBody.serializer()), + ).toByteArray(), + uAppShareID = 0, + vMsgCookies = "08 90 04 10 90 84 A0 81 80 80 80 80 02 18 00 20 E3 86 03".hexToBytes(), + lMsgUid = 14411, + lLastChangeTime = 1, + vCPicInfo = mutableListOf(), + stShareData = ShareData( + ), + lFromInstId = 0, + vRemarkOfSender = net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY, + strFromMobile = "", + strFromName = "", + vNickName = mutableListOf(), + ) + ), + svrip = 1273521418, + ) + + } + + setBot(1230002).apply { + addFriend(1230001, nick = "aaa") + } + + runTest().toList().run { + assertEquals(1, size, toString()) + get(0).run { + assertIs(this) + assertEquals(1230001, friend.id) + assertEquals("aaa", from) + assertEquals("ABC", to) + } + } + + } +} \ No newline at end of file