From 1f3434e54a3041e5af66bf11a61bffaa6e117e7b Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 18 Nov 2020 13:32:52 +0800 Subject: [PATCH] Support AutoLoginConfig, close #198 --- .../MiraiConsoleImplementationBridge.kt | 47 +++++++++++---- .../internal/data/builtins/AutoLoginConfig.kt | 57 +++++++++++++------ buildSrc/src/main/kotlin/Versions.kt | 4 +- tools/gradle-plugin/src/VersionConstants.kt | 2 +- 4 files changed, 77 insertions(+), 33 deletions(-) diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 7ead9dae3..be186c14b 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -30,6 +30,9 @@ import net.mamoe.mirai.console.extensions.PostStartupExtension import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.internal.command.CommandConfig import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig +import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.ConfigurationKey.protocol +import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.MD5 +import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.PLAIN import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope import net.mamoe.mirai.console.internal.data.builtins.LoggerConfig import net.mamoe.mirai.console.internal.data.castOrNull @@ -217,20 +220,40 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI phase `auto-login bots`@{ runBlocking { - for ((id, password) in AutoLoginConfig.plainPasswords.filterNot { it.key == 123456654321L }) { - mainLogger.info { "Auto-login $id" } - MiraiConsole.addBot(id, password).alsoLogin() + val accounts = AutoLoginConfig.accounts.toList() + for (account in accounts) { + val id = kotlin.runCatching { + account.account.toLong() + }.getOrElse { + error("Bad auto-login account: '${account.account}'") + } + if (id == 123456L) continue + val config = BotConfiguration() + for ((key, value) in account.configuration) { + runCatching { + when (key) { + protocol -> config.protocol = BotConfiguration.MiraiProtocol.valueOf(value.toString()) + } + }.onFailure { + error("Bad auto-login config value for $key for account $id") + } + } + when (account.password.kind) { + PLAIN -> { + mainLogger.info { "Auto-login ${account.account}" } + MiraiConsole.addBot(id, account.password.value).alsoLogin() + } + MD5 -> { + val md5 = kotlin.runCatching { + account.password.value.autoHexToBytes() + }.getOrElse { + error("Bad auto-login md5: '${account.password.value}' for account $id") + } + MiraiConsole.addBot(id, md5).alsoLogin() + } + } } - for ((id, password) in AutoLoginConfig.md5Passwords.filterNot { it.key == 123456654321L }) { - mainLogger.info { "Auto-login $id" } - val x = runCatching { - password.autoHexToBytes() - }.getOrElse { - error("Bad auto-login md5: '$password'") - } - MiraiConsole.addBot(id, x).alsoLogin() - } } } diff --git a/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt b/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt index a871d754a..f3c3d5b6c 100644 --- a/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt +++ b/backend/mirai-console/src/internal/data/builtins/AutoLoginConfig.kt @@ -9,29 +9,50 @@ package net.mamoe.mirai.console.internal.data.builtins +import kotlinx.serialization.Serializable import net.mamoe.mirai.console.data.AutoSavePluginConfig import net.mamoe.mirai.console.data.ValueDescription import net.mamoe.mirai.console.data.value -import net.mamoe.mirai.console.internal.util.md5 -import net.mamoe.mirai.console.internal.util.toUHexString +import net.mamoe.yamlkt.Comment +import net.mamoe.yamlkt.YamlDynamicSerializer +@ValueDescription("自动登录配置") internal object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") { - @ValueDescription( - """ - 账号和明文密码列表 - """ - ) - val plainPasswords: MutableMap by value(mutableMapOf(123456654321L to "example")) - - @ValueDescription( - """ - 账号和 MD5 密码列表 - """ - ) - val md5Passwords: MutableMap by value( - mutableMapOf( - 123456654321L to "example".toByteArray().md5().toUHexString() + @Serializable + data class Account( + @Comment("账号, 现只支持 QQ 数字账号") + val account: String, + val password: Password, + @Comment(""" + 账号配置. 可用配置列表 (注意大小写): + "protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH" + """) + val configuration: Map = mapOf(), + ) { + @Serializable + data class Password( + @Comment("密码种类, 可选 PLAIN 或 MD5") + val kind: PasswordKind, + @Comment("密码内容, PLAIN 时为密码文本, MD5 时为 16 进制") + val value: String, ) - ) + + @Suppress("EnumEntryName") + @Serializable + enum class ConfigurationKey { + protocol, + + } + + @Serializable + enum class PasswordKind { + PLAIN, + MD5 + } + } + + val accounts: MutableList by value(mutableListOf( + Account("123456", Account.Password(Account.PasswordKind.PLAIN, "pwd"), mapOf(Account.ConfigurationKey.protocol to "ANDROID_PHONE")) + )) } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 6957a14d2..625e6b77d 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -11,7 +11,7 @@ object Versions { const val core = "1.3.3" - const val console = "1.0-RC2-dev-6" + const val console = "1.0.0-dev-1" const val consoleGraphical = "0.0.7" const val consoleTerminal = console @@ -34,7 +34,7 @@ object Versions { const val blockingBridge = "1.4.1" @Suppress("SpellCheckingInspection") - const val yamlkt = "0.7.1" + const val yamlkt = "0.7.3" const val intellijGradlePlugin = "0.4.16" } diff --git a/tools/gradle-plugin/src/VersionConstants.kt b/tools/gradle-plugin/src/VersionConstants.kt index 5b584aadf..33ae9c1da 100644 --- a/tools/gradle-plugin/src/VersionConstants.kt +++ b/tools/gradle-plugin/src/VersionConstants.kt @@ -10,6 +10,6 @@ package net.mamoe.mirai.console.gradle internal object VersionConstants { - const val CONSOLE_VERSION = "1.0-RC2-dev-6" // value is written here automatically during build + const val CONSOLE_VERSION = "1.0.0-dev-1" // value is written here automatically during build const val CORE_VERSION = "1.3.3" // value is written here automatically during build } \ No newline at end of file