diff options
author | href <href@random.sh> | 2021-01-11 15:53:02 +0100 |
---|---|---|
committer | href <href@random.sh> | 2021-01-11 15:53:02 +0100 |
commit | 0f3f0e035b43eabd3f739c41964446962cf54208 (patch) | |
tree | 9279c54e100c92375c9d980e2031e0a153245025 /lib/irc/user.ex | |
parent | Some fixes (diff) |
Cont. wipmaster
Diffstat (limited to 'lib/irc/user.ex')
-rw-r--r-- | lib/irc/user.ex | 74 |
1 files changed, 60 insertions, 14 deletions
diff --git a/lib/irc/user.ex b/lib/irc/user.ex index 5e2f526..06a6b97 100644 --- a/lib/irc/user.ex +++ b/lib/irc/user.ex @@ -1,12 +1,19 @@ defmodule Irc.User do + require Record @moduledoc """ Represents an IRC user. Not all field may be returned in events. + + Its main structure is the struct `t()`, but the record `irc_user()` can be used to write queries against an ets collection. """ - defstruct [ + @record :irc_user + + @attrs [ + :id, + :owner, :nick, :user, :host, @@ -20,27 +27,66 @@ defmodule Irc.User do {:modes, []}, {:chanmodes, %{}}, {:channels, []}, + {:assigns, %{}}, + {:private, %{persisted: false}}, ] - @type t :: %__MODULE__{ - nick: String.t(), - user: nil | String.t(), - host: nil | String.t(), - name: nil | String.t(), - account: nil | String.t(), - server: nil | String.t(), - idle: nil | {non_neg_integer(), NaiveDateTime.t()}, - operator: boolean(), - modes: [], - channels: [], - chanmodes: %{String.t() => String.t()} - } + defstruct @attrs + + record_attrs = Enum.map(@attrs, fn + ({_,_}=kv) -> kv + (k) -> {k, :undefined} + end) + keys = :lists.map(&elem(&1, 0), record_attrs) + vals = :lists.map(&{&1, [], nil}, keys) + pairs = :lists.zip(keys, vals) + + Record.defrecord(@record, record_attrs) + +# @type t :: %__MODULE__{ +# id: String.t(), +# owner: nil | pid(), +# nick: String.t(), +# user: nil | String.t(), +# host: nil | String.t(), +# name: nil | String.t(), +# account: nil | String.t(), +# server: nil | String.t(), +# idle: nil | {non_neg_integer(), NaiveDateTime.t()}, +# operator: boolean(), +# modes: [], +# channels: [], +# chanmodes: %{String.t() => String.t()}, +## assigns: %{any() => any()} +# } +# @type record :: record(:irc_user) + + @doc false + def __attrs__, do: @attrs + +# @spec to_record(t()) :: t() + def to_record(%__MODULE__{unquote_splicing(pairs)}) do + {@record, unquote_splicing(vals)} + end + +# @spec from_record(t()) :: t() + def from_record({@record, unquote_splicing(vals)}) do + %__MODULE__{unquote_splicing(pairs)} + end + +# @spec from_mask(IRC.Mask.t()) :: t() def from_mask(%Irc.Mask{user: user, nick: nick, host: host}) when is_binary(nick) do %__MODULE__{nick: nick, user: user, host: host} end +# @spec to_mask(t()) :: IRC.Mask.t() def to_mask(%__MODULE__{nick: nick, user: user, host: host}) do Irc.Mask.new(nick, user, host) end + defimpl Irc.Addressable, for: __MODULE__ do + def nick(%{nick: nick}), do: nick + def user(%{user: user}), do: user + def host(%{host: host}), do: host + end end |