From 3a79834e4cdbe8f0848292a2152cd764256c73c0 Mon Sep 17 00:00:00 2001 From: nikky Date: Mon, 25 Jun 2018 14:12:14 +0200 Subject: [PATCH] added permisssion request commands --- .../matterlink/command/CommandMatterlink.kt | 2 +- .../matterlink/command/CommandMatterlink.kt | 2 +- .../matterlink/command/CommandMatterlink.kt | 2 +- .../matterlink/command/CommandMatterlink.kt | 2 +- .../bridge/command/BridgeCommandRegistry.kt | 1 + .../matterlink/bridge/command/PermCommand.kt | 57 +++++++++++++++++++ .../kotlin/matterlink/command/CommandCore.kt | 14 ++++- .../kotlin/matterlink/config/BaseConfig.kt | 15 +++-- .../matterlink/config/PermissionConfig.kt | 36 +++++++++++- gradle.properties | 2 +- 10 files changed, 116 insertions(+), 17 deletions(-) create mode 100644 core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt diff --git a/1.10.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt b/1.10.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt index eea2edf..ac2f57d 100644 --- a/1.10.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt +++ b/1.10.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt @@ -25,7 +25,7 @@ class CommandMatterlink : CommandBase() { throw WrongUsageException("Invalid command! Valid uses: ${this.getUsage(sender)}") } - val reply = CommandCore.execute(args) + val reply = CommandCore.execute(args, sender.name) if (reply.isNotEmpty() && sender.sendCommandFeedback()) { sender.sendMessage(TextComponentString(reply)) diff --git a/1.11.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt b/1.11.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt index eea2edf..ac2f57d 100644 --- a/1.11.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt +++ b/1.11.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt @@ -25,7 +25,7 @@ class CommandMatterlink : CommandBase() { throw WrongUsageException("Invalid command! Valid uses: ${this.getUsage(sender)}") } - val reply = CommandCore.execute(args) + val reply = CommandCore.execute(args, sender.name) if (reply.isNotEmpty() && sender.sendCommandFeedback()) { sender.sendMessage(TextComponentString(reply)) diff --git a/1.12.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt b/1.12.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt index 5e605c5..9b97497 100644 --- a/1.12.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt +++ b/1.12.2/src/main/kotlin/matterlink/command/CommandMatterlink.kt @@ -25,7 +25,7 @@ class CommandMatterlink : CommandBase() { throw WrongUsageException("Invalid command! Valid uses: ${this.getUsage(sender)}") } - val reply = CommandCore.execute(args) + val reply = CommandCore.execute(args, sender.name) if (reply.isNotEmpty() && sender.sendCommandFeedback()) { sender.sendMessage(TextComponentString(reply)) diff --git a/1.7.10/src/main/kotlin/matterlink/command/CommandMatterlink.kt b/1.7.10/src/main/kotlin/matterlink/command/CommandMatterlink.kt index 74f35dd..2f3fdc8 100644 --- a/1.7.10/src/main/kotlin/matterlink/command/CommandMatterlink.kt +++ b/1.7.10/src/main/kotlin/matterlink/command/CommandMatterlink.kt @@ -12,7 +12,7 @@ class CommandMatterlink : CommandBase() { throw WrongUsageException("Invalid command! Valid uses: ${this.getCommandUsage(sender)}") } - val reply = CommandCore.execute(args) + val reply = CommandCore.execute(args, sender.commandSenderName) if (reply.isNotEmpty()) { sender.addChatMessage(ChatComponentText(reply)) diff --git a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt index 1253ef7..75e3c15 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt @@ -58,6 +58,7 @@ object BridgeCommandRegistry { commandMap.clear() val permStatus = PermissionConfig.loadPermFile() register("help", HelpCommand) + register("req", PermCommand) val cmdStatus = CommandConfig.readConfig() CommandConfig.commands.forEach { (alias, command) -> register(alias, command) diff --git a/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt new file mode 100644 index 0000000..2bf5a1b --- /dev/null +++ b/core/src/main/kotlin/matterlink/bridge/command/PermCommand.kt @@ -0,0 +1,57 @@ +package matterlink.bridge.command + +import matterlink.api.ApiMessage +import matterlink.bridge.MessageHandlerInst +import matterlink.config.PermissionConfig +import matterlink.config.PermissionRequest + +object PermCommand : IBridgeCommand { + override val help: String = "Requests permissions on the bridge. Syntax: req [powerlevel] [key]" + override val permLevel = 0.0 + override fun execute(alias: String, user: String, userId: String, server: String, args: String): Boolean { + val argList = args.split(' ', limit = 2) + val requestedLevel = args.toDoubleOrNull() ?: 20.0 + var unfilteredKey = user + var key = unfilteredKey.replace("[^A-Za-z0-9 ]".toRegex(), "").toLowerCase() + if (argList.size > 1) { + unfilteredKey = argList[1] + key = unfilteredKey.replace("[^A-Za-z0-9 ]".toRegex(), "").toLowerCase() + } + if (key.isBlank()) { + MessageHandlerInst.transmit( + ApiMessage( + text = "$unfilteredKey is made up of invalid characters.. please specifiy a key for tracking this request" + ) + ) + return true + } else if (PermissionConfig.permissionRequests.containsKey(key)) { + MessageHandlerInst.transmit( + ApiMessage( + text = "there is already a permission request for $key" + ) + ) + return true + } + val currentPowerlevel = IBridgeCommand.getPermLevel(userId, server) + if(currentPowerlevel < 0.0) { + MessageHandlerInst.transmit( + ApiMessage( + text = "Your poermission level is $currentPowerlevel seems like someone banned you from making any more requests" + ) + ) + return true + } + + MessageHandlerInst.transmit( + ApiMessage( + text = "requet for powerlevel $requestedLevel from user $user userID: $userId server: $server\n" + + "accept this by executing `bridge acceptPerm $key `\n" + + "setting a negative level will prevent people from sending any more requests" + ) + ) + PermissionConfig.permissionRequests[key.toLowerCase()] = PermissionRequest(user, server, userId, requestedLevel) + // PermissionConfig.add(server, userId, requestedlevel, "authorized by $user") + return true + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/command/CommandCore.kt b/core/src/main/kotlin/matterlink/command/CommandCore.kt index ccbfb6f..8b78ddb 100644 --- a/core/src/main/kotlin/matterlink/command/CommandCore.kt +++ b/core/src/main/kotlin/matterlink/command/CommandCore.kt @@ -2,6 +2,7 @@ package matterlink.command import matterlink.bridge.MessageHandlerInst import matterlink.bridge.command.BridgeCommandRegistry +import matterlink.config.PermissionConfig import matterlink.config.baseCfg import matterlink.config.cfg @@ -10,9 +11,9 @@ object CommandCore { fun getAliases() = listOf("BRIDGE", "bridge") - fun getUsage() = "bridge " + fun getUsage() = "bridge " - fun execute(args: Array): String { + fun execute(args: Array, user: String): String { val cmd = args[0].toLowerCase() return when (cmd) { @@ -33,6 +34,15 @@ object CommandCore { MessageHandlerInst.start("Bridge reconnected", false) "Bridge config reloaded!" } + "acceptperm" -> { + val key = args.getOrNull(1)?.toLowerCase() ?: return "No argument providing they request key" + val request = PermissionConfig.permissionRequests[key] ?: return "No request found for key $key" + val powerLevelArg = args.getOrNull(2)?.toDoubleOrNull() + val powerLevel = powerLevelArg ?: request.powerlevel ?: return "no powerLevel provided or it cannot be parsed" + PermissionConfig.add(request.platform, request.userId, powerLevel, "${request.user} Authorized by $user") + PermissionConfig.permissionRequests.remove(key) + "added ${request.user} (platform: ${request.platform} userId: ${request.userId}) with power level: $powerLevel" + } else -> { "Invalid arguments for command!" } diff --git a/core/src/main/kotlin/matterlink/config/BaseConfig.kt b/core/src/main/kotlin/matterlink/config/BaseConfig.kt index 1b2d128..bf92e5d 100644 --- a/core/src/main/kotlin/matterlink/config/BaseConfig.kt +++ b/core/src/main/kotlin/matterlink/config/BaseConfig.kt @@ -374,19 +374,18 @@ data class BaseConfig(val rootDir: File) { MatterLinkConfig() } - // val defaultJsonObject = jankson.load("{}") // jankson.fromJson(defaultJsonObject, MatterLinkConfig::class.java) // val nonDefault = jsonObject.getDelta(defaultJsonObject) - MessageHandlerInst.config.url = cfg.connect.url - MessageHandlerInst.config.token = cfg.connect.authToken - MessageHandlerInst.config.gateway = cfg.connect.gateway - MessageHandlerInst.config.reconnectWait = cfg.connect.reconnectWait + MessageHandlerInst.config.url = tmpCfg.connect.url + MessageHandlerInst.config.token = tmpCfg.connect.authToken + MessageHandlerInst.config.gateway = tmpCfg.connect.gateway + MessageHandlerInst.config.reconnectWait = tmpCfg.connect.reconnectWait - MessageHandlerInst.config.systemUser = cfg.outgoing.systemUser - MessageHandlerInst.config.announceConnect = cfg.outgoing.announceConnect - MessageHandlerInst.config.announceDisconnect = cfg.outgoing.announceDisconnect + MessageHandlerInst.config.systemUser = tmpCfg.outgoing.systemUser + MessageHandlerInst.config.announceConnect = tmpCfg.outgoing.announceConnect + MessageHandlerInst.config.announceDisconnect = tmpCfg.outgoing.announceDisconnect return tmpCfg } diff --git a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt index 294350a..dc4281e 100644 --- a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt +++ b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt @@ -4,13 +4,22 @@ import blue.endless.jankson.Jankson import blue.endless.jankson.JsonObject import blue.endless.jankson.impl.Marshaller import blue.endless.jankson.impl.SyntaxError +import matterlink.getOrDefault import matterlink.instance import java.io.File import java.io.FileNotFoundException typealias PermissionMap = MutableMap> +data class PermissionRequest( + val user: String, + val platform: String, + val userId: String, + val powerlevel: Double? = null +) + object PermissionConfig { + val permissionRequests = mutableMapOf() private val jankson = Jankson .builder() .build() @@ -27,10 +36,12 @@ object PermissionConfig { ) ) - var perms: PermissionMap = mutableMapOf() + val perms: PermissionMap = mutableMapOf() private var jsonObject: JsonObject = JsonObject() fun loadPermFile(): Boolean { + permissionRequests.clear() + jsonObject = try { jankson.load(configFile) } catch (e: SyntaxError) { @@ -45,7 +56,7 @@ object PermissionConfig { val jsonUserMap = jsonObject.getOrDefault(platform, JsonObject()) if(jsonUserMap is JsonObject) { userMap.forEach { user, (powerlevel, comment) -> - instance.trace("loading platform: $platform user: $user powwerlevel: $powerlevel") + instance.trace("loading platform: $platform user: $user powerlevel: $powerlevel") val element = Marshaller.getFallback().serialize(powerlevel) jsonUserMap.putDefault(user, element, comment.takeUnless { it.isBlank() }) } @@ -70,4 +81,25 @@ object PermissionConfig { return true } + + fun add(platform: String, userid: String, powerlevel: Double, comment: String? = null) { + val platformObject = jsonObject.getObject(platform) ?: JsonObject() + platformObject.getOrDefault(userid, powerlevel, comment) + jsonObject[platform] = platformObject + + perms.clear() + jsonObject.forEach { platform, jsonUserMap -> + val userMap = perms[platform] ?: mutableMapOf() + if (jsonUserMap is JsonObject) { + jsonUserMap.forEach { user, powerlevel -> + instance.info("$platform $user $powerlevel") + userMap[user] = jsonUserMap.get(Double::class.java, user) ?: 0.0 + } + } + perms[platform] = userMap + } + instance.info("Permissions reloaded") + + configFile.writeText(jsonObject.toJson(true, true)) + } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 4b29348..5a9cd00 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ mod_name = MatterLink -mod_version = 1.6.1 +mod_version = 1.6.2 forgelin_version = 1.6.0 curse_id = 287323 curse_release_type = beta \ No newline at end of file