summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/base_plugin.ex
blob: a95f45c4029b076a7dfd980fce15e1d075c03e69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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