summaryrefslogtreecommitdiff
path: root/lib/irc/user_track_handler.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/irc/user_track_handler.ex')
-rw-r--r--lib/irc/user_track_handler.ex93
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/irc/user_track_handler.ex b/lib/irc/user_track_handler.ex
new file mode 100644
index 0000000..0ae802a
--- /dev/null
+++ b/lib/irc/user_track_handler.ex
@@ -0,0 +1,93 @@
+defmodule IRC.UserTrackHandler do
+ @moduledoc """
+ # User Track Handler
+
+ This handlers keeps track of users presence and privileges.
+
+ Planned API:
+
+ UserTrackHandler.operator?(%ExIRC.Sender{nick: "href", …}, "#channel") :: boolean
+
+ """
+
+ def irc_doc, do: nil
+
+ def start_link(client) do
+ GenServer.start_link(__MODULE__, [client])
+ end
+
+ defstruct client: nil, ets: nil
+
+ def init([client]) do
+ ExIRC.Client.add_handler client, self
+ {:ok, %__MODULE__{client: client}}
+ end
+
+ def handle_info({:joined, channel}, state) do
+ ExIRC.Client.who(state.client, channel)
+ {:noreply, state}
+ end
+
+ def handle_info({:who, channel, whos}, state) do
+ Enum.map(whos, fn(who = %ExIRC.Who{nick: nick, operator?: operator}) ->
+ priv = if operator, do: [:operator], else: []
+ IRC.UserTrack.joined(channel, who, priv)
+ end)
+ {:noreply, state}
+ end
+
+ def handle_info({:quit, _reason, sender}, state) do
+ IRC.UserTrack.quitted(sender)
+ {:noreply, state}
+ end
+
+ def handle_info({:joined, channel, sender}, state) do
+ IRC.UserTrack.joined(channel, sender, [])
+ {:noreply, state}
+ end
+
+ def handle_info({:kicked, nick, _by, channel, _reason}, state) do
+ parted(channel, nick)
+ {:noreply, state}
+ end
+
+ def handle_info({:parted, channel, %ExIRC.SenderInfo{nick: nick}}, state) do
+ parted(channel, nick)
+ {:noreply, state}
+ end
+
+ def handle_info({:mode, [channel, mode, nick]}, state) do
+ mode(channel, nick, mode)
+ {:noreply, state}
+ end
+
+ def handle_info({:nick_changed, old_nick, new_nick}, state) do
+ rename(old_nick, new_nick)
+ {:noreply, state}
+ end
+
+ def handle_info(msg, state) do
+ {:noreply, state}
+ end
+
+ defp parted(channel, nick) do
+ IRC.UserTrack.parted(channel, nick)
+ :ok
+ end
+
+ defp mode(channel, nick, "+o") do
+ IRC.UserTrack.change_privileges(channel, nick, {[:operator], []})
+ :ok
+ end
+
+ defp mode(channel, nick, "-o") do
+ IRC.UserTrack.change_privileges(channel, nick, {[], [:operator]})
+ :ok
+ end
+
+ defp rename(old, new) do
+ IRC.UserTrack.renamed(old, new)
+ :ok
+ end
+
+end