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
24404f9a
Commit
24404f9a
authored
Oct 17, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename qqNumber as account
parent
09046ae6
Changes
17
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
80 additions
and
83 deletions
+80
-83
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
+6
-6
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotHelper.kt
...i-core/src/commonMain/kotlin/net.mamoe.mirai/BotHelper.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
...nMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
+4
-4
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt
...c/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt
+10
-10
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
....mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
+19
-19
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt
...mirai/network/protocol/tim/handler/ActionPacketHandler.kt
+12
-16
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/DataPacketSocketAdapter.kt
...i/network/protocol/tim/handler/DataPacketSocketAdapter.kt
+3
-3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/EventPacketHandler.kt
....mirai/network/protocol/tim/handler/EventPacketHandler.kt
+5
-5
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
...mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
+2
-2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt
...ai/network/protocol/tim/handler/TemporaryPacketHandler.kt
+6
-6
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/UploadImage.kt
...et.mamoe.mirai/network/protocol/tim/packet/UploadImage.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/BotAccount.kt
...src/commonMain/kotlin/net.mamoe.mirai/utils/BotAccount.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
...onMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
+1
-0
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt
...c/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt
+5
-5
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/UploadImageJvm.kt
...mamoe/mirai/network/protocol/tim/packet/UploadImageJvm.kt
+2
-2
mirai-core/src/jvmTest/kotlin/BadQQFilter.kt
mirai-core/src/jvmTest/kotlin/BadQQFilter.kt
+1
-1
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
+1
-1
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
View file @
24404f9a
...
...
@@ -21,9 +21,9 @@ import net.mamoe.mirai.utils.MiraiLogger
* [Bot] 由 3 个模块组成.
* [联系人管理][ContactSystem]: 可通过 [Bot.contacts] 访问
* [网络处理器][TIMBotNetworkHandler]: 可通过 [Bot.network] 访问
* [机器人账号信息][BotAccount]: 可通过 [Bot.
a
ccount] 访问
* [机器人账号信息][BotAccount]: 可通过 [Bot.
qqA
ccount] 访问
*
* 若你需要得到机器人的 QQ 账号, 请访问 [Bot.
a
ccount]
* 若你需要得到机器人的 QQ 账号, 请访问 [Bot.
qqA
ccount]
* 若你需要得到服务器上所有机器人列表, 请访问 [Bot.instances]
*
*
...
...
@@ -34,7 +34,7 @@ import net.mamoe.mirai.utils.MiraiLogger
* a [TIMBotNetworkHandler], which manages the connection to the server;
* a [BotAccount], which stores the account information(e.g. qq number the bot)
*
* To of all the QQ contacts, access [Bot.
a
ccount]
* To of all the QQ contacts, access [Bot.
qqA
ccount]
* To of all the Robot instance, access [Bot.instances]
*
*
...
...
@@ -52,10 +52,10 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
init
{
instances
.
add
(
this
)
this
.
logger
.
identity
=
"Bot"
+
this
.
id
+
"("
+
this
.
account
.
qqNumber
+
")"
this
.
logger
.
identity
=
"Bot"
+
this
.
id
+
"("
+
this
.
account
.
account
+
")"
}
override
fun
toString
():
String
=
"Bot{id=$id,qq=${account.
qqNumber
}}"
override
fun
toString
():
String
=
"Bot{id=$id,qq=${account.
account
}}"
/**
* [关闭][BotNetworkHandler.close]网络处理器, 取消所有运行在 [BotNetworkHandler.NetworkScope] 下的协程.
...
...
@@ -83,7 +83,7 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
* 通过群号码获取群对象.
* 注意: 在并发调用时, 这个方法并不是原子的.
*/
fun
getQQ
(
qqNumber
:
Long
):
QQ
=
qqs
.
getOrPut
(
qqNumber
)
{
QQ
(
this
@Bot
,
qqNumber
)
}
fun
getQQ
(
account
:
Long
):
QQ
=
qqs
.
getOrPut
(
account
)
{
QQ
(
this
@Bot
,
account
)
}
/**
* 通过群号码获取群对象.
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotHelper.kt
View file @
24404f9a
...
...
@@ -41,7 +41,7 @@ suspend fun Bot.login(configuration: LoginConfiguration.() -> Unit): LoginResult
suspend
fun
Bot
.
login
():
LoginResult
=
this
.
network
.
login
(
LoginConfiguration
.
Default
)
//BotAccount
val
Bot
.
qq
Number
:
Long
get
()
=
this
.
account
.
qqNumber
val
Bot
.
qq
Account
:
Long
get
()
=
this
.
account
.
account
//logging
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
View file @
24404f9a
...
...
@@ -2,7 +2,7 @@ package net.mamoe.mirai.network
import
kotlinx.coroutines.*
import
kotlinx.io.core.Closeable
import
net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.BotSocket
import
net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.BotSocket
Adapter
import
net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.LoginHandler
import
net.mamoe.mirai.network.protocol.tim.handler.*
import
net.mamoe.mirai.network.protocol.tim.packet.*
...
...
@@ -17,8 +17,8 @@ import kotlin.coroutines.ContinuationInterceptor
* [BotNetworkHandler] 是全异步和线程安全的.
*
* [BotNetworkHandler] 由 2 个模块构成:
* - [BotSocket]: 处理数据包底层的发送([ByteArray])
* - [PacketHandler]: 制作 [ClientPacket] 并传递给 [BotSocket] 发送; 分析 [ServerPacket] 并处理
* - [BotSocket
Adapter
]: 处理数据包底层的发送([ByteArray])
* - [PacketHandler]: 制作 [ClientPacket] 并传递给 [BotSocket
Adapter
] 发送; 分析 [ServerPacket] 并处理
*
* 其中, [PacketHandler] 由 3 个子模块构成:
* - [LoginHandler] 处理 sendTouch/login/verification code 相关
...
...
@@ -27,7 +27,7 @@ import kotlin.coroutines.ContinuationInterceptor
*
* A BotNetworkHandler is used to connect with Tencent servers.
*/
interface
BotNetworkHandler
<
Socket
:
DataPacketSocket
>
:
Closeable
{
interface
BotNetworkHandler
<
Socket
:
DataPacketSocket
Adapter
>
:
Closeable
{
/**
* [BotNetworkHandler] 的协程作用域.
* 所有 [BotNetworkHandler] 的协程均启动在此作用域下.
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/
Login
Session.kt
→
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/
Bot
Session.kt
View file @
24404f9a
...
...
@@ -4,13 +4,12 @@ import kotlinx.coroutines.CompletableJob
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Job
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocket
import
net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocket
Adapter
import
net.mamoe.mirai.network.protocol.tim.handler.TemporaryPacketHandler
import
net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
import
net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
import
net.mamoe.mirai.utils.getGTK
import
kotlin.coroutines.coroutineContext
import
kotlin.jvm.JvmSynthetic
/**
* 登录会话. 当登录完成后, 客户端会拿到 sessionKey.
...
...
@@ -18,10 +17,10 @@ import kotlin.jvm.JvmSynthetic
*
* @author Him188moe
*/
class
Login
Session
(
class
Bot
Session
(
val
bot
:
Bot
,
val
sessionKey
:
ByteArray
,
//TODO 协议抽象? 可能并不是所有协议均需要 sessionKey
val
socket
:
DataPacketSocket
,
val
socket
:
DataPacketSocket
Adapter
,
val
scope
:
CoroutineScope
)
{
...
...
@@ -35,7 +34,7 @@ class LoginSession(
*/
@ExperimentalStdlibApi
var
sKey
:
String
=
""
set
(
value
)
{
internal
set
(
value
)
{
field
=
value
gtk
=
getGTK
(
value
)
}
...
...
@@ -44,8 +43,7 @@ class LoginSession(
* Web api 使用
*/
var
gtk
:
Int
=
0
val
isOpen
:
Boolean
get
()
=
socket
.
isOpen
private
set
/**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket].
...
...
@@ -73,7 +71,7 @@ class LoginSession(
/**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket].
* 发送成功后, 该方法会等待收到 [ServerPacket] 直到超时.
* 由于包名可能过长, 可使用 `DataPacketSocket.expectPacket(PacketProcessor)` 替代.
* 由于包名可能过长, 可使用 `DataPacketSocket
Adapter
.expectPacket(PacketProcessor)` 替代.
*
* 实现方法:
* ```kotlin
...
...
@@ -86,7 +84,7 @@ class LoginSession(
* @param toSend 将要发送的包
* @param handler 处理期待的包
*/
@JvmSynthetic
//
@JvmSynthetic
suspend
inline
fun
<
reified
P
:
ServerPacket
>
expectPacket
(
toSend
:
ClientPacket
,
noinline
handler
:
suspend
(
P
)
->
Unit
):
CompletableJob
{
val
job
=
coroutineContext
[
Job
].
takeIf
{
it
!=
null
}
?.
let
{
Job
(
it
)
}
?:
Job
()
this
.
bot
.
network
.
addHandler
(
TemporaryPacketHandler
(
P
::
class
,
job
,
this
).
also
{
...
...
@@ -98,4 +96,6 @@ class LoginSession(
}
suspend
fun
LoginSession
.
distributePacket
(
packet
:
ServerPacket
)
=
this
.
socket
.
distributePacket
(
packet
)
\ No newline at end of file
suspend
fun
BotSession
.
distributePacket
(
packet
:
ServerPacket
)
=
this
.
socket
.
distributePacket
(
packet
)
val
BotSession
.
isOpen
:
Boolean
get
()
=
socket
.
isOpen
val
BotSession
.
account
:
Long
get
()
=
bot
.
account
.
account
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
View file @
24404f9a
...
...
@@ -14,7 +14,7 @@ import net.mamoe.mirai.event.events.PacketSentEvent
import
net.mamoe.mirai.event.events.ServerPacketReceivedEvent
import
net.mamoe.mirai.event.subscribe
import
net.mamoe.mirai.network.BotNetworkHandler
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.protocol.tim.handler.*
import
net.mamoe.mirai.network.protocol.tim.packet.*
import
net.mamoe.mirai.network.protocol.tim.packet.login.*
...
...
@@ -26,10 +26,10 @@ import net.mamoe.mirai.utils.*
*
* @see BotNetworkHandler
*/
internal
class
TIMBotNetworkHandler
internal
constructor
(
private
val
bot
:
Bot
)
:
BotNetworkHandler
<
TIMBotNetworkHandler
.
BotSocket
>,
PacketHandlerList
()
{
internal
class
TIMBotNetworkHandler
internal
constructor
(
private
val
bot
:
Bot
)
:
BotNetworkHandler
<
TIMBotNetworkHandler
.
BotSocket
Adapter
>,
PacketHandlerList
()
{
override
val
NetworkScope
:
CoroutineScope
=
CoroutineScope
(
Dispatchers
.
Default
)
override
lateinit
var
socket
:
BotSocket
override
lateinit
var
socket
:
BotSocket
Adapter
internal
val
temporaryPacketHandlers
=
mutableListOf
<
TemporaryPacketHandler
<
*
>>()
private
val
handlersLock
=
Mutex
()
...
...
@@ -47,7 +47,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
override
suspend
fun
login
(
configuration
:
LoginConfiguration
):
LoginResult
{
TIMProtocol
.
SERVER_IP
.
forEach
{
bot
.
logger
.
logInfo
(
"Connecting server $it"
)
this
.
socket
=
BotSocket
(
it
,
configuration
)
this
.
socket
=
BotSocket
Adapter
(
it
,
configuration
)
loginResult
=
CompletableDeferred
()
...
...
@@ -69,7 +69,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
//private | internal
private
fun
onLoggedIn
(
sessionKey
:
ByteArray
)
{
require
(
size
==
0
)
{
"Already logged in"
}
val
session
=
Login
Session
(
bot
,
sessionKey
,
socket
,
NetworkScope
)
val
session
=
Bot
Session
(
bot
,
sessionKey
,
socket
,
NetworkScope
)
add
(
EventPacketHandler
(
session
).
asNode
(
EventPacketHandler
))
add
(
ActionPacketHandler
(
session
).
asNode
(
ActionPacketHandler
))
...
...
@@ -96,7 +96,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
override
suspend
fun
sendPacket
(
packet
:
ClientPacket
)
=
socket
.
sendPacket
(
packet
)
internal
inner
class
BotSocket
(
override
val
serverIp
:
String
,
val
configuration
:
LoginConfiguration
)
:
DataPacketSocket
{
internal
inner
class
BotSocket
Adapter
(
override
val
serverIp
:
String
,
val
configuration
:
LoginConfiguration
)
:
DataPacketSocketAdapter
{
override
val
channel
:
PlatformDatagramChannel
=
PlatformDatagramChannel
(
serverIp
,
8000
)
override
val
isOpen
:
Boolean
get
()
=
channel
.
isOpen
...
...
@@ -147,7 +147,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
loginResult
.
complete
(
LoginResult
.
TIMEOUT
)
}
}
sendPacket
(
ClientTouchPacket
(
bot
.
qq
Number
,
this
.
serverIp
))
sendPacket
(
ClientTouchPacket
(
bot
.
qq
Account
,
this
.
serverIp
))
return
loginResult
.
await
()
}
...
...
@@ -189,7 +189,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
if
(
packet
is
ServerEventPacket
)
{
//no need to sync acknowledgement packets
NetworkScope
.
launch
{
sendPacket
(
packet
.
ResponsePacket
(
bot
.
qq
Number
,
sessionKey
))
sendPacket
(
packet
.
ResponsePacket
(
bot
.
qq
Account
,
sessionKey
))
}
}
...
...
@@ -293,7 +293,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
is
ServerTouchResponsePacket
->
{
if
(
packet
.
serverIP
!=
null
)
{
//redirection
socket
.
close
()
socket
=
BotSocket
(
packet
.
serverIP
!!
,
socket
.
configuration
)
socket
=
BotSocket
Adapter
(
packet
.
serverIP
!!
,
socket
.
configuration
)
bot
.
logger
.
logPurple
(
"Redirecting to ${packet.serverIP}"
)
loginResult
.
complete
(
socket
.
resendTouch
())
}
else
{
//password submission
...
...
@@ -302,7 +302,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
this
.
token0825
=
packet
.
token0825
socket
.
sendPacket
(
ClientPasswordSubmissionPacket
(
bot
=
bot
.
qq
Number
,
bot
=
bot
.
qq
Account
,
password
=
bot
.
account
.
password
,
loginTime
=
loginTime
,
loginIP
=
loginIP
,
...
...
@@ -324,7 +324,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
this
.
token00BA
=
packet
.
token00BA
socket
.
sendPacket
(
ClientPasswordSubmissionPacket
(
bot
=
bot
.
qq
Number
,
bot
=
bot
.
qq
Account
,
password
=
bot
.
account
.
password
,
loginTime
=
loginTime
,
loginIP
=
loginIP
,
...
...
@@ -342,7 +342,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
if
(
packet
.
unknownBoolean
==
true
)
{
this
.
captchaSectionId
=
1
socket
.
sendPacket
(
ClientCaptchaTransmissionRequestPacket
(
bot
.
qq
Number
,
this
.
token0825
,
this
.
captchaSectionId
++,
packet
.
token00BA
))
socket
.
sendPacket
(
ClientCaptchaTransmissionRequestPacket
(
bot
.
qq
Account
,
this
.
token0825
,
this
.
captchaSectionId
++,
packet
.
token00BA
))
}
}
...
...
@@ -363,19 +363,19 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
this
.
captchaCache
=
null
if
(
code
==
null
)
{
this
.
captchaSectionId
=
1
//意味着正在刷新验证码
socket
.
sendPacket
(
ClientCaptchaRefreshPacket
(
bot
.
qq
Number
,
token0825
))
socket
.
sendPacket
(
ClientCaptchaRefreshPacket
(
bot
.
qq
Account
,
token0825
))
}
else
{
this
.
captchaSectionId
=
0
//意味着已经提交验证码
socket
.
sendPacket
(
ClientCaptchaSubmitPacket
(
bot
.
qq
Number
,
token0825
,
code
,
packet
.
verificationToken
))
socket
.
sendPacket
(
ClientCaptchaSubmitPacket
(
bot
.
qq
Account
,
token0825
,
code
,
packet
.
verificationToken
))
}
}
else
{
socket
.
sendPacket
(
ClientCaptchaTransmissionRequestPacket
(
bot
.
qq
Number
,
token0825
,
captchaSectionId
++,
packet
.
token00BA
))
socket
.
sendPacket
(
ClientCaptchaTransmissionRequestPacket
(
bot
.
qq
Account
,
token0825
,
captchaSectionId
++,
packet
.
token00BA
))
}
}
is
ServerLoginResponseSuccessPacket
->
{
this
.
sessionResponseDecryptionKey
=
packet
.
sessionResponseDecryptionKey
socket
.
sendPacket
(
ClientSessionRequestPacket
(
bot
.
qq
Number
,
socket
.
serverIp
,
packet
.
token38
,
packet
.
token88
,
packet
.
encryptionKey
))
socket
.
sendPacket
(
ClientSessionRequestPacket
(
bot
.
qq
Account
,
socket
.
serverIp
,
packet
.
token38
,
packet
.
token88
,
packet
.
encryptionKey
))
}
//是ClientPasswordSubmissionPacket之后服务器回复的可能之一
...
...
@@ -383,7 +383,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
this
.
privateKey
=
packet
.
privateKeyUpdate
socket
.
sendPacket
(
ClientPasswordSubmissionPacket
(
bot
=
bot
.
qq
Number
,
bot
=
bot
.
qq
Account
,
password
=
bot
.
account
.
password
,
loginTime
=
loginTime
,
loginIP
=
loginIP
,
...
...
@@ -400,7 +400,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
heartbeatJob
=
NetworkScope
.
launch
{
while
(
socket
.
isOpen
)
{
delay
(
90000
)
socket
.
sendPacket
(
ClientHeartbeatPacket
(
bot
.
qq
Number
,
sessionKey
))
socket
.
sendPacket
(
ClientHeartbeatPacket
(
bot
.
qq
Account
,
sessionKey
))
}
}
...
...
@@ -445,7 +445,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
@Suppress
(
"MemberVisibilityCanBePrivate"
)
suspend
fun
setOnlineStatus
(
status
:
OnlineStatus
)
{
socket
.
sendPacket
(
ClientChangeOnlineStatusPacket
(
bot
.
qq
Number
,
sessionKey
,
status
))
socket
.
sendPacket
(
ClientChangeOnlineStatusPacket
(
bot
.
qq
Account
,
sessionKey
,
status
))
}
fun
close
()
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt
View file @
24404f9a
...
...
@@ -6,7 +6,8 @@ import kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.launch
import
net.mamoe.mirai.network.LoginSession
import
net.mamoe.mirai.network.BotSession
import
net.mamoe.mirai.network.isOpen
import
net.mamoe.mirai.network.protocol.tim.packet.*
import
net.mamoe.mirai.network.protocol.tim.packet.action.AddFriendResult
import
net.mamoe.mirai.network.protocol.tim.packet.action.ClientAddFriendPacket
...
...
@@ -15,9 +16,6 @@ import net.mamoe.mirai.network.protocol.tim.packet.action.ServerCanAddFriendResp
import
net.mamoe.mirai.network.protocol.tim.packet.login.ClientSKeyRefreshmentRequestPacket
import
net.mamoe.mirai.network.protocol.tim.packet.login.ClientSKeyRequestPacket
import
net.mamoe.mirai.network.protocol.tim.packet.login.ServerSKeyResponsePacket
import
net.mamoe.mirai.utils.getGTK
import
net.mamoe.mirai.utils.hexToBytes
import
kotlin.properties.Delegates
/**
* 动作: 获取好友列表, 点赞, 踢人等.
...
...
@@ -25,7 +23,7 @@ import kotlin.properties.Delegates
*
* @author Him188moe
*/
class
ActionPacketHandler
(
session
:
Login
Session
)
:
PacketHandler
(
session
)
{
class
ActionPacketHandler
(
session
:
Bot
Session
)
:
PacketHandler
(
session
)
{
companion
object
Key
:
PacketHandler
.
Key
<
ActionPacketHandler
>
private
val
addFriendSessions
=
mutableListOf
<
AddFriendSession
>()
...
...
@@ -60,17 +58,15 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
is
ServerSKeyResponsePacket
.
Encrypted
->
session
.
socket
.
distributePacket
(
packet
.
decrypt
(
session
.
sessionKey
))
is
ServerSKeyResponsePacket
->
{
session
.
sKey
=
packet
.
sKey
session
.
cookies
=
"uin=o"
+
session
.
bot
.
account
.
qqNumber
+
";skey="
+
session
.
sKey
+
";"
session
.
cookies
=
"uin=o"
+
session
.
bot
.
account
.
account
+
";skey="
+
session
.
sKey
+
";"
sKeyRefresherJob
=
session
.
scope
.
launch
{
while
(
session
.
isOpen
)
{
delay
(
1800000
)
session
.
socket
.
sendPacket
(
ClientSKeyRefreshmentRequestPacket
(
session
.
bot
.
account
.
qqNumber
,
session
.
sessionKey
))
session
.
socket
.
sendPacket
(
ClientSKeyRefreshmentRequestPacket
(
session
.
bot
.
account
.
account
,
session
.
sessionKey
))
}
}
session
.
gtk
=
getGTK
(
session
.
sKey
)
}
is
ServerEventPacket
.
Raw
.
Encrypted
->
session
.
socket
.
distributePacket
(
packet
.
decrypt
(
session
.
sessionKey
))
...
...
@@ -82,9 +78,9 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
}
//@JvmSynthetic
suspend
fun
addFriend
(
qqNumber
:
Long
,
message
:
Lazy
<
String
>
=
lazyOf
(
""
)):
CompletableDeferred
<
AddFriendResult
>
{
suspend
fun
addFriend
(
account
:
Long
,
message
:
Lazy
<
String
>
=
lazyOf
(
""
)):
CompletableDeferred
<
AddFriendResult
>
{
val
future
=
CompletableDeferred
<
AddFriendResult
>()
val
session
=
AddFriendSession
(
qqNumber
,
future
,
message
)
val
session
=
AddFriendSession
(
account
,
future
,
message
)
// uploadImageSessions.add(session)
session
.
sendAddRequest
()
return
future
...
...
@@ -92,12 +88,12 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
suspend
fun
requestSKey
()
{
session
.
socket
.
sendPacket
(
ClientSKeyRequestPacket
(
session
.
bot
.
account
.
qqNumber
,
session
.
sessionKey
))
session
.
socket
.
sendPacket
(
ClientSKeyRequestPacket
(
session
.
bot
.
account
.
account
,
session
.
sessionKey
))
}
suspend
fun
requestAccountInfo
()
{
session
.
socket
.
sendPacket
(
ClientAccountInfoRequestPacket
(
session
.
bot
.
account
.
qqNumber
,
session
.
sessionKey
))
session
.
socket
.
sendPacket
(
ClientAccountInfoRequestPacket
(
session
.
bot
.
account
.
account
,
session
.
sessionKey
))
}
override
fun
close
()
{
...
...
@@ -135,7 +131,7 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
}
ServerCanAddFriendResponsePacket
.
State
.
REQUIRE_VERIFICATION
->
{
// session.socket.sendPacket(ClientAddFriendPacket(session.bot.account.
qqNumber
, qq, session.sessionKey))
// session.socket.sendPacket(ClientAddFriendPacket(session.bot.account.
account
, qq, session.sessionKey))
}
ServerCanAddFriendResponsePacket
.
State
.
NOT_REQUIRE_VERIFICATION
->
{
...
...
@@ -188,7 +184,7 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
}
ServerCanAddFriendResponsePacket
.
State
.
REQUIRE_VERIFICATION
->
{
session
.
socket
.
sendPacket
(
ClientAddFriendPacket
(
session
.
bot
.
account
.
qqNumber
,
qq
,
session
.
sessionKey
))
session
.
socket
.
sendPacket
(
ClientAddFriendPacket
(
session
.
bot
.
account
.
account
,
qq
,
session
.
sessionKey
))
}
ServerCanAddFriendResponsePacket
.
State
.
NOT_REQUIRE_VERIFICATION
->
{
...
...
@@ -203,7 +199,7 @@ class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
suspend
fun
sendAddRequest
()
{
session
.
socket
.
sendPacket
(
ClientCanAddFriendPacket
(
session
.
bot
.
account
.
qqNumber
,
qq
,
session
.
sessionKey
))
session
.
socket
.
sendPacket
(
ClientCanAddFriendPacket
(
session
.
bot
.
account
.
account
,
qq
,
session
.
sessionKey
))
}
fun
close
()
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/DataPacketSocket.kt
→
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/DataPacketSocket
Adapter
.kt
View file @
24404f9a
...
...
@@ -3,7 +3,7 @@ package net.mamoe.mirai.network.protocol.tim.handler
import
kotlinx.io.core.Closeable
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.event.events.ServerPacketReceivedEvent
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler
import
net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
import
net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
...
...
@@ -16,7 +16,7 @@ import net.mamoe.mirai.utils.PlatformDatagramChannel
*
* @author Him188moe
*/
interface
DataPacketSocket
:
Closeable
{
interface
DataPacketSocket
Adapter
:
Closeable
{
val
owner
:
Bot
val
serverIp
:
String
...
...
@@ -41,7 +41,7 @@ interface DataPacketSocket : Closeable {
*
* 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回.
*
* @see [
Login
Session.expectPacket] kotlin DSL
* @see [
Bot
Session.expectPacket] kotlin DSL
*/
suspend
fun
sendPacket
(
packet
:
ClientPacket
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/EventPacketHandler.kt
View file @
24404f9a
...
...
@@ -8,7 +8,7 @@ import net.mamoe.mirai.event.events.GroupMessageEvent
import
net.mamoe.mirai.getGroupByNumber
import
net.mamoe.mirai.getQQ
import
net.mamoe.mirai.message.MessageChain
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.distributePacket
import
net.mamoe.mirai.network.protocol.tim.packet.*
import
net.mamoe.mirai.network.protocol.tim.packet.action.ClientSendFriendMessagePacket
...
...
@@ -23,7 +23,7 @@ import net.mamoe.mirai.utils.MiraiLogger
* @author Him188moe
*/
@Suppress
(
"EXPERIMENTAL_API_USAGE"
)
class
EventPacketHandler
(
session
:
Login
Session
)
:
PacketHandler
(
session
)
{
class
EventPacketHandler
(
session
:
Bot
Session
)
:
PacketHandler
(
session
)
{
companion
object
Key
:
PacketHandler
.
Key
<
EventPacketHandler
>
...
...
@@ -44,7 +44,7 @@ class EventPacketHandler(session: LoginSession) : PacketHandler(session) {
is
ServerGroupMessageEventPacket
->
{
if
(
ignoreMessage
)
return
if
(
packet
.
qq
.
toLong
()
==
bot
.
account
.
qqNumber
)
return
if
(
packet
.
qq
.
toLong
()
==
bot
.
account
.
account
)
return
GroupMessageEvent
(
bot
,
bot
.
getGroupByNumber
(
packet
.
groupNumber
),
bot
.
getQQ
(
packet
.
qq
),
packet
.
message
).
broadcast
()
}
...
...
@@ -80,10 +80,10 @@ class EventPacketHandler(session: LoginSession) : PacketHandler(session) {
}
suspend
fun
sendFriendMessage
(
qq
:
QQ
,
message
:
MessageChain
)
{
session
.
socket
.
sendPacket
(
ClientSendFriendMessagePacket
(
session
.
bot
.
account
.
qqNumber
,
qq
.
number
,
session
.
sessionKey
,
message
))
session
.
socket
.
sendPacket
(
ClientSendFriendMessagePacket
(
session
.
bot
.
account
.
account
,
qq
.
number
,
session
.
sessionKey
,
message
))
}
suspend
fun
sendGroupMessage
(
group
:
Group
,
message
:
MessageChain
)
{
session
.
socket
.
sendPacket
(
ClientSendGroupMessagePacket
(
session
.
bot
.
account
.
qqNumber
,
group
.
groupId
,
session
.
sessionKey
,
message
))
session
.
socket
.
sendPacket
(
ClientSendGroupMessagePacket
(
session
.
bot
.
account
.
account
,
group
.
groupId
,
session
.
sessionKey
,
message
))
}
}
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
View file @
24404f9a
package
net.mamoe.mirai.network.protocol.tim.handler
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
import
kotlin.reflect.KClass
...
...
@@ -8,7 +8,7 @@ import kotlin.reflect.KClass
* 数据包(接受/发送)处理器
*/
abstract
class
PacketHandler
(
val
session
:
Login
Session
val
session
:
Bot
Session
)
{
abstract
suspend
fun
onPacketReceived
(
packet
:
ServerPacket
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt
View file @
24404f9a
package
net.mamoe.mirai.network.protocol.tim.handler
import
kotlinx.coroutines.CompletableJob
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
import
net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
import
kotlin.reflect.KClass
...
...
@@ -17,19 +17,19 @@ import kotlin.reflect.KClass
* }
* ```
*
* @see
Login
Session.expectPacket
* @see
Bot
Session.expectPacket
*/
class
TemporaryPacketHandler
<
P
:
ServerPacket
>(
private
val
expectationClass
:
KClass
<
P
>,
private
val
deferred
:
CompletableJob
,
private
val
fromSession
:
Login
Session
private
val
fromSession
:
Bot
Session
)
{
private
lateinit
var
toSend
:
ClientPacket
private
lateinit
var
expect
:
suspend
(
P
)
->
Unit
lateinit
var
session
:
Login
Session
//无需覆盖
lateinit
var
session
:
Bot
Session
//无需覆盖
fun
toSend
(
packet
:
()
->
ClientPacket
)
{
this
.
toSend
=
packet
()
...
...
@@ -44,12 +44,12 @@ class TemporaryPacketHandler<P : ServerPacket>(
this
.
expect
=
handler
}
suspend
fun
send
(
session
:
Login
Session
)
{
suspend
fun
send
(
session
:
Bot
Session
)
{
this
.
session
=
session
session
.
socket
.
sendPacket
(
toSend
)
}
suspend
fun
shouldRemove
(
session
:
Login
Session
,
packet
:
ServerPacket
):
Boolean
{
suspend
fun
shouldRemove
(
session
:
Bot
Session
,
packet
:
ServerPacket
):
Boolean
{
if
(
expectationClass
.
isInstance
(
packet
)
&&
session
===
this
.
fromSession
)
{
kotlin
.
runCatching
{
@Suppress
(
"UNCHECKED_CAST"
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/UploadImage.kt
View file @
24404f9a
...
...
@@ -12,7 +12,7 @@ expect class PlatformImage
expect
class
ClientTryGetImageIDPacket
(
botNumber
:
Long
,
sessionKey
:
ByteArray
,
groupNumberOr
QQNumber
:
Long
,
groupNumberOr
Account
:
Long
,
image
:
PlatformImage
)
:
ClientPacket
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/BotAccount.kt
View file @
24404f9a
...
...
@@ -2,6 +2,6 @@ package net.mamoe.mirai.utils
data class
BotAccount
(
val
qqNumber
:
Long
,
//实际上是 UInt
val
account
:
Long
,
//实际上是 UInt
val
password
:
String
//todo 不保存 password?
)
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
View file @
24404f9a
...
...
@@ -32,6 +32,7 @@ fun ByteReadPacket.parseServerPacket(size: Int): ServerPacket {
return
when
(
id
.
toUInt
())
{
0
x08_25u
->
ServerTouchResponsePacket
.
Encrypted
(
this
)
0
x08_36u
->
{
//todo 不要用size分析
when
(
size
)
{
271
,
207
->
return
ServerLoginResponseKeyExchangePacket
.
Encrypted
(
this
).
applySequence
(
sequenceId
)
871
->
return
ServerLoginResponseCaptchaInitPacket
.
Encrypted
(
this
).
applySequence
(
sequenceId
)
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt
View file @
24404f9a
...
...
@@ -4,12 +4,12 @@ import kotlinx.coroutines.CompletableJob
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.withContext
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.network.
Login
Session
import
net.mamoe.mirai.network.
Bot
Session
import
net.mamoe.mirai.network.protocol.tim.packet.ClientTryGetImageIDPacketJvm
import
net.mamoe.mirai.network.protocol.tim.packet.ServerTryGetImageIDFailedPacket
import
net.mamoe.mirai.network.protocol.tim.packet.ServerTryGetImageIDResponsePacket
import
net.mamoe.mirai.network.protocol.tim.packet.ServerTryGetImageIDSuccessPacket
import
net.mamoe.mirai.qq
Number
import
net.mamoe.mirai.qq
Account
import
net.mamoe.mirai.utils.ImageNetworkUtils
import
net.mamoe.mirai.utils.md5
import
net.mamoe.mirai.utils.toByteArray
...
...
@@ -30,9 +30,9 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) {
constructor
(
imageFile
:
File
)
:
this
(
imageFile
.
name
,
ImageIO
.
read
(
imageFile
))
constructor
(
url
:
URL
)
:
this
(
File
(
url
.
file
))
suspend
fun
upload
(
session
:
Login
Session
,
contact
:
Contact
):
CompletableJob
{
suspend
fun
upload
(
session
:
Bot
Session
,
contact
:
Contact
):
CompletableJob
{
return
session
.
expectPacket
<
ServerTryGetImageIDResponsePacket
>
{
toSend
{
ClientTryGetImageIDPacketJvm
(
session
.
bot
.
qq
Number
,
session
.
sessionKey
,
contact
.
number
,
image
)
}
toSend
{
ClientTryGetImageIDPacketJvm
(
session
.
bot
.
qq
Account
,
session
.
sessionKey
,
contact
.
number
,
image
)
}
onExpect
{
when
(
it
)
{
...
...
@@ -43,7 +43,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) {
is
ServerTryGetImageIDSuccessPacket
->
{
val
data
=
image
.
toByteArray
()
withContext
(
Dispatchers
.
IO
)
{
if
(!
ImageNetworkUtils
.
postImage
(
it
.
uKey
.
toUHexString
(),
data
.
size
,
session
.
bot
.
qq
Number
,
contact
.
number
,
data
))
{
if
(!
ImageNetworkUtils
.
postImage
(
it
.
uKey
.
toUHexString
(),
data
.
size
,
session
.
bot
.
qq
Account
,
contact
.
number
,
data
))
{
throw
RuntimeException
(
"cannot upload image"
)
}
}
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/network/protocol/tim/packet/UploadImageJvm.kt
View file @
24404f9a
...
...
@@ -23,7 +23,7 @@ actual typealias ClientTryGetImageIDPacket = ClientTryGetImageIDPacketJvm
class
ClientTryGetImageIDPacketJvm
(
private
val
botNumber
:
Long
,
private
val
sessionKey
:
ByteArray
,
private
val
groupNumberOr
QQNumber
:
Long
,
private
val
groupNumberOr
Account
:
Long
,
private
val
image
:
PlatformImage
)
:
ClientPacket
()
{
override
fun
encode
(
builder
:
BytePacketBuilder
)
=
with
(
builder
)
{
...
...
@@ -52,7 +52,7 @@ class ClientTryGetImageIDPacketJvm(
writeHex
(
"57"
)
//原5A
writeHex
(
"08"
)
writeUVarInt
(
groupNumberOr
QQNumber
)
//FB D2 D8 94
writeUVarInt
(
groupNumberOr
Account
)
//FB D2 D8 94
writeByte
(
0
x02
)
writeHex
(
"10"
)
...
...
mirai-core/src/jvmTest/kotlin/BadQQFilter.kt
View file @
24404f9a
...
...
@@ -62,5 +62,5 @@ suspend fun main() {
}
println
(
"Filtering finished"
)
println
(
goodBotList
.
joinToString
(
"\n"
)
{
it
.
account
.
qqNumber
.
toString
()
+
" "
+
it
.
account
.
password
})
println
(
goodBotList
.
joinToString
(
"\n"
)
{
it
.
account
.
account
.
toString
()
+
" "
+
it
.
account
.
password
})
}
mirai-demos/mirai-demo-1/src/main/java/demo1/Main.kt
View file @
24404f9a
...
...
@@ -23,7 +23,7 @@ import kotlin.system.measureTimeMillis
suspend
fun
main
()
{
val
bot
=
Bot
(
BotAccount
(
//填写你的账号
qqNumber
=
2903772581
,
account
=
2903772581
,
password
=
"zxc123456"
),
Console
())
...
...
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