diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt index ec571f7..bee3340 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Mobibot.kt @@ -36,6 +36,7 @@ import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.isChannelOp +import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.sendList import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.Utils.toIsoLocalDate @@ -106,7 +107,7 @@ import java.util.regex.Pattern import kotlin.system.exitProcess @Version(properties = "version.properties", className = "ReleaseInfo", template = "version.mustache", type = "kt") -class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() { +class Mobibot(nickname: String, val channel: String, logsDirPath: String, p: Properties) : ListenerAdapter() { // The bot configuration. private val config: Configuration @@ -116,9 +117,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert // Tell module private val tell: Tell - /** Main channel. */ - val channel: String - /** Logger. */ val logger: Logger = LoggerFactory.getLogger(Mobibot::class.java) @@ -185,9 +183,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert if (logger.isTraceEnabled) logger.trace("<<< ${user.nick}: ${event.message}") val cmds = event.message.trim().split(" ".toRegex(), 2) val cmd = cmds[0].lowercase() - val args = if (cmds.size > 1) { - cmds[1].trim() - } else "" + val args = cmds.lastOrEmpty().trim() if (cmd.startsWith(Constants.HELP_CMD)) { // help helpResponse(event, args) } else if (!addons.exec(channel, cmd, args, event)) { // Execute command or module @@ -217,9 +213,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert if (logger.isTraceEnabled) logger.trace(">>> $sender: $message") val cmds = message.substring(message.indexOf(':') + 1).trim().split(" ".toRegex(), 2) val cmd = cmds[0].lowercase() - val args = if (cmds.size > 1) { - cmds[1].trim() - } else "" + val args = cmds.lastOrEmpty().trim() if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help helpResponse(event, args) } else { @@ -354,7 +348,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert * Initialize the bot. */ init { - this.channel = channel val ircServer = p.getProperty("server", Constants.DEFAULT_SERVER) config = Configuration.Builder().apply { name = nickname diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt index 9ec9f70..e3b731e 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/links/View.kt @@ -35,6 +35,7 @@ package net.thauvin.erik.mobibot.commands.links import net.thauvin.erik.mobibot.Utils.bot import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.sendMessage import net.thauvin.erik.mobibot.commands.AbstractCommand import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.entries @@ -77,11 +78,7 @@ class View : AbstractCommand() { val split = query.split(" ", limit = 2) try { start = split[0].toInt() - 1 - query = if (split.size == 2) { - split[1].trim() - } else { - "" - } + query = split.lastOrEmpty().trim() if (start > entries.links.size) { start = 0 } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index 92ac36f..0cc1b41 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -46,6 +46,7 @@ import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.getIntProperty import net.thauvin.erik.mobibot.Utils.green import net.thauvin.erik.mobibot.Utils.helpFormat +import net.thauvin.erik.mobibot.Utils.lastOrEmpty import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.plural import net.thauvin.erik.mobibot.Utils.red @@ -187,6 +188,14 @@ class UtilsTest { assertThat(localDateTime.toIsoLocalDate(), "isoLocalDate(localDate)").isEqualTo("1952-02-17") } + @Test + fun testLastOrEmpty() { + val two = listOf("1", "2") + assertThat(two.lastOrEmpty(), "two").isEqualTo("2") + val one = listOf("1") + assertThat(one.lastOrEmpty(), "one").isEqualTo("") + } + @Test fun testObfuscate() { assertThat(ascii.obfuscate(), "obfuscate()").all {