summaryrefslogtreecommitdiff
path: root/lib/nola_plugins/base_plugin.ex
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-20 00:21:54 +0000
committerJordan Bracco <href@random.sh>2022-12-20 19:29:41 +0100
commit2d83df8b32bff7f0028923bb5b64dc0b55f20d03 (patch)
tree1207e67b5b15f540963db05e7be89f3ca950e724 /lib/nola_plugins/base_plugin.ex
parentNola rename, the end. pt 6. Refs T77. (diff)
Nola rename: The Big Move, Refs T77
Diffstat (limited to 'lib/nola_plugins/base_plugin.ex')
-rw-r--r--lib/nola_plugins/base_plugin.ex131
1 files changed, 131 insertions, 0 deletions
diff --git a/lib/nola_plugins/base_plugin.ex b/lib/nola_plugins/base_plugin.ex
new file mode 100644
index 0000000..a2b9ffb
--- /dev/null
+++ b/lib/nola_plugins/base_plugin.ex
@@ -0,0 +1,131 @@
+defmodule Nola.IRC.BasePlugin do
+
+ def irc_doc, do: nil
+
+ def start_link() do
+ GenServer.start_link(__MODULE__, [], name: __MODULE__)
+ end
+
+ def init([]) do
+ regopts = [plugin: __MODULE__]
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:version", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:help", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:liquidrender", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:plugin", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:plugins", regopts)
+ {:ok, nil}
+ end
+
+ def handle_info({:irc, :trigger, "plugins", msg = %{trigger: %{type: :bang, args: []}}}, _) do
+ enabled_string = IRC.Plugin.enabled()
+ |> Enum.map(fn(mod) ->
+ mod
+ |> Macro.underscore()
+ |> String.split("/", parts: :infinity)
+ |> List.last()
+ |> String.replace("_plugin", "")
+ |> Enum.sort()
+ end)
+ |> Enum.join(", ")
+ msg.replyfun.("Enabled plugins: #{enabled_string}")
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "plugin", %{trigger: %{type: :query, args: [plugin]}} = m}, _) do
+ module = Module.concat([Nola.IRC, Macro.camelize(plugin<>"_plugin")])
+ with true <- Code.ensure_loaded?(module),
+ pid when is_pid(pid) <- GenServer.whereis(module)
+ do
+ m.replyfun.("loaded, active: #{inspect(pid)}")
+ else
+ false -> m.replyfun.("not loaded")
+ nil ->
+ msg = case IRC.Plugin.get(module) do
+ :disabled -> "disabled"
+ {_, false, _} -> "disabled"
+ _ -> "not active"
+ end
+ m.replyfun.(msg)
+ end
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "plugin", %{trigger: %{type: :plus, args: [plugin]}} = m}, _) do
+ module = Module.concat([Nola.IRC, Macro.camelize(plugin<>"_plugin")])
+ with true <- Code.ensure_loaded?(module),
+ IRC.Plugin.switch(module, true),
+ {:ok, pid} <- IRC.Plugin.start(module)
+ do
+ m.replyfun.("started: #{inspect(pid)}")
+ else
+ false -> m.replyfun.("not loaded")
+ :ignore -> m.replyfun.("disabled or throttled")
+ {:error, _} -> m.replyfun.("start error")
+ end
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "plugin", %{trigger: %{type: :tilde, args: [plugin]}} = m}, _) do
+ module = Module.concat([Nola.IRC, Macro.camelize(plugin<>"_plugin")])
+ with true <- Code.ensure_loaded?(module),
+ pid when is_pid(pid) <- GenServer.whereis(module),
+ :ok <- GenServer.stop(pid),
+ {:ok, pid} <- IRC.Plugin.start(module)
+ do
+ m.replyfun.("restarted: #{inspect(pid)}")
+ else
+ false -> m.replyfun.("not loaded")
+ nil -> m.replyfun.("not active")
+ end
+ {:noreply, nil}
+ end
+
+
+ def handle_info({:irc, :trigger, "plugin", %{trigger: %{type: :minus, args: [plugin]}} = m}, _) do
+ module = Module.concat([Nola.IRC, Macro.camelize(plugin<>"_plugin")])
+ with true <- Code.ensure_loaded?(module),
+ pid when is_pid(pid) <- GenServer.whereis(module),
+ :ok <- GenServer.stop(pid)
+ do
+ IRC.Plugin.switch(module, false)
+ m.replyfun.("stopped: #{inspect(pid)}")
+ else
+ false -> m.replyfun.("not loaded")
+ nil -> m.replyfun.("not active")
+ end
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "liquidrender", m = %{trigger: %{args: args}}}, _) do
+ template = Enum.join(args, " ")
+ m.replyfun.(Tmpl.render(template, m))
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "help", m = %{trigger: %{type: :bang}}}, _) do
+ url = NolaWeb.Router.Helpers.irc_url(NolaWeb.Endpoint, :index, m.network, NolaWeb.format_chan(m.channel))
+ m.replyfun.("-> #{url}")
+ {:noreply, nil}
+ end
+
+ def handle_info({:irc, :trigger, "version", message = %{trigger: %{type: :bang}}}, _) do
+ {:ok, vsn} = :application.get_key(:nola, :vsn)
+ ver = List.to_string(vsn)
+ url = NolaWeb.Router.Helpers.irc_url(NolaWeb.Endpoint, :index)
+ elixir_ver = Application.started_applications() |> List.keyfind(:elixir, 0) |> elem(2) |> to_string()
+ otp_ver = :erlang.system_info(:system_version) |> to_string() |> String.trim()
+ system = :erlang.system_info(:system_architecture) |> to_string()
+ message.replyfun.([
+ <<"🤖 I am a robot running", 2, "beautte, version #{ver}", 2, " — source: #{Nola.source_url()}">>,
+ "🦾 Elixir #{elixir_ver} #{otp_ver} on #{system}",
+ "👷‍♀️ Owner: href <href@random.sh>",
+ "🌍 Web interface: #{url}"
+ ])
+ {:noreply, nil}
+ end
+
+ def handle_info(msg, _) do
+ {:noreply, nil}
+ end
+
+end