added permisssion request commands

This commit is contained in:
nikky 2018-06-25 14:12:14 +02:00
parent 5eb79935ba
commit 3a79834e4c
10 changed files with 116 additions and 17 deletions

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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)

View File

@ -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 <level>`\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
}
}

View File

@ -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 <connect|disconnect|reload>"
fun getUsage() = "bridge <connect|disconnect|reload|acceptPerm>"
fun execute(args: Array<String>): String {
fun execute(args: Array<String>, 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!"
}

View File

@ -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
}

View File

@ -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<String, MutableMap<String, Double>>
data class PermissionRequest(
val user: String,
val platform: String,
val userId: String,
val powerlevel: Double? = null
)
object PermissionConfig {
val permissionRequests = mutableMapOf<String, PermissionRequest>()
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))
}
}

View File

@ -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