From d95a54734724694a6c10dfa7c74e9939d8eaf2d8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 8 Nov 2019 23:20:11 +0800 Subject: [PATCH] Adjustments --- .../commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt | 6 +++--- .../network/protocol/tim/TIMBotNetworkHandler.kt | 6 +++--- .../network/protocol/tim/handler/ActionPacketHandler.kt | 2 +- .../network/protocol/tim/packet/action/AddContact.kt | 6 +++--- .../network/protocol/tim/packet/action/GradeInfo.kt | 3 ++- .../network/protocol/tim/packet/action/Profile.kt | 6 ++++-- .../network/protocol/tim/packet/action/UploadImage.kt | 2 +- .../protocol/tim/packet/event/FriendConversationIniliaze.kt | 2 +- .../network/protocol/tim/packet/event/PhoneMessage.kt | 4 ++++ .../network/protocol/tim/packet/event/Unknown.kt | 5 +++-- .../kotlin/net.mamoe.mirai/utils/io/InputUtils.kt | 6 ++++++ 11 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/PhoneMessage.kt diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt index 04b0d33b8..962aed329 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt @@ -62,7 +62,7 @@ class BotSession( /** * 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P]. - * 这个方法会立即返回. + * 这个方法会立即发出这个数据包然后返回一个 [CompletableDeferred]. * * 实现方法: * ```kotlin @@ -74,9 +74,9 @@ class BotSession( * ``` * @sample net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage * - * @param checkSequence 是否期待 [ServerPacket.sequenceId] 与 [OutgoingPacket.sequenceId] 相同的包. + * @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包. * @param P 期待的包 - * @param handler 处理期待的包. 将会在调用 [sendAndExpect] 的函数所在 [coroutineContext] 下执行. + * @param handler 处理期待的包. 将会在调用本函数的 [coroutineContext] 下执行. * * @see Bot.withSession 转换接收器 (receiver, 即 `this` 的指向) 为 [BotSession] */ diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt index 605c98d35..3b1c078e7 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt @@ -233,6 +233,9 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot packet: TPacket, factory: PacketFactory ) { + if (ServerPacketReceivedEvent(bot, packet).broadcast().cancelled) + return + if (!packet::class.annotations.filterIsInstance().any()) { bot.logger.verbose("Packet received: $packet") } @@ -250,9 +253,6 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot it.doReceiveWithoutExceptions(packet) } - if (ServerPacketReceivedEvent(bot, packet).broadcast().cancelled) - return - if (factory is SessionPacketFactory<*>) { with(factory as SessionPacketFactory) { handlePacket(packet) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt index 3bcfbe4ea..e8f0a6628 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.withContext import net.mamoe.mirai.network.BotSession import net.mamoe.mirai.network.isOpen import net.mamoe.mirai.network.protocol.tim.packet.Packet -import net.mamoe.mirai.network.protocol.tim.packet.RequestAccountInfoPacket +import net.mamoe.mirai.network.protocol.tim.packet.action.RequestAccountInfoPacket import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket import net.mamoe.mirai.network.protocol.tim.packet.login.SKey import net.mamoe.mirai.network.qqAccount diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt index b80197498..d210fe288 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt @@ -10,8 +10,8 @@ import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol import net.mamoe.mirai.network.protocol.tim.packet.* import net.mamoe.mirai.network.protocol.tim.packet.action.CanAddFriendResponse.State +import net.mamoe.mirai.network.protocol.tim.packet.event.EventPacket import net.mamoe.mirai.utils.io.* -import kotlin.properties.Delegates // 01BC 曾用名查询. 查到的是这个人的 @@ -119,8 +119,8 @@ object CanAddFriendPacket : SessionPacketFactory() { } -class CanAddFriendResponse : Packet { - var qq: UInt by Delegates.notNull() +class CanAddFriendResponse : EventPacket { + var qq: UInt = 0u lateinit var state: State enum class State { diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GradeInfo.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GradeInfo.kt index 80c4ffa8f..6f6fef729 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GradeInfo.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GradeInfo.kt @@ -1,11 +1,12 @@ @file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") -package net.mamoe.mirai.network.protocol.tim.packet +package net.mamoe.mirai.network.protocol.tim.packet.action import kotlinx.io.core.ByteReadPacket import kotlinx.io.core.writeUByte import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.protocol.tim.TIMProtocol +import net.mamoe.mirai.network.protocol.tim.packet.* import net.mamoe.mirai.utils.io.encryptAndWrite import net.mamoe.mirai.utils.io.writeHex import net.mamoe.mirai.utils.io.writeQQ diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt index fea1e19e5..23205a2ae 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt @@ -34,6 +34,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory): RequestProfileDetailsResponse = RequestProfileDetailsResponse().apply { discardExact(3) @@ -59,8 +61,8 @@ object RequestProfileDetailsPacket : SessionPacketFactory error("Cannot determine gender, entry 0x4E29u not found") 0x02u -> Gender.FEMALE 0x01u -> Gender.MALE - 0x00u -> Gender.SECRET // 猜的 - else -> error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}") + else -> Gender.SECRET // 猜的 + //else -> error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}") }, birthday = map[0x4E3Fu]?.let { Date(it.toUInt().toInt()) } ) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt index e02a03bc3..9c37f34c8 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt @@ -338,7 +338,7 @@ object GroupImageIdRequestPacket : SessionPacketFactory(0x0079u) { override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): FriendConversationInitialize { discardExact(4)// 00 00 00 00 diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/PhoneMessage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/PhoneMessage.kt new file mode 100644 index 000000000..6e062e25e --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/PhoneMessage.kt @@ -0,0 +1,4 @@ +package net.mamoe.mirai.network.protocol.tim.packet.event + +//来自 Android 给我的电脑发消息, ID 0211, 内容 "你好" +//00 00 00 20 00 05 00 02 00 01 00 06 00 04 00 01 01 01 00 09 00 06 03 E9 20 02 EB 94 00 0A 00 04 01 00 00 00 00 00 00 54 00 00 00 3E 08 12 1A 16 08 07 10 91 04 18 DD F1 92 B7 07 20 83 76 38 DD F1 92 B7 07 50 04 42 21 08 DD F1 92 B7 07 10 DD F1 92 B7 07 18 01 20 07 40 DC 85 96 EE 05 48 DC 85 96 EE 05 50 FA AF FD 18 52 15 0A 06 08 01 10 00 50 01 1A 0B 08 E9 07 10 94 D7 8B 80 02 50 02 08 04 12 1D 08 E9 07 10 94 D7 8B 80 02 18 01 20 01 28 DD F1 92 B7 07 30 DD F1 92 B7 07 48 02 50 01 32 1B 08 80 80 B8 AE B5 02 10 01 18 00 20 01 2A 0C 0A 0A 08 01 12 06 E4 BD A0 E5 A5 BD \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt index f3b83a898..c4b808f4d 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt @@ -13,9 +13,10 @@ import net.mamoe.mirai.utils.io.toUHexString data class UnknownEventPacket( val id: UShort, + val identity: EventPacketIdentity, val body: ByteReadPacket ) : EventPacket { - override fun toString(): String = "UnknownEventPacket(id=${id.toUHexString()})" + override fun toString(): String = "UnknownEventPacket(id=${id.toUHexString()}, identity=$identity)" } //TODO This class should be declared with `inline`, but a CompilationException will be thrown @@ -23,7 +24,7 @@ class UnknownEventParserAndHandler(override val id: UShort) : EventParserAndHand override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): UnknownEventPacket { MiraiLogger.debug("UnknownEventPacket(${id.toUHexString()}) = ${readBytes().toUHexString()}") - return UnknownEventPacket(id, this) //TODO the cause is that `this` reference. + return UnknownEventPacket(id, identity, this) //TODO the cause is that `this` reference. } override suspend fun BotNetworkHandler<*>.handlePacket(packet: UnknownEventPacket) { diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt index c8de96fb3..64030753a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt @@ -49,6 +49,12 @@ fun Input.readTLVMap(expectingEOF: Boolean = false, tagSize: Int = 1): MutableMa type }.toUByte() != UByte.MAX_VALUE) { + check(!map.containsKey(type.toUInt())) { + "Count not readTLVMap: duplicated key 0x${type.toUInt().toUHexString("")}. " + + "map=$map" + + ", duplicating value=${this.readUShortLVByteArray()}" + + ", remaining=" + if (expectingEOF) this.readBytes().toUHexString() else "[Not expecting EOF]" + } map[type.toUInt()] = this.readUShortLVByteArray() } return map