summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/logger_plugin.ex
blob: e5307bc9c736a8d0dd56a13b239de7f4beed7d6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
defmodule LSG.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, "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