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
4164end
0 commit comments