Skip to content

Commit 1299935

Browse files
committed
Prohibit sending FileMessage manually, except those from FileMessage.upload. close #1715
1 parent cd02041 commit 1299935

File tree

3 files changed

+20
-36
lines changed

3 files changed

+20
-36
lines changed

mirai-core/src/commonMain/kotlin/contact/util.kt

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* Copyright 2019-2021 Mamoe Technologies and contributors.
2+
* Copyright 2019-2022 Mamoe Technologies and contributors.
33
*
4-
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5-
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
4+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
66
*
7-
* https://github.com/mamoe/mirai/blob/master/LICENSE
7+
* https://github.com/mamoe/mirai/blob/dev/LICENSE
88
*/
99

1010
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@@ -13,6 +13,7 @@ package net.mamoe.mirai.internal.contact
1313

1414
import net.mamoe.mirai.Bot
1515
import net.mamoe.mirai.contact.*
16+
import net.mamoe.mirai.internal.message.FileMessageImpl
1617
import net.mamoe.mirai.internal.message.LongMessageInternal
1718
import net.mamoe.mirai.internal.utils.estimateLength
1819
import net.mamoe.mirai.message.data.*
@@ -33,13 +34,6 @@ internal fun MessageChain.countImages(): Int = this.count { it is Image }
3334

3435
private val logger by lazy { MiraiLogger.Factory.create(SendMessageHandler::class) }
3536

36-
// Fixme: Remove in the future, see #1715
37-
private val fileMessageWarningShown = object : ExceptionCollector() {
38-
override fun addSuppressed(receiver: Throwable, e: Throwable) {
39-
}
40-
}
41-
42-
// Fixme: Remove in the future, see #1715
4337
private val ALLOW_SENDING_FILE_MESSAGE = systemProp("mirai.message.allow.sending.file.message", false)
4438

4539
internal fun Message.verifySendingValid() {
@@ -49,26 +43,14 @@ internal fun Message.verifySendingValid() {
4943
this.forEach { it.verifySendingValid() }
5044
}
5145
is FileMessage -> {
52-
// Fixme: https://github.com/mamoe/mirai/issues/1715
53-
54-
if (!ALLOW_SENDING_FILE_MESSAGE) {
55-
val e =
56-
Exception("This stacktrace might help you find your code causing this problem. It is shown once for each distinct line.")
57-
val log =
58-
"Sending FileMessage manually is error-prone and is planned to be prohibited in the future. " +
59-
"Please use AbsoluteFolder.uploadNewFile (recommended) or RemoteFile.uploadAndSend instead (deprecated)." +
60-
"You can add JVM argument '-Dmirai.message.allow.sending.file.message=true' to ignore this warning, " +
61-
"however, your code might not work in the future."
62-
63-
// Show stacktrace for each call only once.
64-
if (fileMessageWarningShown.collect(e)) {
65-
logger.warning(log, e)
66-
} else {
67-
logger.warning(log)
68-
}
46+
if (!ALLOW_SENDING_FILE_MESSAGE) { // #1715
47+
if (this !is FileMessageImpl) error("Customized FileMessage cannot be send")
48+
if (!this.allowSend) error(
49+
"Sending FileMessage is not allowed, as it may cause unexpected results. " +
50+
"Add JVM argument `-Dmirai.message.allow.sending.file.message=true` to disable this check. " +
51+
"Do this only for compatibility!"
52+
)
6953
}
70-
71-
// fail("Sending FileMessage is not in support")
7254
}
7355
}
7456
}

mirai-core/src/commonMain/kotlin/message/FileMessageImpl.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* Copyright 2019-2021 Mamoe Technologies and contributors.
2+
* Copyright 2019-2022 Mamoe Technologies and contributors.
33
*
4-
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5-
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
4+
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
5+
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
66
*
7-
* https://github.com/mamoe/mirai/blob/master/LICENSE
7+
* https://github.com/mamoe/mirai/blob/dev/LICENSE
88
*/
99

1010

@@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.firstOrNull
1515
import kotlinx.coroutines.flow.onEach
1616
import kotlinx.serialization.SerialName
1717
import kotlinx.serialization.Serializable
18+
import kotlinx.serialization.Transient
1819
import net.mamoe.mirai.contact.FileSupported
1920
import net.mamoe.mirai.contact.file.AbsoluteFile
2021
import net.mamoe.mirai.contact.file.AbsoluteFolder
@@ -44,6 +45,7 @@ internal data class FileMessageImpl(
4445
@SerialName("internalId") val busId: Int,
4546
override val name: String,
4647
override val size: Long,
48+
@Transient val allowSend: Boolean = false,
4749
) : FileMessage {
4850
override val internalId: Int
4951
get() = busId

mirai-core/src/commonMain/kotlin/utils/RemoteFileImpl.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 Mamoe Technologies and contributors.
2+
* Copyright 2019-2022 Mamoe Technologies and contributors.
33
*
44
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
55
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -534,7 +534,7 @@ internal class RemoteFileImpl(
534534
): FileMessage {
535535
val resp = upload0(resource, callback) ?: error("Failed to upload file.")
536536
return FileMessageImpl(
537-
resp.fileId, resp.busId, name, resource.size
537+
resp.fileId, resp.busId, name, resource.size, allowSend = true
538538
)
539539
}
540540

0 commit comments

Comments
 (0)