This commit is contained in:
Erik C. Thauvin 2020-12-02 14:20:53 -08:00
parent 550e7a0bfb
commit d83ce652e7
9 changed files with 39 additions and 29 deletions

View file

@ -76,7 +76,7 @@ class Utils private constructor() {
*/ */
@JvmStatic @JvmStatic
fun colorize(s: String?, color: String): String { fun colorize(s: String?, color: String): String {
if (s == null) { if (s.isNullOrBlank()) {
return Colors.NORMAL return Colors.NORMAL
} else if (Colors.BOLD == color || Colors.REVERSE == color) { } else if (Colors.BOLD == color || Colors.REVERSE == color) {
return color + s + color return color + s + color
@ -95,7 +95,7 @@ class Utils private constructor() {
/** /**
* URL encodes the given string. * URL encodes the given string.
*/ */
fun encodeUrl(s: String?): String { fun encodeUrl(s: String): String {
return URLEncoder.encode(s, StandardCharsets.UTF_8) return URLEncoder.encode(s, StandardCharsets.UTF_8)
} }
@ -127,11 +127,15 @@ class Utils private constructor() {
* Returns a property as an int. * Returns a property as an int.
*/ */
@JvmStatic @JvmStatic
fun getIntProperty(property: String, def: Int): Int { fun getIntProperty(property: String?, def: Int): Int {
return try { return if (property == null) {
property.toInt()
} catch (ignore: NumberFormatException) {
def def
} else {
try {
property.toInt()
} catch (ignore: NumberFormatException) {
def
}
} }
} }
@ -148,7 +152,7 @@ class Utils private constructor() {
* nick. * nick.
*/ */
@JvmStatic @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) val replace = arrayOf(if (isPrivate) "/msg $botNick" else "$botNick:", botNick)
return StringUtils.replaceEach(text, searchFlags, replace) return StringUtils.replaceEach(text, searchFlags, replace)
} }
@ -158,7 +162,7 @@ class Utils private constructor() {
*/ */
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun helpIndent(help: String?, isBold: Boolean = true): String { fun helpIndent(help: String, isBold: Boolean = true): String {
return " " + if (isBold) bold(help) else help return " " + if (isBold) bold(help) else help
} }
@ -182,8 +186,8 @@ class Utils private constructor() {
* Obfuscates the given string. * Obfuscates the given string.
*/ */
@JvmStatic @JvmStatic
fun obfuscate(s: String?): String { fun obfuscate(s: String): String {
return if (s!!.isNotBlank()) { return if (s.isNotBlank()) {
StringUtils.repeat('x', s.length) StringUtils.repeat('x', s.length)
} else s } else s
} }
@ -228,7 +232,7 @@ class Utils private constructor() {
* Converts XML/XHTML entities to plain text. * Converts XML/XHTML entities to plain text.
*/ */
@JvmStatic @JvmStatic
fun unescapeXml(str: String?): String { fun unescapeXml(str: String): String {
return Jsoup.parse(str).text() return Jsoup.parse(str).text()
} }

View file

@ -87,10 +87,10 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> { fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> {
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.") 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<Message>() val results = ArrayList<Message>()
try { try {
val url = URL( val url = URL(

View file

@ -52,7 +52,9 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
if (args.matches("(\\S.)+(\\S)+".toRegex())) { if (args.matches("(\\S.)+(\\S)+".toRegex())) {
with(bot) { with(bot) {
try { try {
send(lookup(args)) lookup(args).split(',').forEach {
send(it.trim())
}
} catch (ignore: UnknownHostException) { } catch (ignore: UnknownHostException) {
if (args.matches( 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]?)\\." + ("(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]?)\\." +

View file

@ -67,12 +67,17 @@ class ModuleException : Exception {
/** /**
* Return the sanitized message (e.g. remove API keys, etc.) * Return the sanitized message (e.g. remove API keys, etc.)
*/ */
fun getSanitizedMessage(vararg sanitize: String?): String { fun getSanitizedMessage(vararg sanitize: String): String {
val obfuscate = arrayOfNulls<String>(sanitize.size) val obfuscate = sanitize.map { Utils.obfuscate(it) }.toTypedArray()
for (i in sanitize.indices) { return when {
obfuscate[i] = Utils.obfuscate(sanitize[i]) 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)
} }
/** /**

View file

@ -122,10 +122,10 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun getQuote(symbol: String, apiKey: String?): List<Message> { fun getQuote(symbol: String, apiKey: String?): List<Message> {
if (StringUtils.isBlank(apiKey)) { if (apiKey.isNullOrBlank()) {
throw ModuleException("${STOCK_CMD.capitalize()} is disabled. The API key is missing.") 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 debugMessage = "getQuote($symbol)"
val messages = ArrayList<Message>() val messages = ArrayList<Message>()
var response: String var response: String

View file

@ -101,17 +101,17 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic @JvmStatic
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun getWeather(query: String, apiKey: String?): List<Message> { fun getWeather(query: String, apiKey: String?): List<Message> {
if (StringUtils.isBlank(apiKey)) { if (apiKey.isNullOrBlank()) {
throw ModuleException("${WEATHER_CMD.capitalize()} is disabled. The API key is missing.") throw ModuleException("${WEATHER_CMD.capitalize()} is disabled. The API key is missing.")
} }
val owm = OWM(apiKey!!) val owm = OWM(apiKey)
val messages = ArrayList<Message>() val messages = ArrayList<Message>()
owm.unit = OWM.Unit.IMPERIAL owm.unit = OWM.Unit.IMPERIAL
if (StringUtils.isNotBlank(query)) { if (query.isNotBlank()) {
val argv = query.split(",").toTypedArray() val argv = query.split(",").toTypedArray()
if (argv.size in 1..2) { if (argv.size in 1..2) {
val city = argv[0].trim() 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() argv[1].trim()
} else { } else {
"US" "US"

View file

@ -36,7 +36,6 @@ import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.msg.ErrorMessage import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage import net.thauvin.erik.mobibot.msg.PublicMessage
import org.apache.commons.lang3.StringUtils
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
@ -72,7 +71,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
*/ */
@JvmStatic @JvmStatic
fun worldTime(query: String): Message { 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) { val response: String = if (tz != null) {
if (BEATS_KEYWORD == tz) { if (BEATS_KEYWORD == tz) {
"The current Internet Time is: " + Utils.bold(internetTime() + ' ' + BEATS_KEYWORD) "The current Internet Time is: " + Utils.bold(internetTime() + ' ' + BEATS_KEYWORD)

View file

@ -62,7 +62,7 @@ class GoogleSearchTest : LocalProperties() {
.`as`("no query").isInstanceOf(ModuleException::class.java).hasNoCause() .`as`("no query").isInstanceOf(ModuleException::class.java).hasNoCause()
} catch (e: ModuleException) { } catch (e: ModuleException) {
// Avoid displaying api keys in CI logs // 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)) throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey, cseKey))
} else { } else {
throw e throw e

View file

@ -63,7 +63,7 @@ class StockQuoteTest : LocalProperties() {
.isInstanceOf(ModuleException::class.java).hasNoCause() .isInstanceOf(ModuleException::class.java).hasNoCause()
} catch (e: ModuleException) { } catch (e: ModuleException) {
// Avoid displaying api keys in CI logs // 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)) throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey))
} else { } else {
throw e throw e