diff options
author | href <href@random.sh> | 2018-01-31 20:36:42 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-01-31 20:36:42 +0100 |
commit | fcb2a082346054108f3f16cffe968450cf961976 (patch) | |
tree | 8e7d2eb9eeebf854e62e08c29b147f34dfeb1881 /lib/lsg_irc |
import
Diffstat (limited to 'lib/lsg_irc')
-rw-r--r-- | lib/lsg_irc/broadcast_handler.ex | 43 | ||||
-rw-r--r-- | lib/lsg_irc/connection_handler.ex | 36 | ||||
-rw-r--r-- | lib/lsg_irc/login_handler.ex | 25 | ||||
-rw-r--r-- | lib/lsg_irc/np_handler.ex | 32 |
4 files changed, 136 insertions, 0 deletions
diff --git a/lib/lsg_irc/broadcast_handler.ex b/lib/lsg_irc/broadcast_handler.ex new file mode 100644 index 0000000..22ffbaf --- /dev/null +++ b/lib/lsg_irc/broadcast_handler.ex @@ -0,0 +1,43 @@ +defmodule LSG.IRC.BroadcastHandler 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 diff --git a/lib/lsg_irc/connection_handler.ex b/lib/lsg_irc/connection_handler.ex new file mode 100644 index 0000000..f3bb1d4 --- /dev/null +++ b/lib/lsg_irc/connection_handler.ex @@ -0,0 +1,36 @@ +defmodule LSG.IRC.ConnectionHandler do + defmodule State do + defstruct host: "irc.quakenet.org", + port: 6667, + pass: "", + nick: "bot115ans", + user: "115ans", + name: "115ans.net", + client: nil + end + + def start_link(client) do + GenServer.start_link(__MODULE__, [%State{client: client}]) + end + + def init([state]) do + ExIRC.Client.add_handler state.client, self + ExIRC.Client.connect! state.client, state.host, state.port + {:ok, state} + end + + def handle_info({:connected, server, port}, state) do + debug "Connected to #{server}:#{port}" + ExIRC.Client.logon state.client, state.pass, state.nick, state.user, state.name + {:noreply, state} + end + + # Catch-all for messages you don't care about + def handle_info(msg, state) do + {:noreply, state} + end + + defp debug(msg) do + IO.puts IO.ANSI.yellow() <> msg <> IO.ANSI.reset() + end +end diff --git a/lib/lsg_irc/login_handler.ex b/lib/lsg_irc/login_handler.ex new file mode 100644 index 0000000..b4757b1 --- /dev/null +++ b/lib/lsg_irc/login_handler.ex @@ -0,0 +1,25 @@ +defmodule LSG.IRC.LoginHandler do + def start_link(client) do + GenServer.start_link(__MODULE__, [client]) + end + + def init([client]) do + ExIRC.Client.add_handler client, self + {:ok, {client, ["#lsg"]}} + end + + def handle_info(:logged_in, state = {client, channels}) do + debug "Logged in to server" + channels |> Enum.map(&ExIRC.Client.join client, &1) + {:noreply, state} + end + + # Catch-all for messages you don't care about + def handle_info(_msg, state) do + {:noreply, state} + end + + defp debug(msg) do + IO.puts IO.ANSI.yellow() <> msg <> IO.ANSI.reset() + end +end diff --git a/lib/lsg_irc/np_handler.ex b/lib/lsg_irc/np_handler.ex new file mode 100644 index 0000000..8bde293 --- /dev/null +++ b/lib/lsg_irc/np_handler.ex @@ -0,0 +1,32 @@ +defmodule LSG.IRC.NpHandler 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} + 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 |