summaryrefslogtreecommitdiff
path: root/lib/irc.ex
blob: 2c7a4687e86b2b83abaa66089f3c316897610da3 (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
defmodule Nola.Irc do
  require Logger

  def env(), do: Nola.env(:irc, [])
  def env(key, default \\ nil), do: Keyword.get(env(), key, default)

  def send_message_as(account, network, channel, text, force_puppet \\ false, meta \\ []) do
    connection = Nola.Irc.Connection.get_network(network)
    if connection && (force_puppet || Nola.Irc.PuppetConnection.whereis(account, connection)) do
      Nola.Irc.PuppetConnection.start_and_send_message(account, connection, channel, text, meta)
    else
      user = Nola.UserTrack.find_by_account(network, account)
      nick = if(user, do: user.nick, else: account.name)
      Nola.Irc.Connection.broadcast_message(network, channel, "<#{nick}> #{text}", meta)
    end
  end

  def admin?(%Nola.Message{sender: sender}), do: admin?(sender)

  def admin?(%{nick: nick, user: user, host: host}) do
    for {n, u, h} <- Nola.Irc.env(:admins, []) do
      admin_part_match?(n, nick) && admin_part_match?(u, user) && admin_part_match?(h, host)
    end
    |> Enum.any?
  end

  defp admin_part_match?(:_, _), do: true
  defp admin_part_match?(a, a), do: true
  defp admin_part_match?(_, _), do: false

  def application_childs do
    import Supervisor.Spec

    Nola.Irc.Connection.setup()

    [
      worker(Registry, [[keys: :duplicate, name: Nola.Irc.ConnectionPubSub]], id: :registr_irc_conn),
      supervisor(Nola.Irc.Connection.Supervisor, [], [name: Nola.Irc.Connection.Supervisor]),
      supervisor(Nola.Irc.PuppetConnection.Supervisor, [], [name: Nola.Irc.PuppetConnection.Supervisor]),
    ]
  end

  # Start plugins first to let them get on connection events.
  def after_start() do
    Logger.info("Starting connections")
    Nola.Irc.Connection.start_all()
  end

end