Commit 34f10fe2 authored by Him188's avatar Him188

adjust login result

parent e2171fd1
package net.mamoe.mirai.network.protocol.tim.packet.login package net.mamoe.mirai.network.protocol.tim.packet.login
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult.SUCCESS
/** /**
* 登录结果. 除 [SUCCESS] 外均为失败. * 登录结果. 除 [SUCCESS] 外均为失败.
* @see LoginResult.requireSuccess 要求成功 * @see LoginResult.requireSuccess 要求成功
...@@ -60,8 +62,29 @@ fun LoginResult.requireSuccess(lazyMessage: (LoginResult) -> String) { ...@@ -60,8 +62,29 @@ fun LoginResult.requireSuccess(lazyMessage: (LoginResult) -> String) {
/** /**
* 如果 [this] 不为 [LoginResult.SUCCESS] 就抛出消息为 "Login failed $this" 的 [IllegalStateException] * 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则 [error]
*/ */
fun LoginResult.requireSuccess() { fun LoginResult.requireSuccess() {
if (this != LoginResult.SUCCESS) error("Login failed: $this") if (requireSuccessOrNull() === null)
error("Login failed: $this")
} }
/**
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则返回 `null`
*
* @return 成功时 [Unit], 失败时 `null`
*/
fun LoginResult.requireSuccessOrNull(): Unit? =
if (this != LoginResult.SUCCESS) Unit else null
/**
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则返回 `null`
*
* @return 成功时 [Unit], 失败时 `null`
*/
inline fun <R> LoginResult.ifFail(block: (LoginResult) -> R): R? =
if (this != LoginResult.SUCCESS) block(this) else null
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import io.ktor.util.cio.writeChannel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.io.core.IoBuffer import kotlinx.io.core.IoBuffer
import kotlinx.io.core.readBytes
import java.awt.Image import java.awt.Image
import java.awt.image.BufferedImage import java.awt.image.BufferedImage
import java.io.File import java.io.File
...@@ -20,19 +20,23 @@ import kotlin.math.min ...@@ -20,19 +20,23 @@ import kotlin.math.min
* @return 用户输入得到的验证码 * @return 用户输入得到的验证码
*/ */
internal actual suspend fun solveCaptcha(captchaBuffer: IoBuffer): String? = captchaLock.withLock { internal actual suspend fun solveCaptcha(captchaBuffer: IoBuffer): String? = captchaLock.withLock {
val captcha = captchaBuffer.readBytes() val tempFile = File(System.getProperty("user.dir") + "/temp/Captcha.png").also {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
MiraiLogger.verbose(ImageIO.read(captcha.inputStream()).createCharImg()) it.createNewFile(); @Suppress("EXPERIMENTAL_API_USAGE")
it.writeChannel().writeFully(captchaBuffer)
} }
MiraiLogger.verbose("需要验证码登录, 验证码为 4 字母") }
withContext(Dispatchers.IO) {
MiraiLogger.info(ImageIO.read(tempFile.inputStream()).createCharImg())
}
MiraiLogger.info("需要验证码登录, 验证码为 4 字母")
try { try {
File(System.getProperty("user.dir") + "/temp/Captcha.png")
.let { withContext(Dispatchers.IO) { it.createNewFile(); it.writeBytes(captcha) } } MiraiLogger.info("若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png")
MiraiLogger.verbose("若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png")
} catch (e: Exception) { } catch (e: Exception) {
MiraiLogger.verbose("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片") MiraiLogger.info("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片")
} }
MiraiLogger.verbose("若要更换验证码, 请直接回车") MiraiLogger.info("若要更换验证码, 请直接回车")
readLine()?.takeUnless { it.isEmpty() || it.length != 4 } readLine()?.takeUnless { it.isEmpty() || it.length != 4 }
} }
......
...@@ -13,7 +13,7 @@ import net.mamoe.mirai.login ...@@ -13,7 +13,7 @@ import net.mamoe.mirai.login
import net.mamoe.mirai.message.* import net.mamoe.mirai.message.*
import net.mamoe.mirai.network.protocol.tim.packet.OutgoingRawPacket import net.mamoe.mirai.network.protocol.tim.packet.OutgoingRawPacket
import net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage import net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage
import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess import net.mamoe.mirai.network.protocol.tim.packet.login.ifFail
import net.mamoe.mirai.network.session import net.mamoe.mirai.network.session
import net.mamoe.mirai.qqAccount import net.mamoe.mirai.qqAccount
import net.mamoe.mirai.utils.io.hexToBytes import net.mamoe.mirai.utils.io.hexToBytes
...@@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.io.toByteArray ...@@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.io.toByteArray
import net.mamoe.mirai.utils.io.toUHexString import net.mamoe.mirai.utils.io.toUHexString
import net.mamoe.mirai.utils.toExternalImage import net.mamoe.mirai.utils.toExternalImage
import java.io.File import java.io.File
import kotlin.system.exitProcess
private fun readTestAccount(): BotAccount? { private fun readTestAccount(): BotAccount? {
val file = File("testAccount.txt") val file = File("testAccount.txt")
...@@ -48,7 +49,10 @@ suspend fun main() { ...@@ -48,7 +49,10 @@ suspend fun main() {
// 覆盖默认的配置 // 覆盖默认的配置
bot.login { bot.login {
randomDeviceName = false randomDeviceName = false
}.requireSuccess() }.ifFail {
bot.logger.error("Login failed: $it")
exitProcess(1)
}
bot.messageDSL() bot.messageDSL()
directlySubscribe(bot) directlySubscribe(bot)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment