This commit is contained in:
Erik C. Thauvin 2021-05-01 21:46:33 -07:00
parent 67f618da71
commit 9f3dc273f0
12 changed files with 49 additions and 51 deletions

View file

@ -51,9 +51,6 @@
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$3600</ID> <ID>MagicNumber:WorldTime.kt$WorldTime.Companion$3600</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$60</ID> <ID>MagicNumber:WorldTime.kt$WorldTime.Companion$60</ID>
<ID>MagicNumber:WorldTime.kt$WorldTime.Companion$86.4</ID> <ID>MagicNumber:WorldTime.kt$WorldTime.Companion$86.4</ID>
<ID>MemberNameEqualsClassName:Calc.kt$Calc.Companion$ @JvmStatic fun calc(query: String): String</ID>
<ID>MemberNameEqualsClassName:Lookup.kt$Lookup.Companion$ @JvmStatic @Throws(UnknownHostException::class) fun lookup(query: String): String</ID>
<ID>MemberNameEqualsClassName:WorldTime.kt$WorldTime.Companion$ @JvmStatic fun worldTime(query: String): Message</ID>
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties)</ID> <ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand, props: Properties)</ID>
<ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID> <ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse( sender: String, login: String, args: String, isOp: Boolean, isPrivate: Boolean )</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID> <ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID>
@ -75,14 +72,8 @@
<ID>NestedBlockDepth:Weather2.kt$Weather2$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID> <ID>NestedBlockDepth:Weather2.kt$Weather2$ override fun run(sender: String, cmd: String, args: String, isPrivate: Boolean)</ID>
<ID>NestedBlockDepth:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID> <ID>NestedBlockDepth:Weather2.kt$Weather2.Companion$ @JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>NestedBlockDepth:WorldTime.kt$WorldTime$override fun commandResponse( sender: String, cmd: String, args: String, isPrivate: Boolean )</ID> <ID>NestedBlockDepth:WorldTime.kt$WorldTime$override fun commandResponse( sender: String, cmd: String, args: String, isPrivate: Boolean )</ID>
<ID>PrintStackTrace:Mobibot.kt$Mobibot$e</ID>
<ID>PrintStackTrace:Mobibot.kt$Mobibot.Companion$e</ID>
<ID>ReturnCount:Addons.kt$Addons$ fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID> <ID>ReturnCount:Addons.kt$Addons$ fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID>
<ID>ReturnCount:Addons.kt$Addons$ fun help(sender: String, topic: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID> <ID>ReturnCount:Addons.kt$Addons$ fun help(sender: String, topic: String, isOp: Boolean, isPrivate: Boolean): Boolean</ID>
<ID>SwallowedException:Calc.kt$Calc.Companion$catch (e: IllegalArgumentException) { "No idea. This is the kind of math I don't get." }</ID>
<ID>SwallowedException:GoogleSearchTest.kt$GoogleSearchTest$catch (e: ModuleException) { // Avoid displaying api keys in CI logs if ("true" == System.getenv("CI") &amp;&amp; apiKey.isNotBlank() &amp;&amp; cseKey.isNotBlank()) { throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey, cseKey)) } else { throw e } }</ID>
<ID>SwallowedException:StockQuoteTest.kt$StockQuoteTest$catch (e: ModuleException) { // Avoid displaying api keys in CI logs if ("true" == System.getenv("CI") &amp;&amp; apiKey.isNotBlank()) { throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey)) } else { throw e } }</ID>
<ID>SwallowedException:TwitterTest.kt$TwitterTest$catch (e: UnknownHostException) { "Unknown Host" }</ID>
<ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$ @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List&lt;Message&gt;</ID> <ID>ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$ @JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$ @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List&lt;Message&gt;</ID> <ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$ @JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List&lt;Message&gt;</ID>
<ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID> <ID>ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject</ID>

View file

@ -202,6 +202,9 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
try { try {
connect(ircServer, ircPort) connect(ircServer, ircPort)
} catch (e: Exception) { } catch (e: Exception) {
if (logger.isDebugEnabled) {
logger.debug("Unable to connect to $ircServer, will try again.", e)
}
var retries = 0 var retries = 0
while (retries++ < MAX_RECONNECT && !isConnected) { while (retries++ < MAX_RECONNECT && !isConnected) {
sleep(10) sleep(10)
@ -212,7 +215,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (logger.isDebugEnabled) { if (logger.isDebugEnabled) {
logger.debug("Unable to reconnect to $ircServer, after $MAX_RECONNECT retries.", ex) logger.debug("Unable to reconnect to $ircServer, after $MAX_RECONNECT retries.", ex)
} }
e.printStackTrace(System.err) System.err.println("An error has occurred while reconnecting; ${ex.message}")
exitProcess(1) exitProcess(1)
} }
} }
@ -528,7 +531,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
commandLine = parser.parse(options, args) commandLine = parser.parse(options, args)
} catch (e: ParseException) { } catch (e: ParseException) {
System.err.println("CLI Parsing failed. Reason: ${e.message}") System.err.println("CLI Parsing failed. Reason: ${e.message}")
e.printStackTrace(System.err)
exitProcess(1) exitProcess(1)
} }
when { when {
@ -550,13 +552,11 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
).use { fis -> ).use { fis ->
p.load(fis) p.load(fis)
} }
} catch (e: FileNotFoundException) { } catch (ignore: FileNotFoundException) {
System.err.println("Unable to find properties file.") System.err.println("Unable to find properties file.")
e.printStackTrace(System.err)
exitProcess(1) exitProcess(1)
} catch (e: IOException) { } catch (ignore: IOException) {
System.err.println("Unable to open properties file.") System.err.println("Unable to open properties file.")
e.printStackTrace(System.err)
exitProcess(1) exitProcess(1)
} }
val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase()) val nickname = p.getProperty("nick", Mobibot::class.java.name.lowercase())
@ -574,9 +574,8 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
), true ), true
) )
System.setOut(stdout) System.setOut(stdout)
} catch (e: IOException) { } catch (ignore: IOException) {
System.err.println("Unable to open output (stdout) log file.") System.err.println("Unable to open output (stdout) log file.")
e.printStackTrace(System.err)
exitProcess(1) exitProcess(1)
} }
try { try {
@ -586,9 +585,8 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
), true ), true
) )
System.setErr(stderr) System.setErr(stderr)
} catch (e: IOException) { } catch (ignore: IOException) {
System.err.println("Unable to open error (stderr) log file.") System.err.println("Unable to open error (stderr) log file.")
e.printStackTrace(System.err)
exitProcess(1) exitProcess(1)
} }
} }

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.objecthunter.exp4j.ExpressionBuilder import net.objecthunter.exp4j.ExpressionBuilder
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException
import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import java.text.DecimalFormat import java.text.DecimalFormat
@ -47,7 +48,15 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
isPrivate: Boolean isPrivate: Boolean
) { ) {
if (args.isNotBlank()) { if (args.isNotBlank()) {
bot.send(calc(args)) try {
bot.send(calculate(args))
} catch (e: IllegalArgumentException) {
if (bot.logger.isWarnEnabled) bot.logger.warn("Failed to calcualte: $args", e)
bot.send("No idea. This is the kind of math I don't get.")
} catch (e: UnknownFunctionOrVariableException) {
if (bot.logger.isWarnEnabled) bot.logger.warn("Unable to calcualte: $args", e)
bot.send("No idea. I must've some form of Dyscalculia.")
}
} else { } else {
helpResponse(sender, isPrivate) helpResponse(sender, isPrivate)
} }
@ -61,14 +70,11 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
* Performs a calculation. e.g.: 1 + 1 * 2 * Performs a calculation. e.g.: 1 + 1 * 2
*/ */
@JvmStatic @JvmStatic
fun calc(query: String): String { @Throws(IllegalArgumentException::class)
fun calculate(query: String): String {
val decimalFormat = DecimalFormat("#.##") val decimalFormat = DecimalFormat("#.##")
return try { val calc = ExpressionBuilder(query).build()
val calc = ExpressionBuilder(query).build() return query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
} catch (e: IllegalArgumentException) {
"No idea. This is the kind of math I don't get."
}
} }
} }

View file

@ -52,7 +52,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
if (args.matches("(\\S.)+(\\S)+".toRegex())) { if (args.matches("(\\S.)+(\\S)+".toRegex())) {
with(bot) { with(bot) {
try { try {
lookup(args).split(',').forEach { nslookup(args).split(',').forEach {
send(it.trim()) send(it.trim())
} }
} catch (ignore: UnknownHostException) { } catch (ignore: UnknownHostException) {
@ -105,7 +105,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
*/ */
@JvmStatic @JvmStatic
@Throws(UnknownHostException::class) @Throws(UnknownHostException::class)
fun lookup(query: String): String { fun nslookup(query: String): String {
val buffer = StringBuilder() val buffer = StringBuilder()
val results = InetAddress.getAllByName(query) val results = InetAddress.getAllByName(query)
var hostInfo: String var hostInfo: String

View file

@ -68,10 +68,10 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
} }
/** /**
* Returns the world time. * Returns the time for the given timezone/city.
*/ */
@JvmStatic @JvmStatic
fun worldTime(query: String): Message { fun time(query: String): Message {
val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).uppercase()] val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).uppercase()]
val response: String = if (tz != null) { val response: String = if (tz != null) {
if (BEATS_KEYWORD == tz) { if (BEATS_KEYWORD == tz) {
@ -193,7 +193,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
send(sender, "The supported countries/zones are: ", isPrivate) send(sender, "The supported countries/zones are: ", isPrivate)
sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false) sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false)
} else { } else {
val msg = worldTime(args) val msg = time(args)
if (isPrivate) { if (isPrivate) {
send(sender, msg.msg, true) send(sender, msg.msg, true)
} else { } else {

View file

@ -31,9 +31,11 @@
*/ */
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.modules.Calc.Companion.calc import net.thauvin.erik.mobibot.modules.Calc.Companion.calculate
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.testng.annotations.Test import org.testng.annotations.Test
/** /**
@ -41,10 +43,11 @@ import org.testng.annotations.Test
*/ */
class CalcTest { class CalcTest {
@Test @Test
fun testCalc() { fun testCalculate() {
assertThat(calc("1 + 1")).`as`("calc(1+1)").isEqualTo("1+1 = %s", Utils.bold(2)) assertThat(calculate("1 + 1")).`as`("calculate(1+1)").isEqualTo("1+1 = %s", Utils.bold(2))
assertThat(calc("1 -3")).`as`("calc(1 -3)").isEqualTo("1-3 = %s", Utils.bold(-2)) assertThat(calculate("1 -3")).`as`("calculate(1 -3)").isEqualTo("1-3 = %s", Utils.bold(-2))
assertThat(calc("pi+π+e+φ")).`as`("calc(pi+π+e+φ)").isEqualTo("pi+π+e+φ = %s", Utils.bold("10.62")) assertThat(calculate("pi+π+e+φ")).`as`("calculate(pi+π+e+φ)").isEqualTo("pi+π+e+φ = %s", Utils.bold("10.62"))
assertThat(calc("one + one")).`as`("calc(one + one)").startsWith("No idea.") assertThatThrownBy { calculate("one + one") }.`as`("calculate(one+one)")
.isInstanceOf(UnknownFunctionOrVariableException::class.java)
} }
} }

View file

@ -64,7 +64,7 @@ class GoogleSearchTest : LocalProperties() {
} 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") && apiKey.isNotBlank() && cseKey.isNotBlank()) { if ("true" == System.getenv("CI") && apiKey.isNotBlank() && cseKey.isNotBlank()) {
throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey, cseKey)) throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey, cseKey), e)
} else { } else {
throw e throw e
} }

View file

@ -31,7 +31,7 @@
*/ */
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.modules.Lookup.Companion.lookup import net.thauvin.erik.mobibot.modules.Lookup.Companion.nslookup
import net.thauvin.erik.mobibot.modules.Lookup.Companion.whois import net.thauvin.erik.mobibot.modules.Lookup.Companion.whois
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test import org.testng.annotations.Test
@ -43,7 +43,7 @@ class LookupTest {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testLookup() { fun testLookup() {
val result = lookup("apple.com") val result = nslookup("apple.com")
assertThat(result).`as`("lookup(apple.com)").contains("17.253.144.10") assertThat(result).`as`("lookup(apple.com)").contains("17.253.144.10")
} }

View file

@ -62,7 +62,7 @@ class StockQuoteTest : LocalProperties() {
} 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") && apiKey.isNotBlank()) { if ("true" == System.getenv("CI") && apiKey.isNotBlank()) {
throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey)) throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey), e)
} else { } else {
throw e throw e
} }

