diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.kt b/src/main/java/net/thauvin/erik/mobibot/Utils.kt index c1cae4b..e14f1eb 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.kt @@ -76,7 +76,7 @@ class Utils private constructor() { */ @JvmStatic fun colorize(s: String?, color: String): String { - if (s == null) { + if (s.isNullOrBlank()) { return Colors.NORMAL } else if (Colors.BOLD == color || Colors.REVERSE == color) { return color + s + color @@ -95,7 +95,7 @@ class Utils private constructor() { /** * URL encodes the given string. */ - fun encodeUrl(s: String?): String { + fun encodeUrl(s: String): String { return URLEncoder.encode(s, StandardCharsets.UTF_8) } @@ -127,11 +127,15 @@ class Utils private constructor() { * Returns a property as an int. */ @JvmStatic - fun getIntProperty(property: String, def: Int): Int { - return try { - property.toInt() - } catch (ignore: NumberFormatException) { + fun getIntProperty(property: String?, def: Int): Int { + return if (property == null) { def + } else { + try { + property.toInt() + } catch (ignore: NumberFormatException) { + def + } } } @@ -148,7 +152,7 @@ class Utils private constructor() { * nick. */ @JvmStatic - fun helpFormat(text: String?, botNick: String, isPrivate: Boolean): String { + fun helpFormat(text: String, botNick: String, isPrivate: Boolean): String { val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick) return StringUtils.replaceEach(text, searchFlags, replace) } @@ -158,7 +162,7 @@ class Utils private constructor() { */ @JvmStatic @JvmOverloads - fun helpIndent(help: String?, isBold: Boolean = true): String { + fun helpIndent(help: String, isBold: Boolean = true): String { return " " + if (isBold) bold(help) else help } @@ -182,8 +186,8 @@ class Utils private constructor() { * Obfuscates the given string. */ @JvmStatic - fun obfuscate(s: String?): String { - return if (s!!.isNotBlank()) { + fun obfuscate(s: String): String { + return if (s.isNotBlank()) { StringUtils.repeat('x', s.length) } else s } @@ -228,7 +232,7 @@ class Utils private constructor() { * Converts XML/XHTML entities to plain text. */ @JvmStatic - fun unescapeXml(str: String?): String { + fun unescapeXml(str: String): String { return Jsoup.parse(str).text() } diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index b05a0cb..891a6f2 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -87,10 +87,10 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) { @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List { - if (StringUtils.isBlank(apiKey) || StringUtils.isBlank(cseKey)) { + if (apiKey.isNullOrBlank() || cseKey.isNullOrBlank()) { throw ModuleException("${StringUtils.capitalize(GOOGLE_CMD)} is disabled. The API keys are missing.") } - return if (StringUtils.isNotBlank(query)) { + return if (query.isNotBlank()) { val results = ArrayList() try { val url = URL( diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt index cca419e..c46baf0 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Lookup.kt @@ -52,7 +52,9 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) { if (args.matches("(\\S.)+(\\S)+".toRegex())) { with(bot) { try { - send(lookup(args)) + lookup(args).split(',').forEach { + send(it.trim()) + } } catch (ignore: UnknownHostException) { if (args.matches( ("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.kt b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.kt index 3b10ada..f9a3d81 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/ModuleException.kt @@ -67,12 +67,17 @@ class ModuleException : Exception { /** * Return the sanitized message (e.g. remove API keys, etc.) */ - fun getSanitizedMessage(vararg sanitize: String?): String { - val obfuscate = arrayOfNulls(sanitize.size) - for (i in sanitize.indices) { - obfuscate[i] = Utils.obfuscate(sanitize[i]) + fun getSanitizedMessage(vararg sanitize: String): String { + val obfuscate = sanitize.map { Utils.obfuscate(it) }.toTypedArray() + return when { + cause != null -> { + cause.javaClass.name + ": " + StringUtils.replaceEach(cause.message, sanitize, obfuscate) + } + message != null -> { + message.javaClass.name + ": " + StringUtils.replaceEach(message, sanitize, obfuscate) + } + else -> "" } - return cause!!.javaClass.name + ": " + StringUtils.replaceEach(cause.message, sanitize, obfuscate) } /** diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt index bcf1838..434ca54 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -122,10 +122,10 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List { - if (StringUtils.isBlank(apiKey)) { + if (apiKey.isNullOrBlank()) { throw ModuleException("${STOCK_CMD.capitalize()} is disabled. The API key is missing.") } - return if (StringUtils.isNotBlank(symbol)) { + return if (symbol.isNotBlank()) { val debugMessage = "getQuote($symbol)" val messages = ArrayList() var response: String diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt index 2dea6c7..d1dcf25 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -101,17 +101,17 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List { - if (StringUtils.isBlank(apiKey)) { + if (apiKey.isNullOrBlank()) { throw ModuleException("${WEATHER_CMD.capitalize()} is disabled. The API key is missing.") } - val owm = OWM(apiKey!!) + val owm = OWM(apiKey) val messages = ArrayList() owm.unit = OWM.Unit.IMPERIAL - if (StringUtils.isNotBlank(query)) { + if (query.isNotBlank()) { val argv = query.split(",").toTypedArray() if (argv.size in 1..2) { val city = argv[0].trim() - val country: String = if (argv.size > 1 && StringUtils.isNotBlank(argv[1])) { + val country: String = if (argv.size > 1 && argv[1].isNotBlank()) { argv[1].trim() } else { "US" diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt index 8ecc469..cafe903 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt +++ b/src/main/java/net/thauvin/erik/mobibot/modules/WorldTime.kt @@ -36,7 +36,6 @@ import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.PublicMessage -import org.apache.commons.lang3.StringUtils import java.time.ZoneId import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @@ -72,7 +71,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) { */ @JvmStatic fun worldTime(query: String): Message { - val tz = COUNTRIES_MAP[StringUtils.upperCase(query.substring(query.indexOf(' ') + 1).trim())] + val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).toUpperCase()] val response: String = if (tz != null) { if (BEATS_KEYWORD == tz) { "The current Internet Time is: " + Utils.bold(internetTime() + ' ' + BEATS_KEYWORD) diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt b/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt index b934b02..019538f 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt +++ b/src/test/java/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt @@ -62,7 +62,7 @@ class GoogleSearchTest : LocalProperties() { .`as`("no query").isInstanceOf(ModuleException::class.java).hasNoCause() } catch (e: ModuleException) { // Avoid displaying api keys in CI logs - if ("true" == System.getenv("CI")) { + if ("true" == System.getenv("CI") && !apiKey.isNullOrBlank() && !cseKey.isNullOrBlank()) { throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey, cseKey)) } else { throw e diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt index 96cc1c1..ea78c34 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -63,7 +63,7 @@ class StockQuoteTest : LocalProperties() { .isInstanceOf(ModuleException::class.java).hasNoCause() } catch (e: ModuleException) { // Avoid displaying api keys in CI logs - if ("true" == System.getenv("CI")) { + if ("true" == System.getenv("CI") && !apiKey.isNullOrBlank()) { throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey)) } else { throw e