Added addons.exec() and addons.match() functions.
Converted TellMessagesMgr and EntriesMgr to objects.
This commit is contained in:
parent
b24c0ebe54
commit
f9edff4813
4 changed files with 261 additions and 270 deletions
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -58,9 +58,6 @@ import net.thauvin.erik.mobibot.commands.Users
|
|||
import net.thauvin.erik.mobibot.commands.Versions
|
||||
import net.thauvin.erik.mobibot.commands.links.Comment
|
||||
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.Tags
|
||||
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
|
||||
helpResponse(sender, args, false)
|
||||
} else {
|
||||
// Commands
|
||||
for (command in addons.commands) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
// Execute module or command
|
||||
addons.exec(sender, login, cmd, args, isOp(sender), false)
|
||||
}
|
||||
} else {
|
||||
// Commands, e.g.: https://www.example.com/
|
||||
for (command in addons.commands) {
|
||||
if (command.matches(message)) {
|
||||
command.commandResponse(sender, login, message, isOp(sender), false)
|
||||
return
|
||||
}
|
||||
}
|
||||
// Links, e.g.: https://www.example.com/ or L1: , etc.
|
||||
addons.match(sender, login, message, isOp(sender), 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!")
|
||||
exitProcess(0)
|
||||
} else {
|
||||
for (command in addons.commands) {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!addons.exec(sender, login, cmd, args, isOp, true)) {
|
||||
helpDefault(sender, isOp, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAction(sender: String, login: String, hostname: String, target: String, action: String) {
|
||||
if (channel == target) {
|
||||
|
@ -600,16 +564,17 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
HelpFormatter().printHelp(Mobibot::class.java.name, options)
|
||||
}
|
||||
commandLine.hasOption(Constants.VERSION_ARG[0]) -> {
|
||||
// Output the version
|
||||
println("${ReleaseInfo.PROJECT} ${ReleaseInfo.VERSION} (${isoLocalDate(ReleaseInfo.BUILDDATE)})")
|
||||
println(ReleaseInfo.WEBSITE)
|
||||
}
|
||||
else -> {
|
||||
// Load the properties
|
||||
val p = Properties()
|
||||
try {
|
||||
Files.newInputStream(
|
||||
Paths.get(commandLine.getOptionValue(Constants.PROPS_ARG[0], "./mobibot.properties"))
|
||||
).use { fis ->
|
||||
// Load the properties files
|
||||
p.load(fis)
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
|
@ -625,7 +590,7 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
val channel = p.getProperty("channel")
|
||||
val logsDir = ensureDir(p.getProperty("logs", "."), false)
|
||||
|
||||
// Redirect the stdout and stderr
|
||||
// Redirect stdout and stderr
|
||||
if (!commandLine.hasOption(Constants.DEBUG_ARG[0])) {
|
||||
try {
|
||||
val stdout = PrintStream(
|
||||
|
@ -680,15 +645,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
// Store the default 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)
|
||||
setAutoNickChange(true)
|
||||
login = p.getProperty("login", name)
|
||||
|
@ -704,6 +660,14 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
weblogUrl = p.getProperty("weblog", "")
|
||||
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
|
||||
setPinboardAuth(p.getProperty("pinboard-api-token", ""))
|
||||
|
||||
|
@ -756,6 +720,6 @@ class Mobibot(nickname: String, channel: String, logsDirPath: String, p: Propert
|
|||
addons.sort()
|
||||
|
||||
// Save the entries
|
||||
saveEntries(this, true)
|
||||
LinksMgr.saveEntries(this, true)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,8 +46,7 @@ import java.time.LocalDateTime
|
|||
/**
|
||||
* The Tell Messages Manager.
|
||||
*/
|
||||
internal class TellMessagesMgr private constructor() {
|
||||
companion object {
|
||||
internal object TellMessagesMgr {
|
||||
/**
|
||||
* Cleans the messages queue.
|
||||
*/
|
||||
|
@ -95,4 +94,3 @@ internal class TellMessagesMgr private constructor() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,10 +52,7 @@ import java.util.*
|
|||
/**
|
||||
* Manages the feed entries.
|
||||
*/
|
||||
class EntriesMgr private constructor() {
|
||||
|
||||
|
||||
companion object {
|
||||
object EntriesMgr {
|
||||
/**
|
||||
* The name of the file containing the current entries.
|
||||
*/
|
||||
|
@ -72,7 +69,7 @@ class EntriesMgr private constructor() {
|
|||
const val XML_EXT = ".xml"
|
||||
|
||||
// Maximum number of backlogs to keep
|
||||
private const val MAX_BACKLOGS = 10
|
||||
private const val maxBacklogs = 10
|
||||
|
||||
/**
|
||||
* Loads the backlogs.
|
||||
|
@ -205,7 +202,7 @@ class EntriesMgr private constructor() {
|
|||
if (bot.backlogsUrl.isNotBlank()) {
|
||||
if (!history.contains(bot.today)) {
|
||||
history.add(bot.today)
|
||||
while (history.size > MAX_BACKLOGS) {
|
||||
while (history.size > maxBacklogs) {
|
||||
history.removeAt(0)
|
||||
}
|
||||
}
|
||||
|
@ -252,4 +249,3 @@ class EntriesMgr private constructor() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue