Migrate network logger and old sendWithoutExpect and sendAndExpect

This commit is contained in:
Him188 2021-04-16 08:58:05 +08:00
parent 840dd19dd0
commit ff80434d2d
13 changed files with 93 additions and 67 deletions

View File

@ -15,7 +15,6 @@ import io.ktor.client.features.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.request.forms.* import io.ktor.client.request.forms.*
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.withContext
import kotlinx.io.core.discardExact import kotlinx.io.core.discardExact
import kotlinx.io.core.readBytes import kotlinx.io.core.readBytes
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@ -567,49 +566,20 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@MiraiExperimentalApi @MiraiExperimentalApi
override suspend fun sendGroupAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String = override suspend fun sendGroupAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String =
bot.asQQAndroidBot().run { bot.asQQAndroidBot().run {
val rep = withContext(network.coroutineContext) { val rep = Mirai.Http.post<String> {
Mirai.Http.post<String> { url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
body = MultiPartFormDataContent(formData {
append("qid", groupId)
append("bkn", bkn)
append("text", announcement.msg.text)
append("pinned", announcement.pinned)
append(
"settings",
json.encodeToString(
GroupAnnouncementSettings.serializer(),
announcement.settings ?: GroupAnnouncementSettings()
)
)
append("format", "json")
})
headers {
append(
"cookie",
"uin=o${id};" +
" skey=${client.wLoginSigInfo.sKey.data.encodeToString()};" +
" p_uin=o${id};" +
" p_skey=${client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString()}; "
)
}
}
}
val jsonObj = json.parseToJsonElement(rep)
return jsonObj.jsonObject["new_fid"]?.jsonPrimitive?.content
?: throw throw IllegalStateException("Send Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}")
}
@LowLevelApi
@MiraiExperimentalApi
override suspend fun deleteGroupAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run {
val data = withContext(network.coroutineContext) {
Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("qid", groupId) append("qid", groupId)
append("bkn", bkn) append("bkn", bkn)
append("fid", fid) append("text", announcement.msg.text)
append("pinned", announcement.pinned)
append(
"settings",
json.encodeToString(
GroupAnnouncementSettings.serializer(),
announcement.settings ?: GroupAnnouncementSettings()
)
)
append("format", "json") append("format", "json")
}) })
headers { headers {
@ -622,6 +592,31 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
) )
} }
} }
val jsonObj = json.parseToJsonElement(rep)
return jsonObj.jsonObject["new_fid"]?.jsonPrimitive?.content
?: throw throw IllegalStateException("Send Announcement fail group:$groupId msg:${jsonObj.jsonObject["em"]} content:${announcement.msg.text}")
}
@LowLevelApi
@MiraiExperimentalApi
override suspend fun deleteGroupAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run {
val data = Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
body = MultiPartFormDataContent(formData {
append("qid", groupId)
append("bkn", bkn)
append("fid", fid)
append("format", "json")
})
headers {
append(
"cookie",
"uin=o${id};" +
" skey=${client.wLoginSigInfo.sKey.data.encodeToString()};" +
" p_uin=o${id};" +
" p_skey=${client.wLoginSigInfo.psKeyMap["qun.qq.com"]?.data?.encodeToString()}; "
)
}
} }
val jsonObj = json.parseToJsonElement(data) val jsonObj = json.parseToJsonElement(data)
if (jsonObj.jsonObject["ec"]?.jsonPrimitive?.int ?: 1 != 0) { if (jsonObj.jsonObject["ec"]?.jsonPrimitive?.int ?: 1 != 0) {

View File

@ -19,6 +19,7 @@ import net.mamoe.mirai.internal.asQQAndroidBot
import net.mamoe.mirai.internal.message.* import net.mamoe.mirai.internal.message.*
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.internal.network.protocol.packet.chat.FileManagement import net.mamoe.mirai.internal.network.protocol.packet.chat.FileManagement

View File

@ -15,6 +15,7 @@ import kotlinx.serialization.protobuf.ProtoBuf
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.contact.info.FriendInfoImpl import net.mamoe.mirai.internal.contact.info.FriendInfoImpl
import net.mamoe.mirai.internal.contact.info.MemberInfoImpl import net.mamoe.mirai.internal.contact.info.MemberInfoImpl
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.jce.StTroopNum import net.mamoe.mirai.internal.network.protocol.data.jce.StTroopNum
import net.mamoe.mirai.internal.utils.ScheduledJob import net.mamoe.mirai.internal.utils.ScheduledJob
import net.mamoe.mirai.internal.utils.groupCacheDir import net.mamoe.mirai.internal.utils.groupCacheDir

View File

@ -27,6 +27,7 @@ import net.mamoe.mirai.internal.contact.info.GroupInfoImpl
import net.mamoe.mirai.internal.contact.info.MemberInfoImpl import net.mamoe.mirai.internal.contact.info.MemberInfoImpl
import net.mamoe.mirai.internal.contact.info.StrangerInfoImpl import net.mamoe.mirai.internal.contact.info.StrangerInfoImpl
import net.mamoe.mirai.internal.contact.toMiraiFriendInfo import net.mamoe.mirai.internal.contact.toMiraiFriendInfo
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.jce.StTroopNum import net.mamoe.mirai.internal.network.protocol.data.jce.StTroopNum
import net.mamoe.mirai.internal.network.protocol.data.jce.SvcRespRegister import net.mamoe.mirai.internal.network.protocol.data.jce.SvcRespRegister
import net.mamoe.mirai.internal.network.protocol.data.jce.isValid import net.mamoe.mirai.internal.network.protocol.data.jce.isValid

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -10,6 +10,7 @@
package net.mamoe.mirai.internal.network package net.mamoe.mirai.internal.network
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
/* /*

View File

@ -23,7 +23,6 @@ import java.net.InetAddress
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.SocketAddress import java.net.SocketAddress
import java.util.concurrent.CancellationException import java.util.concurrent.CancellationException
import kotlin.coroutines.CoroutineContext
/** /**
* Immutable context for [NetworkHandler] * Immutable context for [NetworkHandler]
@ -60,12 +59,6 @@ internal class NetworkHandlerContextImpl(
internal interface NetworkHandler { internal interface NetworkHandler {
val context: NetworkHandlerContext val context: NetworkHandlerContext
val logger get() = context.logger // TODO: 2021/4/14 just for migration
@Deprecated("") // TODO: 2021/4/14 migrate NetworkHandler.coroutineContext
val coroutineContext: CoroutineContext
get() = error("ERROR")
/** /**
* State of this handler. * State of this handler.
*/ */
@ -123,28 +116,44 @@ internal interface NetworkHandler {
*/ */
suspend fun sendWithoutExpect(packet: OutgoingPacket) suspend fun sendWithoutExpect(packet: OutgoingPacket)
@Suppress("INAPPLICABLE_JVM_NAME") /**
@JvmName("sendWithoutExpect1") // TODO: 2021/4/14 just for migration * Closes this handler gracefully.
suspend fun OutgoingPacket.sendWithoutExpect() = sendWithoutExpect(this) */
fun close()
@Suppress("INAPPLICABLE_JVM_NAME") ///////////////////////////////////////////////////////////////////////////
@JvmName("sendWithoutExpect1") // TODO: 2021/4/14 just for migration // compatibility
suspend fun <R> OutgoingPacket.sendAndExpect(timeoutMillis: Long = 5000, retry: Int = 2): R = TODO() ///////////////////////////////////////////////////////////////////////////
@Suppress("INAPPLICABLE_JVM_NAME") /**
@JvmName("sendWithoutExpect1") // TODO: 2021/4/14 just for migration * @suppress This is for compatibility with old code. Use [sendWithoutExpect] without extension receiver instead.
*/
suspend fun OutgoingPacket.sendWithoutExpect(
antiCollisionParam: Any? = null
) = this@NetworkHandler.sendWithoutExpect(this)
/**
* @suppress This is for compatibility with old code. Use [sendAndExpect] without extension receiver instead.
*/
@Suppress("UNCHECKED_CAST")
suspend fun <R> OutgoingPacket.sendAndExpect(
timeoutMillis: Long = 5000,
retry: Int = 2,
antiCollisionParam: Any? = null // signature collision
): R = sendAndExpect(this, timeoutMillis, retry) as R
/**
* @suppress This is for compatibility with old code. Use [sendAndExpect] without extension receiver instead.
*/
@Suppress("UNCHECKED_CAST")
suspend fun <R : Packet?> OutgoingPacketWithRespType<R>.sendAndExpect( suspend fun <R : Packet?> OutgoingPacketWithRespType<R>.sendAndExpect(
timeoutMillis: Long = 5000, timeoutMillis: Long = 5000,
retry: Int = 2 retry: Int = 2
): R = TODO() ): R = sendAndExpect(this, timeoutMillis, retry) as R
/**
* Closes this handler gracefully and suspends the coroutine for its completion.
*/
fun close()
} }
internal val NetworkHandler.logger: MiraiLogger get() = context.logger
/** /**
* Factory for a specific [NetworkHandler] implementation. * Factory for a specific [NetworkHandler] implementation.
*/ */

View File

@ -23,6 +23,7 @@ import kotlinx.io.core.writeFully
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.network.BdhSession import net.mamoe.mirai.internal.network.BdhSession
import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.proto.CSDataHighwayHead import net.mamoe.mirai.internal.network.protocol.data.proto.CSDataHighwayHead
import net.mamoe.mirai.internal.network.subAppId import net.mamoe.mirai.internal.network.subAppId
import net.mamoe.mirai.internal.utils.PlatformSocket import net.mamoe.mirai.internal.utils.PlatformSocket

View File

@ -36,6 +36,7 @@ import net.mamoe.mirai.internal.message.toMessageChainOnline
import net.mamoe.mirai.internal.network.MultiPacket import net.mamoe.mirai.internal.network.MultiPacket
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.proto.FrdSysMsg import net.mamoe.mirai.internal.network.protocol.data.proto.FrdSysMsg
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgSvc import net.mamoe.mirai.internal.network.protocol.data.proto.MsgSvc
@ -45,7 +46,7 @@ import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketFactory
import net.mamoe.mirai.internal.network.protocol.packet.buildOutgoingUniPacket import net.mamoe.mirai.internal.network.protocol.packet.buildOutgoingUniPacket
import net.mamoe.mirai.internal.network.protocol.packet.chat.NewContact import net.mamoe.mirai.internal.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.internal.network.protocol.packet.list.FriendList import net.mamoe.mirai.internal.network.protocol.packet.list.FriendList
import net.mamoe.mirai.internal.utils.* import net.mamoe.mirai.internal.utils.broadcastWithBot
import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.loadAs
import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf
import net.mamoe.mirai.internal.utils.io.serialization.writeProtoBuf import net.mamoe.mirai.internal.utils.io.serialization.writeProtoBuf

View File

@ -1,3 +1,12 @@
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.internal.network.protocol.packet.chat.receive package net.mamoe.mirai.internal.network.protocol.packet.chat.receive
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
@ -16,6 +25,7 @@ import net.mamoe.mirai.internal.contact.appId
import net.mamoe.mirai.internal.createOtherClient import net.mamoe.mirai.internal.createOtherClient
import net.mamoe.mirai.internal.message.contextualBugReportException import net.mamoe.mirai.internal.message.contextualBugReportException
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.jce.RequestPushStatus import net.mamoe.mirai.internal.network.protocol.data.jce.RequestPushStatus
import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacketFactory import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacketFactory
import net.mamoe.mirai.internal.utils._miraiContentToString import net.mamoe.mirai.internal.utils._miraiContentToString

View File

@ -19,10 +19,14 @@ import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.event.events.GroupMessageSyncEvent import net.mamoe.mirai.event.events.GroupMessageSyncEvent
import net.mamoe.mirai.event.events.MemberCardChangeEvent import net.mamoe.mirai.event.events.MemberCardChangeEvent
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.contact.* import net.mamoe.mirai.internal.contact.GroupImpl
import net.mamoe.mirai.internal.contact.NormalMemberImpl
import net.mamoe.mirai.internal.contact.info
import net.mamoe.mirai.internal.contact.info.MemberInfoImpl import net.mamoe.mirai.internal.contact.info.MemberInfoImpl
import net.mamoe.mirai.internal.contact.newAnonymous
import net.mamoe.mirai.internal.message.toMessageChainOnline import net.mamoe.mirai.internal.message.toMessageChainOnline
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgOnlinePush import net.mamoe.mirai.internal.network.protocol.data.proto.MsgOnlinePush

View File

@ -29,6 +29,7 @@ import net.mamoe.mirai.internal.contact.info.MemberInfoImpl
import net.mamoe.mirai.internal.network.MultiPacketBySequence import net.mamoe.mirai.internal.network.MultiPacketBySequence
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.jce.MsgInfo import net.mamoe.mirai.internal.network.protocol.data.jce.MsgInfo
import net.mamoe.mirai.internal.network.protocol.data.jce.MsgType0x210 import net.mamoe.mirai.internal.network.protocol.data.jce.MsgType0x210
import net.mamoe.mirai.internal.network.protocol.data.jce.OnlinePushPack import net.mamoe.mirai.internal.network.protocol.data.jce.OnlinePushPack
@ -36,7 +37,6 @@ import net.mamoe.mirai.internal.network.protocol.data.jce.RequestPacket
import net.mamoe.mirai.internal.network.protocol.data.proto.* import net.mamoe.mirai.internal.network.protocol.data.proto.*
import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27.SubMsgType0x27.* import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x27.SubMsgType0x27.*
import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x44.Submsgtype0x44 import net.mamoe.mirai.internal.network.protocol.data.proto.Submsgtype0x44.Submsgtype0x44
import net.mamoe.mirai.internal.network.protocol.data.proto.TroopTips0x857
import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacketFactory import net.mamoe.mirai.internal.network.protocol.packet.IncomingPacketFactory
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.internal.network.protocol.packet.buildResponseUniPacket import net.mamoe.mirai.internal.network.protocol.packet.buildResponseUniPacket

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.message.contextualBugReportException import net.mamoe.mirai.internal.message.contextualBugReportException
import net.mamoe.mirai.internal.network.BdhSession import net.mamoe.mirai.internal.network.BdhSession
import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.data.jce.FileStoragePushFSSvcList import net.mamoe.mirai.internal.network.protocol.data.jce.FileStoragePushFSSvcList
import net.mamoe.mirai.internal.network.protocol.data.jce.PushResp import net.mamoe.mirai.internal.network.protocol.data.jce.PushResp
import net.mamoe.mirai.internal.network.protocol.data.jce.RequestPacket import net.mamoe.mirai.internal.network.protocol.data.jce.RequestPacket

View File

@ -17,6 +17,7 @@ import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.network.* import net.mamoe.mirai.internal.network.*
import net.mamoe.mirai.internal.network.DebuggingProperties.SHOW_TLV_MAP_ON_LOGIN_SUCCESS import net.mamoe.mirai.internal.network.DebuggingProperties.SHOW_TLV_MAP_ON_LOGIN_SUCCESS
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.protocol.packet.* import net.mamoe.mirai.internal.network.protocol.packet.*
import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.WtLoginExt import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.WtLoginExt
import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.analysisTlv0x531 import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.analysisTlv0x531