summaryrefslogtreecommitdiff
path: root/lib/lsg_irc
diff options
context:
space:
mode:
authorhref <href@random.sh>2021-09-01 23:29:12 +0200
committerhref <href@random.sh>2021-09-01 23:29:12 +0200
commit65da31d17c7b9fa1eb9738b49563da2c26dc5cc2 (patch)
tree771a0c48ee1431b0015b5efe5d0af9c7a1802821 /lib/lsg_irc
parentconnection: fix crash with incomplete track_change (diff)
txt: allow _ in file, try jaro distance
Diffstat (limited to 'lib/lsg_irc')
-rw-r--r--lib/lsg_irc/txt_plugin.ex24
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