summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/outline_plugin.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2020-03-11 21:18:34 +0100
committerhref <href@random.sh>2020-03-11 21:18:34 +0100
commita28d24470ddeca6196219a1333c1ccac1319efef (patch)
tree4f29e3c8fb6afbb1f99d6b8737f844c95fca54df /lib/lsg_irc/outline_plugin.ex
parentup to 420*100 (diff)
welp
Diffstat (limited to '')
-rw-r--r--lib/lsg_irc/outline_plugin.ex72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/lsg_irc/outline_plugin.ex b/lib/lsg_irc/outline_plugin.ex
new file mode 100644
index 0000000..7bfaac1
--- /dev/null
+++ b/lib/lsg_irc/outline_plugin.ex
@@ -0,0 +1,72 @@
+defmodule LSG.IRC.OutlinePlugin do
+ @moduledoc """
+ # outline auto-link
+
+ Envoie un lien vers Outline quand un lien est envoyé.
+
+ * **+outline `<host>`** active outline pour `<host>`.
+ * **-outline `<host>`** désactive outline pour `<host>`.
+ """
+ def short_irc_doc, do: false
+ def irc_doc, do: @moduledoc
+ require Logger
+
+ def start_link() do
+ GenServer.start_link(__MODULE__, [])
+ end
+
+ defstruct [:file, :hosts]
+
+ def init([]) do
+ {:ok, _} = Registry.register(IRC.PubSub, "trigger:outline", [])
+ {:ok, _} = Registry.register(IRC.PubSub, "message", [])
+ file = Path.join(LSG.data_path, "/outline.txt")
+ hosts = case File.read(file) do
+ {:error, :enoent} ->
+ []
+ {:ok, lines} ->
+ String.split(lines, "\n", trim: true)
+ end
+ {:ok, %__MODULE__{file: file, hosts: hosts}}
+ end
+
+ def handle_info({:irc, :trigger, "outline", message = %IRC.Message{trigger: %IRC.Trigger{type: :plus, args: [host]}}}, state) do
+ state = %{state | hosts: [host | state.hosts]}
+ save(state)
+ message.replyfun.("ok")
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :trigger, "outline", message = %IRC.Message{trigger: %IRC.Trigger{type: :minus, args: [host]}}}, state) do
+ state = %{state | hosts: List.delete(state.hosts, host)}
+ save(state)
+ message.replyfun.("ok")
+ {:noreply, state}
+ end
+
+ def handle_info({:irc, :text, message = %IRC.Message{text: text}}, state) do
+ String.split(text)
+ |> Enum.map(fn(word) ->
+ if String.starts_with?(word, "http://") || String.starts_with?(word, "https://") do
+ uri = URI.parse(word)
+ if uri.scheme && uri.host do
+ if Enum.any?(state.hosts, fn(host) -> String.ends_with?(uri.host, host) end) do
+ line = "-> https://outline.com/#{word}"
+ message.replyfun.(line)
+ end
+ end
+ end
+ end)
+ {:noreply, state}
+ end
+
+ def handle_info(msg, state) do
+ {:noreply, state}
+ end
+
+ def save(state = %{file: file, hosts: hosts}) do
+ string = Enum.join(hosts, "\n")
+ File.write(file, string)
+ end
+
+end