Skip to content

Commit

Permalink
Fix bug in Utils.parse_from
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwalker committed Aug 12, 2014
1 parent 4c3ab5c commit e494c8f
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/exirc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ defmodule ExIrc.Utils do
end
end

@split_pattern ~r/(!|@|\.)/
defp parse_from(from, msg) do
binary_from = IO.iodata_to_binary(from)
fully_qualified_regex = ~r/(?<nick>.*)!(?<user>.*)@(?<host>.*)/
missing_user_regex = ~r/(?<nick>.*)@(?<host>.*)/
host_only_regex = ~r/.+\..+/
cond do
captures = Regex.named_captures fully_qualified_regex, binary_from ->
%{msg | :nick => captures[:nick], :user => captures[:user], :host => captures[:host]}
captures = Regex.named_captures missing_user_regex, binary_from ->
%{msg | :nick => captures[:nick], :host => captures[:host]}
Regex.match? host_only_regex, binary_from ->
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)}
true ->
%{msg | :nick => to_string(from)}
[nick] ->
%{msg | :nick => nick}
end
end

Expand Down Expand Up @@ -172,4 +175,9 @@ defmodule ExIrc.Utils do
end
end

defp weave(xs, ys), do: do_weave(xs, ys, [])
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

0 comments on commit e494c8f

Please sign in to comment.