summaryrefslogtreecommitdiff
path: root/lib/nola_plugins/correction_plugin.ex
blob: 5f9b278425e52485688a0f3c72be0a390e2cc0c3 (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
defmodule Nola.IRC.CorrectionPlugin do
  @moduledoc """
  # correction

  * `s/pattern/replace` replace `pattern` by `replace` in the last matching message
  """

  def irc_doc, do: @moduledoc
  def start_link() do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(_) do
    {:ok, _} = Registry.register(IRC.PubSub, "messages", [plugin: __MODULE__])
    {:ok, _} = Registry.register(IRC.PubSub, "triggers", [plugin: __MODULE__])
    {:ok, %{}}
  end

  # Trigger fallback
  def handle_info({:irc, :trigger, _, m = %IRC.Message{}}, state) do
    {:noreply, correction(m, state)}
  end

  def handle_info({:irc, :text, m = %IRC.Message{}}, state) do
    {:noreply, correction(m, state)}
  end

  def correction(m, state) do
    history = Map.get(state, key(m), [])
    if String.starts_with?(m.text, "s/") do
      case String.split(m.text, "/") do
        ["s", match, replace | _] ->
          case Regex.compile(match) do
            {:ok, reg} ->
              repl = Enum.find(history, fn(m) -> Regex.match?(reg, m.text) end)
              if repl do
                new_text = String.replace(repl.text, reg, replace)
                m.replyfun.("correction: <#{repl.sender.nick}> #{new_text}")
              end
            _ ->
              m.replyfun.("correction: invalid regex")
          end
        _ -> m.replyfun.("correction: invalid regex format")
      end
      state
    else
      history = if length(history) > 100 do
        {_, history} = List.pop_at(history, 99)
        [m | history]
      else
        [m | history]
      end
      Map.put(state, key(m), history)
    end
  end

  defp key(%{network: net, channel: chan}), do: "#{net}/#{chan}"

end