Skip to content

Commit

Permalink
Revert prohibition of sending file message: (#1716)
Browse files Browse the repository at this point in the history
* Revert prohibition of sending file message:
- close #1715
- Report a warning in logging with stacktrace
- Show stacktrace only once

* Update util.kt
  • Loading branch information
Him188 committed Dec 6, 2021
1 parent bc1cce3 commit 2367ee0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
6 changes: 5 additions & 1 deletion mirai-core-utils/src/commonMain/kotlin/ExceptionCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ public open class ExceptionCollector {
if (!hashCodes.add(hash(e))) return false // filter out duplications
// we can also check suppressed exceptions of [e] but actual influence would be slight.
beforeCollect(e)
this.last?.let { e.addSuppressed(it) }
this.last?.let { addSuppressed(e, it) }
this.last = e
return true
}

protected open fun addSuppressed(receiver: Throwable, e: Throwable) {
receiver.addSuppressed(e)
}

private fun hash(e: Throwable): Long {
return e.stackTrace.fold(0L) { acc, stackTraceElement ->
acc * 31 + hash(stackTraceElement).toLongUnsigned()
Expand Down
41 changes: 35 additions & 6 deletions mirai-core/src/commonMain/kotlin/contact/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ package net.mamoe.mirai.internal.contact
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.internal.message.LongMessageInternal
import net.mamoe.mirai.internal.message.MiraiInternalMessageFlag
import net.mamoe.mirai.internal.utils.estimateLength
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.cast
import net.mamoe.mirai.utils.castOrNull
import net.mamoe.mirai.utils.verbose
import net.mamoe.mirai.utils.*

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

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

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

// Fixme: Remove in the future, see #1715
private val fileMessageWarningShown = object : ExceptionCollector() {
override fun addSuppressed(receiver: Throwable, e: Throwable) {
}
}

// Fixme: Remove in the future, see #1715
private val ALLOW_SENDING_FILE_MESSAGE = systemProp("mirai.message.allow.sending.file.message", false)

internal fun Message.verifySendingValid() {
fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
// fun fail(msg: String): Nothing = throw IllegalArgumentException(msg)
when (this) {
is MessageChain -> {
this.forEach { it.verifySendingValid() }
}
is FileMessage -> fail("Sending FileMessage is not in support")
is FileMessage -> {
// Fixme: https://github.com/mamoe/mirai/issues/1715

if (!ALLOW_SENDING_FILE_MESSAGE) {
val e =
Exception("This stacktrace might help you find your code causing this problem. It is shown once for each distinct line.")
val log =
"Sending FileMessage manually is error-prone and is planned to be prohibited in the future. " +
"Please use AbsoluteFolder.uploadNewFile (recommended) or RemoteFile.uploadAndSend instead (deprecated)." +
"You can add JVM argument '-Dmirai.message.allow.sending.file.message=true' to ignore this warning, " +
"however, your code might not work in the future."

// Show stacktrace for each call only once.
if (fileMessageWarningShown.collect(e)) {
logger.warning(log, e)
} else {
logger.warning(log)
}
}

// fail("Sending FileMessage is not in support")
}
}
}

Expand Down

0 comments on commit 2367ee0

Please sign in to comment.