diff options
| author | href <href@random.sh> | 2018-02-17 21:21:42 +0100 |
|---|---|---|
| committer | href <href@random.sh> | 2018-02-17 21:21:42 +0100 |
| commit | 50c6a09ff64cb081b27a0c30790b86873449d172 (patch) | |
| tree | ba1bebfc7e367f169276692e0ac02709d62d6516 /lib/lsg_irc/user_track_handler.ex | |
| parent | txt: fix against malicious filenames (aka 'fuck you shiv') (diff) | |
:)
Diffstat (limited to '')
| -rw-r--r-- | lib/lsg_irc/user_track_handler.ex | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/lsg_irc/user_track_handler.ex b/lib/lsg_irc/user_track_handler.ex new file mode 100644 index 0000000..d167af5 --- /dev/null +++ b/lib/lsg_irc/user_track_handler.ex @@ -0,0 +1,93 @@ +defmodule LSG.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: [] + LSG.IRC.UserTrack.joined(channel, who, priv) + end) + {:noreply, state} + end + + def handle_info({:quit, _reason, sender}, state) do + LSG.IRC.UserTrack.quitted(sender) + {:noreply, state} + end + + def handle_info({:joined, channel, sender}, state) do + LSG.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 + LSG.IRC.UserTrack.parted(channel, nick) + :ok + end + + defp mode(channel, nick, "+o") do + LSG.IRC.UserTrack.change_privileges(channel, nick, {[:operator], []}) + :ok + end + + defp mode(channel, nick, "-o") do + LSG.IRC.UserTrack.change_privileges(channel, nick, {[], [:operator]}) + :ok + end + + defp rename(old, new) do + LSG.IRC.UserTrack.renamed(old, new) + :ok + end + +end |
