summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Schoenfelder <paulschoenfelder@gmail.com>2013-12-07 03:09:17 -0600
committerPaul Schoenfelder <paulschoenfelder@gmail.com>2013-12-07 03:09:17 -0600
commita3d594e6170f339360cfda7212be267c356d7c22 (patch)
tree9f96d7f60bd9d4a0d6a85c84a124c1231253ef8b
parentUse :error_logger for logging (diff)
Fix irc message parsing
-rw-r--r--lib/exirc/client.ex6
-rw-r--r--lib/exirc/commands.ex2
-rw-r--r--lib/exirc/utils.ex65
-rw-r--r--test/utils_test.exs1
4 files changed, 39 insertions, 35 deletions
diff --git a/lib/exirc/client.ex b/lib/exirc/client.ex
index 63bc572..95d1829 100644
--- a/lib/exirc/client.ex
+++ b/lib/exirc/client.ex
@@ -215,9 +215,9 @@ defmodule ExIrc.Client do
{:noreply, state.event_handlers(handlers)}
end
- def handle_info({:tcp_closed, _socket}, ClientState[server: server, port: port, channels: channels]) do
+ def handle_info({:tcp_closed, _socket}, ClientState[server: server, port: port] = state) do
info "Connection to #{server}:#{port} closed!"
- {:noreply, channels(Channels.init())}
+ {:noreply, state.channels(Channels.init())}
end
def handle_info({:tcp_error, socket}, state) do
@@ -299,7 +299,7 @@ defmodule ExIrc.Client do
end
# NAMES reply
- def handle_data(IrcMessage[cmd: @rpl_NAMREPLY] = msg, state) do
+ def handle_data(IrcMessage[cmd: @rpl_NAMEREPLY] = msg, state) do
{channel_type, channel, names} = case msg.args do
[_nick, channel_type, channel, names] -> {channel_type, channel, names}
[channel_type, channel, names] -> {channel_type, channel, names}
diff --git a/lib/exirc/commands.ex b/lib/exirc/commands.ex
index 3f78d4d..ded3704 100644
--- a/lib/exirc/commands.ex
+++ b/lib/exirc/commands.ex
@@ -63,7 +63,7 @@ defmodule Irc.Commands do
@rpl_LOCALUSERS '265'
@rpl_GLOBALUSERS '266'
@rpl_TOPIC '332'
- @rpl_NAMREPLY '353'
+ @rpl_NAMEREPLY '353'
@rpl_ENDOFNAMES '366'
@rpl_MOTD '372'
@rpl_MOTDSTART '375'
diff --git a/lib/exirc/utils.ex b/lib/exirc/utils.ex
index bdc283a..f480e1f 100644
--- a/lib/exirc/utils.ex
+++ b/lib/exirc/utils.ex
@@ -3,20 +3,14 @@ defmodule ExIrc.Utils do
alias ExIrc.Client.IrcMessage, as: IrcMessage
@doc """
- Parse IRC message data
+ Parse an IRC message
"""
def parse(raw_data) do
- data = Enum.slice(raw_data, 1, Enum.count(raw_data) - 2)
- case data do
- [?:, _] ->
- [[?: | from] | rest] = :string.tokens(data, ' ')
- get_cmd rest, parse_from(from, IrcMessage.new(ctcp: false))
- data ->
- get_cmd :string.tokens(data, ' '), IrcMessage.new(ctcp: false)
- end
+ [[?: | from] | rest] = :string.tokens(raw_data, ' ')
+ get_cmd rest, parse_from(from, IrcMessage.new(ctcp: false))
end
- def parse_from(from, msg) do
+ defp parse_from(from, msg) do
case Regex.split(%r/(!|@|\.)/, from) do
[nick, '!', user, '@', host | host_rest] ->
msg.nick(nick).user(user).host(host ++ host_rest)
@@ -30,41 +24,52 @@ defmodule ExIrc.Utils do
end
end
- def get_cmd([cmd, arg1, [':', 1 | ctcp_trail] | rest], msg) when cmd == 'PRIVMSG' or cmd == 'NOTICE' do
- get_cmd([cmd, arg1, [1 | ctcp_trail] | rest], msg)
+ # Parse command from message
+ defp get_cmd([cmd, arg1, [?:, 1 | ctcp_trail] | restargs], msg) when cmd == 'PRIVMSG' or cmd == 'NOTICE' do
+ get_cmd([cmd, arg1, [1 | ctcp_trail] | restargs], msg)
end
- def get_cmd([cmd, _arg1, [1 | ctcp_trail] | rest], msg) when cmd == 'PRIVMSG' or cmd == 'NOTICE' do
- list = (ctcp_trail ++ (lc arg inlist rest, do: ' ' ++ arg))
- |> Enum.flatten
- |> Enum.reverse
- case list do
+
+ defp get_cmd([cmd, _arg1, [1 | ctcp_trail] | restargs], msg) when cmd == 'PRIVMSG' or cmd == 'NOTICE' do
+ args = ctcp_trail ++ lc arg inlist restargs, do: ' ' ++ arg
+ |> Enum.flatten
+ |> Enum.reverse
+ case args do
[1 | ctcp_rev] ->
- [ctcp_cmd | args] = Enum.reverse(ctcp_rev) |> String.split(' ')
- msg = msg.cmd(ctcp_cmd).args(args).ctcp(true)
+ [ctcp_cmd | args] = ctcp_rev |> Enum.reverse |> :string.tokens(' ')
+ msg.cmd(ctcp_cmd).args(args).ctcp(true)
_ ->
- msg = msg.cmd(cmd).ctcp(:invalid)
+ msg.cmd(cmd).ctcp(:invalid)
end
end
- def get_cmd([cmd | rest], msg) do
+
+ defp get_cmd([cmd | rest], msg) do
get_args(rest, msg.cmd(cmd))
end
- def get_args([], msg) do
- msg.args(Enum.reverse(msg.args))
+
+ # Parse command args from message
+ defp get_args([], msg) do
+ msg.args
+ |> Enum.reverse
+ |> Enum.filter(fn(arg) -> arg != [] end)
+ |> msg.args
end
- def get_args([[':' | first_arg] | rest], msg) do
- list = lc arg inlist [first_arg | rest], do: ' ' ++ arg
- case Enum.flatten(list) do
+
+ defp get_args([[':' | first_arg] | rest], msg) do
+ args = lc arg inlist [first_arg | rest], do: ' ' ++ arg |> Enum.flatten
+ case args do
[_ | []] ->
- get_args([], msg.args(['' | msg.args]))
+ get_args([], msg.args([msg.args]))
[_ | full_trail] ->
get_args([], msg.args([full_trail | msg.args]))
end
end
- def get_args([arg | []], msg) do
- get_args([], msg.args(['', arg | msg.args]))
+
+ defp get_args([arg | []], msg) do
+ get_args([], msg.args([arg | msg.args]))
end
- def get_args([arg | rest], msg) do
+
+ defp get_args([arg | rest], msg) do
get_args(rest, msg.args([arg | msg.args]))
end
diff --git a/test/utils_test.exs b/test/utils_test.exs
index 3086f7d..cfd7cfb 100644
--- a/test/utils_test.exs
+++ b/test/utils_test.exs
@@ -2,7 +2,6 @@ defmodule ExIrc.UtilsTest do
use ExUnit.Case
alias ExIrc.Utils, as: Utils
alias ExIrc.Client.IrcMessage, as: IrcMessage
- alias ExIrc.Client.ClientState, as: ClientState
test "Given a local date/time as a tuple, can retrieve get the CTCP formatted time" do
local_time = {{2013,12,6},{14,5,00}}