Command parser: check userId format
This commit is contained in:
parent
81ddb8c5fb
commit
3b52fa4be8
|
@ -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,52 +103,76 @@ 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]
|
||||||
val reason = textMessage.substring(Command.KICK_USER.command.length
|
if (MatrixPatterns.isUserId(userId)) {
|
||||||
+ 1
|
val reason = textMessage.substring(Command.KICK_USER.command.length
|
||||||
+ user.length).trim()
|
+ 1
|
||||||
|
+ 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]
|
||||||
val reason = textMessage.substring(Command.BAN_USER.command.length
|
if (MatrixPatterns.isUserId(userId)) {
|
||||||
+ 1
|
val reason = textMessage.substring(Command.BAN_USER.command.length
|
||||||
+ user.length).trim()
|
+ 1
|
||||||
|
+ 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]
|
||||||
val powerLevelsAsString = messageParts[2]
|
if (MatrixPatterns.isUserId(userId)) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
|
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -158,7 +183,11 @@ object CommandParser {
|
||||||
return if (messageParts.size == 2) {
|
return if (messageParts.size == 2) {
|
||||||
val userId = messageParts[1]
|
val userId = messageParts[1]
|
||||||
|
|
||||||
ParsedCommand.SetUserPowerLevel(userId, 0)
|
if (MatrixPatterns.isUserId(userId)) {
|
||||||
|
ParsedCommand.SetUserPowerLevel(userId, 0)
|
||||||
|
} else {
|
||||||
|
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
|
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue