diff options
author | Jordan Bracco <href@random.sh> | 2022-12-20 02:13:47 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-20 19:29:41 +0100 |
commit | 70b9bba56f5319361ce5a7df5c489b9c0d6905ce (patch) | |
tree | f9b4438965f4c5e3e1f3a6129904cbb9a37047f2 /lib/nola_plugins/base.ex | |
parent | Update repo URL, refs T77. (diff) |
Rename to Nola
Summary:
Nola rename cont. pt. 2. Refs T77.
`find lib -name "*.ex" -type f | xargs sed -i '' 's/LSG/Nola/g'`
Nola rename, cont. pt. 3. Refs T77.
`s/:lsg/:nola/g`
Nola rename, cont. pt. 4. Refs T77.
Nola rename, cont. pt. 5. Refs T77. Configs.
find config -type f | xargs sed -i '' 's/LSG/Nola/g'
find config -type f | xargs sed -i '' 's/lsg/nola/g'
BREAKING CHANGE: Config keys switch from `:lsg` to `:nola`
Nola rename, the end. pt 6. Refs T77.
Nola rename: The Big Move, Refs T77
Update repo URL, refs T77.
Nola rename: Nola.Plugins, refs T77
Maniphest Tasks: T77
Differential Revision: https://phab.random.sh/D3
Diffstat (limited to 'lib/nola_plugins/base.ex')
-rw-r--r-- | lib/nola_plugins/base.ex | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/lib/nola_plugins/base.ex b/lib/nola_plugins/base.ex new file mode 100644 index 0000000..b7b5e16 --- /dev/null +++ b/lib/nola_plugins/base.ex @@ -0,0 +1,132 @@ +defmodule Nola.Plugins.Base 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 = Nola.Plugins.enabled() + |> Enum.map(fn(mod) -> + mod + |> Macro.underscore() + |> String.split("/", parts: :infinity) + |> List.last() + |> 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.Plugins, Macro.camelize(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 Nola.Plugins.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.Plugins, Macro.camelize(plugin)]) + with true <- Code.ensure_loaded?(module), + Nola.Plugins.switch(module, true), + {:ok, pid} <- Nola.Plugins.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.Plugins, Macro.camelize(plugin)]) + with true <- Code.ensure_loaded?(module), + pid when is_pid(pid) <- GenServer.whereis(module), + :ok <- GenServer.stop(pid), + {:ok, pid} <- Nola.Plugins.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.Plugins, Macro.camelize(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() + brand = Nola.brand(:name) + owner = "#{Nola.brand(:owner)} <#{Nola.brand(:owner_email)}>" + message.replyfun.([ + <<"🤖 I am a robot running", 2, "#{brand}, version #{ver}", 2, " — source: #{Nola.source_url()}">>, + "🦾 Elixir #{elixir_ver} #{otp_ver} on #{system}", + "👷♀️ Owner: h#{owner}", + "🌍 Web interface: #{url}" + ]) + {:noreply, nil} + end + + def handle_info(msg, _) do + {:noreply, nil} + end + +end |