Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
34f10fe2
Commit
34f10fe2
authored
Nov 01, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adjust login result
parent
e2171fd1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
14 deletions
+45
-14
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/LoginResult.kt
...oe.mirai/network/protocol/tim/packet/login/LoginResult.kt
+26
-3
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/CaptchaResolverJvm.kt
...vmMain/kotlin/net/mamoe/mirai/utils/CaptchaResolverJvm.kt
+13
-9
mirai-demos/mirai-demo-1/src/main/java/demo/subscribe/SubscribeSamples.kt
...i-demo-1/src/main/java/demo/subscribe/SubscribeSamples.kt
+6
-2
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/LoginResult.kt
View file @
34f10fe2
package
net.mamoe.mirai.network.protocol.tim.packet.login
import
net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult.SUCCESS
/**
* 登录结果. 除 [SUCCESS] 外均为失败.
* @see LoginResult.requireSuccess 要求成功
...
...
@@ -60,8 +62,29 @@ fun LoginResult.requireSuccess(lazyMessage: (LoginResult) -> String) {
/**
* 如果 [this] 不为 [LoginResult.SUCCESS] 就抛出消息为 "Login failed $this" 的 [IllegalStateException]
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则 [error]
*/
fun
LoginResult
.
requireSuccess
()
{
if
(
this
!=
LoginResult
.
SUCCESS
)
error
(
"Login failed: $this"
)
}
\ No newline at end of file
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
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/CaptchaResolverJvm.kt
View file @
34f10fe2
package
net.mamoe.mirai.utils
import
io.ktor.util.cio.writeChannel
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.sync.Mutex
import
kotlinx.coroutines.sync.withLock
import
kotlinx.coroutines.withContext
import
kotlinx.io.core.IoBuffer
import
kotlinx.io.core.readBytes
import
java.awt.Image
import
java.awt.image.BufferedImage
import
java.io.File
...
...
@@ -20,19 +20,23 @@ import kotlin.math.min
* @return 用户输入得到的验证码
*/
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
)
{
it
.
createNewFile
();
@Suppress
(
"EXPERIMENTAL_API_USAGE"
)
it
.
writeChannel
().
writeFully
(
captchaBuffer
)
}
}
withContext
(
Dispatchers
.
IO
)
{
MiraiLogger
.
verbose
(
ImageIO
.
read
(
captcha
.
inputStream
()).
createCharImg
())
MiraiLogger
.
info
(
ImageIO
.
read
(
tempFile
.
inputStream
()).
createCharImg
())
}
MiraiLogger
.
verbose
(
"需要验证码登录, 验证码为 4 字母"
)
MiraiLogger
.
info
(
"需要验证码登录, 验证码为 4 字母"
)
try
{
File
(
System
.
getProperty
(
"user.dir"
)
+
"/temp/Captcha.png"
)
.
let
{
withContext
(
Dispatchers
.
IO
)
{
it
.
createNewFile
();
it
.
writeBytes
(
captcha
)
}
}
MiraiLogger
.
verbose
(
"若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png"
)
MiraiLogger
.
info
(
"若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png"
)
}
catch
(
e
:
Exception
)
{
MiraiLogger
.
verbose
(
"无法写出验证码文件(${e.message}), 请尝试查看以上字符图片"
)
MiraiLogger
.
info
(
"无法写出验证码文件(${e.message}), 请尝试查看以上字符图片"
)
}
MiraiLogger
.
verbose
(
"若要更换验证码, 请直接回车"
)
MiraiLogger
.
info
(
"若要更换验证码, 请直接回车"
)
readLine
()
?.
takeUnless
{
it
.
isEmpty
()
||
it
.
length
!=
4
}
}
...
...
mirai-demos/mirai-demo-1/src/main/java/demo/subscribe/SubscribeSamples.kt
View file @
34f10fe2
...
...
@@ -13,7 +13,7 @@ import net.mamoe.mirai.login
import
net.mamoe.mirai.message.*
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.login.
requireSuccess
import
net.mamoe.mirai.network.protocol.tim.packet.login.
ifFail
import
net.mamoe.mirai.network.session
import
net.mamoe.mirai.qqAccount
import
net.mamoe.mirai.utils.io.hexToBytes
...
...
@@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.io.toByteArray
import
net.mamoe.mirai.utils.io.toUHexString
import
net.mamoe.mirai.utils.toExternalImage
import
java.io.File
import
kotlin.system.exitProcess
private
fun
readTestAccount
():
BotAccount
?
{
val
file
=
File
(
"testAccount.txt"
)
...
...
@@ -48,7 +49,10 @@ suspend fun main() {
// 覆盖默认的配置
bot
.
login
{
randomDeviceName
=
false
}.
requireSuccess
()
}.
ifFail
{
bot
.
logger
.
error
(
"Login failed: $it"
)
exitProcess
(
1
)
}
bot
.
messageDSL
()
directlySubscribe
(
bot
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment