Parse anonymous chat, close #277

This commit is contained in:
ryoii 2020-04-26 16:51:28 +08:00
parent 6731aad521
commit 73923d46f6
3 changed files with 52 additions and 18 deletions

View File

@ -28,10 +28,12 @@ import net.mamoe.mirai.qqandroid.message.MessageSourceToGroupImpl
import net.mamoe.mirai.qqandroid.message.ensureSequenceIdAvailable import net.mamoe.mirai.qqandroid.message.ensureSequenceIdAvailable
import net.mamoe.mirai.qqandroid.message.firstIsInstanceOrNull import net.mamoe.mirai.qqandroid.message.firstIsInstanceOrNull
import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper import net.mamoe.mirai.qqandroid.network.highway.HighwayHelper
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.TroopManagement
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.ImgStore
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.network.protocol.packet.list.ProfileService import net.mamoe.mirai.qqandroid.network.protocol.packet.list.ProfileService
import net.mamoe.mirai.qqandroid.utils.encodeToString
import net.mamoe.mirai.qqandroid.utils.estimateLength import net.mamoe.mirai.qqandroid.utils.estimateLength
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
@ -252,6 +254,16 @@ internal class GroupImpl(
) )
} }
internal fun newAnonymous(name: String): Member = newMember(
object : MemberInfo {
override val nameCard = name
override val permission = MemberPermission.MEMBER
override val specialTitle = "匿名"
override val muteTimestamp = 0
override val uin = 80000000L
override val nick = name
}
)
override operator fun get(id: Long): Member { override operator fun get(id: Long): Member {
if (id == bot.id) { if (id == bot.id) {

View File

@ -12,18 +12,23 @@
package net.mamoe.mirai.qqandroid.message package net.mamoe.mirai.qqandroid.message
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.LowLevelAPI
import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.data.MemberInfo
import net.mamoe.mirai.event.internal.MiraiAtomicBoolean import net.mamoe.mirai.event.internal.MiraiAtomicBoolean
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.MessageSource
import net.mamoe.mirai.message.data.OnlineMessageSource import net.mamoe.mirai.message.data.OnlineMessageSource
import net.mamoe.mirai.qqandroid.contact.GroupImpl
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.qqandroid.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgComm import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.SourceMsg import net.mamoe.mirai.qqandroid.network.protocol.data.proto.SourceMsg
import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.utils._miraiContentToString import net.mamoe.mirai.qqandroid.utils._miraiContentToString
import net.mamoe.mirai.qqandroid.utils.encodeToString
import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray
internal interface MessageSourceInternal { internal interface MessageSourceInternal {
@ -141,13 +146,19 @@ internal data class MessageSourceFromGroupImpl(
override val originalMessage: MessageChain by lazy { override val originalMessage: MessageChain by lazy {
msg.toMessageChain(bot, groupIdOrZero = group.id, onlineSource = false) msg.toMessageChain(bot, groupIdOrZero = group.id, onlineSource = false)
} }
override val sender: Member
get() = bot.getGroup( override val sender: Member by lazy {
(bot.getGroup(
msg.msgHead.groupInfo?.groupCode msg.msgHead.groupInfo?.groupCode
?: error("cannot find groupCode for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}") ?: error("cannot find groupCode for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}")
).getOrNull(msg.msgHead.fromUin) ) as GroupImpl).run {
getOrNull(msg.msgHead.fromUin)
?: msg.msgBody.richText.elems.firstOrNull { it.anonGroupMsg != null }?.run {
newAnonymous(anonGroupMsg!!.anonNick.encodeToString())
}
?: error("cannot find member for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}") ?: error("cannot find member for MessageSourceFromGroupImpl. msg=${msg._miraiContentToString()}")
}
}
override fun toJceData(): ImMsgBody.SourceMsg { override fun toJceData(): ImMsgBody.SourceMsg {
return ImMsgBody.SourceMsg( return ImMsgBody.SourceMsg(

View File

@ -69,9 +69,6 @@ internal class OnlinePush {
if (!bot.firstLoginSucceed) return null if (!bot.firstLoginSucceed) return null
val pbPushMsg = readProtoBuf(MsgOnlinePush.PbPushMsg.serializer()) val pbPushMsg = readProtoBuf(MsgOnlinePush.PbPushMsg.serializer())
val extraInfo: ImMsgBody.ExtraInfo? =
pbPushMsg.msg.msgBody.richText.elems.firstOrNull { it.extraInfo != null }?.extraInfo
if (pbPushMsg.msg.msgHead.fromUin == bot.id) { if (pbPushMsg.msg.msgHead.fromUin == bot.id) {
return SendGroupMessageReceipt( return SendGroupMessageReceipt(
pbPushMsg.msg.msgBody.richText.attr!!.random, pbPushMsg.msg.msgBody.richText.attr!!.random,
@ -79,9 +76,22 @@ internal class OnlinePush {
) )
} }
val group = bot.getGroupOrNull(pbPushMsg.msg.msgHead.groupInfo!!.groupCode) ?: return null // 机器人还正在进群 val extraInfo: ImMsgBody.ExtraInfo? =
val sender = group[pbPushMsg.msg.msgHead.fromUin] as MemberImpl pbPushMsg.msg.msgBody.richText.elems.firstOrNull { it.extraInfo != null }?.extraInfo
val name = extraInfo?.groupCard?.takeIf { it.isNotEmpty() }?.run {
val anonymous = pbPushMsg.msg.msgBody.richText.elems.firstOrNull { it.anonGroupMsg != null }?.anonGroupMsg
val group = bot.getGroupOrNull(pbPushMsg.msg.msgHead.groupInfo!!.groupCode) as GroupImpl ?: return null // 机器人还正在进群
val sender = if (anonymous != null) {
group.newAnonymous(anonymous.anonNick.encodeToString())
} else {
group[pbPushMsg.msg.msgHead.fromUin]
} as MemberImpl
val name = if (anonymous != null) {
sender.nameCard
} else {
extraInfo?.groupCard?.takeIf { it.isNotEmpty() }?.run {
kotlin.runCatching { kotlin.runCatching {
if (this[0] == 0x0A.toByte()) if (this[0] == 0x0A.toByte())
loadAs(Oidb0x8fc.CommCardNameBuf.serializer()).richCardName?.joinToString("") { it.text.encodeToString() } loadAs(Oidb0x8fc.CommCardNameBuf.serializer()).richCardName?.joinToString("") { it.text.encodeToString() }
@ -89,6 +99,7 @@ internal class OnlinePush {
}.getOrNull() ?: encodeToString() }.getOrNull() ?: encodeToString()
} ?: pbPushMsg.msg.msgHead.groupInfo.groupCard.takeIf { it.isNotEmpty() } } ?: pbPushMsg.msg.msgHead.groupInfo.groupCard.takeIf { it.isNotEmpty() }
?: sender.nameCardOrNick // 没有 extraInfo 就从 head 里取 ?: sender.nameCardOrNick // 没有 extraInfo 就从 head 里取
}
val flags = extraInfo?.flags ?: 0 val flags = extraInfo?.flags ?: 0
return GroupMessage( return GroupMessage(