Skip to content

Commit 4f36b08

Browse files
committed
Add support for port numbers in server name
1 parent 654919e commit 4f36b08

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

lib/matrix/misc.ex

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
##
2-
# Copyright (C) 2021 Valentin Lorentz
2+
# Copyright (C) 2021-2022 Valentin Lorentz
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU Affero General Public License version 3,
@@ -18,15 +18,38 @@ defmodule M51.Matrix.Misc do
1818
def parse_userid(userid) do
1919
case String.split(userid, ":") do
2020
[local_name, hostname] ->
21-
if Regex.match?(~r|^[0-9a-z.=_/-]+$|, local_name) do
22-
if Regex.match?(~r/.*\s.*/u, hostname) do
23-
{:error, "\"" <> hostname <> "\" is not a valid hostname"}
24-
else
21+
cond do
22+
!Regex.match?(~r|^[0-9a-z.=_/-]+$|, local_name) ->
23+
{:error,
24+
"your local name may only contain lowercase latin letters, digits, and the following characters: -.=_/"}
25+
26+
Regex.match?(~r/.*\s.*/u, hostname) ->
27+
{:error, "\"#{hostname}\" is not a valid hostname"}
28+
29+
true ->
2530
{:ok, {local_name, hostname}}
31+
end
32+
33+
[local_name, hostname, port_str] ->
34+
port =
35+
case Integer.parse(port_str) do
36+
{i, ""} -> i
37+
_ -> nil
2638
end
27-
else
28-
{:error,
29-
"your local name may only contain lowercase latin letters, digits, and the following characters: -.=_/"}
39+
40+
cond do
41+
!Regex.match?(~r|^[0-9a-z.=_/-]+$|, local_name) ->
42+
{:error,
43+
"your local name may only contain lowercase latin letters, digits, and the following characters: -.=_/"}
44+
45+
Regex.match?(~r/.*\s.*/u, hostname) ->
46+
{:error, "\"#{hostname}\" is not a valid hostname"}
47+
48+
port == nil ->
49+
{:error, "\"#{port_str}\" is not a valid port number"}
50+
51+
true ->
52+
{:ok, {local_name, "#{hostname}:#{port}"}}
3053
end
3154

3255
[nick] ->
@@ -35,7 +58,7 @@ defmodule M51.Matrix.Misc do
3558
nick <> ":matrix.org"}
3659

3760
_ ->
38-
{:error, "must not contain more than one colon."}
61+
{:error, "must not contain more than two colons."}
3962
end
4063
end
4164
end

test/irc/handler_test.exs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
##
2-
# Copyright (C) 2021 Valentin Lorentz
2+
# Copyright (C) 2021-2022 Valentin Lorentz
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU Affero General Public License version 3,
@@ -282,9 +282,14 @@ defmodule M51.IrcConn.HandlerTest do
282282
":server. 904 foo:bar :Invalid account/user id: must contain a colon (':'), to separate the username and hostname. For example: foo:matrix.org\r\n"
283283
)
284284

285+
try_userid.(
286+
"foo:bar:baz:qux",
287+
":server. 904 foo:bar :Invalid account/user id: must not contain more than two colons.\r\n"
288+
)
289+
285290
try_userid.(
286291
"foo:bar:baz",
287-
":server. 904 foo:bar :Invalid account/user id: must not contain more than one colon.\r\n"
292+
":server. 904 foo:bar :Invalid account/user id: \"baz\" is not a valid port number\r\n"
288293
)
289294

290295
try_userid.(

0 commit comments

Comments
 (0)