add color stripping to incoming chat

This commit is contained in:
nikky 2018-05-24 19:56:47 +02:00
parent e652a27a06
commit cee1673728
8 changed files with 35 additions and 21 deletions

View File

@ -34,7 +34,7 @@ fun String.lazyFormat(env: Map<String, () -> String>): String {
return result return result
} }
val String.stripColor: String val String.stripColorOut: String
get() = get() =
if (cfg.outgoing.stripColors) if (cfg.outgoing.stripColors)
this.replace("§.".toRegex(), "") this.replace("§.".toRegex(), "")
@ -42,6 +42,14 @@ val String.stripColor: String
this this
val String.stripColorIn: String
get() =
if (cfg.incoming.stripColors)
this.replace("§.".toRegex(), "")
else
this
val Exception.stackTraceString: String val Exception.stackTraceString: String
get() { get() {
val sw = StringWriter() val sw = StringWriter()

View File

@ -5,7 +5,7 @@ import matterlink.bridge.MessageHandlerInst
import matterlink.handlers.TickHandler import matterlink.handlers.TickHandler
import matterlink.instance import matterlink.instance
import matterlink.lazyFormat import matterlink.lazyFormat
import matterlink.stripColor import matterlink.stripColorOut
data class CustomCommand( data class CustomCommand(
val type: CommandType = CommandType.RESPONSE, val type: CommandType = CommandType.RESPONSE,
@ -35,7 +35,7 @@ data class CustomCommand(
if (!canExecute(userId, server)) { if (!canExecute(userId, server)) {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = "$user is not permitted to perform command: $alias".stripColor text = "$user is not permitted to perform command: $alias".stripColorOut
) )
) )
return false return false
@ -54,7 +54,7 @@ data class CustomCommand(
CommandType.RESPONSE -> { CommandType.RESPONSE -> {
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = (response?.lazyFormat(getReplacements(user, userId, server, args))?.stripColor ?: "") text = (response?.lazyFormat(getReplacements(user, userId, server, args))?.stripColorOut ?: "")
) )
) )
true true

View File

@ -1,7 +1,6 @@
package matterlink.config package matterlink.config
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.stripColor
import java.io.File import java.io.File
import java.util.regex.Pattern import java.util.regex.Pattern
@ -40,7 +39,8 @@ abstract class BaseConfig(rootDir: File) {
data class IncomingOption( data class IncomingOption(
val chat: String = "<{username}> {text}", val chat: String = "<{username}> {text}",
val joinPart: String = "§6-- {username} {text}", val joinPart: String = "§6-- {username} {text}",
val action: String = "§5* {username} {text}" val action: String = "§5* {username} {text}",
var stripColors: Boolean = true
) )
data class OutgoingOptions( data class OutgoingOptions(
@ -193,6 +193,12 @@ abstract class BaseConfig(rootDir: File) {
category, category,
incoming.action, incoming.action,
"User actions (/me) sent by users from other gateways" "User actions (/me) sent by users from other gateways"
),
stripColors = getBoolean(
"stripColors",
category,
incoming.stripColors,
"strip colors from incoming text"
) )
) )

View File

@ -3,15 +3,15 @@ package matterlink.handlers
import matterlink.api.ApiMessage import matterlink.api.ApiMessage
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.instance import matterlink.instance
import matterlink.stripColor import matterlink.stripColorOut
object ChatProcessor { object ChatProcessor {
fun sendToBridge(user: String, msg: String, event: String) { fun sendToBridge(user: String, msg: String, event: String) {
val message = msg.trim() val message = msg.trim()
when { when {
message.isNotBlank() -> MessageHandlerInst.transmit(ApiMessage( message.isNotBlank() -> MessageHandlerInst.transmit(ApiMessage(
username = user.stripColor, username = user.stripColorOut,
text = message.stripColor, text = message.stripColorOut,
event = event) event = event)
) )
else -> instance.warn("WARN: dropped blank message by '$user'") else -> instance.warn("WARN: dropped blank message by '$user'")

View File

@ -4,7 +4,7 @@ import matterlink.antiping
import matterlink.api.ApiMessage import matterlink.api.ApiMessage
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.stripColor import matterlink.stripColorOut
import java.util.* import java.util.*
object DeathHandler { object DeathHandler {
@ -16,7 +16,7 @@ object DeathHandler {
damageType: String damageType: String
) { ) {
if (cfg.outgoing.death.enable) { if (cfg.outgoing.death.enable) {
var msg = deathMessage.replace(player, player.stripColor.antiping) var msg = deathMessage.replace(player, player.stripColorOut.antiping)
if (cfg.outgoing.death.damageType) { if (cfg.outgoing.death.damageType) {
val emojis = cfg.outgoing.death.damageTypeMapping[damageType]?.split(' ') val emojis = cfg.outgoing.death.damageTypeMapping[damageType]?.split(' ')
?: listOf("\uD83D\uDC7B unknown type '$damageType'") ?: listOf("\uD83D\uDC7B unknown type '$damageType'")

View File

@ -6,15 +6,15 @@ import matterlink.api.ApiMessage.Companion.JOIN_LEAVE
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.mapFormat import matterlink.mapFormat
import matterlink.stripColor import matterlink.stripColorOut
object JoinLeaveHandler { object JoinLeaveHandler {
fun handleJoin(player: String) { fun handleJoin(player: String) {
if (cfg.outgoing.joinPart.enable) { if (cfg.outgoing.joinPart.enable) {
val msg = cfg.outgoing.joinPart.joinServer.mapFormat( val msg = cfg.outgoing.joinPart.joinServer.mapFormat(
mapOf( mapOf(
"{username}" to player.stripColor, "{username}" to player.stripColorOut,
"{username:antiping}" to player.stripColor.antiping "{username:antiping}" to player.stripColorOut.antiping
) )
) )
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
@ -30,8 +30,8 @@ object JoinLeaveHandler {
if (cfg.outgoing.joinPart.enable) { if (cfg.outgoing.joinPart.enable) {
val msg = cfg.outgoing.joinPart.partServer.mapFormat( val msg = cfg.outgoing.joinPart.partServer.mapFormat(
mapOf( mapOf(
"{username}" to player.stripColor, "{username}" to player.stripColorOut,
"{username:antiping}" to player.stripColor.antiping "{username:antiping}" to player.stripColorOut.antiping
) )
) )
MessageHandlerInst.transmit( MessageHandlerInst.transmit(

View File

@ -4,16 +4,16 @@ import matterlink.antiping
import matterlink.api.ApiMessage import matterlink.api.ApiMessage
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.stripColor import matterlink.stripColorOut
object ProgressHandler { object ProgressHandler {
fun handleProgress(name: String, message: String, display: String) { fun handleProgress(name: String, message: String, display: String) {
if (!cfg.outgoing.advancements) return if (!cfg.outgoing.advancements) return
val usr = name.stripColor.antiping val usr = name.stripColorOut.antiping
MessageHandlerInst.transmit( MessageHandlerInst.transmit(
ApiMessage( ApiMessage(
text = "$usr $message $display".stripColor text = "$usr $message $display".stripColorOut
) )
) )
} }

View File

@ -1,11 +1,11 @@
package matterlink.handlers package matterlink.handlers
import com.sun.xml.internal.ws.util.StringUtils
import matterlink.bridge.MessageHandlerInst import matterlink.bridge.MessageHandlerInst
import matterlink.bridge.command.BridgeCommandRegistry import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.bridge.format import matterlink.bridge.format
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.instance import matterlink.instance
import matterlink.stripColorIn
object ServerChatHandler { object ServerChatHandler {
@ -24,7 +24,7 @@ object ServerChatHandler {
"" -> { "" -> {
// try to handle command and do not handle as a chat message // try to handle command and do not handle as a chat message
if (BridgeCommandRegistry.handleCommand(nextMessage)) return if (BridgeCommandRegistry.handleCommand(nextMessage)) return
nextMessage.format(cfg.incoming.chat) nextMessage.format(cfg.incoming.chat.stripColorIn)
} }
"join_leave" -> nextMessage.format(cfg.incoming.joinPart) "join_leave" -> nextMessage.format(cfg.incoming.joinPart)
else -> { else -> {