From c09f8ab1927540cad3c133c7620331663d37688a Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Mon, 27 Sep 2021 22:39:01 +0800 Subject: [PATCH] Support for switching anonymousChat settings (#1522) * Support for switching anonymousChat settings * apiDump * Applied suggestion from him188 --- ...binary-compatibility-validator-android.api | 1 + .../api/binary-compatibility-validator.api | 1 + .../src/commonMain/kotlin/contact/Group.kt | 2 +- .../kotlin/contact/GroupSettingsImpl.kt | 11 ++++- .../kotlin/contact/info/GroupInfoImpl.kt | 2 +- .../network/protocol/packet/PacketFactory.kt | 1 + .../protocol/packet/chat/TroopManagement.kt | 40 +++++++++++++++++++ 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api index 3ff4d76d1..b30c5713b 100644 --- a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api +++ b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api @@ -392,6 +392,7 @@ public abstract interface class net/mamoe/mirai/contact/GroupSettings { public abstract fun isAutoApproveEnabled ()Z public abstract fun isMuteAll ()Z public abstract fun setAllowMemberInvite (Z)V + public abstract fun setAnonymousChatEnabled (Z)V public abstract fun setEntranceAnnouncement (Ljava/lang/String;)V public abstract fun setMuteAll (Z)V } diff --git a/binary-compatibility-validator/api/binary-compatibility-validator.api b/binary-compatibility-validator/api/binary-compatibility-validator.api index 93a85ca72..b96c341b3 100644 --- a/binary-compatibility-validator/api/binary-compatibility-validator.api +++ b/binary-compatibility-validator/api/binary-compatibility-validator.api @@ -392,6 +392,7 @@ public abstract interface class net/mamoe/mirai/contact/GroupSettings { public abstract fun isAutoApproveEnabled ()Z public abstract fun isMuteAll ()Z public abstract fun setAllowMemberInvite (Z)V + public abstract fun setAnonymousChatEnabled (Z)V public abstract fun setEntranceAnnouncement (Ljava/lang/String;)V public abstract fun setMuteAll (Z)V } diff --git a/mirai-core-api/src/commonMain/kotlin/contact/Group.kt b/mirai-core-api/src/commonMain/kotlin/contact/Group.kt index f34456571..4f6119cc5 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/Group.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/Group.kt @@ -273,7 +273,7 @@ public interface GroupSettings { /** * 匿名聊天 */ - public val isAnonymousChatEnabled: Boolean + public var isAnonymousChatEnabled: Boolean } /** diff --git a/mirai-core/src/commonMain/kotlin/contact/GroupSettingsImpl.kt b/mirai-core/src/commonMain/kotlin/contact/GroupSettingsImpl.kt index 5eac1e7f9..870872e07 100644 --- a/mirai-core/src/commonMain/kotlin/contact/GroupSettingsImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/GroupSettingsImpl.kt @@ -23,6 +23,8 @@ import net.mamoe.mirai.event.events.GroupNameChangeEvent import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.internal.network.protocol.packet.chat.TroopManagement.GroupOperation +import net.mamoe.mirai.internal.network.protocol.packet.chat.TroopManagement.SwitchAnonymousChat +import net.mamoe.mirai.internal.network.protocol.packet.sendAndExpect @Suppress("SetterBackingFieldAssignment") internal class GroupSettingsImpl( @@ -91,9 +93,14 @@ internal class GroupSettingsImpl( @Deprecated("Don't use public var internally", level = DeprecationLevel.HIDDEN) override var isAnonymousChatEnabled: Boolean get() = isAnonymousChatEnabledField - @Suppress("UNUSED_PARAMETER") set(newValue) { - throw UnsupportedOperationException() + group.run { + checkBotPermission(MemberPermission.ADMINISTRATOR) + launch { + //Handle it in NoticePipelineContext#processAllowAnonymousChat + SwitchAnonymousChat(bot.client, id, newValue).sendAndExpect(bot.network) + } + } } @Deprecated("Don't use public var internally", level = DeprecationLevel.HIDDEN) diff --git a/mirai-core/src/commonMain/kotlin/contact/info/GroupInfoImpl.kt b/mirai-core/src/commonMain/kotlin/contact/info/GroupInfoImpl.kt index 921f41615..1f30ab41e 100644 --- a/mirai-core/src/commonMain/kotlin/contact/info/GroupInfoImpl.kt +++ b/mirai-core/src/commonMain/kotlin/contact/info/GroupInfoImpl.kt @@ -35,7 +35,7 @@ internal data class GroupInfoImpl( memo = stTroopNum.groupMemo, name = stTroopNum.groupName, allowMemberInvite = stTroopNum.dwGroupFlagExt?.and(0x000000c0) != 0L, - allowAnonymousChat = stTroopNum.dwGroupFlagExt?.and(0x40000000) == 0L, + allowAnonymousChat = stTroopNum.dwGroupFlagExt?.and(0x40000000) != 0L, autoApprove = stTroopNum.dwGroupFlagExt3?.and(0x00100000) == 0L, confessTalk = stTroopNum.dwGroupFlagExt3?.and(0x00002000) == 0L, muteAll = stTroopNum.dwShutUpTimestamp != 0L, diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/PacketFactory.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/PacketFactory.kt index 39c213889..71040bbd5 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/PacketFactory.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/PacketFactory.kt @@ -154,6 +154,7 @@ internal object KnownPacketFactories { // TroopManagement.GetGroupInfo, TroopManagement.EditGroupNametag, TroopManagement.Kick, + TroopManagement.SwitchAnonymousChat, TroopEssenceMsgManager.SetEssence, NudgePacket, Heartbeat.Alive, diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/TroopManagement.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/TroopManagement.kt index fac63c318..a80e9d555 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/TroopManagement.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/TroopManagement.kt @@ -218,6 +218,46 @@ internal class TroopManagement { } + internal object SwitchAnonymousChat : OutgoingPacketFactory("OidbSvc.0x568_22") { + override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response { + val ret = this.readBytes() + .loadAs(OidbSso.OIDBSSOPkg.serializer()).result + return Response( + ret == 0 + ) + } + + class Response( + val success: Boolean + ) : Packet { + override fun toString(): String = "TroopManagement.SwitchAnonymousChat.Response($success)" + } + + operator fun invoke( + client: QQAndroidClient, + groupCode: Long, + switch: Boolean + ) = buildOutgoingUniPacket(client) { + writeProtoBuf( + OidbSso.OIDBSSOPkg.serializer(), + OidbSso.OIDBSSOPkg( + command = 1384, + serviceType = 22, + result = 0, + bodybuffer = buildPacket { + writeInt(groupCode.toInt()) + if (switch) { + writeByte(1) + } else { + writeByte(0) + } + }.readBytes() + ) + ) + } + + } + internal object GroupOperation : OutgoingPacketFactory("OidbSvc.0x89a_0") { override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response = Response