diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lsg_irc/txt_plugin.ex | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/lsg_irc/txt_plugin.ex b/lib/lsg_irc/txt_plugin.ex index 4b3aaab..ae369d1 100644 --- a/lib/lsg_irc/txt_plugin.ex +++ b/lib/lsg_irc/txt_plugin.ex @@ -327,7 +327,10 @@ defmodule LSG.IRC.TxtPlugin do msg.replyfun.("#{msg.sender.nick}: ajouté à #{trigger}. (#{idx})") {:noreply, %__MODULE__{state | triggers: load()}} else - _ -> + {:error, {:jaro, string, idx}} -> + msg.replyfun.("#{msg.sender.nick}: doublon #{trigger}##{idx}: #{string}") + error -> + Logger.debug("txt add failed: #{inspect error}") {:noreply, state} end end @@ -456,10 +459,19 @@ defmodule LSG.IRC.TxtPlugin do case String.split(trigger_and_content, " ", parts: 2) do [trigger, content] -> {trigger, _} = clean_trigger(trigger) + + if Map.has_key?(triggers, trigger) do - File.write!(directory() <> "/" <> trigger <> ".txt", content<>"\n", [:append]) - idx = Enum.count(triggers[trigger])+1 - {:ok, idx} + jaro = Enum.find(triggers[trigger], fn({string, idx}) -> String.jaro_distance(content, string) > 0.9 end) + + if jaro do + {string, idx} = jaro + {:error, {:jaro, string, idx}} + else + File.write!(directory() <> "/" <> trigger <> ".txt", content<>"\n", [:append]) + idx = Enum.count(triggers[trigger])+1 + {:ok, idx} + end else {:error, :notxt} end @@ -476,9 +488,9 @@ defmodule LSG.IRC.TxtPlugin do trigger = trigger |> String.downcase |> :unicode.characters_to_nfd_binary() + |> String.replace(~r/[^a-z0-9._]/, "") |> String.trim(".") - |> String.replace(~r/[^a-z0-9.]/, "") - |> String.trim(".") + |> String.trim("_") {trigger, opts} end |