summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw1gz <w1gz@noreply.com>2017-09-06 23:50:13 +0200
committerw1gz <w1gz@noreply.com>2017-09-06 23:53:29 +0200
commit96578d178ccee8f591b01ea4762bec8a0112a8ad (patch)
tree2487d86bdfd013fefce8f914f7f0d451f91dd19a
parentMerge pull request #69 from shymega/tweaks/readme (diff)
Fallback to latin1 when we're not dealing with valid unicode
-rw-r--r--lib/exirc/utils.ex8
-rw-r--r--test/utils_test.exs17
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