Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert prohibition of sending file message: #1716

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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