diff options
author | Paul Schoenfelder <paulschoenfelder@gmail.com> | 2017-09-06 18:32:06 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-06 18:32:06 -0500 |
commit | f4c96bb1332e36635af617c9c3770bd4f52acaed (patch) | |
tree | 2487d86bdfd013fefce8f914f7f0d451f91dd19a | |
parent | Merge pull request #69 from shymega/tweaks/readme (diff) | |
parent | Fallback to latin1 when we're not dealing with valid unicode (diff) |
Merge pull request #70 from w1gz/master
Fallback to latin1 when we're not dealing with valid unicode
-rw-r--r-- | lib/exirc/utils.ex | 8 | ||||
-rw-r--r-- | test/utils_test.exs | 17 |
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/exirc/utils.ex b/lib/exirc/utils.ex index 2573cc3..46cba11 100644 --- a/lib/exirc/utils.ex +++ b/lib/exirc/utils.ex @@ -76,7 +76,13 @@ defmodule ExIrc.Utils do |> Enum.filter(fn arg -> arg != [] end) |> Enum.map(&trim_crlf/1) |> Enum.map(&:binary.list_to_bin/1) - |> Enum.map(&:unicode.characters_to_binary/1) + |> Enum.map(fn(s) -> + case String.valid?(s) do + true -> :unicode.characters_to_binary(s) + false -> :unicode.characters_to_binary(s, :latin1, :unicode) + end + end) + post_process(%{msg | args: args}) end diff --git a/test/utils_test.exs b/test/utils_test.exs index dce38c0..3beb447 100644 --- a/test/utils_test.exs +++ b/test/utils_test.exs @@ -189,4 +189,21 @@ defmodule ExIrc.UtilsTest do } = Utils.parse(message) end + test "Can parse latin1" do + # ':foo!~user@172.17.0.1 PRIVMSG #bar :ééé\r\n' + message = [58, 102, 111, 111, 33, 126, 117, 115, 101, 114, 64, 49, 55, 50, + 46, 49, 55, 46, 48, 46, 49, 32, 80, 82, 73, 86, 77, 83, 71, 32, + 35, 98, 97, 114, 32, 58, 233, 233, 233, 13, 10] + + assert %IrcMessage{ + args: ["#bar", "ééé"], + cmd: "PRIVMSG", + ctcp: false, + host: "172.17.0.1", + nick: "foo", + server: [], + user: "~user" + } = Utils.parse(message) + end + end |