summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/broadcast_handler.ex
blob: 19c41ea9ae6d9d36bd3df842e84cd5f6ef5d8908 (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
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