Sfoglia il codice sorgente

Add qAge and personalStatement for Profile

Him188 6 anni fa
parent
commit
eb1da6a326

+ 4 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt

@@ -193,11 +193,14 @@ enum class MemberPermission {
 data class Profile(
     val qq: UInt,
     val nickname: String,
+    val englishName: String?,
+    val chineseName: String?,
+    val qAge: Int?, // q 龄
     val zipCode: String?,
     val phone: String?,
     val gender: Gender,
     val birthday: Date?,
-    val personalStatus: String?,
+    val personalStatement: String?,// 个人说明
     val school: String?,
     val homepage: String?,
     val email: String?,

+ 6 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt

@@ -60,9 +60,14 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
         val qq = readUInt()
         discardExact(6)
         val map = readTLVMap(tagSize = 2, expectingEOF = true)
+        map.printTLVMap("Profile(qq=$qq) raw=")
+        map.mapValues { it.value.encodeToString() }.printTLVMap("Profile(qq=$qq) str=")
         val profile = Profile(
             qq = qq,
             nickname = map[0x4E22u]?.encodeToString() ?: "",//error("Cannot determine nickname")
+            englishName = map[0x4E54u]?.encodeToString(),
+            chineseName = map[0x4E2Au]?.encodeToString(),
+            qAge = map[0x6597u]?.toUInt()?.toInt(),
             zipCode = map[0x4E25u]?.encodeToString(),
             phone = map[0x4E27u]?.encodeToString(),
             gender = when (map[0x4E29u]?.let { it[0] }?.toUInt()) {
@@ -73,7 +78,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
                 //else -> error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}")
             },
             birthday = map[0x4E3Fu]?.let { Date(it.toUInt().toInt()) },
-            personalStatus = map[0x4E33u]?.encodeToString(),
+            personalStatement = map[0x4E33u]?.encodeToString(),
             homepage = map[0x4E2Du]?.encodeToString(),
             company = map[0x5DC8u]?.encodeToString(),
             school = map[0x4E35u]?.encodeToString(),

+ 6 - 2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt

@@ -66,8 +66,12 @@ fun Input.readTLVMap(expectingEOF: Boolean = false, tagSize: Int = 1): MutableMa
     return map
 }
 
-fun Map<*, ByteArray>.printTLVMap(name: String) =
-    debugPrintln("TLVMap $name= " + this.mapValues { (_, value) -> value.toUHexString() })
+fun Map<UInt, ByteArray>.printTLVMap(name: String) =
+    debugPrintln("TLVMap $name= " + this.mapValues { (_, value) -> value.toUHexString() }.mapKeys { it.key.toInt().toUShort().toUHexString() })
+
+@JvmName("printTLVStringMap")
+fun Map<UInt, String>.printTLVMap(name: String) =
+    debugPrintln("TLVMap $name= " + this.mapKeys { it.key.toInt().toUShort().toUHexString() })
 
 fun Input.readString(length: Int): String = String(this.readBytes(length))
 fun Input.readString(length: Long): String = String(this.readBytes(length.toInt()))

+ 4 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt

@@ -120,7 +120,10 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String =
  * 本函数将 4 个 [Byte] 的 bits 连接得到 [Int]
  */
 fun ByteArray.toUInt(): UInt =
-    this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0)
+    (this[0].toUInt().and(255u) shl 24) + (this[1].toUInt().and(255u) shl 16) + (this[2].toUInt().and(255u) shl 8) + (this[3].toUInt().and(255u) shl 0)
+
+fun ByteArray.toUShort(): UShort =
+    ((this[0].toUInt().and(255u) shl 8) + (this[1].toUInt().and(255u) shl 0)).toUShort()
 
 /**
  * 从 [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入.

+ 8 - 6
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt

@@ -7,13 +7,10 @@ import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
-import net.mamoe.mirai.Bot
-import net.mamoe.mirai.BotAccount
-import net.mamoe.mirai.addFriend
+import net.mamoe.mirai.*
 import net.mamoe.mirai.event.Subscribable
 import net.mamoe.mirai.event.subscribeAlways
 import net.mamoe.mirai.event.subscribeMessages
-import net.mamoe.mirai.login
 import net.mamoe.mirai.message.Image
 import net.mamoe.mirai.network.protocol.tim.packet.action.downloadAsByteArray
 import net.mamoe.mirai.network.protocol.tim.packet.event.FriendMessage
@@ -55,8 +52,13 @@ suspend fun main() {
     bot.subscribeMessages {
         "你好" reply "你好!"
 
-        "profile" reply {
-            sender.profile.await().toString()
+        startsWith("profile", removePrefix = true) {
+            val account = it.trim()
+            if (account.isNotEmpty()) {
+                bot.getQQ(account.toUInt())
+            } else {
+                sender
+            }.profile.await().toString()
         }
 
         has<Image> {