Support AutoLoginConfig, close #198

This commit is contained in:
Him188 2020-11-18 13:32:52 +08:00
parent 38f8773b98
commit 1f3434e54a
4 changed files with 77 additions and 33 deletions

View File

@ -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()
}
}
}

View File

@ -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<Long, String> by value(mutableMapOf(123456654321L to "example"))
@Serializable
data class Account(
@Comment("账号, 现只支持 QQ 数字账号")
val account: String,
val password: Password,
@Comment("""
账号配置. 可用配置列表 (注意大小写):
"protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH"
""")
val configuration: Map<ConfigurationKey, @Serializable(with = YamlDynamicSerializer::class) Any> = mapOf(),
) {
@Serializable
data class Password(
@Comment("密码种类, 可选 PLAIN 或 MD5")
val kind: PasswordKind,
@Comment("密码内容, PLAIN 时为密码文本, MD5 时为 16 进制")
val value: String,
)
@ValueDescription(
"""
账号和 MD5 密码列表
"""
)
val md5Passwords: MutableMap<Long, String> by value(
mutableMapOf(
123456654321L to "example".toByteArray().md5().toUHexString()
)
)
@Suppress("EnumEntryName")
@Serializable
enum class ConfigurationKey {
protocol,
}
@Serializable
enum class PasswordKind {
PLAIN,
MD5
}
}
val accounts: MutableList<Account> by value(mutableListOf(
Account("123456", Account.Password(Account.PasswordKind.PLAIN, "pwd"), mapOf(Account.ConfigurationKey.protocol to "ANDROID_PHONE"))
))
}

View File

@ -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"
}

View File

@ -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
}