summaryrefslogblamecommitdiff
path: root/lib/lsg_irc/base_plugin.ex
blob: 69b02e88dd05c38ef1458d730db00a79cf97128a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                   

                                                                        


              









































































                                                                                                          

















                                                                                          
defmodule LSG.IRC.BasePlugin do

  def irc_doc, do: nil

  def start_link() do
    GenServer.start_link(__MODULE__, [])
  end

  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, "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

  def handle_info({:irc, :trigger, "version", message = %{trigger: %{type: :bang}}}, _) do
    {:ok, vsn} = :application.get_key(:lsg, :vsn)
    ver = List.to_string(vsn)
    url = LSGWeb.Router.Helpers.irc_url(LSGWeb.Endpoint, :index)
    version = "v#{ver} ; #{url} ; source: https://git.yt/115ans/sys"
    message.replyfun.(version)
    {:noreply, nil}
  end

  def handle_info(msg, _) do
    {:noreply, nil}
  end

end