-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
主动兼容 cssxsh 的所有插件中使用的 mirai-core 内部方法 (#19)
* [skip ci] good job * add QQAndroidClient get keys methods * move MarketFace to mirai internal * add Stub MessageRefiner * [skip ci] Update TODO.md
- Loading branch information
Showing
12 changed files
with
277 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/AbstractBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright 2020 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 | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import net.mamoe.mirai.Bot | ||
|
||
abstract class AbstractBot : Bot, CoroutineScope |
30 changes: 30 additions & 0 deletions
30
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/QQAndroidBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2020 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 | ||
|
||
import net.mamoe.mirai.Bot | ||
import net.mamoe.mirai.internal.network.QQAndroidClient | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.contract | ||
|
||
@OptIn(ExperimentalContracts::class) | ||
internal fun Bot.asQQAndroidBot(): QQAndroidBot { | ||
contract { | ||
returns() implies (this@asQQAndroidBot is QQAndroidBot) | ||
} | ||
|
||
return this as QQAndroidBot | ||
} | ||
|
||
internal abstract class QQAndroidBot : AbstractBot() { | ||
abstract val implGetter: () -> cn.evolvefield.onebot.client.core.Bot | ||
val client: QQAndroidClient by lazy { | ||
QQAndroidClient(implGetter) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/message/RefinableMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2019-2022 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/dev/LICENSE | ||
*/ | ||
package net.mamoe.mirai.internal.message | ||
|
||
import net.mamoe.mirai.message.data.* | ||
|
||
internal sealed class MessageRefiner | ||
|
||
/** | ||
* 执行不需要 `suspend` 的 refine. 用于 [MessageSource.originalMessage]. | ||
* | ||
* 兼容 cssxsh/mirai-hibernate-plugin | ||
* | ||
* https://github.com/cssxsh/mirai-hibernate-plugin/blob/8f425db01629ff84900b53b1bf86c741ef7f81be/src/main/kotlin/xyz/cssxsh/mirai/hibernate/MiraiHibernateRecorder.kt | ||
*/ | ||
internal object LightMessageRefiner : MessageRefiner() { | ||
/** | ||
* 去除 [MessageChain] 携带的内部标识 | ||
* | ||
* 用于 [createMessageReceipt] <- `RemoteFile.uploadAndSend` (文件操作API v1) | ||
*/ | ||
fun MessageChain.dropMiraiInternalFlags(): MessageChain = this | ||
} |
28 changes: 28 additions & 0 deletions
28
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/message/data/MarketFaceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.mamoe.mirai.internal.message.data | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.Transient | ||
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody | ||
import net.mamoe.mirai.message.data.MarketFace | ||
import net.mamoe.mirai.utils.MiraiExperimentalApi | ||
|
||
@SerialName(MarketFaceImpl.SERIAL_NAME) | ||
@Serializable | ||
internal data class MarketFaceImpl internal constructor( | ||
internal val delegate: ImMsgBody.MarketFace, | ||
) : MarketFace { | ||
|
||
override val name: String get() = delegate.faceName.decodeToString() | ||
|
||
@Transient | ||
@MiraiExperimentalApi | ||
override val id: Int = delegate.tabId | ||
|
||
@OptIn(MiraiExperimentalApi::class) | ||
override fun toString() = "[mirai:marketface:$id,$name]" | ||
|
||
companion object { | ||
const val SERIAL_NAME = MarketFace.SERIAL_NAME | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/network/QQAndroidClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2020 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 | ||
|
||
import net.mamoe.mirai.internal.network.components.SsoSession | ||
|
||
internal open class QQAndroidClient( | ||
implGetter: () -> cn.evolvefield.onebot.client.core.Bot | ||
) : SsoSession { | ||
override var loginState: Int = 0 | ||
override var wLoginSigInfo: WLoginSigInfo = WLoginSigInfo(implGetter) | ||
} |
29 changes: 29 additions & 0 deletions
29
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/network/components/SsoProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2020 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.components | ||
|
||
import net.mamoe.mirai.internal.network.WLoginSigInfo | ||
|
||
/** | ||
* Contains secrets for encryption and decryption during a session created by [SsoProcessor] and [PacketCodec]. | ||
* | ||
* @see AccountSecrets | ||
*/ | ||
internal interface SsoSession { | ||
//var outgoingPacketSessionId: ByteArray | ||
|
||
/** | ||
* always 0 for now. | ||
*/ | ||
var loginState: Int | ||
|
||
// also present in AccountSecrets | ||
var wLoginSigInfo: WLoginSigInfo | ||
//val randomKey: ByteArray | ||
} |
46 changes: 46 additions & 0 deletions
46
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/network/keys.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2020 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 | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import net.mamoe.mirai.internal.AbstractBot | ||
import net.mamoe.mirai.internal.QQAndroidBot | ||
|
||
/** | ||
* 兼容 cssxsh/meme-helper 中请求网络接口所需的 bkn、sKey、psKey 等参数 | ||
* | ||
* https://github.com/cssxsh/meme-helper/blob/61fbfd967a5e45dd9923c470622cd90d5bce1ce4/src/main/kotlin/face/MarketFaceHelper.kt | ||
*/ | ||
internal data class WLoginSigInfo( | ||
val impl: () -> cn.evolvefield.onebot.client.core.Bot | ||
) { | ||
val bkn: Int | ||
get() = sKey.encodeToByteArray() | ||
.fold(5381) { acc: Int, b: Byte -> acc + acc.shl(5) + b.toInt() } | ||
.and(Int.MAX_VALUE) | ||
|
||
val sKey: String | ||
get() = runBlocking { | ||
val data = impl().getCredentials("qun.qq.com").data ?: throw IllegalStateException("Onebot 获取 Credentials (sKey) 失败") | ||
val matches = Regex("[^_]skey=([^;]+);?").find(data.cookies) ?: throw IllegalStateException("Onebot 获取 Credentials 返回的 cookie 中没有 skey") | ||
return@runBlocking matches.groupValues[1] | ||
} | ||
|
||
fun getPsKey(name: String): String { | ||
return runBlocking { | ||
val data = impl().getCredentials(name).data ?: throw IllegalStateException("Onebot 获取 Credentials (psKey) 失败") | ||
val matches = Regex("p_skey=([^;]+);?").find(data.cookies) ?: throw IllegalStateException("Onebot 获取 Credentials 返回的 cookie 中没有 p_skey") | ||
return@runBlocking matches.groupValues[1] | ||
} | ||
} | ||
} | ||
|
||
internal val AbstractBot.sKey get() = client.wLoginSigInfo.sKey | ||
internal fun AbstractBot.psKey(name: String) = client.wLoginSigInfo.getPsKey(name) | ||
internal val AbstractBot.client get() = (this as QQAndroidBot).client |
73 changes: 73 additions & 0 deletions
73
overflow-core/src/main/kotlin/net/mamoe/mirai/internal/network/protocol/data/proto/Msg.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2019-2022 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/dev/LICENSE | ||
*/ | ||
package net.mamoe.mirai.internal.network.protocol.data.proto | ||
|
||
import kotlinx.serialization.Serializable | ||
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY | ||
import net.mamoe.mirai.utils.isSameType | ||
|
||
@Serializable | ||
internal class ImMsgBody /*: ProtoBuf*/ { | ||
|
||
@Serializable | ||
internal data class MarketFace( | ||
/*@ProtoNumber(1) */@JvmField var faceName: ByteArray = EMPTY_BYTE_ARRAY, | ||
/*@ProtoNumber(2) */@JvmField val itemType: Int = 0, | ||
/*@ProtoNumber(3) */@JvmField val faceInfo: Int = 0, | ||
/*@ProtoNumber(4) */@JvmField val faceId: ByteArray = EMPTY_BYTE_ARRAY, | ||
/*@ProtoNumber(5) */@JvmField val tabId: Int = 0, | ||
/*@ProtoNumber(6) */@JvmField val subType: Int = 0, | ||
/*@ProtoNumber(7) */@JvmField val key: ByteArray = EMPTY_BYTE_ARRAY, | ||
/*@ProtoNumber(8) */@JvmField val param: ByteArray = EMPTY_BYTE_ARRAY, | ||
/*@ProtoNumber(9) */@JvmField val mediaType: Int = 0, | ||
/*@ProtoNumber(10) */@JvmField val imageWidth: Int = 0, | ||
/*@ProtoNumber(11) */@JvmField val imageHeight: Int = 0, | ||
/*@ProtoNumber(12) */@JvmField val mobileParam: ByteArray = EMPTY_BYTE_ARRAY, | ||
/*@ProtoNumber(13) */@JvmField val pbReserve: ByteArray = EMPTY_BYTE_ARRAY, | ||
) /*: ProtoBuf*/ { | ||
@Suppress("DuplicatedCode") | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (!isSameType(this, other)) return false | ||
|
||
if (!faceName.contentEquals(other.faceName)) return false | ||
if (itemType != other.itemType) return false | ||
if (faceInfo != other.faceInfo) return false | ||
if (!faceId.contentEquals(other.faceId)) return false | ||
if (tabId != other.tabId) return false | ||
if (subType != other.subType) return false | ||
if (!key.contentEquals(other.key)) return false | ||
if (!param.contentEquals(other.param)) return false | ||
if (mediaType != other.mediaType) return false | ||
if (imageWidth != other.imageWidth) return false | ||
if (imageHeight != other.imageHeight) return false | ||
if (!mobileParam.contentEquals(other.mobileParam)) return false | ||
if (!pbReserve.contentEquals(other.pbReserve)) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = faceName.contentHashCode() | ||
result = 31 * result + itemType | ||
result = 31 * result + faceInfo | ||
result = 31 * result + faceId.contentHashCode() | ||
result = 31 * result + tabId | ||
result = 31 * result + subType | ||
result = 31 * result + key.contentHashCode() | ||
result = 31 * result + param.contentHashCode() | ||
result = 31 * result + mediaType | ||
result = 31 * result + imageWidth | ||
result = 31 * result + imageHeight | ||
result = 31 * result + mobileParam.contentHashCode() | ||
result = 31 * result + pbReserve.contentHashCode() | ||
return result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
...flow-core/src/main/kotlin/top/mrxiaom/overflow/internal/message/data/WrappedMarketFace.kt
This file was deleted.
Oops, something went wrong.