diff options
Diffstat (limited to 'lib/irc/parser')
-rw-r--r-- | lib/irc/parser/capabs.ex | 32 | ||||
-rw-r--r-- | lib/irc/parser/line.ex | 20 |
2 files changed, 48 insertions, 4 deletions
diff --git a/lib/irc/parser/capabs.ex b/lib/irc/parser/capabs.ex new file mode 100644 index 0000000..88bf017 --- /dev/null +++ b/lib/irc/parser/capabs.ex @@ -0,0 +1,32 @@ +defmodule Irc.Parser.Capabs do + @moduledoc "Helper to parse capability lists" + + @spec parse(String.t) :: %{capab :: String.t => true | args :: Map.t} + def parse(string) do + string + |> String.split(" ") + |> Enum.map(&parse_capab/1) + |> Enum.into(Map.new) + end + + defp parse_capab(string) do + case String.split(string, "=", parts: 2) do + [capab] -> + {capab, true} + [capab, args] -> + parse_capab_with_args(capab, args) + end + end + + defp parse_capab_with_args(capab, args) do + args = args + |> String.split(",") + |> Map.reduce(%{}, fn(k_v, acc) -> + [k, v] = String.split(k_v, "=") + Map.put(acc, k, v) + end) + {capab, args} + end + +end + diff --git a/lib/irc/parser/line.ex b/lib/irc/parser/line.ex index 17c381c..5aa5a9b 100644 --- a/lib/irc/parser/line.ex +++ b/lib/irc/parser/line.ex @@ -37,13 +37,25 @@ defmodule Irc.Parser.Line do |> Enum.join(" ") end - def new(command, args \\ [], tags \\ %{}) do + def new(command) do + new(command, []) + end + + def new(command, args) do + new(command, args, %{}) + end + + def new(command, arg, tags) when not is_list(arg) do + new(command, [arg], tags) + end + + def new(command, args, tags) do %__MODULE__{command: command, args: args, tags: tags, __private__: %{at: DateTime.utc_now()}} end @doc "Returns the line date (server time if sent, otherwise, parse time)" @spec at(t()) :: DateTime.t() - def at(%__MODULE__{__private__: %{at: at}, tags: %{"time" => server_time}}) do + def at(line = %__MODULE__{__private__: %{at: at}, tags: %{"time" => server_time}}) do case DateTime.from_iso8601(server_time) do {:ok, date} -> date _ -> at @@ -54,14 +66,14 @@ defmodule Irc.Parser.Line do @spec to?(t(), Irc.Connection.t() | Irc.Mask.t() | Irc.User.t()) :: boolean @doc "Returns true if the line is adressed to the connection." - def to?(%__MODULE__{args: [nick | _]}, %{__struct__: s, nick: nick}) when s in [Irc.Connection, Irc.Mask, Irc.User] do + def to?(line = %__MODULE__{args: [nick | _]}, target = %{__struct__: s, nick: nick}) when s in [Irc.Connection, Irc.Mask, Irc.User] do true end def to?(%__MODULE__{}, %{__struct__: s}) when s in [Irc.Connection, Irc.Mask, Irc.User], do: false @spec self?(t(), Irc.Connection.t() | Irc.Mask.t() | Irc.User.t()) :: boolean @doc "Returns true if the line source is the from the given connection/mask." - def self?(%__MODULE__{source: %Irc.Mask{nick: nick}}, %Irc.Connection{nick: nick}) do + def self?(line = %__MODULE__{source: %Irc.Mask{nick: nick}}, target = %Irc.Connection{nick: nick}) do true end def self?(%__MODULE__{source: mask = %Irc.Mask{}}, mask = %Irc.Mask{}) do |