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
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()
}

View file

@ -87,10 +87,10 @@ class GoogleSearch(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic
@Throws(ModuleException::class)
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.")
}
return if (StringUtils.isNotBlank(query)) {
return if (query.isNotBlank()) {
val results = ArrayList<Message>()
try {
val url = URL(

View file

@ -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]?)\\." +

View file

@ -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<String>(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)
}
/**

View file

@ -122,10 +122,10 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic
@Throws(ModuleException::class)
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.")
}
return if (StringUtils.isNotBlank(symbol)) {
return if (symbol.isNotBlank()) {
val debugMessage = "getQuote($symbol)"
val messages = ArrayList<Message>()
var response: String

View file

@ -101,17 +101,17 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
@JvmStatic
@Throws(ModuleException::class)
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.")
}
val owm = OWM(apiKey!!)
val owm = OWM(apiKey)
val messages = ArrayList<Message>()
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"

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.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)

View file

@ -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

View file

@ -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