This commit is contained in:
Him188 2020-03-04 09:27:37 +08:00
parent b9d5fa4c14
commit 095feaf5cb
2 changed files with 70 additions and 71 deletions

View File

@ -36,6 +36,8 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.io.toUHexString import net.mamoe.mirai.utils.io.toUHexString
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import net.mamoe.mirai.qqandroid.network.protocol.data.jce.FriendInfo as JceFriendInfo import net.mamoe.mirai.qqandroid.network.protocol.data.jce.FriendInfo as JceFriendInfo
@ -356,10 +358,14 @@ internal class MemberInfoImpl(
override val muteTimestamp: Int = jceInfo.dwShutupTimestap?.toInt() ?: 0 override val muteTimestamp: Int = jceInfo.dwShutupTimestap?.toInt() ?: 0
} }
/** @OptIn(ExperimentalContracts::class)
* 对GroupImpl internal fun GroupImpl.Companion.checkIsInstance(expression: Boolean) {
* 中name/announcement的更改会直接向服务器异步汇报 contract {
*/ returns() implies expression
}
check(expression) { "group is not an instanceof GroupImpl!! DO NOT interlace two or more protocol implementations!!" }
}
@Suppress("PropertyName") @Suppress("PropertyName")
@OptIn(MiraiInternalAPI::class) @OptIn(MiraiInternalAPI::class)
internal class GroupImpl( internal class GroupImpl(
@ -368,6 +374,10 @@ internal class GroupImpl(
groupInfo: GroupInfo, groupInfo: GroupInfo,
members: Sequence<MemberInfo> members: Sequence<MemberInfo>
) : Group() { ) : Group() {
companion object {
}
override val bot: QQAndroidBot by bot.unsafeWeakRef() override val bot: QQAndroidBot by bot.unsafeWeakRef()
val uin: Long = groupInfo.uin val uin: Long = groupInfo.uin

View File

@ -22,6 +22,7 @@ import net.mamoe.mirai.message.GroupMessage
import net.mamoe.mirai.qqandroid.GroupImpl import net.mamoe.mirai.qqandroid.GroupImpl
import net.mamoe.mirai.qqandroid.MemberImpl import net.mamoe.mirai.qqandroid.MemberImpl
import net.mamoe.mirai.qqandroid.QQAndroidBot import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.checkIsInstance
import net.mamoe.mirai.qqandroid.io.serialization.decodeUniPacket import net.mamoe.mirai.qqandroid.io.serialization.decodeUniPacket
import net.mamoe.mirai.qqandroid.io.serialization.readProtoBuf import net.mamoe.mirai.qqandroid.io.serialization.readProtoBuf
import net.mamoe.mirai.qqandroid.message.toMessageChain import net.mamoe.mirai.qqandroid.message.toMessageChain
@ -111,7 +112,9 @@ internal class OnlinePush {
val group = bot.getGroupByUin(content.fromUin) as GroupImpl val group = bot.getGroupByUin(content.fromUin) as GroupImpl
if (var5 == 0L && this.remaining == 1L) {//管理员变更 if (var5 == 0L && this.remaining == 1L) {//管理员变更
val newPermission = val newPermission =
if (this.readByte().toInt() == 1) MemberPermission.ADMINISTRATOR else MemberPermission.MEMBER if (this.readByte()
.toInt() == 1
) MemberPermission.ADMINISTRATOR else MemberPermission.MEMBER
return if (target == bot.uin) { return if (target == bot.uin) {
BotGroupPermissionChangeEvent( BotGroupPermissionChangeEvent(
@ -188,22 +191,21 @@ internal class OnlinePush {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): Packet { override suspend fun ByteReadPacket.decode(bot: QQAndroidBot, sequenceId: Int): Packet {
val reqPushMsg = decodeUniPacket(OnlinePushPack.SvcReqPushMsg.serializer(), "req") val reqPushMsg = decodeUniPacket(OnlinePushPack.SvcReqPushMsg.serializer(), "req")
@Suppress("USELESS_CAST") // 不要信任 kotlin 类型推断
val packets: Sequence<Packet> = val packets: Sequence<Packet> =
reqPushMsg.vMsgInfos.asSequence().flatMap<MsgInfo, Packet> { msgInfo: MsgInfo -> reqPushMsg.vMsgInfos.asSequence().flatMap<MsgInfo, Packet> { msgInfo: MsgInfo ->
msgInfo.vMsg!!.read { msgInfo.vMsg!!.read {
when (msgInfo.shMsgType.toInt()) { when (msgInfo.shMsgType.toInt()) {
732 -> { 732 -> {
val group = bot.getGroup(this.readUInt().toLong()) val group = bot.getGroup(this.readUInt().toLong())
group as GroupImpl GroupImpl.checkIsInstance(group is GroupImpl)
when (val internalType = this.readByte().toInt().also { this.discardExact(1) }) { when (val internalType = this.readByte().toInt().also { this.discardExact(1) }) {
0x0c -> { // mute 0x0c -> { // mute
val operatorUin = this.readUInt().toLong() val operatorUin = this.readUInt().toLong()
if (operatorUin == bot.uin) { if (operatorUin == bot.uin) {
return@flatMap sequenceOf<Packet>() return@flatMap sequenceOf()
} }
val operator = group[operatorUin] val operator = group.getOrNull(operatorUin) ?: return@flatMap sequenceOf()
this.readUInt().toLong() // time this.readUInt().toLong() // time
this.discardExact(2) this.discardExact(2)
val target = this.readUInt().toLong() val target = this.readUInt().toLong()
@ -217,7 +219,7 @@ internal class OnlinePush {
new = false, new = false,
operator = operator, operator = operator,
group = group group = group
) as Packet )
) )
} else { } else {
return@flatMap sequenceOf( return@flatMap sequenceOf(
@ -226,38 +228,40 @@ internal class OnlinePush {
new = true, new = true,
operator = operator, operator = operator,
group = group group = group
) as Packet )
) )
} }
} else { } else {
if (target == bot.uin) { if (target == bot.uin) {
if (group._botMuteTimestamp != time) { if (group._botMuteTimestamp == time) {
return@flatMap sequenceOf()
}
if (time == 0) { if (time == 0) {
group._botMuteTimestamp = 0 group._botMuteTimestamp = 0
return@flatMap sequenceOf(BotUnmuteEvent(operator) as Packet) return@flatMap sequenceOf(BotUnmuteEvent(operator))
} else { } else {
group._botMuteTimestamp = time group._botMuteTimestamp = time
return@flatMap sequenceOf( return@flatMap sequenceOf(
BotMuteEvent( BotMuteEvent(
durationSeconds = time, durationSeconds = time,
operator = operator operator = operator
) as Packet )
) )
} }
} else { } else {
val member = group.getOrNull(target) ?: return@flatMap sequenceOf()
member as MemberImpl
if (member._muteTimestamp == time) {
return@flatMap sequenceOf() return@flatMap sequenceOf()
} }
} else {
val member = group[target]
member as MemberImpl
if (member._muteTimestamp != time) {
if (time == 0) { if (time == 0) {
member._muteTimestamp = 0 member._muteTimestamp = 0
return@flatMap sequenceOf( return@flatMap sequenceOf(
MemberUnmuteEvent( MemberUnmuteEvent(
member, member,
operator operator
) as Packet )
) )
} else { } else {
member._muteTimestamp = time member._muteTimestamp = time
@ -266,18 +270,16 @@ internal class OnlinePush {
member, member,
time, time,
operator operator
) as Packet
) )
} )
} else {
return@flatMap sequenceOf()
} }
} }
} }
} }
0x0e -> { 0x0e -> {
// 匿名 // 匿名
val operator = group[this.readUInt().toLong()] val operator =
group.getOrNull(this.readUInt().toLong()) ?: return@flatMap sequenceOf()
val switch = this.readInt() == 0 val switch = this.readInt() == 0
return@flatMap sequenceOf( return@flatMap sequenceOf(
GroupAllowAnonymousChatEvent( GroupAllowAnonymousChatEvent(
@ -342,6 +344,9 @@ internal class OnlinePush {
discard(1) discard(1)
val proto = readProtoBuf(TroopTips0x857.NotifyMsgBody.serializer()) val proto = readProtoBuf(TroopTips0x857.NotifyMsgBody.serializer())
proto.optMsgRecall?.let { recallReminder -> proto.optMsgRecall?.let { recallReminder ->
val memebr =
group.getOrNull(recallReminder.uin) ?: return@flatMap sequenceOf()
return@flatMap recallReminder.recalledMsgList.asSequence() return@flatMap recallReminder.recalledMsgList.asSequence()
.mapNotNull { meta -> .mapNotNull { meta ->
if (meta.authorUin == bot.uin) { if (meta.authorUin == bot.uin) {
@ -349,32 +354,13 @@ internal class OnlinePush {
} else MessageRecallEvent.GroupRecall( } else MessageRecallEvent.GroupRecall(
bot, bot,
meta.authorUin, meta.authorUin,
meta.seq.toLong().shl(32) or meta.msgRandom.toLong().and( meta.seq.toLong().shl(32) or
0xffffffff meta.msgRandom.toLong().and(0xffffffff),
),
meta.time, meta.time,
group.get(recallReminder.uin), memebr,
group group
) )
} }
/*
optMsgRecall=MessageRecallReminder#1345636186 {
groupType=0x00000000(0)
nickname=<Empty ByteArray>
opType=0x00000000(0)
recalledMsgList=[MessageMeta#1828757853 {
authorUin=0x000000003E033FA2(1040400290)
msgFlag=0x00000000(0)
msgRandom=0x509119DC(1351686620)
msgType=0x00000000(0)
seq=0x0000EB4B(60235)
time=0x5E50C4EC(1582351596)
}]
reminderContent=<Empty ByteArray>
uin=0x000000003E033FA2(1040400290)
userdef=08 00 12 0A 08 CB D6 03 10 00 18 01 20 00
}
*/
} }
return@flatMap sequenceOf() return@flatMap sequenceOf()
@ -384,7 +370,10 @@ internal class OnlinePush {
// println(msgInfo.vMsg.toUHexString()) // println(msgInfo.vMsg.toUHexString())
// } // }
else -> { else -> {
bot.network.logger.debug { "unknown group internal type $internalType , data: " + this.readBytes().toUHexString() + " " } bot.network.logger.debug {
"unknown group internal type $internalType , data: " + this.readBytes()
.toUHexString() + " "
}
return@flatMap sequenceOf() return@flatMap sequenceOf()
} }
} }