summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/txt_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2021-09-01 23:03:33 +0200
committerhref <href@random.sh>2021-09-01 23:03:33 +0200
commit0f0d3c53eb635023edd0bad18f3a8bcfa0abc597 (patch)
tree42c2d3ebf304eee61731356796c05ed1be7556f9 /lib/lsg_irc/txt_plugin.ex
parentadmin.md (diff)
txt: random/1, allow dots in filenames
Diffstat (limited to 'lib/lsg_irc/txt_plugin.ex')
-rw-r--r--lib/lsg_irc/txt_plugin.ex36
1 files changed, 27 insertions, 9 deletions
diff --git a/lib/lsg_irc/txt_plugin.ex b/lib/lsg_irc/txt_plugin.ex
index 0f97bcc..4b3aaab 100644
--- a/lib/lsg_irc/txt_plugin.ex
+++ b/lib/lsg_irc/txt_plugin.ex
@@ -38,6 +38,20 @@ defmodule LSG.IRC.TxtPlugin do
defstruct triggers: %{}, rw: true, locks: nil, markov_handler: nil, markov: nil
+ def random(file) do
+ GenServer.call(__MODULE__, {:random, file})
+ end
+
+ def reply_random(message, file) do
+ if line = random(file) do
+ line
+ |> format_line(nil, message)
+ |> message.replyfun.()
+
+ line
+ end
+ end
+
def init([]) do
dets_locks_filename = (LSG.data_path() <> "/" <> "txtlocks.dets") |> String.to_charlist
{:ok, locks} = :dets.open_file(dets_locks_filename, [])
@@ -167,7 +181,7 @@ defmodule LSG.IRC.TxtPlugin do
if result do
{source, line} = result
- msg.replyfun.(["#{source}: ", line])
+ msg.replyfun.(["#{source}: " | line])
end
{:noreply, state}
end
@@ -180,7 +194,6 @@ defmodule LSG.IRC.TxtPlugin do
end
def with_stateful_results(key, initfun) do
- IO.puts("Stateful results key is #{inspect key}")
pid = case :global.whereis_name(key) do
:undefined ->
start_stateful_results(key, initfun.())
@@ -295,7 +308,6 @@ defmodule LSG.IRC.TxtPlugin do
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(msg, state.triggers, trigger, String.trim(Enum.join(opts, " ")))
if line do
msg.replyfun.(format_line(line, nil, msg))
@@ -348,10 +360,14 @@ defmodule LSG.IRC.TxtPlugin do
end
def handle_info(msg, state) do
- IO.puts "txt unhandled #{inspect msg}"
{:noreply, state}
end
+ def handle_call({:random, file}, _from, state) do
+ random = get_random(nil, state.triggers, file, [])
+ {:reply, random, state}
+ end
+
def terminate(_reason, state) do
if state.locks do
:dets.sync(state.locks)
@@ -365,7 +381,7 @@ defmodule LSG.IRC.TxtPlugin do
triggers = Path.wildcard(directory() <> "/*.txt")
|> Enum.reduce(%{}, fn(path, m) ->
file = Path.basename(path)
- [key, "txt"] = String.split(file, ".", parts: 2)
+ key = String.replace(file, ".txt", "")
data = directory() <> file
|> File.read!
|> String.split("\n")
@@ -445,9 +461,9 @@ defmodule LSG.IRC.TxtPlugin do
idx = Enum.count(triggers[trigger])+1
{:ok, idx}
else
- :error
+ {:error, :notxt}
end
- _ -> :error
+ _ -> {:error, :badarg}
end
end
@@ -460,12 +476,14 @@ 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(".")
{trigger, opts}
end
- defp format_line(line, prefix, msg) do
+ def format_line(line, prefix, msg) do
prefix = unless(prefix, do: "", else: prefix)
prefix <> line
|> String.split("\\\\")