All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.message.data.LongMessageInternal.kt Maven / Gradle / Ivy

There is a newer version: 2.16.0
Show newest version
/*
 * Copyright 2019-2022 Mamoe Technologies and contributors.
 *
 * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
 * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
 *
 * https://github.com/mamoe/mirai/blob/dev/LICENSE
 */

package net.mamoe.mirai.internal.message.data

import net.mamoe.mirai.Bot
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.internal.asQQAndroidBot
import net.mamoe.mirai.internal.getMiraiImpl
import net.mamoe.mirai.internal.message.RefinableMessage
import net.mamoe.mirai.internal.message.RefineContext
import net.mamoe.mirai.internal.message.RefineContextKey
import net.mamoe.mirai.internal.message.visitor.ex
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgTransmit
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.safeCast

// internal runtime value, not serializable
internal data class LongMessageInternal internal constructor(override val content: String, val resId: String) :
    AbstractServiceMessage(), RefinableMessage {
    override val serviceId: Int get() = 35

    override suspend fun refine(bot: Bot, context: MessageChain, refineContext: RefineContext): Message {
        bot.asQQAndroidBot()
        val long = Mirai.downloadLongMessage(bot, resId)

        return MessageOrigin(SimpleServiceMessage(serviceId, content), resId, MessageOriginKind.LONG) + long
    }

    override fun  accept(visitor: MessageVisitor, data: D): R {
        return visitor.ex()?.visitLongMessageInternal(this, data) ?: super.accept(visitor, data)
    }

    companion object Key :
        AbstractPolymorphicMessageKey(ServiceMessage, { it.safeCast() })
}

// internal runtime value, not serializable
@Suppress("RegExpRedundantEscape", "UnnecessaryVariable")
internal data class ForwardMessageInternal(
    override val content: String,
    val resId: String?,
    /**
     * null means top-level.
     * not null means nested and need [ForwardMessageInternal.MsgTransmits] in [RefineContext]
     */
    val fileName: String?,

    /**
     * For light refine before constructing [MessageReceipt].
     * See [OutgoingMessageSourceInternal] for more details.
     */
    val origin: ForwardMessage? = null,
) : AbstractServiceMessage(),
    RefinableMessage {
    override val serviceId: Int get() = 35

    override fun tryRefine(bot: Bot, context: MessageChain, refineContext: RefineContext): Message {
        return origin ?: this
    }

    override suspend fun refine(bot: Bot, context: MessageChain, refineContext: RefineContext): Message {
        bot.asQQAndroidBot()

        val msgXml = content.substringAfter(" accept(visitor: MessageVisitor, data: D): R {
        return visitor.ex()?.visitForwardMessageInternal(this, data) ?: super.accept(
            visitor,
            data
        )
    }

    companion object Key :
        AbstractPolymorphicMessageKey(ServiceMessage, { it.safeCast() }) {

        val SUMMARY_REGEX = """\(.*?)\<\/summary\>""".toRegex()

        @Suppress("SpellCheckingInspection")
        val TITLE_REGEX = """\([\u0000-\uFFFF]*?)\<\/title\>""".toRegex()


        fun String.findField(type: String): String {
            return substringAfter("$type=\"", "")
                .substringBefore("\"", "")
        }

        val MsgTransmits = RefineContextKey>("MsgTransmit")
    }
}

private fun String.xmlEnc(): String {
    return this.replace("&", "&")
}

internal fun RichMessage.Key.forwardMessage(
    resId: String,
    fileName: String,
    forwardMessage: ForwardMessage,
): ForwardMessageInternal = with(forwardMessage) {
    val template = """
        
        
            
                ${title.take(50).xmlEnc()}
                ${
        when {
            preview.size > 4 -> {
                preview.take(3).joinToString("") {
                    """${it.take(50).xmlEnc()}"""
                } + """..."""
            }
            else -> {
                preview.joinToString("") {
                    """${it.take(50).xmlEnc()}"""
                }
            }
        }
    }
                
                ${summary.take(50).xmlEnc()}
            
            
        
    """.trimIndent().replace("\n", " ").trim()
    return ForwardMessageInternal(template, resId, null, forwardMessage)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy