summaryrefslogtreecommitdiff
path: root/lib/irc/line.ex
blob: 56836a8d84d7a5f61dfa2ce95435401941c51850 (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 Irc.Line do
  @type t :: %__MODULE__{
    tags: Map.t(),
    source: Irc.Mask.t() | String.t() | nil,
    command: String.t(),
    args: list()
  }
  defstruct __private__: %{}, tags: %{}, source: nil, command: nil, args: []

  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
    case DateTime.from_iso8601(server_time) do
      {:ok, date} -> date
      _ -> at
    end
  end
  def at(%__MODULE__{__private__: %{at: at}}), do: at
  def at(_), do: nil

  @spec to?(t(), Irc.Addressable.t()) :: boolean
  @doc "Returns true if the line is adressed to the connection."
  def to?(%__MODULE__{args: [nick | _]}, addressable) do
    Irc.Addressable.nick(addressable) == nick
  end

  @spec self?(t(), Irc.Addressable.t()) :: boolean
  @doc "Returns true if the line source is the from the given connection/mask."
  def self?(%__MODULE__{source: nick}, addressable) when is_binary(nick) do
    Irc.Addressable.nick(addressable) == nick
  end
  def self?(%__MODULE__{source: source_addressable}, addressable) do
    Irc.Addressable.equal?(source_addressable, addressable)
  end

  defimpl String.Chars, for: __MODULE__ do
    def to_string(line) do
      Irc.Parser.Line.encode(line)
    end
  end

  defimpl Inspect, for: __MODULE__ do
    @moduledoc false
    import Inspect.Algebra

    def inspect(struct, _opts) do
      tags = Enum.map(struct.tags, fn({k, v}) -> concat([k, "=", v]) end) |> Enum.join(",")
      Enum.join(["#IRC.Line<", struct.source, " ", struct.command, " ", inspect(struct.args), " (", tags, ")>"], "")
    end
  end


end