summaryrefslogtreecommitdiff
path: root/lib/plugins/base.ex
blob: 7fb285be0a755353cea0aefdc66fcc8cf6477efe (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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(Nola.PubSub, "trigger:version", regopts)
    {:ok, _} = Registry.register(Nola.PubSub, "trigger:help", regopts)
    {:ok, _} = Registry.register(Nola.PubSub, "trigger:liquidrender", regopts)
    {:ok, _} = Registry.register(Nola.PubSub, "trigger:plugin", regopts)
    {:ok, _} = Registry.register(Nola.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 string_or_module ->
        case string_or_module do
          string when is_binary(string) ->
            string

          module when is_atom(module) ->
            module
            |> Macro.underscore()
            |> String.split("/", parts: :infinity)
            |> List.last()
        end
      end)
      |> Enum.sort()
      |> 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
      Nola.Plugins.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)}>"

    if message.channel do
      message.replyfun.([
        <<"🤖 ", 2, "#{brand}", 2, " v", 2, "#{ver}", 2,
          "! My owner is #{Nola.brand(:owner)} and help is at #{url}">>
      ])
    else
      message.replyfun.([
        <<"🤖 I am a robot running ", 2, "#{brand}", 2, " version ", 2, "#{ver}", 2,
          " — source: #{Nola.source_url()}">>,
        "Source code: #{Nola.source_url()}",
        "🦾 Elixir #{elixir_ver} #{otp_ver} on #{system}",
        "🙋🏻 Owner: #{owner}",
        "🌍 Web interface: #{url}"
      ])
    end

    {:noreply, nil}
  end

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