Added addons.exec() and addons.match() functions.

Converted TellMessagesMgr and EntriesMgr to objects.
This commit is contained in:
Erik C. Thauvin 2020-12-07 01:33:59 -08:00
parent b24c0ebe54
commit f9edff4813
4 changed files with 261 additions and 270 deletions

View file

@ -87,6 +87,39 @@ class Addons {
} }
} }
/**
* Execute a command or module.
*/
fun exec(sender: String, login: String, cmd: String, args: String, isOp: Boolean, isPrivate: Boolean): Boolean {
for (command in commands) {
if (command.name.startsWith(cmd)) {
command.commandResponse(sender, login, args, isOp, isPrivate)
return true
}
}
for (module in modules) {
if ((isPrivate && module.isPrivateMsgEnabled) || !isPrivate) {
if (module.commands.contains(cmd)) {
module.commandResponse(sender, cmd, args, isPrivate)
return true
}
}
}
return false
}
/**
* Match a command.
*/
fun match(sender: String, login: String, message: String, isOp: Boolean, isPrivate: Boolean) {
for (command in commands) {
if (command.matches(message)) {
command.commandResponse(sender, login, message, isOp, isPrivate)
break
}
}
}
/** /**
* Sort commands and modules names. * Sort commands and modules names.
*/ */

View file

