diff options
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_irc/buffer_plugin.ex | 42 | ||||
-rw-r--r-- | mix.exs | 1 | ||||
-rw-r--r-- | mix.lock | 1 |
3 files changed, 44 insertions, 0 deletions
diff --git a/lib/lsg_irc/buffer_plugin.ex b/lib/lsg_irc/buffer_plugin.ex new file mode 100644 index 0000000..8d3cacd --- /dev/null +++ b/lib/lsg_irc/buffer_plugin.ex @@ -0,0 +1,42 @@ +defmodule LSG.IRC.BufferPlugin do + @table __MODULE__.ETS + def irc_doc, do: nil + + def table(), do: @table + + def select_buffer(network, channel, limit \\ 50) do + import Ex2ms + spec = fun do {{n, c, _}, m} when n == ^network and (c == ^channel or is_nil(c)) -> m end + :ets.select(@table, spec, limit) + end + + def start_link() do + GenServer.start_link(__MODULE__, [], name: __MODULE__) + end + + def init(_) do + for e <- ~w(messages triggers events outputs) do + {:ok, _} = Registry.register(IRC.PubSub, e, plugin: __MODULE__) + end + {:ok, :ets.new(@table, [:named_table, :ordered_set, :protected])} + end + + def handle_info({:irc, :trigger, _, message}, ets), do: handle_message(message, ets) + def handle_info({:irc, :text, message}, ets), do: handle_message(message, ets) + def handle_info({:irc, :event, event}, ets), do: handle_message(event, ets) + + defp handle_message(message = %{network: network}, ets) do + key = {network, Map.get(message, :channel), ts(message.at)} + :ets.insert(ets, {key, message}) + {:noreply, ets} + end + + defp ts(naive = %NaiveDateTime{}) do + ts = naive + |> DateTime.from_naive!("Etc/UTC") + |> DateTime.to_unix() + + -ts + end + +end @@ -69,6 +69,7 @@ defmodule LSG.Mixfile do {:liquex, "~> 0.3"}, {:html_entities, "0.4.0", override: true}, {:file_size, "~> 3.0"}, + {:ex2ms, "~> 1.0"}, ] end end @@ -16,6 +16,7 @@ "ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"}, "elixir_make": {:hex, :elixir_make, "0.6.2", "7dffacd77dec4c37b39af867cedaabb0b59f6a871f89722c25b28fcd4bd70530", [:mix], [], "hexpm", "03e49eadda22526a7e5279d53321d1cced6552f344ba4e03e619063de75348d9"}, "entropy_string": {:hex, :entropy_string, "1.0.7", "61a5a989e78fd2798e35a17a98a17f81fb504e8d4ba620bcd4f19063eb782943", [:mix], [], "hexpm", "c497fc9cf6bae2075c4c985e66b4306baa4cb19f142d97e0aa1d7a993ae3bb47"}, + "ex2ms": {:hex, :ex2ms, "1.6.1", "66d472eb14da43087c156e0396bac3cc7176b4f24590a251db53f84e9a0f5f72", [:mix], [], "hexpm", "a7192899d84af03823a8ec2f306fa858cbcce2c2e7fd0f1c49e05168fb9c740e"}, "ex_aws": {:hex, :ex_aws, "2.2.4", "b6b9a73468205c67851f6c195429b435741ec3d5f45be4cfdaa8f54a62491f15", [:mix], [{:configparser_ex, "~> 4.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "cc9af9199d869c0532819f87678a547c7dd7c57088d932b43dcbdbc315886601"}, "ex_aws_s3": {:hex, :ex_aws_s3, "2.3.0", "5dfe50116bad048240bae7cd9418bfe23296542ff72a01b9138113a1cd31451c", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "0b13b11478825d62d2f6e57ae763695331be06f2216468f31bb304316758b096"}, "ex_chain": {:git, "https://github.com/eljojo/ex_chain.git", "09d88a10613b6acc33340c9fa1b3540493e431b8", []}, |