diff options
author | Paul Schoenfelder <paulschoenfelder@gmail.com> | 2014-02-19 21:30:46 -0600 |
---|---|---|
committer | Paul Schoenfelder <paulschoenfelder@gmail.com> | 2014-02-19 21:30:46 -0600 |
commit | 104e3a5e27700b73bb4e189de6cea34e78f5c588 (patch) | |
tree | 2f92e636b6f22f0142e114c4aa62ce57c35852b6 | |
parent | Add support for /INVITE (diff) |
Add events for kick, invite, and privmsg
-rw-r--r-- | lib/exirc/client.ex | 38 | ||||
-rw-r--r-- | lib/exirc/example_handler.ex | 26 | ||||
-rw-r--r-- | lib/exirc/utils.ex | 2 | ||||
-rw-r--r-- | test/utils_test.exs | 18 |
4 files changed, 83 insertions, 1 deletions
diff --git a/lib/exirc/client.ex b/lib/exirc/client.ex index 26a820d..2667236 100644 --- a/lib/exirc/client.ex +++ b/lib/exirc/client.ex @@ -565,6 +565,44 @@ defmodule ExIrc.Client do end {:noreply, state}; end + # Called when we are invited to a channel + def handle_data(IrcMessage[cmd: "INVITE", args: [nick, channel], nick: by] = msg, ClientState[nick: nick] = state) do + if state.debug?, do: debug "RECEIVED AN INVITE: #{msg.args |> Enum.join(" ")}" + send_event {:invited, by, channel}, state + {:noreply, state} + end + # Called when we are kicked from a channel + def handle_data(IrcMessage[cmd: "KICK", args: [channel, nick], nick: by] = _msg, ClientState[nick: nick] = state) do + if state.debug?, do: debug "WE WERE KICKED FROM #{channel} BY #{by}" + send_event {:kicked, by, channel}, state + {:noreply, state} + end + # Called when someone else was kicked from a channel + def handle_data(IrcMessage[cmd: "KICK", args: [channel, nick], nick: by] = _msg, state) do + if state.debug?, do: debug "#{nick} WAS KICKED FROM #{channel} BY #{by}" + send_event {:kicked, nick, by, channel}, state + {:noreply, state} + end + # Called when someone sends us a message + def handle_data(IrcMessage[nick: from, cmd: "PRIVMSG", args: [nick, message]] = _msg, ClientState[nick: nick] = state) do + if state.debug?, do: debug "#{from} SENT US #{message}" + send_event {:received, message, from}, state + {:noreply, state} + end + # Called when someone sends a message to a channel we're in, or a list of users + def handle_data(IrcMessage[nick: from, cmd: "PRIVMSG", args: [to, message]] = _msg, ClientState[nick: nick] = state) do + if state.debug?, do: debug "#{from} SENT #{message} TO #{to}" + case String.contains?(to, nick) do + # Treat it like a private message if message was sent to a list of users that includes us + true -> send_event {:received, message, from}, state + # Otherwise it was just a channel message + _ -> + send_event {:received, message, from, to}, state + # If we were mentioned, fire that event as well + if String.contains?(message, nick), do: send_event({:mentioned, message, from, to}, state) + end + {:noreply, state} + end # Called any time we receive an unrecognized message def handle_data(msg, state) do if state.debug? do debug "UNRECOGNIZED MSG: #{msg.cmd}"; IO.inspect(msg) end diff --git a/lib/exirc/example_handler.ex b/lib/exirc/example_handler.ex index ff52947..f22b9d8 100644 --- a/lib/exirc/example_handler.ex +++ b/lib/exirc/example_handler.ex @@ -77,6 +77,31 @@ defmodule ExampleHandler do debug "#{nick} left #{channel}" {:noreply, nil} end + def handle_info({:invited, by, channel}, _state) do + debug "#{by} invited us to #{channel}" + {:noreply, nil} + end + def handle_info({:kicked, by, channel}, _state) do + debug "We were kicked from #{channel} by #{by}" + {:noreply, nil} + end + def handle_info({:kicked, nick, by, channel}, _state) do + debug "#{nick} was kicked from #{channel} by #{by}" + {:noreply, nil} + end + def handle_info({:received, message, from}, _state) do + debug "#{from} sent us a private message: #{message}" + {:noreply, nil} + end + def handle_info({:received, message, from, channel}, _state) do + debug "#{from} sent a message to #{channel}: #{message}" + {:noreply, nil} + end + def handle_info({:mentioned, message, from, channel}, _state) do + debug "#{from} mentioned us in #{channel}: #{message}" + {:noreply, nil} + end + # This is an example of how you can manually catch commands if ExIrc.Client doesn't send a specific message for it def handle_info(IrcMessage[nick: from, cmd: "PRIVMSG", args: ["testnick", msg]], _state) do debug "Received a private message from #{from}: #{msg}" {:noreply, nil} @@ -85,6 +110,7 @@ defmodule ExampleHandler do debug "Received a message in #{to} from #{from}: #{msg}" {:noreply, nil} end + # Catch-all for messages you don't care about def handle_info(msg, _state) do debug "Received IrcMessage:" IO.inspect msg diff --git a/lib/exirc/utils.ex b/lib/exirc/utils.ex index 374bee8..0c24c83 100644 --- a/lib/exirc/utils.ex +++ b/lib/exirc/utils.ex @@ -39,7 +39,7 @@ defmodule ExIrc.Utils do # from is probably a server name msg.server(from_char_list!(from)) [nick] -> - msg.nick(from_char_list!(nick)) + msg.nick(nick) end end diff --git a/test/utils_test.exs b/test/utils_test.exs index eb83e6f..a32b380 100644 --- a/test/utils_test.exs +++ b/test/utils_test.exs @@ -23,6 +23,24 @@ defmodule ExIrc.UtilsTest do ] = Utils.parse(message) end + test "Parse INVITE message" do + message = ':pschoenf INVITE testuser #awesomechan' + assert IrcMessage[ + nick: "pschoenf", + cmd: "INVITE", + args: ["testuser", "#awesomechan"] + ] = Utils.parse(message) + end + + test "Parse KICK message" do + message = ':pschoenf KICK #testchan lameuser' + assert IrcMessage[ + nick: "pschoenf", + cmd: "KICK", + args: ["#testchan", "lameuser"] + ] = Utils.parse(message) + end + test "Can parse RPL_ISUPPORT commands" do message = ':irc.example.org 005 nick NETWORK=Freenode PREFIX=(ov)@+ CHANTYPES=#&' parsed = Utils.parse(message) |