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




                               
                                                          


                 




                                                                             


              









































































                                                                                                          
















                                                                                          
defmodule LSG.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, 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