Cleanup.
This commit is contained in:
parent
550e7a0bfb
commit
d83ce652e7
9 changed files with 39 additions and 29 deletions
|
@ -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,13 +127,17 @@ class Utils private constructor() {
|
|||
* Returns a property as an int.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getIntProperty(property: String, def: Int): Int {
|
||||
return try {
|
||||
fun getIntProperty(property: String?, def: Int): Int {
|
||||
return if (property == null) {
|
||||
def
|
||||
} else {
|
||||
try {
|
||||
property.toInt()
|
||||
} catch (ignore: NumberFormatException) {
|
||||
def
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the given string green.
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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]?)\\." +
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue