summaryrefslogtreecommitdiff
path: root/lib/irc.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/irc.ex')
-rw-r--r--lib/irc.ex49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/irc.ex b/lib/irc.ex
new file mode 100644
index 0000000..dd1a5d2
--- /dev/null
+++ b/lib/irc.ex
@@ -0,0 +1,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) do
+ connection = IRC.Connection.get_network(network)
+ if connection && (force_puppet || IRC.PuppetConnection.whereis(account, connection)) do
+ IRC.PuppetConnection.start_and_send_message(account, connection, channel, text)
+ else
+ user = Nola.UserTrack.find_by_account(network, account)
+ nick = if(user, do: user.nick, else: account.name)
+ IRC.Connection.broadcast_message(network, channel, "<#{nick}> #{text}")
+ end
+ end
+
+ def admin?(%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
+
+ IRC.Connection.setup()
+
+ [
+ worker(Registry, [[keys: :duplicate, name: IRC.ConnectionPubSub]], id: :registr_irc_conn),
+ supervisor(IRC.Connection.Supervisor, [], [name: IRC.Connection.Supervisor]),
+ supervisor(IRC.PuppetConnection.Supervisor, [], [name: IRC.PuppetConnection.Supervisor]),
+ ]
+ end
+
+ # Start plugins first to let them get on connection events.
+ def after_start() do
+ Logger.info("Starting connections")
+ IRC.Connection.start_all()
+ end
+
+end