From 10a9a035e133e5264a47e5eb1f5637063154ec43 Mon Sep 17 00:00:00 2001 From: mzdluo123 Date: Tue, 18 Aug 2020 00:51:17 +0800 Subject: [PATCH 1/5] fix --- .../network/QQAndroidBotNetworkHandler.kt | 5 ++--- .../qqandroid/network/QQAndroidClient.kt | 2 -- .../chat/receive/MessageSvc.PbGetMsg.kt | 22 +++++++------------ 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 3e632640a..ade390552 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -413,9 +413,8 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo logger.info { "Syncing friend message history..." } withTimeoutOrNull(30000) { launch(CoroutineName("Syncing friend message history")) { syncFromEvent { Unit } } - // 别问我为什么要发两个 我也不知道 反正它能用 - MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, null, firstSync = true).sendAndExpect() - MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, null, firstSync = true).sendAndExpect() + MessageSvcPbGetMsg(bot.client, MsgSvc.SyncFlag.START, null).sendAndExpect() + } ?: error("timeout syncing friend message history") logger.info { "Syncing friend message history: Success" } } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt index 30b9b84d3..6fe7187d4 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt @@ -128,8 +128,6 @@ internal open class QQAndroidClient( lateinit var fileStoragePushFSSvcList: FileStoragePushFSSvcListFuckKotlin - internal val firstSyncPackets: AtomicInt = atomic(0) // 启动时候仅将所有好友信息设为已读的包 - internal suspend inline fun useNextServers(crossinline block: suspend (host: String, port: Int) -> Unit) { if (bot.client.serverList.isEmpty()) { throw NoServerAvailableException(null) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index 437be2b69..41ace422f 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -61,14 +61,10 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory msg.msgHead.msgTime > it.lastReadTime.toLong() and 4294967295L } + }.also { + MessageSvcPbDeleteMsg.delete(bot, it) // 删除消息 + // todo 实现一个锁来防止重复收到消息 + } .mapNotNull { msg -> suspend fun createGroupForBot(groupUin: Long): Group? { @@ -277,9 +276,6 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory { - if (bot.client.firstSyncPackets.value != 0) { - return@mapNotNull null - } if (msg.msgHead.fromUin == bot.id) { loop@ while (true) { val instance = bot.client.getFriendSeq() @@ -383,9 +379,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory { - if (client.firstSyncPackets.value != 0) { - client.firstSyncPackets.getAndDecrement() - } + } MsgSvc.SyncFlag.START -> { From c848ab2146a6403b86ade1bdd36142ab525a0e74 Mon Sep 17 00:00:00 2001 From: yyuueexxiinngg Date: Tue, 18 Aug 2020 00:53:48 +0800 Subject: [PATCH 2/5] Add send silk format voice support, fix typo. --- .../net/mamoe/mirai/qqandroid/contact/GroupImpl.kt | 11 +++++++++-- .../kotlin/net.mamoe.mirai/contact/Group.kt | 2 +- .../net/mamoe/mirai/message/SendImageUtilsJvm.kt | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 19c57f841..3c9ba12f2 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -449,7 +449,7 @@ internal class GroupImpl( * 上传一个语音消息以备发送. * 请注意,这是一个实验性api且随时会被删除 * @throws EventCancelledException 当发送消息事件被取消 - * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 1 MB) + * @throws OverFileSizeMaxException 当语音文件过大而被服务器拒绝上传时. (最大大小约为 1 MB) */ @JvmSynthetic @MiraiExperimentalAPI @@ -460,9 +460,16 @@ internal class GroupImpl( throw OverFileSizeMaxException() } val md5 = MiraiPlatformUtils.md5(content) + val codec = with(content.copyOfRange(0, 10).toUHexString("")) { + when { + startsWith("2321414D52") -> 0 // amr + startsWith("02232153494C4B5F5633") -> 1 // silk V3 + else -> 0 // use amr by default + } + } return bot.network.run { val response: PttStore.GroupPttUp.Response.RequireUpload = - PttStore.GroupPttUp(bot.client, bot.id, 0L, md5, content.size.toLong()).sendAndExpect() + PttStore.GroupPttUp(bot.client, bot.id, 0L, md5, content.size.toLong(), codec).sendAndExpect() HighwayHelper.uploadPttToServers( bot, response.uploadIpList.zip(response.uploadPortList), diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index 5dea700df..7b144c947 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -175,7 +175,7 @@ public abstract class Group : Contact(), CoroutineScope { /** * 上传一个语音消息以备发送. * 请手动关闭输入流 - * 请使用mar格式 + * 请使用amr或silk格式 * 请注意,这是一个实验性api且随时会被删除 * @throws EventCancelledException 当发送消息事件被取消 * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 1 MB) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt index 5ba1a4580..f85271806 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt @@ -119,9 +119,9 @@ public suspend fun File.uploadAsImage(contact: Contact): Image { } /** - * 在 [Dispatchers.IO] 中将文件作为语音上传后构造 [Image] + * 在 [Dispatchers.IO] 中将文件作为语音上传后构造 [Voice] * 请手动关闭输入流 - * 请使用mar格式 + * 请使用amr或silk格式 * 注意,这只是个实验性功能且随时可能会删除 * @throws OverFileSizeMaxException */ From ea8ed178445aa6715f7e16a70b0ca249ae20d607 Mon Sep 17 00:00:00 2001 From: mzdluo123 Date: Tue, 18 Aug 2020 10:31:03 +0800 Subject: [PATCH 3/5] add @SinceMirai("1.2.0") to voice upload api --- .../kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt | 3 ++- .../src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt | 1 + .../kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 19c57f841..48efde3a9 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -453,6 +453,7 @@ internal class GroupImpl( */ @JvmSynthetic @MiraiExperimentalAPI + @SinceMirai("1.2.0") override suspend fun uploadGroupVoice(input: InputStream): Voice { val content = ByteArray(input.available()) input.read(content) @@ -462,7 +463,7 @@ internal class GroupImpl( val md5 = MiraiPlatformUtils.md5(content) return bot.network.run { val response: PttStore.GroupPttUp.Response.RequireUpload = - PttStore.GroupPttUp(bot.client, bot.id, 0L, md5, content.size.toLong()).sendAndExpect() + PttStore.GroupPttUp(bot.client, bot.id, id, md5, content.size.toLong()).sendAndExpect() HighwayHelper.uploadPttToServers( bot, response.uploadIpList.zip(response.uploadPortList), diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index 5dea700df..a4b250e4e 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -182,6 +182,7 @@ public abstract class Group : Contact(), CoroutineScope { */ @JvmSynthetic @MiraiExperimentalAPI + @SinceMirai("1.2.0") public abstract suspend fun uploadGroupVoice(input: InputStream): Voice diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt index 5ba1a4580..025cea72e 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/SendImageUtilsJvm.kt @@ -127,6 +127,7 @@ public suspend fun File.uploadAsImage(contact: Contact): Image { */ @Throws(OverFileSizeMaxException::class) @MiraiExperimentalAPI +@SinceMirai("1.2.0") public suspend fun InputStream.uploadAsGroupVoice(group: Group): Voice { return group.uploadGroupVoice(this) } From f779ac08ac81e76d0f6ca1da5444707d9090eb05 Mon Sep 17 00:00:00 2001 From: mzdluo123 Date: Tue, 18 Aug 2020 10:39:01 +0800 Subject: [PATCH 4/5] fix typo --- .../src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt index 8c7f12105..e6b3fb275 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt @@ -178,7 +178,7 @@ public abstract class Group : Contact(), CoroutineScope { * 请使用amr或silk格式 * 请注意,这是一个实验性api且随时会被删除 * @throws EventCancelledException 当发送消息事件被取消 - * @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 1 MB) + * @throws OverFileSizeMaxException 当语音文件过大而被服务器拒绝上传时. (最大大小约为 1 MB) */ @JvmSynthetic @MiraiExperimentalAPI From e6374c88498f67b0fbdc6b971ff73836e593c39e Mon Sep 17 00:00:00 2001 From: mzdluo123 Date: Tue, 18 Aug 2020 11:50:52 +0800 Subject: [PATCH 5/5] custom codec --- .../net/mamoe/mirai/qqandroid/contact/GroupImpl.kt | 5 +++-- .../mirai/qqandroid/network/highway/HighwayHelper.kt | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 1f9396b22..d993c4f9b 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -469,14 +469,15 @@ internal class GroupImpl( } return bot.network.run { val response: PttStore.GroupPttUp.Response.RequireUpload = - PttStore.GroupPttUp(bot.client, bot.id, id, md5, content.size.toLong()).sendAndExpect() + PttStore.GroupPttUp(bot.client, bot.id, id, md5, content.size.toLong(), codec).sendAndExpect() HighwayHelper.uploadPttToServers( bot, response.uploadIpList.zip(response.uploadPortList), content, md5, response.uKey, - response.fileKey + response.fileKey, + codec ) Voice("${md5.toUHexString("")}.amr", md5, content.size.toLong(), "") } diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt index 61540e764..fffff8d6f 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/HighwayHelper.kt @@ -190,7 +190,9 @@ internal object HighwayHelper { servers: List>, content: ByteArray, md5: ByteArray, - uKey: ByteArray, fileKey: ByteArray + uKey: ByteArray, + fileKey: ByteArray, + codec: Int ) { servers.retryWithServers(10 * 1000, { throw IllegalStateException("cannot upload ptt, failed on all servers.", it) @@ -199,7 +201,7 @@ internal object HighwayHelper { "[Highway] Uploading ptt to ${s}:$i, size=${content.size.toLong().sizeToString()}" } val time = measureTime { - uploadPttToServer(s, i, content, md5, uKey, fileKey) + uploadPttToServer(s, i, content, md5, uKey, fileKey, codec) } bot.network.logger.verbose { "[Highway] Uploading ptt: succeed at ${(content.size.toDouble() / 1024 / time.inSeconds).roundToInt()} KiB/s" @@ -214,7 +216,9 @@ internal object HighwayHelper { serverPort: Int, content: ByteArray, md5: ByteArray, - uKey: ByteArray, fileKey: ByteArray + uKey: ByteArray, + fileKey: ByteArray, + codec: Int ) { MiraiPlatformUtils.Http.post { url("http://$serverIp:$serverPort") @@ -224,7 +228,7 @@ internal object HighwayHelper { parameter("filesize", content.size) parameter("bmd5", md5.toUHexString("")) parameter("mType", "pttDu") - parameter("voice_encodec", 0) + parameter("voice_encodec", codec) body = content } }