Skip to content

Commit

Permalink
fix may deserialize plain CQCode as Json
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Jul 9, 2024
1 parent cb48904 commit d7b9a37
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 49 deletions.
1 change: 1 addition & 0 deletions onebot/src/main/kotlin/sdk/event/message/MessageEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import lombok.experimental.SuperBuilder
open class MessageEvent : Event() {
var messageType = ""
var userId = 0L
var isJsonMessage = false
var message = ""
var rawMessage = ""
var font = 0
Expand Down
1 change: 1 addition & 0 deletions onebot/src/main/kotlin/sdk/response/group/GetMsgResp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GetMsgResp {
var realId = 0
lateinit var sender: Sender
var time = 0
var isJsonMessage = false
var message = ""
var rawMessage = ""
var peerId = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MessageEventAdapter : JsonDeserializerKt<MessageEvent> {
time = obj.long("time")
selfId = obj.long("self_id")
userId = obj.long("user_id")
isJsonMessage = obj.has("message") && obj["message"].isJsonArray
message = obj.forceString("message")
rawMessage = obj.ignorable("raw_message", "")
font = obj.ignorable("font", 0)
Expand Down
1 change: 1 addition & 0 deletions onebot/src/main/kotlin/sdk/util/json/MsgAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MsgAdapter : JsonDeserializerKt<GetMsgResp> {
realId = obj.ignorable("real_id", 0)
sender = obj.fromJson("sender")!!
time = obj.int("time")
isJsonMessage = obj.has("message") && obj["message"].isJsonArray
message = obj.forceString("message")
rawMessage = obj.ignorable("raw_message", "")
peerId = obj.ignorable("peer_id", 0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface OverflowAPI {
fun videoFromFile(file: String): ShortVideo

/**
* 序列化 mirai 格式消息为 Onebot 格式 json 数组消息
* 序列化 mirai 格式消息为 Onebot 格式 json 消息段
*/
@Deprecated(
message = "Please use serializeMessage(bot, message)",
Expand All @@ -45,16 +45,38 @@ interface OverflowAPI {
fun serializeMessage(message: Message): String = serializeMessage(null, message)

/**
* 序列化 mirai 格式消息为 Onebot 格式 json 数组消息
* 序列化 mirai 格式消息为 Onebot 格式 json 消息段
*/
fun serializeMessage(bot: RemoteBot?, message: Message): String

/**
* 反序列化 Onebot 格式 json 数组消息为 mirai 格式消息
* 序列化 Onebot 格式 json 消息段为 CQ 码
*/
fun serializeJsonToCQCode(messageJson: String): String

/**
* 序列化 Onebot 格式 CQ 码 为 json 消息段
*/
fun serializeCQCodeToJson(messageCQCode: String): String

/**
* 反序列化 Onebot 格式 json 消息段或 CQ 码,反序列化为 mirai 格式消息
*/
@JvmBlockingBridge
suspend fun deserializeMessage(bot: Bot, message: String): MessageChain

/**
* 反序列化 json 消息段为 mirai 格式消息
*/
@JvmBlockingBridge
suspend fun deserializeMessageFromJson(bot: Bot, message: String): MessageChain?

/**
* 反序列化 CQ 码 为 mirai 格式消息
*/
@JvmBlockingBridge
suspend fun deserializeMessageFromCQCode(bot: Bot, message: String): MessageChain?

companion object {
public val logger = MiraiLogger.Factory.create(OverflowAPI::class, "Overflow")
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cn.evolvefield.onebot.sdk.response.contact.FriendInfoResp
import cn.evolvefield.onebot.sdk.util.gson
import cn.evolvefield.onebot.client.config.BotConfig
import cn.evolvefield.onebot.client.connection.ConnectFactory
import cn.evolvefield.onebot.sdk.util.CQCode
import com.google.gson.JsonParser
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json
import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
Expand Down Expand Up @@ -48,7 +50,6 @@ import top.mrxiaom.overflow.internal.plugin.OverflowCoreAsPlugin
import top.mrxiaom.overflow.internal.utils.wrapAsOtherClientInfo
import java.io.File
import kotlin.coroutines.CoroutineContext
import kotlin.system.exitProcess

internal val OverflowAPI.scope: CoroutineScope
get() = this as Overflow
Expand Down Expand Up @@ -300,9 +301,27 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
override fun imageFromFile(file: String): Image = OnebotMessages.imageFromFile(file)
override fun audioFromFile(file: String): Audio = OnebotMessages.audioFromFile(file)
override fun videoFromFile(file: String): ShortVideo = OnebotMessages.videoFromFile(file)
override fun serializeMessage(bot: RemoteBot?, message: Message): String = OnebotMessages.serializeToOneBotJson(bot, message)
override fun serializeMessage(bot: RemoteBot?, message: Message): String {
return OnebotMessages.serializeToOneBotJson(bot, message)
}
override fun serializeJsonToCQCode(messageJson: String): String {
return CQCode.fromJson(JsonParser.parseString(messageJson).asJsonArray)
}
override fun serializeCQCodeToJson(messageCQCode: String): String {
return gson.toJson(CQCode.toJson(messageCQCode))
}
@JvmBlockingBridge
override suspend fun deserializeMessage(bot: Bot, message: String): MessageChain {
return OnebotMessages.deserializeFromOneBot(bot.asRemoteBot, message, null)
}
@JvmBlockingBridge
override suspend fun deserializeMessageFromJson(bot: Bot, message: String): MessageChain? {
return OnebotMessages.deserializeMessageFromJson(bot.asRemoteBot, message, null)
}
@JvmBlockingBridge
override suspend fun deserializeMessage(bot: Bot, message: String): MessageChain = OnebotMessages.deserializeFromOneBot(bot.asRemoteBot, message, null)
override suspend fun deserializeMessageFromCQCode(bot: Bot, message: String): MessageChain? {
return OnebotMessages.deserializeMessageFromCQCode(bot.asRemoteBot, message, null)
}
override suspend fun queryProfile(bot: Bot, targetId: Long): UserProfile {
if (bot.asOnebot.appName == "shamrock") {
val data = bot.asOnebot.impl.getUserInfo(targetId, false).data
Expand Down Expand Up @@ -391,7 +410,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {

@LowLevelApi
override suspend fun getRawGroupList(bot: Bot): Sequence<Long> {
return (bot.asOnebot.impl.getGroupList().data?.map { it.groupId } ?: listOf()).asSequence()
return bot.asOnebot.impl.getGroupList().data.map { it.groupId }.asSequence()
}

@LowLevelApi
Expand All @@ -401,7 +420,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
groupCode: Long,
ownerId: Long
): Sequence<MemberInfo> {
return (bot.asOnebot.impl.getGroupMemberList(groupUin).data?.map { it.asMirai } ?: listOf()).asSequence()
return bot.asOnebot.impl.getGroupMemberList(groupUin).data.map { it.asMirai }.asSequence()
}

@LowLevelApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ internal class BotWrapper private constructor(
}
@JvmBlockingBridge
override suspend fun getMsg(messageId: Int): MessageChain? {
val bot = (net.mamoe.mirai.Bot.instances.firstOrNull() as? BotWrapper) ?: return null
val data = bot.impl.getMsg(messageId).data ?: return null
val data = impl.getMsg(messageId).data ?: return null
if (data.message.isEmpty()) return null
return OnebotMessages.deserializeFromOneBot(bot, data.message)
return OnebotMessages.toMiraiMessage(data.isJsonMessage, data.message, this)
}
override val id: Long
get() = loginInfo.userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.mamoe.mirai.contact.remarkOrNick
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.utils.MiraiInternalApi
import top.mrxiaom.overflow.internal.Overflow
import top.mrxiaom.overflow.internal.message.OnebotMessages
import top.mrxiaom.overflow.internal.message.OnebotMessages.toMiraiMessage
import top.mrxiaom.overflow.internal.message.data.IncomingSource
import top.mrxiaom.overflow.internal.utils.*

Expand All @@ -34,20 +34,20 @@ internal class FriendMessageListener : EventListener<PrivateMessageEvent> {
// TODO: 过滤自己发送的消息
return
}
var miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()
val messageSource = IncomingSource.friend(
bot = bot,
ids = intArrayOf(e.messageId),
internalIds = intArrayOf(e.messageId),
isOriginalMessageInitialized = true,
originalMessage = miraiMessage,
originalMessage = message,
sender = friend,
subject = friend,
target = bot,
time = (e.time / 1000).toInt()
)
miraiMessage = messageSource.plus(miraiMessage)
val miraiMessage = messageSource.plus(message)
bot.logger.verbose("${friend.remarkOrNick}(${friend.id}) -> $messageString")
bot.eventDispatcher.broadcastAsync(FriendMessageEvent(
friend, miraiMessage, messageSource.time
Expand All @@ -64,20 +64,20 @@ internal class FriendMessageListener : EventListener<PrivateMessageEvent> {
// TODO: 过滤自己发送的消息
return
}
var miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()
val messageSource = IncomingSource.temp(
bot = bot,
ids = intArrayOf(e.messageId),
internalIds = intArrayOf(e.messageId),
originalMessage = miraiMessage,
originalMessage = message,
sender = member,
subject = member,
isOriginalMessageInitialized = true,
target = bot,
time = (e.time / 1000).toInt()
)
miraiMessage = messageSource.plus(miraiMessage)
val miraiMessage = messageSource.plus(message)
bot.logger.verbose("[群临时消息] ${member.remarkOrNick}(${member.id}) -> $messageString")
bot.eventDispatcher.broadcastAsync(GroupTempMessageEvent(
member, miraiMessage, messageSource.time
Expand All @@ -90,20 +90,20 @@ internal class FriendMessageListener : EventListener<PrivateMessageEvent> {
// TODO: 过滤自己发送的消息
return
}
var miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()
val messageSource = IncomingSource.stranger(
bot = bot,
ids = intArrayOf(e.messageId),
internalIds = intArrayOf(e.messageId),
isOriginalMessageInitialized = true,
originalMessage = miraiMessage,
originalMessage = message,
sender = stranger,
subject = stranger,
target = bot,
time = (e.time / 1000).toInt()
)
miraiMessage = messageSource.plus(miraiMessage)
val miraiMessage = messageSource.plus(message)
bot.logger.verbose("${stranger.remarkOrNick}(${stranger.id}) -> $messageString")
bot.eventDispatcher.broadcastAsync(StrangerMessageEvent(
stranger, miraiMessage, messageSource.time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.utils.MiraiInternalApi
import top.mrxiaom.overflow.internal.Overflow
import top.mrxiaom.overflow.internal.message.OnebotMessages
import top.mrxiaom.overflow.internal.message.OnebotMessages.toMiraiMessage
import top.mrxiaom.overflow.internal.message.data.IncomingSource
import top.mrxiaom.overflow.internal.utils.bot
import top.mrxiaom.overflow.internal.utils.group
Expand Down Expand Up @@ -38,18 +38,18 @@ internal class GroupMessageListener : EventListener<GroupMessageEvent> {
val group = bot.group(e.groupId)
val member = e.sender?.wrapAsMember(group) ?: return

var miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()
val messageSource = IncomingSource.group(
bot = bot,
ids = intArrayOf(e.messageId),
internalIds = intArrayOf(e.messageId),
isOriginalMessageInitialized = true,
originalMessage = miraiMessage,
originalMessage = message,
sender = member,
time = (e.time / 1000).toInt()
)
miraiMessage = messageSource.plus(miraiMessage)
val miraiMessage = messageSource.plus(message)
if (member.id == bot.id) {
// TODO: 过滤自己发送的消息
} else {
Expand All @@ -63,19 +63,18 @@ internal class GroupMessageListener : EventListener<GroupMessageEvent> {
val group = bot.group(e.groupId)
val member = e.anonymous?.wrapAsMember(group) ?: return

var miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()
val messageSource = IncomingSource.group(
bot = bot,
ids = intArrayOf(e.messageId),
internalIds = intArrayOf(e.messageId),
isOriginalMessageInitialized = true,
originalMessage = miraiMessage,
originalMessage = message,
sender = member,
time = (e.time / 1000).toInt()
)
miraiMessage = messageSource.plus(miraiMessage)

val miraiMessage = messageSource.plus(message)
bot.logger.verbose("[${group.name}(${group.id})] ${member.nameCard}(${member.anonymousId}) -> $messageString")
bot.eventDispatcher.broadcastAsync(
GroupMessageEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cn.evolvefield.onebot.client.listener.EventListener
import cn.evolvefield.onebot.sdk.entity.GuildSender
import net.mamoe.mirai.contact.MemberPermission
import top.mrxiaom.overflow.event.LegacyGuildMessageEvent
import top.mrxiaom.overflow.internal.message.OnebotMessages
import top.mrxiaom.overflow.internal.message.OnebotMessages.toMiraiMessage
import top.mrxiaom.overflow.internal.utils.bot

internal fun addGuildListeners() {
Expand All @@ -21,8 +21,8 @@ internal class GuildMessageListener : EventListener<GuildMessageEvent> {
val bot = e.bot ?: return
when (e.subType) {
"channel" -> {
val miraiMessage = OnebotMessages.deserializeFromOneBot(bot, e.message)
val messageString = miraiMessage.toString()
val message = e.toMiraiMessage(bot)
val messageString = message.toString()

if (e.sender.userId == bot.id) {
// TODO: 过滤自己发送的消息
Expand All @@ -33,7 +33,7 @@ internal class GuildMessageListener : EventListener<GuildMessageEvent> {
guildId = e.guildId,
channelId = e.channelId,
messageId = e.messageId,
message = miraiMessage,
message = message,
senderId = e.sender.userId,
senderTinyId = e.sender.tinyId,
senderNick = e.sender.nickname,
Expand Down
Loading

0 comments on commit d7b9a37

Please sign in to comment.