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

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

View file

@ -32,6 +32,7 @@
package net.thauvin.erik.mobibot.modules
import net.objecthunter.exp4j.ExpressionBuilder
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import java.text.DecimalFormat
@ -47,7 +48,15 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
isPrivate: Boolean
) {
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 {
helpResponse(sender, isPrivate)
}
@ -61,14 +70,11 @@ class Calc(bot: Mobibot) : AbstractModule(bot) {
* Performs a calculation. e.g.: 1 + 1 * 2
*/
@JvmStatic
fun calc(query: String): String {
@Throws(IllegalArgumentException::class)
fun calculate(query: String): String {
val decimalFormat = DecimalFormat("#.##")
return try {
val calc = ExpressionBuilder(query).build()
query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
} catch (e: IllegalArgumentException) {
"No idea. This is the kind of math I don't get."
}
val calc = ExpressionBuilder(query).build()
return query.replace(" ", "") + " = " + Utils.bold(decimalFormat.format(calc.evaluate()))
}
}

View file

@ -52,7 +52,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
if (args.matches("(\\S.)+(\\S)+".toRegex())) {
with(bot) {
try {
lookup(args).split(',').forEach {
nslookup(args).split(',').forEach {
send(it.trim())
}
} catch (ignore: UnknownHostException) {
@ -105,7 +105,7 @@ class Lookup(bot: Mobibot) : AbstractModule(bot) {
*/
@JvmStatic
@Throws(UnknownHostException::class)
fun lookup(query: String): String {
fun nslookup(query: String): String {
val buffer = StringBuilder()
val results = InetAddress.getAllByName(query)
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
fun worldTime(query: String): Message {
fun time(query: String): Message {
val tz = COUNTRIES_MAP[(query.substring(query.indexOf(' ') + 1).trim()).uppercase()]
val response: String = if (tz != null) {
if (BEATS_KEYWORD == tz) {
@ -193,7 +193,7 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) {
send(sender, "The supported countries/zones are: ", isPrivate)
sendList(sender, ArrayList(COUNTRIES_MAP.keys), 17, isPrivate = false)
} else {
val msg = worldTime(args)
val msg = time(args)
if (isPrivate) {
send(sender, msg.msg, true)
} else {

View file

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

View file

@ -64,7 +64,7 @@ class GoogleSearchTest : LocalProperties() {
} catch (e: ModuleException) {
// Avoid displaying api keys in CI logs
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 {
throw e
}

View file

@ -31,7 +31,7 @@
*/
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 org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test
@ -43,7 +43,7 @@ class LookupTest {
@Test
@Throws(Exception::class)
fun testLookup() {
val result = lookup("apple.com")
val result = nslookup("apple.com")
assertThat(result).`as`("lookup(apple.com)").contains("17.253.144.10")
}

View file

@ -62,7 +62,7 @@ class StockQuoteTest : LocalProperties() {
} catch (e: ModuleException) {
// Avoid displaying api keys in CI logs
if ("true" == System.getenv("CI") && apiKey.isNotBlank()) {
throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey))
throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey), e)
} else {
throw e
}

View file

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

View file

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