defmodule LSG.IRC.BroadcastHandler do
def irc_doc, do: ""
def start_link(client) do
GenServer.start_link(__MODULE__, [client])
end
def init([client]) do
ExIRC.Client.add_handler client, self
{:ok, _} = Registry.register(LSG.BroadcastRegistry, "icecast", [])
{:ok, {client, ["#lsg"], false}}
end
def handle_info({:icecast, stats = %{live: true}}, s = {client, channels, true}) do
for c <- channels do
ExIRC.Client.msg(client, :privmsg, c, "en direct: "<>format_genre(stats.genre) <> stats.np)
end
{:noreply, s}
end
def handle_info({:icecast, stats = %{live: true}}, s = {client, channels, false}) do
for c <- channels do
ExIRC.Client.msg(client, :privmsg, c, "115ANS EN DIREK! " <> format_genre(stats.genre) <> stats.np)
end
{:noreply, {client, channels, true}}
end
def handle_info({:icecast, stats = %{live: false}}, s = {client, channels, true}) do
for c <- channels do
ExIRC.Client.msg(client, :privmsg, c, "Le direct c'est fini, retour aux Smashing Pumpkins...")
ExIRC.Client.msg(client, :privmsg, c, "np: " <> stats.np)
end
{:noreply, {client, channels, false}}
end
# Catch-all for messages you don't care about
def handle_info(msg, state) do
{:noreply, state}
end
defp format_genre(nil), do: ""
defp format_genre(""), do: ""
defp format_genre(text), do: "[#{text}] "
end