diff options
Diffstat (limited to 'lib/lsg_irc/correction_plugin.ex')
-rw-r--r-- | lib/lsg_irc/correction_plugin.ex | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/lsg_irc/correction_plugin.ex b/lib/lsg_irc/correction_plugin.ex index a5d3d31..e7b2577 100644 --- a/lib/lsg_irc/correction_plugin.ex +++ b/lib/lsg_irc/correction_plugin.ex @@ -12,10 +12,21 @@ defmodule LSG.IRC.CorrectionPlugin do def init(_) do {:ok, _} = Registry.register(IRC.PubSub, "message", []) - {:ok, []} + {:ok, _} = Registry.register(IRC.PubSub, "triggers", []) + {:ok, %{}} end - def handle_info({:irc, :text, m = %IRC.Message{}}, history) do + # 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 + + defp 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 | _] -> @@ -31,7 +42,7 @@ defmodule LSG.IRC.CorrectionPlugin do end _ -> m.replyfun.("correction: invalid regex format") end - {:noreply, history} + state else history = if length(history) > 100 do {_, history} = List.pop_at(history, 99) @@ -39,8 +50,10 @@ defmodule LSG.IRC.CorrectionPlugin do else [m | history] end - {:noreply, history} + Map.put(state, key(m), history) end end + defp key(%{network: net, channel: chan}), do: "#{net}/#{chan}" + end |