@ -58,9 +58,6 @@ import net.thauvin.erik.mobibot.commands.Users
import net.thauvin.erik.mobibot.commands.Versions import net.thauvin.erik.mobibot.commands.Versions
import net.thauvin.erik.mobibot.commands.links.Comment import net.thauvin.erik.mobibot.commands.links.Comment
import net.thauvin.erik.mobibot.commands.links.LinksMgr import net.thauvin.erik.mobibot.commands.links.LinksMgr
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.saveEntries
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.startDate
import net.thauvin.erik.mobibot.commands.links.LinksMgr.Companion.startup
import net.thauvin.erik.mobibot.commands.links.Posting import net.thauvin.erik.mobibot.commands.links.Posting
import net.thauvin.erik.mobibot.commands.links.Tags import net.thauvin.erik.mobibot.commands.links.Tags
import net.thauvin.erik.mobibot.commands.links.View import net.thauvin.erik.mobibot.commands.links.View
@ -363,31 +360,12 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help if (cmd.startsWith(Constants.HELP_CMD)) { // mobibot: help
helpResponse(sender, args, false) helpResponse(sender, args, false)
} else { } else {
// Commands // Execute module or command
for (command in addons.commands) { addons.exec(sender, login, cmd, args, isOp(sender), false)
if (command.isPublic && command.name.startsWith(cmd)) {
command.commandResponse(sender, login, args, isOp(sender), false)
return
}
}
// Modules
for (module in addons.modules) { // modules
for (c in module.commands) {
if (cmd.startsWith(c)) {
module.commandResponse(sender, cmd, args, false)
return
}
}
}
} }
} else { } else {
// Commands, e.g.: https://www.example.com/ // Links, e.g.: https://www.example.com/ or L1: , etc.
for (command in addons.commands) { addons.match(sender, login, message, isOp(sender), false)
if (command.matches(message)) {
command.commandResponse(sender, login, message, isOp(sender), false)
return
}
}
} }
storeRecap(sender, message, false) storeRecap(sender, message, false)
} }
@ -420,25 +398,11 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
quitServer("The Bot Is Out There!") quitServer("The Bot Is Out There!")
exitProcess(0) exitProcess(0)
} else { } else {
for (command in addons.commands) { if (!addons.exec(sender, login, cmd, args, isOp, true)) {
if (command.name.startsWith(cmd)) {
command.commandResponse(sender, login, args, isOp, true)
return
}
}
for (module in addons.modules) {
if (module.isPrivateMsgEnabled) {
for (c in module.commands) {
if (cmd == c) {
module.commandResponse(sender, cmd, args, true)
return
}
}
}
}
helpDefault(sender, isOp, true) helpDefault(sender, isOp, true)
} }
} }
}
override fun onAction(sender: String, login: String, hostname: String, target: String, action: String) { override fun onAction(sender: String, login: String, hostname: String, target: String, action: String) {
if (channel == target) { if (channel == target) {
@ -600,16 +564,17 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
HelpFormatter().printHelp(Mobibot::class.java.name, options) HelpFormatter().printHelp(Mobibot::class.java.name, options)
} }
commandLine.hasOption(Constants.VERSION_ARG[0]) -> { commandLine.hasOption(Constants.VERSION_ARG[0]) -> {
// Output the version
println("${ReleaseInfo.PROJECT} ${ReleaseInfo.VERSION} (${isoLocalDate(ReleaseInfo.BUILDDATE)})") println("${ReleaseInfo.PROJECT} ${ReleaseInfo.VERSION} (${isoLocalDate(ReleaseInfo.BUILDDATE)})")
println(ReleaseInfo.WEBSITE) println(ReleaseInfo.WEBSITE)
} }
else -> { else -> {
// Load the properties
val p = Properties() val p = Properties()
try { try {
Files.newInputStream( Files.newInputStream(
Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties")) Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties"))
).use { fis -> ).use { fis ->
// Load the properties files
p.load(fis) p.load(fis)
} }
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
@ -625,7 +590,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
val channel = p.getProperty("channel") val channel = p.getProperty("channel")
val logsDir = ensureDir(p.getProperty("logs", "."), false) val logsDir = ensureDir(p.getProperty("logs", "."), false)
// Redirect the stdout and stderr // Redirect stdout and stderr
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) { if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) {
try { try {
val stdout = PrintStream( val stdout = PrintStream(
@ -680,15 +645,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
// Store the default logger level // Store the default logger level
loggerLevel = logger.level loggerLevel = logger.level
// Load the current entries and backlogs, if any
try {
startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, this.channel)
if (logger.isDebugEnabled) logger.debug("Last feed: $startDate")
} catch (e: Exception) {
logger.error("An error occurred while loading the logs.", e)
}
// Initialize the bot
setVerbose(true) setVerbose(true)
setAutoNickChange(true) setAutoNickChange(true)
login = p.getProperty("login", name) login = p.getProperty("login", name)
@ -704,6 +660,14 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
weblogUrl = p.getProperty("weblog", "") weblogUrl = p.getProperty("weblog", "")
backlogsUrl = ensureDir(p.getProperty("backlogs", weblogUrl), true) backlogsUrl = ensureDir(p.getProperty("backlogs", weblogUrl), true)
// Load the current entries and backlogs, if any
try {
LinksMgr.startup(logsDir + EntriesMgr.CURRENT_XML, logsDir + EntriesMgr.NAV_XML, this.channel)
if (logger.isDebugEnabled) logger.debug("Last feed: ${LinksMgr.startDate}")
} catch (e: Exception) {
logger.error("An error occurred while loading the logs.", e)
}
// Set the pinboard authentication // Set the pinboard authentication
setPinboardAuth(p.getProperty("pinboard-api-token", "")) setPinboardAuth(p.getProperty("pinboard-api-token", ""))
@ -756,6 +720,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
addons.sort() addons.sort()
// Save the entries // Save the entries
saveEntries(this, true) LinksMgr.saveEntries(this, true)
} }
} }

View file

@ -46,8 +46,7 @@ import java.time.LocalDateTime
/** /**
* The Tell Messages Manager. * The Tell Messages Manager.
*/ */
internal class TellMessagesMgr private constructor() { internal object TellMessagesMgr {
companion object {
/** /**
* Cleans the messages queue. * Cleans the messages queue.
*/ */
@ -95,4 +94,3 @@ internal class TellMessagesMgr private constructor() {
} }
} }
} }
}

View file

@ -52,10 +52,7 @@ import java.util.*
/** /**
* Manages the feed entries. * Manages the feed entries.
*/ */
class EntriesMgr private constructor() { object EntriesMgr {
companion object {
/** /**
* The name of the file containing the current entries. * The name of the file containing the current entries.
*/ */
@ -72,7 +69,7 @@ class EntriesMgr private constructor() {
const val XML_EXT = ".xml" const val XML_EXT = ".xml"
// Maximum number of backlogs to keep // Maximum number of backlogs to keep
private const val MAX_BACKLOGS = 10 private const val maxBacklogs = 10
/** /**
* Loads the backlogs. * Loads the backlogs.
@ -205,7 +202,7 @@ class EntriesMgr private constructor() {
if (bot.backlogsUrl.isNotBlank()) { if (bot.backlogsUrl.isNotBlank()) {
if (!history.contains(bot.today)) { if (!history.contains(bot.today)) {
history.add(bot.today) history.add(bot.today)
while (history.size > MAX_BACKLOGS) { while (history.size > maxBacklogs) {
history.removeAt(0) history.removeAt(0)
} }
} }
@ -252,4 +249,3 @@ class EntriesMgr private constructor() {
} }
} }
} }
}