|
@@ -0,0 +1,84 @@
|
|
|
+package org.congressodeiradicali.karlmarx.coreplugins
|
|
|
+
|
|
|
+import com.bot4s.telegram.api.declarative.{CommandFilterMagnet, CommandImplicits}
|
|
|
+import com.bot4s.telegram.models.Message
|
|
|
+import org.congressodeiradicali.karlmarx.{Bot, Localizer, Plugin}
|
|
|
+
|
|
|
+import scala.concurrent.{ExecutionContext, Future}
|
|
|
+
|
|
|
+class PluginManagerPlugin(localizer: Localizer, bot: Bot) extends CorePlugin with CommandImplicits {
|
|
|
+
|
|
|
+ override val name = "Plugin Manager"
|
|
|
+ override val description = "Manages other plugins"
|
|
|
+
|
|
|
+ override val identifier = "pluginmanager"
|
|
|
+
|
|
|
+ var managedPlugins: Map[String, Plugin] = Map()
|
|
|
+ var pluginStatus: Map[String, Boolean] = Map()
|
|
|
+
|
|
|
+ def managePlugin(plugin: Plugin) = {
|
|
|
+ managedPlugins += (plugin.identifier -> plugin)
|
|
|
+ pluginStatus += (plugin.identifier -> false)
|
|
|
+ }
|
|
|
+
|
|
|
+ def enableByIdentifier(id: String) : Boolean = managedPlugins.get(id).fold {
|
|
|
+ false
|
|
|
+ } { plugin =>
|
|
|
+ bot.plugins :+= plugin
|
|
|
+ pluginStatus = pluginStatus.updated(plugin.identifier, true)
|
|
|
+ true
|
|
|
+ }
|
|
|
+
|
|
|
+ def disableByIdentifier(id: String) : Boolean = managedPlugins.get(id).fold { /*checks if the plugin is managed*/
|
|
|
+ false
|
|
|
+ } { plugin =>
|
|
|
+ bot.plugins = bot.plugins.filter { _.identifier != plugin.identifier }
|
|
|
+ pluginStatus = pluginStatus.updated(plugin.identifier, false)
|
|
|
+ true
|
|
|
+ }
|
|
|
+
|
|
|
+ override var handlers: Map[CommandFilterMagnet, Handler] = Map {
|
|
|
+ stringToCommandFilter("enable") -> { msg : Message =>
|
|
|
+ val response = msg.text.fold {
|
|
|
+ "Enter a plugin name"
|
|
|
+ } { text =>
|
|
|
+ if (enableByIdentifier(text)) {
|
|
|
+ "Enabled!"
|
|
|
+ } else {
|
|
|
+ "Plugin not found"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Future { Some(response) }(ExecutionContext.global)
|
|
|
+ }
|
|
|
+
|
|
|
+ stringToCommandFilter("disable") -> { msg : Message =>
|
|
|
+ val response = msg.text.fold {
|
|
|
+ "Enter a plugin name"
|
|
|
+ } { text =>
|
|
|
+ if (disableByIdentifier(text)) {
|
|
|
+ "Disabled!"
|
|
|
+ } else {
|
|
|
+ "Plugin not found"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Future { Some(response) }(ExecutionContext.global)
|
|
|
+ }
|
|
|
+
|
|
|
+ stringToCommandFilter("status") -> { _ =>
|
|
|
+ var response = ""
|
|
|
+
|
|
|
+ pluginStatus.foreach { pair =>
|
|
|
+ response += managedPlugins.get(pair._1).fold {
|
|
|
+ // ignore and do the rest
|
|
|
+ ""
|
|
|
+ } { plugin =>
|
|
|
+ s"${plugin.name}. Status: ${if (pair._2) "UP" else "DOWN"}"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Future { Some(response) }(ExecutionContext.global)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|