LoginSolver.kt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. package net.mamoe.mirai.utils
  10. import net.mamoe.mirai.Bot
  11. import net.mamoe.mirai.network.LoginFailedException
  12. import net.mamoe.mirai.utils.LoginSolver.Companion.Default
  13. import kotlin.jvm.JvmField
  14. /**
  15. * 验证码, 设备锁解决器
  16. *
  17. * @see Default
  18. * @see BotConfiguration.loginSolver
  19. */
  20. public expect abstract class LoginSolver() {
  21. /**
  22. * 处理图片验证码.
  23. *
  24. * 返回 `null` 以表示无法处理验证码, 将会刷新验证码或重试登录.
  25. * 抛出一个 [LoginFailedException] 以正常地终止登录, 抛出任意其他 [Exception] 将视为异常终止
  26. *
  27. * @throws LoginFailedException
  28. */
  29. public abstract suspend fun onSolvePicCaptcha(bot: Bot, data: ByteArray): String?
  30. /**
  31. * 为 `true` 表示支持滑动验证码, 遇到滑动验证码时 mirai 会请求 [onSolveSliderCaptcha].
  32. * 否则会跳过滑动验证码并告诉服务器此客户端不支持, 有可能导致登录失败
  33. */
  34. public open val isSliderCaptchaSupported: Boolean
  35. /**
  36. * 处理滑动验证码.
  37. *
  38. * 返回 `null` 以表示无法处理验证码, 将会刷新验证码或重试登录.
  39. * 抛出一个 [LoginFailedException] 以正常地终止登录, 抛出任意其他 [Exception] 将视为异常终止
  40. *
  41. * @throws LoginFailedException
  42. * @return 验证码解决成功后获得的 ticket.
  43. */
  44. public abstract suspend fun onSolveSliderCaptcha(bot: Bot, url: String): String?
  45. /**
  46. * 处理不安全设备验证.
  47. *
  48. * 返回值保留给将来使用. 目前在处理完成后返回任意内容 (包含 `null`) 均视为处理成功.
  49. * 抛出一个 [LoginFailedException] 以正常地终止登录, 抛出任意其他 [Exception] 将视为异常终止.
  50. *
  51. * @return 任意内容. 返回值保留以供未来更新.
  52. * @throws LoginFailedException
  53. */
  54. public abstract suspend fun onSolveUnsafeDeviceLoginVerify(bot: Bot, url: String): String?
  55. public companion object {
  56. /**
  57. * 当前平台默认的 [LoginSolver]。
  58. *
  59. * 检测策略:
  60. * 1. 若是 `mirai-core-api-android` 或 `android.util.Log` 存在, 返回 `null`.
  61. * 2. 检测 JVM 属性 `mirai.no-desktop`. 若存在, 返回 `StandardCharImageLoginSolver`
  62. * 3. 检测 JVM 桌面环境, 若支持, 返回 `SwingSolver`
  63. * 4. 返回 `StandardCharImageLoginSolver`
  64. *
  65. * @return `SwingSolver` 或 `StandardCharImageLoginSolver` 或 `null`
  66. */
  67. @JvmField
  68. public val Default: LoginSolver?
  69. @Suppress("unused")
  70. @Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
  71. public fun getDefault(): LoginSolver
  72. }
  73. }