Skip to content

Commit 2367ee0

Browse files
authored
Revert prohibition of sending file message: (#1716)
* Revert prohibition of sending file message: - close #1715 - Report a warning in logging with stacktrace - Show stacktrace only once * Update util.kt
1 parent bc1cce3 commit 2367ee0

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ public open class ExceptionCollector {
4141
if (!hashCodes.add(hash(e))) return false // filter out duplications
4242
// we can also check suppressed exceptions of [e] but actual influence would be slight.
4343
beforeCollect(e)
44-
this.last?.let { e.addSuppressed(it) }
44+
this.last?.let { addSuppressed(e, it) }
4545
this.last = e
4646
return true
4747
}
4848

49+
protected open fun addSuppressed(receiver: Throwable, e: Throwable) {
50+
receiver.addSuppressed(e)
51+
}
52+
4953
private fun hash(e: Throwable): Long {
5054
return e.stackTrace.fold(0L) { acc, stackTraceElement ->
5155
acc * 31 + hash(stackTraceElement).toLongUnsigned()

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

+35-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ package net.mamoe.mirai.internal.contact
1414
import net.mamoe.mirai.Bot
1515
import net.mamoe.mirai.contact.*
1616
import net.mamoe.mirai.internal.message.LongMessageInternal
17-
import net.mamoe.mirai.internal.message.MiraiInternalMessageFlag
1817
import net.mamoe.mirai.internal.utils.estimateLength
1918
import net.mamoe.mirai.message.data.*
20-
import net.mamoe.mirai.utils.cast
21-
import net.mamoe.mirai.utils.castOrNull
22-
import net.mamoe.mirai.utils.verbose
19+
import net.mamoe.mirai.utils.*
2320

2421
internal inline val Group.uin: Long get() = this.cast<GroupImpl>().uin
2522
internal inline val Group.groupCode: Long get() = this.id
@@ -34,13 +31,45 @@ internal fun Contact.logMessageSent(message: Message) {
3431

3532
internal fun MessageChain.countImages(): Int = this.count { it is Image }
3633

34+
private val logger by lazy { MiraiLogger.Factory.create(SendMessageHandler::class) }
35+
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
43+
private val ALLOW_SENDING_FILE_MESSAGE = systemProp("mirai.message.allow.sending.file.message", false)
44+
3745
internal fun Message.verifySendingValid() {
38-
fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
46+
// fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
3947
when (this) {
4048
is MessageChain -> {
4149
this.forEach { it.verifySendingValid() }
4250
}
43-
is FileMessage -> fail("Sending FileMessage is not in support")
51+
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+
}
69+
}
70+
71+
// fail("Sending FileMessage is not in support")
72+
}
4473
}
4574
}
4675

0 commit comments

Comments
 (0)