View file

@ -47,7 +47,7 @@ class TwitterTest : LocalProperties() {
val ciName = System.getenv("CI_NAME") val ciName = System.getenv("CI_NAME")
return ciName ?: try { return ciName ?: try {
InetAddress.getLocalHost().hostName InetAddress.getLocalHost().hostName
} catch (e: UnknownHostException) { } catch (ignore: UnknownHostException) {
"Unknown Host" "Unknown Host"
} }
} }

View file

@ -32,7 +32,7 @@
package net.thauvin.erik.mobibot.modules package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.mobibot.Utils import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.modules.WorldTime.Companion.worldTime import net.thauvin.erik.mobibot.modules.WorldTime.Companion.time
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test import org.testng.annotations.Test
@ -41,9 +41,9 @@ import org.testng.annotations.Test
*/ */
class WordTimeTest { class WordTimeTest {
@Test @Test
fun testWorldTime() { fun testTime() {
assertThat(worldTime("PST").msg).`as`("PST").endsWith(Utils.bold("Los Angeles")) assertThat(time("PST").msg).`as`("PST").endsWith(Utils.bold("Los Angeles"))
assertThat(worldTime("BLAH").isError).`as`("BLAH").isTrue assertThat(time("BLAH").isError).`as`("BLAH").isTrue
assertThat(worldTime("BEATS").msg).`as`("BEATS").contains("@") assertThat(time("BEATS").msg).`as`("BEATS").contains("@")
} }
} }

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Fri Apr 30 21:34:46 PDT 2021 #Sat May 01 21:41:40 PDT 2021
version.buildmeta=538 version.buildmeta=568
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=beta version.prerelease=beta
version.project=mobibot version.project=mobibot
version.semver=0.8.0-beta+538 version.semver=0.8.0-beta+568