summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/txt_plugin.ex
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/lsg_irc/txt_plugin.ex39
1 files changed, 22 insertions, 17 deletions
diff --git a/lib/lsg_irc/txt_plugin.ex b/lib/lsg_irc/txt_plugin.ex
index 6b7edbd..ca1be9c 100644
--- a/lib/lsg_irc/txt_plugin.ex
+++ b/lib/lsg_irc/txt_plugin.ex
@@ -132,6 +132,7 @@ defmodule LSG.IRC.TxtPlugin do
def handle_info({:irc, :trigger, "txt", msg = %{trigger: %{type: :bang, args: []}}}, state) do
result = Enum.reduce(state.triggers, [], fn({trigger, data}, acc) ->
+ IO.puts inspect(data)
Enum.reduce(data, acc, fn({l, _}, acc) ->
[{trigger, l} | acc]
end)
@@ -140,7 +141,7 @@ defmodule LSG.IRC.TxtPlugin do
if !Enum.empty?(result) do
{source, line} = Enum.random(result)
- msg.replyfun.("#{source}: #{line}")
+ msg.replyfun.(format_line(line, "#{source}: "))
end
{:noreply, state}
end
@@ -157,7 +158,7 @@ defmodule LSG.IRC.TxtPlugin do
if !Enum.empty?(result) do
{source, line} = Enum.random(result)
- msg.replyfun.("#{source}: #{line}")
+ msg.replyfun.(["#{source}: ", line])
end
{:noreply, state}
end
@@ -208,11 +209,12 @@ defmodule LSG.IRC.TxtPlugin do
# TXT: RANDOM
#
- def handle_info({:irc, :trigger, trigger, msg = %{trigger: %{type: :bang, args: []}}}, state) do
- {trigger, opts} = clean_trigger(trigger)
- line = get_random(state.triggers, trigger, opts)
+ def handle_info({:irc, :trigger, trigger, msg = %{trigger: %{type: :bang, args: opts}}}, state) do
+ {trigger, _} = clean_trigger(trigger)
+ IO.puts "OPTS : #{inspect {trigger, opts}}"
+ line = get_random(state.triggers, trigger, String.trim(Enum.join(opts, " ")))
if line do
- msg.replyfun.(line)
+ msg.replyfun.(format_line(line))
end
{:noreply, state}
end
@@ -317,7 +319,7 @@ defmodule LSG.IRC.TxtPlugin do
end
end
- defp get_random(triggers, trigger, [opt]) do
+ defp get_random(triggers, trigger, opt) do
arg = case Integer.parse(opt) do
{pos, ""} -> {:index, pos}
{_pos, _some_string} -> {:grep, opt}
@@ -372,21 +374,24 @@ defmodule LSG.IRC.TxtPlugin do
trigger = trigger
|> String.downcase
- |> String.replace("à", "a")
- |> String.replace("ä", "a")
- |> String.replace("â", "a")
- |> String.replace("é", "e")
- |> String.replace("è", "e")
- |> String.replace("ê", "e")
- |> String.replace("ë", "e")
- |> String.replace("ç", "c")
- |> String.replace("ï", "i")
- |> String.replace("î", "i")
+ |> :unicode.characters_to_nfd_binary()
|> String.replace(~r/[^a-z0-9]/, "")
{trigger, opts}
end
+ defp format_line(line, prefix \\ "") do
+ prefix <> line
+ |> String.split("\\\\")
+ |> Enum.map(fn(line) ->
+ String.split(line, "\\\\\\\\")
+ end)
+ |> List.flatten()
+ |> Enum.map(fn(line) ->
+ String.trim(line)
+ end)
+ end
+
def directory() do
Application.get_env(:lsg, :data_path) <> "/irc.txt/"
end