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
a42fbe47
Commit
a42fbe47
authored
Nov 12, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adjust function name
parent
3d8a8b5a
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
50 additions
and
23 deletions
+50
-23
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
+4
-5
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/contact/Contact.kt
.../src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt
+2
-2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
...nMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt
...c/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt
+35
-7
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
....mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/DataPacketSocketAdapter.kt
...i/network/protocol/tim/handler/DataPacketSocketAdapter.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt
...ai/network/protocol/tim/handler/TemporaryPacketHandler.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt
...e.mirai/network/protocol/tim/packet/action/UploadImage.kt
+3
-3
mirai-debug/src/main/kotlin/PacketDebuger.kt
mirai-debug/src/main/kotlin/PacketDebuger.kt
+1
-1
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
View file @
a42fbe47
...
@@ -213,14 +213,14 @@ Mirai 22:04:48 : Packet received: UnknownEventPacket(id=00 D6, identity=(2092749
...
@@ -213,14 +213,14 @@ Mirai 22:04:48 : Packet received: UnknownEventPacket(id=00 D6, identity=(2092749
* @param lazyMessage 若需要验证请求时的验证消息.
* @param lazyMessage 若需要验证请求时的验证消息.
*/
*/
suspend
fun
ContactSystem
.
addFriend
(
id
:
UInt
,
lazyMessage
:
()
->
String
=
{
""
},
lazyRemark
:
()
->
String
=
{
""
}):
AddFriendResult
=
bot
.
withSession
{
suspend
fun
ContactSystem
.
addFriend
(
id
:
UInt
,
lazyMessage
:
()
->
String
=
{
""
},
lazyRemark
:
()
->
String
=
{
""
}):
AddFriendResult
=
bot
.
withSession
{
when
(
CanAddFriendPacket
(
bot
.
qqAccount
,
id
,
bot
.
sessionKey
).
sendAndExpect
<
CanAddFriendResponse
>()
.
await
()
)
{
when
(
CanAddFriendPacket
(
bot
.
qqAccount
,
id
,
bot
.
sessionKey
).
sendAndExpect
<
CanAddFriendResponse
>())
{
is
CanAddFriendResponse
.
AlreadyAdded
->
AddFriendResult
.
ALREADY_ADDED
is
CanAddFriendResponse
.
AlreadyAdded
->
AddFriendResult
.
ALREADY_ADDED
is
CanAddFriendResponse
.
Rejected
->
AddFriendResult
.
REJECTED
is
CanAddFriendResponse
.
Rejected
->
AddFriendResult
.
REJECTED
is
CanAddFriendResponse
.
ReadyToAdd
,
is
CanAddFriendResponse
.
ReadyToAdd
,
is
CanAddFriendResponse
.
RequireVerification
->
{
is
CanAddFriendResponse
.
RequireVerification
->
{
val
key
=
RequestFriendAdditionKeyPacket
(
bot
.
qqAccount
,
id
,
sessionKey
).
sendAndExpect
<
RequestFriendAdditionKeyPacket
.
Response
>().
await
().
key
val
key
=
RequestFriendAdditionKeyPacket
(
bot
.
qqAccount
,
id
,
sessionKey
).
sendAndExpect
<
RequestFriendAdditionKeyPacket
.
Response
>().
key
AddFriendPacket
(
bot
.
qqAccount
,
id
,
sessionKey
,
lazyMessage
(),
lazyRemark
(),
key
).
sendAndExpect
<
AddFriendPacket
.
Response
>()
.
await
()
AddFriendPacket
(
bot
.
qqAccount
,
id
,
sessionKey
,
lazyMessage
(),
lazyRemark
(),
key
).
sendAndExpect
<
AddFriendPacket
.
Response
>()
return
AddFriendResult
.
WAITING_FOR_APPROVE
return
AddFriendResult
.
WAITING_FOR_APPROVE
}
}
//这个做的是需要验证消息的情况, 不确定 ReadyToAdd 的是啥
//这个做的是需要验证消息的情况, 不确定 ReadyToAdd 的是啥
...
@@ -232,13 +232,12 @@ suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }
...
@@ -232,13 +232,12 @@ suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }
/*is CanAddFriendResponse.ReadyToAdd -> {
/*is CanAddFriendResponse.ReadyToAdd -> {
// TODO: 2019/11/11 这不需要验证信息的情况
// TODO: 2019/11/11 这不需要验证信息的情况
//AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpect<AddFriendPacket.Response>().await()
//AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpect
Async
<AddFriendPacket.Response>().await()
TODO()
TODO()
}*/
}*/
}
}
}
}
/*
/*
1494695429 同意好友请求后收到以下包:
1494695429 同意好友请求后收到以下包:
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotHelper.kt
View file @
a42fbe47
...
@@ -41,7 +41,7 @@ inline val Bot.qqs: ContactList<QQ>
...
@@ -41,7 +41,7 @@ inline val Bot.qqs: ContactList<QQ>
/**
/**
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect]
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect
Async
]
*/
*/
inline
fun
<
R
>
Bot
.
withSession
(
block
:
BotSession
.()
->
R
):
R
=
with
(
this
.
network
.
session
)
{
block
()
}
inline
fun
<
R
>
Bot
.
withSession
(
block
:
BotSession
.()
->
R
):
R
=
with
(
this
.
network
.
session
)
{
block
()
}
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt
View file @
a42fbe47
...
@@ -104,7 +104,7 @@ class Group internal constructor(bot: Bot, val groupId: GroupId) : Contact(bot,
...
@@ -104,7 +104,7 @@ class Group internal constructor(bot: Bot, val groupId: GroupId) : Contact(bot,
/**
/**
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 以 [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect]
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, 如 [BotSession.sendAndExpect
Async
]
*/
*/
inline
fun
<
R
>
Contact
.
withSession
(
block
:
BotSession
.()
->
R
):
R
=
bot
.
withSession
(
block
)
inline
fun
<
R
>
Contact
.
withSession
(
block
:
BotSession
.()
->
R
):
R
=
bot
.
withSession
(
block
)
...
@@ -138,7 +138,7 @@ open class QQ internal constructor(bot: Bot, id: UInt) : Contact(bot, id) {
...
@@ -138,7 +138,7 @@ open class QQ internal constructor(bot: Bot, id: UInt) : Contact(bot, id) {
*/
*/
suspend
fun
updateProfile
():
Profile
=
bot
.
withSession
{
suspend
fun
updateProfile
():
Profile
=
bot
.
withSession
{
RequestProfileDetailsPacket
(
bot
.
qqAccount
,
id
,
sessionKey
)
RequestProfileDetailsPacket
(
bot
.
qqAccount
,
id
,
sessionKey
)
.
sendAndExpect
<
RequestProfileDetailsResponse
,
Profile
>
{
it
.
profile
}
.
sendAndExpect
Async
<
RequestProfileDetailsResponse
,
Profile
>
{
it
.
profile
}
.
await
().
let
{
.
await
().
let
{
@Suppress
(
"UNCHECKED_CAST"
)
@Suppress
(
"UNCHECKED_CAST"
)
if
((
::
profile
as
SuspendLazy
<
Profile
>).
isInitialized
())
{
if
((
::
profile
as
SuspendLazy
<
Profile
>).
isInitialized
())
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
View file @
a42fbe47
...
@@ -66,7 +66,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
...
@@ -66,7 +66,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
/**
/**
* 添加一个临时包处理器, 并发送相应的包
* 添加一个临时包处理器, 并发送相应的包
*
*
* @see [BotSession.sendAndExpect] 发送并期待一个包
* @see [BotSession.sendAndExpect
Async
] 发送并期待一个包
* @see [TemporaryPacketHandler] 临时包处理器
* @see [TemporaryPacketHandler] 临时包处理器
*/
*/
suspend
fun
addHandler
(
temporaryPacketHandler
:
TemporaryPacketHandler
<
*
,
*
>)
suspend
fun
addHandler
(
temporaryPacketHandler
:
TemporaryPacketHandler
<
*
,
*
>)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt
View file @
a42fbe47
...
@@ -4,6 +4,7 @@ package net.mamoe.mirai.network
...
@@ -4,6 +4,7 @@ package net.mamoe.mirai.network
import
kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.CompletableDeferred
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Deferred
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.Job
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.contact.Group
import
net.mamoe.mirai.contact.Group
...
@@ -75,7 +76,7 @@ class BotSession(
...
@@ -75,7 +76,7 @@ class BotSession(
* 实现方法:
* 实现方法:
* ```kotlin
* ```kotlin
* with(session){
* with(session){
* ClientPacketXXX(...).sendAndExpect<ServerPacketXXX> {
* ClientPacketXXX(...).sendAndExpect
Async
<ServerPacketXXX> {
* //it: ServerPacketXXX
* //it: ServerPacketXXX
* }
* }
* }
* }
...
@@ -84,14 +85,14 @@ class BotSession(
...
@@ -84,14 +85,14 @@ class BotSession(
*
*
* @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包.
* @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包.
* @param P 期待的包
* @param P 期待的包
* @param handler 处理期待的包.
将会在调用本函数的 [coroutineContext] 下执行.
* @param handler 处理期待的包.
**将会在调用本函数的 [coroutineContext] 下执行.**
*
*
* @see Bot.withSession 转换
接收器 (receiver, 即 `this` 的指向)
为 [BotSession]
* @see Bot.withSession 转换
receiver, 即 `this` 的指向,
为 [BotSession]
*/
*/
suspend
inline
fun
<
reified
P
:
Packet
,
R
>
OutgoingPacket
.
sendAndExpect
(
suspend
inline
fun
<
reified
P
:
Packet
,
R
>
OutgoingPacket
.
sendAndExpect
Async
(
checkSequence
:
Boolean
=
true
,
checkSequence
:
Boolean
=
true
,
noinline
handler
:
suspend
(
P
)
->
R
noinline
handler
:
suspend
(
P
)
->
R
):
Completable
Deferred
<
R
>
{
):
Deferred
<
R
>
{
val
deferred
:
CompletableDeferred
<
R
>
=
CompletableDeferred
(
coroutineContext
[
Job
])
val
deferred
:
CompletableDeferred
<
R
>
=
CompletableDeferred
(
coroutineContext
[
Job
])
bot
.
network
.
addHandler
(
TemporaryPacketHandler
(
P
::
class
,
deferred
,
this
@BotSession
,
checkSequence
,
coroutineContext
+
deferred
).
also
{
bot
.
network
.
addHandler
(
TemporaryPacketHandler
(
P
::
class
,
deferred
,
this
@BotSession
,
checkSequence
,
coroutineContext
+
deferred
).
also
{
it
.
toSend
(
this
)
it
.
toSend
(
this
)
...
@@ -103,9 +104,36 @@ class BotSession(
...
@@ -103,9 +104,36 @@ class BotSession(
/**
/**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
* 将能从本函数的返回值 [CompletableDeferred] 接收到所期待的 [P]
* 将能从本函数的返回值 [CompletableDeferred] 接收到所期待的 [P]
* 本函数将能帮助实现清晰的包处理逻辑
*
* 实现方法:
* ```kotlin
* with(session){
* val deferred = ClientPacketXXX(...).sendAndExpectAsync<ServerPacketXXX>()
* // do something
*
* deferred.await() // or deferred.join()
* }
* ```
*/
suspend
inline
fun
<
reified
P
:
Packet
>
OutgoingPacket
.
sendAndExpectAsync
(
checkSequence
:
Boolean
=
true
):
Deferred
<
P
>
=
sendAndExpectAsync
<
P
,
P
>(
checkSequence
)
{
it
}
/**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
* 将调用 [CompletableDeferred.await], 挂起等待接收到指定的包后才返回.
* 本函数将能帮助实现清晰的包处理逻辑
*
* 实现方法:
* ```kotlin
* with(session){
* val packet:AddFriendPacket.Response = AddFriendPacket(...).sendAndExpect<AddFriendPacket.Response>()
* }
* ```
* @sample Bot.addFriend 添加好友
*/
*/
suspend
inline
fun
<
reified
P
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
checkSequence
:
Boolean
=
true
):
CompletableDeferred
<
P
>
=
suspend
inline
fun
<
reified
P
:
Packet
>
OutgoingPacket
.
sendAndExpect
(
checkSequence
:
Boolean
=
true
):
P
=
sendAndExpect
<
P
,
P
>(
checkSequence
)
{
it
}
sendAndExpect
Async
<
P
,
P
>(
checkSequence
)
{
it
}.
await
()
suspend
inline
fun
OutgoingPacket
.
send
()
=
socket
.
sendPacket
(
this
)
suspend
inline
fun
OutgoingPacket
.
send
()
=
socket
.
sendPacket
(
this
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
View file @
a42fbe47
...
@@ -513,7 +513,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
...
@@ -513,7 +513,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
HeartbeatPacket
(
HeartbeatPacket
(
bot
.
qqAccount
,
bot
.
qqAccount
,
sessionKey
sessionKey
).
sendAndExpect
<
HeartbeatPacketResponse
>().
join
()
).
sendAndExpect
Async
<
HeartbeatPacketResponse
>().
join
()
}
==
null
)
{
}
==
null
)
{
bot
.
logger
.
warning
(
"Heartbeat timed out"
)
bot
.
logger
.
warning
(
"Heartbeat timed out"
)
bot
.
reinitializeNetworkHandler
(
configuration
,
HeartbeatTimeoutException
())
bot
.
reinitializeNetworkHandler
(
configuration
,
HeartbeatTimeoutException
())
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/DataPacketSocketAdapter.kt
View file @
a42fbe47
...
@@ -41,7 +41,7 @@ interface DataPacketSocketAdapter : Closeable {
...
@@ -41,7 +41,7 @@ interface DataPacketSocketAdapter : Closeable {
*
*
* 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回.
* 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回.
*
*
* @see [BotSession.sendAndExpect] kotlin DSL
* @see [BotSession.sendAndExpect
Async
] kotlin DSL
*/
*/
suspend
fun
sendPacket
(
packet
:
OutgoingPacket
)
suspend
fun
sendPacket
(
packet
:
OutgoingPacket
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt
View file @
a42fbe47
...
@@ -21,7 +21,7 @@ import kotlin.reflect.KClass
...
@@ -21,7 +21,7 @@ import kotlin.reflect.KClass
* }
* }
* ```
* ```
*
*
* @see BotSession.sendAndExpect
* @see BotSession.sendAndExpect
Async
*/
*/
class
TemporaryPacketHandler
<
P
:
Packet
,
R
>(
class
TemporaryPacketHandler
<
P
:
Packet
,
R
>(
private
val
expectationClass
:
KClass
<
P
>,
private
val
expectationClass
:
KClass
<
P
>,
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt
View file @
a42fbe47
...
@@ -37,7 +37,7 @@ class OverFileSizeMaxException : IllegalStateException()
...
@@ -37,7 +37,7 @@ class OverFileSizeMaxException : IllegalStateException()
suspend
fun
Group
.
uploadImage
(
image
:
ExternalImage
):
ImageId
=
withSession
{
suspend
fun
Group
.
uploadImage
(
image
:
ExternalImage
):
ImageId
=
withSession
{
val
userContext
=
coroutineContext
val
userContext
=
coroutineContext
GroupImageIdRequestPacket
(
bot
.
qqAccount
,
internalId
,
image
,
sessionKey
)
GroupImageIdRequestPacket
(
bot
.
qqAccount
,
internalId
,
image
,
sessionKey
)
.
sendAndExpect
<
GroupImageIdRequestPacket
.
Response
,
Unit
>
{
.
sendAndExpect
Async
<
GroupImageIdRequestPacket
.
Response
,
Unit
>
{
withContext
(
userContext
)
{
withContext
(
userContext
)
{
when
(
it
)
{
when
(
it
)
{
is
GroupImageIdRequestPacket
.
Response
.
RequireUpload
->
httpClient
.
postImage
(
is
GroupImageIdRequestPacket
.
Response
.
RequireUpload
->
httpClient
.
postImage
(
...
@@ -69,8 +69,8 @@ suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
...
@@ -69,8 +69,8 @@ suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
*/
*/
suspend
fun
QQ
.
uploadImage
(
image
:
ExternalImage
):
ImageId
=
bot
.
withSession
{
suspend
fun
QQ
.
uploadImage
(
image
:
ExternalImage
):
ImageId
=
bot
.
withSession
{
FriendImageIdRequestPacket
(
qqAccount
,
sessionKey
,
id
,
image
)
FriendImageIdRequestPacket
(
qqAccount
,
sessionKey
,
id
,
image
)
.
sendAndExpect
<
FriendImageIdRequestPacket
.
Response
,
ImageId
>
{
.
sendAndExpect
Async
<
FriendImageIdRequestPacket
.
Response
,
ImageId
>
{
return
@
sendAndExpect
when
(
it
)
{
return
@
sendAndExpect
Async
when
(
it
)
{
is
FriendImageIdRequestPacket
.
Response
.
RequireUpload
->
{
is
FriendImageIdRequestPacket
.
Response
.
RequireUpload
->
{
httpClient
.
postImage
(
httpClient
.
postImage
(
htcmd
=
"0x6ff0070"
,
htcmd
=
"0x6ff0070"
,
...
...
mirai-debug/src/main/kotlin/PacketDebuger.kt
View file @
a42fbe47
...
@@ -312,7 +312,7 @@ internal object DebugNetworkHandler : BotNetworkHandler<DataPacketSocketAdapter>
...
@@ -312,7 +312,7 @@ internal object DebugNetworkHandler : BotNetworkHandler<DataPacketSocketAdapter>
get
()
=
bot
get
()
=
bot
}
}
override
val
bot
:
Bot
=
Bot
(
0
u
,
""
)
override
val
bot
:
Bot
=
Bot
(
qq
,
""
)
val
session
=
BotSession
(
bot
,
sessionKey
,
socket
,
this
)
val
session
=
BotSession
(
bot
,
sessionKey
,
socket
,
this
)
val
action
=
ActionPacketHandler
(
session
)
val
action
=
ActionPacketHandler
(
session
)
...
...
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