mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-27 04:30:08 +08:00
[core] Move QRCodeLoginListener to .auth
This commit is contained in:
parent
6ceff4b6db
commit
b15c0b9cde
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2019-2023 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.auth
|
||||||
|
|
||||||
|
import net.mamoe.mirai.Bot
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二维码扫描登录监听器
|
||||||
|
* @since 2.15
|
||||||
|
*/
|
||||||
|
public interface QRCodeLoginListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用二维码登录时获取的二维码图片大小.
|
||||||
|
*/
|
||||||
|
public val qrCodeSize: Int get() = 3
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用二维码登录时获取的二维码边框宽度.
|
||||||
|
*/
|
||||||
|
public val qrCodeMargin: Int get() = 4
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用二维码登录时获取的二维码校正等级,必须为 1-3 之间.
|
||||||
|
*/
|
||||||
|
public val qrCodeEcLevel: Int get() = 2
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从服务器获取二维码时调用,在下级显示二维码并扫描.
|
||||||
|
*/
|
||||||
|
public fun onFetchQRCode(bot: Bot, data: ByteArray)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当二维码状态变化时调用.
|
||||||
|
* @see State
|
||||||
|
*/
|
||||||
|
public fun onStatusChanged(bot: Bot, state: State)
|
||||||
|
|
||||||
|
public enum class State {
|
||||||
|
/**
|
||||||
|
* 等待扫描中,请在此阶段请扫描二维码.
|
||||||
|
* @see QRCodeLoginListener.onFetchQRCode
|
||||||
|
*/
|
||||||
|
WAITING_FOR_SCAN,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二维码已扫描,等待扫描端确认登录.
|
||||||
|
*/
|
||||||
|
WAITING_FOR_CONFIRM,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扫描后取消了确认.
|
||||||
|
*/
|
||||||
|
CANCELLED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二维码超时,必须重新获取二维码.
|
||||||
|
*/
|
||||||
|
TIMEOUT,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二维码已确认,将会继续登录.
|
||||||
|
*/
|
||||||
|
CONFIRMED,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认状态,在登录前通常为此状态.
|
||||||
|
*/
|
||||||
|
DEFAULT,
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@ package net.mamoe.mirai.utils
|
|||||||
|
|
||||||
import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
|
import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
|
import net.mamoe.mirai.auth.QRCodeLoginListener
|
||||||
import net.mamoe.mirai.network.LoginFailedException
|
import net.mamoe.mirai.network.LoginFailedException
|
||||||
import net.mamoe.mirai.network.RetryLaterException
|
import net.mamoe.mirai.network.RetryLaterException
|
||||||
import net.mamoe.mirai.network.UnsupportedSmsLoginException
|
import net.mamoe.mirai.network.UnsupportedSmsLoginException
|
||||||
@ -27,21 +28,6 @@ import kotlin.jvm.JvmName
|
|||||||
* @see BotConfiguration.loginSolver
|
* @see BotConfiguration.loginSolver
|
||||||
*/
|
*/
|
||||||
public abstract class LoginSolver {
|
public abstract class LoginSolver {
|
||||||
/**
|
|
||||||
* 使用二维码登录时获取的二维码图片大小.
|
|
||||||
*/
|
|
||||||
public open val qrCodeSize: Int = 3
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用二维码登录时获取的二维码边框宽度.
|
|
||||||
*/
|
|
||||||
public open val qrCodeMargin: Int = 4
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用二维码登录时获取的二维码校正等级,必须为 1-3 之间.
|
|
||||||
*/
|
|
||||||
public open val qrCodeEcLevel: Int = 2
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理图片验证码, 返回图片验证码内容.
|
* 处理图片验证码, 返回图片验证码内容.
|
||||||
*
|
*
|
||||||
@ -148,56 +134,6 @@ public abstract class LoginSolver {
|
|||||||
throw UnsupportedSmsLoginException("This login session requires device verification, but current LoginSolver($this) does not support it. Please override `LoginSolver.onSolveDeviceVerification`.")
|
throw UnsupportedSmsLoginException("This login session requires device verification, but current LoginSolver($this) does not support it. Please override `LoginSolver.onSolveDeviceVerification`.")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 二维码扫描登录监听器
|
|
||||||
* @since 2.15
|
|
||||||
*/
|
|
||||||
public interface QRCodeLoginListener {
|
|
||||||
/**
|
|
||||||
* 从服务器获取二维码时调用,在下级显示二维码并扫描.
|
|
||||||
*/
|
|
||||||
public fun onFetchQRCode(bot: Bot, data: ByteArray)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当二维码状态变化时调用.
|
|
||||||
* @see State
|
|
||||||
*/
|
|
||||||
public fun onStatusChanged(bot: Bot, state: State)
|
|
||||||
|
|
||||||
public enum class State {
|
|
||||||
/**
|
|
||||||
* 等待扫描中,请在此阶段请扫描二维码.
|
|
||||||
* @see QRCodeLoginListener.onFetchQRCode
|
|
||||||
*/
|
|
||||||
WAITING_FOR_SCAN,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 二维码已扫描,等待扫描端确认登录.
|
|
||||||
*/
|
|
||||||
WAITING_FOR_CONFIRM,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 扫描后取消了确认.
|
|
||||||
*/
|
|
||||||
CANCELLED,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 二维码超时,必须重新获取二维码.
|
|
||||||
*/
|
|
||||||
TIMEOUT,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 二维码已确认,将会继续登录.
|
|
||||||
*/
|
|
||||||
CONFIRMED,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 默认状态,在登录前通常为此状态.
|
|
||||||
*/
|
|
||||||
DEFAULT,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public companion object {
|
public companion object {
|
||||||
/**
|
/**
|
||||||
* 当前平台默认的 [LoginSolver]。
|
* 当前平台默认的 [LoginSolver]。
|
||||||
|
@ -17,6 +17,7 @@ import kotlinx.coroutines.sync.Mutex
|
|||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
|
import net.mamoe.mirai.auth.QRCodeLoginListener
|
||||||
import net.mamoe.mirai.network.NoStandardInputForCaptchaException
|
import net.mamoe.mirai.network.NoStandardInputForCaptchaException
|
||||||
import net.mamoe.mirai.utils.StandardCharImageLoginSolver.Companion.createBlocking
|
import net.mamoe.mirai.utils.StandardCharImageLoginSolver.Companion.createBlocking
|
||||||
import java.awt.Image
|
import java.awt.Image
|
||||||
|
@ -11,13 +11,13 @@ package net.mamoe.mirai.internal.network.components
|
|||||||
|
|
||||||
import kotlinx.atomicfu.atomic
|
import kotlinx.atomicfu.atomic
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import net.mamoe.mirai.auth.QRCodeLoginListener
|
||||||
import net.mamoe.mirai.internal.network.QQAndroidClient
|
import net.mamoe.mirai.internal.network.QQAndroidClient
|
||||||
import net.mamoe.mirai.internal.network.QRCodeLoginData
|
import net.mamoe.mirai.internal.network.QRCodeLoginData
|
||||||
import net.mamoe.mirai.internal.network.component.ComponentKey
|
import net.mamoe.mirai.internal.network.component.ComponentKey
|
||||||
import net.mamoe.mirai.internal.network.handler.NetworkHandler
|
import net.mamoe.mirai.internal.network.handler.NetworkHandler
|
||||||
import net.mamoe.mirai.internal.network.protocol.packet.login.WtLogin
|
import net.mamoe.mirai.internal.network.protocol.packet.login.WtLogin
|
||||||
import net.mamoe.mirai.internal.utils.MiraiProtocolInternal.Companion.asInternal
|
import net.mamoe.mirai.internal.utils.MiraiProtocolInternal.Companion.asInternal
|
||||||
import net.mamoe.mirai.utils.LoginSolver
|
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
import net.mamoe.mirai.utils.debug
|
import net.mamoe.mirai.utils.debug
|
||||||
|
|
||||||
@ -66,14 +66,14 @@ internal class QRCodeLoginProcessorPreLoaded(
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal class QRCodeLoginProcessorImpl(
|
internal class QRCodeLoginProcessorImpl(
|
||||||
private val qrCodeLoginListener: LoginSolver.QRCodeLoginListener,
|
private val qrCodeLoginListener: QRCodeLoginListener,
|
||||||
private val size: Int,
|
private val size: Int,
|
||||||
private val margin: Int,
|
private val margin: Int,
|
||||||
private val ecLevel: Int,
|
private val ecLevel: Int,
|
||||||
private val logger: MiraiLogger,
|
private val logger: MiraiLogger,
|
||||||
) : QRCodeLoginProcessor {
|
) : QRCodeLoginProcessor {
|
||||||
|
|
||||||
private var state = atomic(LoginSolver.QRCodeLoginListener.State.DEFAULT)
|
private var state = atomic(QRCodeLoginListener.State.DEFAULT)
|
||||||
|
|
||||||
private suspend fun requestQRCode(
|
private suspend fun requestQRCode(
|
||||||
handler: NetworkHandler,
|
handler: NetworkHandler,
|
||||||
@ -133,24 +133,24 @@ internal class QRCodeLoginProcessorImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun WtLogin.TransEmp.Response.mapProtocolState(): LoginSolver.QRCodeLoginListener.State {
|
private fun WtLogin.TransEmp.Response.mapProtocolState(): QRCodeLoginListener.State {
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is WtLogin.TransEmp.Response.QRCodeStatus -> when (this.state) {
|
is WtLogin.TransEmp.Response.QRCodeStatus -> when (this.state) {
|
||||||
WtLogin.TransEmp.Response.QRCodeStatus.State.WAITING_FOR_SCAN ->
|
WtLogin.TransEmp.Response.QRCodeStatus.State.WAITING_FOR_SCAN ->
|
||||||
LoginSolver.QRCodeLoginListener.State.WAITING_FOR_SCAN
|
QRCodeLoginListener.State.WAITING_FOR_SCAN
|
||||||
|
|
||||||
WtLogin.TransEmp.Response.QRCodeStatus.State.WAITING_FOR_CONFIRM ->
|
WtLogin.TransEmp.Response.QRCodeStatus.State.WAITING_FOR_CONFIRM ->
|
||||||
LoginSolver.QRCodeLoginListener.State.WAITING_FOR_CONFIRM
|
QRCodeLoginListener.State.WAITING_FOR_CONFIRM
|
||||||
|
|
||||||
WtLogin.TransEmp.Response.QRCodeStatus.State.CANCELLED ->
|
WtLogin.TransEmp.Response.QRCodeStatus.State.CANCELLED ->
|
||||||
LoginSolver.QRCodeLoginListener.State.CANCELLED
|
QRCodeLoginListener.State.CANCELLED
|
||||||
|
|
||||||
WtLogin.TransEmp.Response.QRCodeStatus.State.TIMEOUT ->
|
WtLogin.TransEmp.Response.QRCodeStatus.State.TIMEOUT ->
|
||||||
LoginSolver.QRCodeLoginListener.State.TIMEOUT
|
QRCodeLoginListener.State.TIMEOUT
|
||||||
}
|
}
|
||||||
|
|
||||||
is WtLogin.TransEmp.Response.QRCodeConfirmed ->
|
is WtLogin.TransEmp.Response.QRCodeConfirmed ->
|
||||||
LoginSolver.QRCodeLoginListener.State.CONFIRMED
|
QRCodeLoginListener.State.CONFIRMED
|
||||||
|
|
||||||
is WtLogin.TransEmp.Response.FetchQRCode ->
|
is WtLogin.TransEmp.Response.FetchQRCode ->
|
||||||
error("$this cannot be mapped to listener state.")
|
error("$this cannot be mapped to listener state.")
|
||||||
|
Loading…
Reference in New Issue
Block a user