summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Schoenfelder <paulschoenfelder@gmail.com>2016-11-07 10:50:21 -0600
committerGitHub <noreply@github.com>2016-11-07 10:50:21 -0600
commitbf9dbf9b8181dce3177123ccbe611b201d24bc40 (patch)
treedffccf9933069c862f6a587712e06ca7c2a4e6b7
parentAdd test for JOIN notification. See #56 (diff)
parentStop over-eager splitting of message prefix (diff)
Merge pull request #55 from isilkor/prefix-split
Fix message prefix handling
-rw-r--r--lib/exirc/utils.ex34
-rw-r--r--test/utils_test.exs36
2 files changed, 48 insertions, 22 deletions
diff --git a/lib/exirc/utils.ex b/lib/exirc/utils.ex
index 41686ad..a5f18b5 100644
--- a/lib/exirc/utils.ex
+++ b/lib/exirc/utils.ex
@@ -25,23 +25,21 @@ defmodule ExIrc.Utils do
end
end
- @split_pattern ~r/(!|@|\.)/
+ @prefix_pattern ~r/^(?<nick>[^!]+)(?:(?:!(?<user>[^@ ]+))?(?:@(?<host>[\w.:-]+)))?$/
defp parse_from(from, msg) do
from_str = IO.iodata_to_binary(from)
- splits = Regex.scan(@split_pattern, from_str, return: :index)
- |> Enum.map(fn [{start, len},_] -> binary_part(from_str, start, len) end)
- parts = Regex.split(@split_pattern, from_str)
- woven = weave(splits, parts)
- case woven do
- [nick, "!", user, "@" | host] ->
- %{msg | nick: nick, user: user, host: Enum.join(host)}
- [nick, "@" | host] ->
- %{msg | nick: nick, host: Enum.join(host)}
- [_, "." | _] ->
- # from is probably a server name
- %{msg | server: to_string(from)}
+ parts = Regex.run(@prefix_pattern, from_str, capture: :all_but_first)
+ case parts do
+ [nick, user, host] ->
+ %{msg | nick: nick, user: user, host: host}
+ [nick, host] ->
+ %{msg | nick: nick, host: host}
[nick] ->
- %{msg | nick: nick}
+ if String.contains?(nick, ".") do
+ %{msg | server: nick}
+ else
+ %{msg | nick: nick}
+ end
end
end
@@ -180,12 +178,4 @@ defmodule ExIrc.Utils do
end
end
- defp weave(xs, ys) do
- do_weave(xs, ys, [])
- |> Enum.filter(fn "" -> false; _ -> true end)
- end
- defp do_weave([], ys, result), do: (ys ++ result) |> Enum.reverse
- defp do_weave(xs, [], result), do: (xs ++ result) |> Enum.reverse
- defp do_weave([hx|xs], [hy|ys], result), do: do_weave(xs, ys, [hx, hy | result])
-
end
diff --git a/test/utils_test.exs b/test/utils_test.exs
index 9b669c7..31dac37 100644
--- a/test/utils_test.exs
+++ b/test/utils_test.exs
@@ -54,6 +54,42 @@ defmodule ExIrc.UtilsTest do
} = Utils.isup(parsed.args, state)
end
+ test "Can parse full prefix in messages" do
+ assert %IrcMessage{
+ nick: "WiZ",
+ user: "jto",
+ host: "tolsun.oulu.fi",
+ } = Utils.parse(':WiZ!jto@tolsun.oulu.fi NICK Kilroy')
+ end
+
+ test "Can parse reduced prefix in messages" do
+ assert %IrcMessage{
+ nick: "Trillian",
+ } = Utils.parse(':Trillian SQUIT cm22.eng.umd.edu :Server out of control')
+ end
+
+ test "Can parse server-only prefix in messages" do
+ assert %IrcMessage{
+ server: "ircd.stealth.net"
+ } = Utils.parse(':ircd.stealth.net 302 yournick :syrk=+syrk@millennium.stealth.net')
+ end
+
+ test "Can parse FULL STOP in username in prefixes" do
+ assert %IrcMessage{
+ nick: "nick",
+ user: "user.name",
+ host: "irc.example.org"
+ } = Utils.parse(':nick!user.name@irc.example.org PART #channel')
+ end
+
+ test "Can parse EXCLAMATION MARK in username in prefixes" do
+ assert %IrcMessage{
+ nick: "nick",
+ user: "user!name",
+ host: "irc.example.org"
+ } = Utils.parse(':nick!user!name@irc.example.org PART #channel')
+ end
+
test "parse join message" do
message = ':pschoenf JOIN #elixir-lang'
assert %IrcMessage{