diff options
author | Jordan Bracco <href@random.sh> | 2022-12-20 00:21:54 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-20 19:29:41 +0100 |
commit | 2d83df8b32bff7f0028923bb5b64dc0b55f20d03 (patch) | |
tree | 1207e67b5b15f540963db05e7be89f3ca950e724 /lib/nola_plugins/logger_plugin.ex | |
parent | Nola rename, the end. pt 6. Refs T77. (diff) |
Nola rename: The Big Move, Refs T77
Diffstat (limited to 'lib/nola_plugins/logger_plugin.ex')
-rw-r--r-- | lib/nola_plugins/logger_plugin.ex | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/nola_plugins/logger_plugin.ex b/lib/nola_plugins/logger_plugin.ex new file mode 100644 index 0000000..b13f33a --- /dev/null +++ b/lib/nola_plugins/logger_plugin.ex @@ -0,0 +1,70 @@ +defmodule Nola.IRC.LoggerPlugin do + require Logger + + @couch_db "bot-logs" + + def irc_doc(), do: nil + + def start_link() do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init([]) do + regopts = [plugin: __MODULE__] + {:ok, _} = Registry.register(IRC.PubSub, "triggers", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "messages", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "irc:outputs", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "messages:private", regopts) + {:ok, nil} + end + + def handle_info({:irc, :trigger, _, m}, state) do + {:noreply, log(m, state)} + end + + def handle_info({:irc, :text, m}, state) do + {:noreply, log(m, state)} + end + + def handle_info(info, state) do + Logger.debug("logger_plugin: unhandled info: #{info}") + {:noreply, state} + end + + def log(entry, state) do + case Couch.post(@couch_db, format_to_db(entry)) do + {:ok, id, _rev} -> + Logger.debug("logger_plugin: saved: #{inspect id}") + state + error -> + Logger.error("logger_plugin: save failed: #{inspect error}") + end + rescue + e -> + Logger.error("logger_plugin: rescued processing for #{inspect entry}: #{inspect e}") + Logger.error(Exception.format(:error, e, __STACKTRACE__)) + state + catch + e, b -> + Logger.error("logger_plugin: catched processing for #{inspect entry}: #{inspect e}") + Logger.error(Exception.format(e, b, __STACKTRACE__)) + state + end + + def format_to_db(msg = %IRC.Message{id: id}) do + msg + |> Poison.encode!() + |> Map.drop("id") + + %{"_id" => id || FlakeId.get(), + "type" => "irc.message/v1", + "object" => msg} + end + + def format_to_db(anything) do + %{"_id" => FlakeId.get(), + "type" => "object", + "object" => anything} + end + +end |