summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/np_handler.ex
blob: 5f724d41865bac135aaae14e9c2a104a17faac7c (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
defmodule LSG.IRC.NpHandler do
  @moduledoc """
  # np

  * **!np** chanson/émission actuellement sur le stream de 115ans.net
  """

  def short_irc_doc, do: "!np (en ce moment sur 115ans)"
  def irc_doc, do: @moduledoc
  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}
  end

  def handle_info({:received, "!np", _, chan}, client) do
    stats = LSG.IcecastAgent.get
    np = if stats.live do
      "en direct: #{format_genre(stats.genre)}#{stats.np}"
    else
      "np: #{stats.np}"
    end
    ExIRC.Client.msg(client, :privmsg, chan, np)
    {:noreply, client}
  end

  # Catch-all for messages you don't care about
  def handle_info(msg, client) do
    {:noreply, client}
  end

  defp format_genre(nil), do: ""
  defp format_genre(""), do: ""
  defp format_genre(text), do: "[#{text}] "

end