diff options
Diffstat (limited to 'lib/lsg_irc/base_plugin.ex')
-rw-r--r-- | lib/lsg_irc/base_plugin.ex | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/lib/lsg_irc/base_plugin.ex b/lib/lsg_irc/base_plugin.ex index c0cfc59..69b02e8 100644 --- a/lib/lsg_irc/base_plugin.ex +++ b/lib/lsg_irc/base_plugin.ex @@ -9,12 +9,85 @@ defmodule LSG.IRC.BasePlugin do def init([]) do {:ok, _} = Registry.register(IRC.PubSub, "trigger:version", []) {:ok, _} = Registry.register(IRC.PubSub, "trigger:help", []) + {:ok, _} = Registry.register(IRC.PubSub, "trigger:liquidrender", []) + {:ok, _} = Registry.register(IRC.PubSub, "trigger:plugin", []) {:ok, nil} end - def handle_info({:irc, :trigger, "help", message = %{trigger: %{type: :bang}}}, _) do - url = LSGWeb.Router.Helpers.irc_url(LSGWeb.Endpoint, :index) - message.replyfun.(url) + def handle_info({:irc, :trigger, "plugin", %{trigger: %{type: :query, args: [plugin]}} = m}, _) do + module = Module.concat([LSG.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([LSG.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([LSG.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([LSG.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 = LSGWeb.Router.Helpers.irc_url(LSGWeb.Endpoint, :index, m.network, LSGWeb.format_chan(m.channel)) + m.replyfun.("-> #{url}") {:noreply, nil} end |