Command parser: check userId format

This commit is contained in:
Benoit Marty 2019-04-09 14:52:31 +02:00
parent 81ddb8c5fb
commit 3b52fa4be8
1 changed files with 51 additions and 22 deletions

View File

@ -16,6 +16,7 @@
package im.vector.riotredesign.features.command package im.vector.riotredesign.features.command
import im.vector.matrix.android.api.MatrixPatterns
import timber.log.Timber import timber.log.Timber
object CommandParser { object CommandParser {
@ -60,10 +61,10 @@ object CommandParser {
when (slashCommand) { when (slashCommand) {
Command.CHANGE_DISPLAY_NAME.command -> { Command.CHANGE_DISPLAY_NAME.command -> {
val newDisplayname = textMessage.substring(Command.CHANGE_DISPLAY_NAME.command.length).trim() val newDisplayName = textMessage.substring(Command.CHANGE_DISPLAY_NAME.command.length).trim()
return if (newDisplayname.isNotEmpty()) { return if (newDisplayName.isNotEmpty()) {
ParsedCommand.ChangeDisplayName(newDisplayname) ParsedCommand.ChangeDisplayName(newDisplayName)
} else { } else {
ParsedCommand.ErrorSyntax(Command.CHANGE_DISPLAY_NAME) ParsedCommand.ErrorSyntax(Command.CHANGE_DISPLAY_NAME)
} }
@ -102,66 +103,94 @@ object CommandParser {
} }
Command.INVITE.command -> { Command.INVITE.command -> {
return if (messageParts.size == 2) { return if (messageParts.size == 2) {
ParsedCommand.Invite(messageParts[1]) val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.Invite(userId)
} else {
ParsedCommand.ErrorSyntax(Command.INVITE)
}
} else { } else {
ParsedCommand.ErrorSyntax(Command.INVITE) ParsedCommand.ErrorSyntax(Command.INVITE)
} }
} }
Command.KICK_USER.command -> { Command.KICK_USER.command -> {
return if (messageParts.size >= 2) { return if (messageParts.size >= 2) {
val user = messageParts[1] val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
val reason = textMessage.substring(Command.KICK_USER.command.length val reason = textMessage.substring(Command.KICK_USER.command.length
+ 1 + 1
+ user.length).trim() + userId.length).trim()
ParsedCommand.KickUser(user, reason) ParsedCommand.KickUser(userId, reason)
} else {
ParsedCommand.ErrorSyntax(Command.KICK_USER)
}
} else { } else {
ParsedCommand.ErrorSyntax(Command.KICK_USER) ParsedCommand.ErrorSyntax(Command.KICK_USER)
} }
} }
Command.BAN_USER.command -> { Command.BAN_USER.command -> {
return if (messageParts.size >= 2) { return if (messageParts.size >= 2) {
val user = messageParts[1] val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
val reason = textMessage.substring(Command.BAN_USER.command.length val reason = textMessage.substring(Command.BAN_USER.command.length
+ 1 + 1
+ user.length).trim() + userId.length).trim()
ParsedCommand.BanUser(user, reason) ParsedCommand.BanUser(userId, reason)
} else {
ParsedCommand.ErrorSyntax(Command.BAN_USER)
}
} else { } else {
ParsedCommand.ErrorSyntax(Command.BAN_USER) ParsedCommand.ErrorSyntax(Command.BAN_USER)
} }
} }
Command.UNBAN_USER.command -> { Command.UNBAN_USER.command -> {
return if (messageParts.size == 2) { return if (messageParts.size == 2) {
ParsedCommand.UnbanUser(messageParts[1]) val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.UnbanUser(userId)
} else {
ParsedCommand.ErrorSyntax(Command.UNBAN_USER)
}
} else { } else {
ParsedCommand.ErrorSyntax(Command.UNBAN_USER) ParsedCommand.ErrorSyntax(Command.UNBAN_USER)
} }
} }
Command.SET_USER_POWER_LEVEL.command -> { Command.SET_USER_POWER_LEVEL.command -> {
return if (messageParts.size == 3) { return if (messageParts.size == 3) {
val userID = messageParts[1] val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
val powerLevelsAsString = messageParts[2] val powerLevelsAsString = messageParts[2]
try { try {
val powerLevelsAsInt = Integer.parseInt(powerLevelsAsString) val powerLevelsAsInt = Integer.parseInt(powerLevelsAsString)
ParsedCommand.SetUserPowerLevel(userID, powerLevelsAsInt) ParsedCommand.SetUserPowerLevel(userId, powerLevelsAsInt)
} catch (e: Exception) { } catch (e: Exception) {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL) ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
} }
} else { } else {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL) ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
} }
} else {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
}
} }
Command.RESET_USER_POWER_LEVEL.command -> { Command.RESET_USER_POWER_LEVEL.command -> {
return if (messageParts.size == 2) { return if (messageParts.size == 2) {
val userId = messageParts[1] val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.SetUserPowerLevel(userId, 0) ParsedCommand.SetUserPowerLevel(userId, 0)
} else { } else {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL) ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
} }
} else {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
}
} }
Command.MARKDOWN.command -> { Command.MARKDOWN.command -> {
return if (messageParts.size == 2) { return if (messageParts.size == 2) {