From 0015b7bcc1d7bd17588e89bea5e5fd3cb7212196 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 22 Feb 2020 13:48:13 +0800 Subject: [PATCH] Make `MessageSource.messageUid` int --- .../mirai/api/http/data/common/MessageDTO.kt | 2 +- .../mirai/api/http/queue/MessageQueue.kt | 2 +- .../net/mamoe/mirai/qqandroid/ContactImpl.kt | 12 ++-- .../qqandroid/message/MessageSourceFromMsg.kt | 9 +-- .../network/protocol/data/proto/MsgSvc.kt | 2 +- .../packet/chat/receive/MessageSvc.kt | 70 ++++++++++++------- .../net.mamoe.mirai/message/MessageReceipt.kt | 7 +- .../message/data/MessageSource.kt | 2 +- 8 files changed, 65 insertions(+), 41 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt index 61ebc46f8..ad0404169 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt @@ -98,7 +98,7 @@ fun MessageChainDTO.toMessageChain(contact: Contact) = @UseExperimental(ExperimentalUnsignedTypes::class) fun Message.toDTO() = when (this) { - is MessageSource -> MessageSourceDTO(messageUid) + is MessageSource -> MessageSourceDTO(messageUid.toLong() and 0xFFffFFff) is At -> AtDTO(target, display) is AtAll -> AtAllDTO(0L) is Face -> FaceDTO(id) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt index eee8f85de..23fb76b90 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/queue/MessageQueue.kt @@ -44,6 +44,6 @@ class MessageQueue : ConcurrentLinkedDeque() { } private fun addCache(msg: GroupMessage) { - quoteCache[msg.message[MessageSource].messageUid] = msg + quoteCache[msg.message[MessageSource].messageUid.toLong() and 0xFFffFFff] = msg } } \ No newline at end of file diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt index 2d740a1f4..dc783581c 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt @@ -70,16 +70,17 @@ internal class QQImpl( if (event.isCancelled) { throw EventCancelledException("cancelled by FriendMessageSendEvent") } + lateinit var source: MessageSource bot.network.run { check( MessageSvc.PbSendMsg.ToFriend( bot.client, id, event.message - ).sendAndExpect() is MessageSvc.PbSendMsg.Response.SUCCESS + ) { source = it }.sendAndExpect() is MessageSvc.PbSendMsg.Response.SUCCESS ) { "send message failed" } } - return MessageReceipt(message, this) + return MessageReceipt(message, source, this) } override suspend fun uploadImage(image: ExternalImage): Image = try { @@ -552,12 +553,13 @@ internal class GroupImpl( if (event.isCancelled) { throw EventCancelledException("cancelled by FriendMessageSendEvent") } + lateinit var source: MessageSource bot.network.run { - val response = MessageSvc.PbSendMsg.ToGroup( + val response: MessageSvc.PbSendMsg.Response = MessageSvc.PbSendMsg.ToGroup( bot.client, id, event.message - ).sendAndExpect() + ) { source = it }.sendAndExpect() check( response is MessageSvc.PbSendMsg.Response.SUCCESS ) { "send message failed: $response" } @@ -566,7 +568,7 @@ internal class GroupImpl( ((message.last() as MessageSource) as MessageSvc.PbSendMsg.MessageSourceFromSend) .startWaitingSequenceId(this) - return MessageReceipt(message, this) + return MessageReceipt(message, source, this) } override suspend fun uploadImage(image: ExternalImage): Image = try { diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt index 540186716..44065d95b 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/MessageSourceFromMsg.kt @@ -27,7 +27,7 @@ internal inline class MessageSourceFromServer( // nothing to do } - override val messageUid: Long get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!! + override val messageUid: Int get() = delegate.pbReserve.loadAs(SourceMsg.ResvAttr.serializer()).origUids!!.toInt() // override val sourceMessage: MessageChain get() = delegate.toMessageChain() override val senderId: Long get() = delegate.senderUin override val groupId: Long get() = Group.calculateGroupCodeByGroupUin(delegate.toUin) @@ -44,7 +44,7 @@ internal inline class MessageSourceFromMsg( // nothing to do } - override val messageUid: Long get() = delegate.msgBody.richText.attr!!.random.toLong() + override val messageUid: Int get() = delegate.msgBody.richText.attr!!.random // override val sourceMessage: MessageChain get() = delegate.toMessageChain() override val senderId: Long get() = delegate.msgHead.fromUin override val groupId: Long get() = delegate.msgHead.groupInfo!!.groupCode @@ -62,7 +62,7 @@ internal inline class MessageSourceFromMsg( type = 0, time = delegate.msgHead.msgTime, pbReserve = SourceMsg.ResvAttr( - origUids = messageUid + origUids = messageUid.toLong() and 0xffFFffFF ).toByteArray(SourceMsg.ResvAttr.serializer()), srcMsg = MsgComm.Msg( msgHead = MsgComm.MsgHead( @@ -72,7 +72,8 @@ internal inline class MessageSourceFromMsg( c2cCmd = delegate.msgHead.c2cCmd, msgSeq = delegate.msgHead.msgSeq, msgTime = delegate.msgHead.msgTime, - msgUid = messageUid, // ok + msgUid = messageUid.toLong() and 0xffFFffFF + , // ok groupInfo = MsgComm.GroupInfo(groupCode = delegate.msgHead.groupInfo.groupCode), isSrcMsg = true ), diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/data/proto/MsgSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/data/proto/MsgSvc.kt index 3007c6036..712ffe1d8 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/data/proto/MsgSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/data/proto/MsgSvc.kt @@ -26,7 +26,7 @@ internal class MsgSvc : ProtoBuf { internal class PbGetMsgResp( @SerialId(1) val result: Int = 0, @SerialId(2) val errmsg: String = "", - @SerialId(3) val syncCookie: ByteArray = EMPTY_BYTE_ARRAY, + @SerialId(3) val syncCookie: ByteArray? = EMPTY_BYTE_ARRAY, @SerialId(4) val syncFlag: SyncFlag, @SerialId(5) val uinPairMsgs: List? = null, @SerialId(6) val bindUin: Long = 0L, diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt index 94e37d2fa..0c79e4345 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt @@ -272,7 +272,7 @@ internal class MessageSvc { } internal class MessageSourceFromSend( - override val messageUid: Long, + override val messageUid: Int, override val time: Long, override val senderId: Long, override val groupId: Long// , @@ -283,7 +283,7 @@ internal class MessageSvc { @UseExperimental(MiraiExperimentalAPI::class) fun startWaitingSequenceId(contact: Contact) { sequenceIdDeferred = contact.subscribingGetAsync { - if (it.messageRandom == messageUid.toInt()) { + if (it.messageRandom == messageUid) { it.sequenceId } else null } @@ -302,25 +302,34 @@ internal class MessageSvc { } } - /** - * 发送好友消息 - */ - @Suppress("FunctionName") - fun ToFriend( + inline fun ToFriend( client: QQAndroidClient, toUin: Long, - message: MessageChain - ): OutgoingPacket = buildOutgoingUniPacket(client) { - ///writeFully("0A 08 0A 06 08 89 FC A6 8C 0B 12 06 08 01 10 00 18 00 1A 1F 0A 1D 12 08 0A 06 0A 04 F0 9F 92 A9 12 11 AA 02 0E 88 01 00 9A 01 08 78 00 F8 01 00 C8 02 00 20 9B 7A 28 F4 CA 9B B8 03 32 34 08 92 C2 C4 F1 05 10 92 C2 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 89 84 F9 A2 06 48 DE 8C EA E5 0E 58 D9 BD BB A0 09 60 1D 68 92 C2 C4 F1 05 70 00 40 01".hexToBytes()) - + message: MessageChain, + crossinline sourceCallback: (MessageSource) -> Unit + ): OutgoingPacket { val source = MessageSourceFromSend( - messageUid = Random.nextInt().absoluteValue.toLong() and 0xffffffff, + messageUid = Random.nextInt().absoluteValue, senderId = client.uin, time = currentTimeSeconds + client.timeDifference, groupId = 0// // sourceMessage = message ) - message.addOrRemove(source) + sourceCallback(source) + return ToFriend(client, toUin, message, source) + } + + /** + * 发送好友消息 + */ + @Suppress("FunctionName") + private fun ToFriend( + client: QQAndroidClient, + toUin: Long, + message: MessageChain, + source: MessageSource + ): OutgoingPacket = buildOutgoingUniPacket(client) { + ///writeFully("0A 08 0A 06 08 89 FC A6 8C 0B 12 06 08 01 10 00 18 00 1A 1F 0A 1D 12 08 0A 06 0A 04 F0 9F 92 A9 12 11 AA 02 0E 88 01 00 9A 01 08 78 00 F8 01 00 C8 02 00 20 9B 7A 28 F4 CA 9B B8 03 32 34 08 92 C2 C4 F1 05 10 92 C2 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 89 84 F9 A2 06 48 DE 8C EA E5 0E 58 D9 BD BB A0 09 60 1D 68 92 C2 C4 F1 05 70 00 40 01".hexToBytes()) ///return@buildOutgoingUniPacket writeProtoBuf( @@ -333,13 +342,32 @@ internal class MessageSvc { ) ), msgSeq = client.atomicNextMessageSequenceId(), - msgRand = source.messageUid.toInt(), + msgRand = source.messageUid, syncCookie = SyncCookie(time = source.time).toByteArray(SyncCookie.serializer()) // msgVia = 1 ) ) } + + inline fun ToGroup( + client: QQAndroidClient, + groupCode: Long, + message: MessageChain, + sourceCallback: (MessageSource) -> Unit + ): OutgoingPacket { + + val source = MessageSourceFromSend( + messageUid = Random.nextInt().absoluteValue, + senderId = client.uin, + time = currentTimeSeconds + client.timeDifference, + groupId = groupCode//, + // sourceMessage = message + ) + sourceCallback(source) + return ToGroup(client, groupCode, message, source) + } + /** * 发送群消息 */ @@ -347,17 +375,9 @@ internal class MessageSvc { fun ToGroup( client: QQAndroidClient, groupCode: Long, - message: MessageChain + message: MessageChain, + source: MessageSource ): OutgoingPacket = buildOutgoingUniPacket(client) { - - val source = MessageSourceFromSend( - messageUid = Random.nextInt().absoluteValue.toLong(), - senderId = client.uin, - time = currentTimeSeconds + client.timeDifference, - groupId = groupCode//, - // sourceMessage = message - ) - ///writeFully("0A 08 0A 06 08 89 FC A6 8C 0B 12 06 08 01 10 00 18 00 1A 1F 0A 1D 12 08 0A 06 0A 04 F0 9F 92 A9 12 11 AA 02 0E 88 01 00 9A 01 08 78 00 F8 01 00 C8 02 00 20 9B 7A 28 F4 CA 9B B8 03 32 34 08 92 C2 C4 F1 05 10 92 C2 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 89 84 F9 A2 06 48 DE 8C EA E5 0E 58 D9 BD BB A0 09 60 1D 68 92 C2 C4 F1 05 70 00 40 01".hexToBytes()) // DebugLogger.debug("sending group message: " + message.toRichTextElems().contentToString()) @@ -373,7 +393,7 @@ internal class MessageSvc { ) ), msgSeq = client.atomicNextMessageSequenceId(), - msgRand = source.messageUid.toInt(), + msgRand = source.messageUid, syncCookie = EMPTY_BYTE_ARRAY, msgVia = 1 ) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt index 6204e77c6..d760a88f9 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt @@ -28,6 +28,7 @@ import net.mamoe.mirai.utils.unsafeWeakRef */ open class MessageReceipt( val originalMessage: MessageChain, + private val source: MessageSource, target: C ) { init { @@ -53,7 +54,7 @@ open class MessageReceipt( if (_isRecalled.compareAndSet(false, true)) { when (val contact = target) { is Group -> { - contact.recall(originalMessage) + contact.recall(source) } is QQ -> { TODO() @@ -76,7 +77,7 @@ open class MessageReceipt( if (_isRecalled.compareAndSet(false, true)) { when (val contact = target) { is Group -> { - return contact.recallIn(originalMessage, millis) + return contact.recallIn(source, millis) } is QQ -> { TODO() @@ -97,7 +98,7 @@ open class MessageReceipt( open fun quote(): MessageChain { val target = target check(target is Group) { "quote is only available for GroupMessage" } - return this.originalMessage.quote(target.botAsMember) + return this.source.quote(target.botAsMember) } /** diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt index 467802330..c0ecb35cd 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageSource.kt @@ -41,7 +41,7 @@ interface MessageSource : Message { /** * 实际上是个随机数, 但服务器确实是用它当做 uid */ - val messageUid: Long + val messageUid: Int /** * 发送时间, 单位为秒