~singpolyma/cheogram-matrix-spa

ref: 0abf85eba72e6c31abe35cadef409d845867db00 cheogram-matrix-spa/src/Main.elm -rw-r--r-- 3.0 KiB
0abf85ebStephen Paul Weber Bifrost uses lowercase escapes 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Url exposing (percentEncode)
import Regex
import Dict

main =
	Browser.element {
		init = init,
		view = view,
		update = update,
		subscriptions = \_ -> Sub.none
	}

type alias Model = {
		jabberId : String,
		bifrost : Bifrost
	}

init : () -> (Model, Cmd Msg)
init _ =
	(Model "" ariaNet, Cmd.none)

type Msg = JabberID String | SMS String | Email String | SIP String | Gateway String

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
	case msg of
		JabberID jid -> ({ model | jabberId = jid }, Cmd.none)
		SMS tel -> ({ model | jabberId = telToJid tel }, Cmd.none)
		Email email -> ({ model | jabberId = emailToJid email }, Cmd.none)
		SIP sip -> ({ model | jabberId = sipToJid sip }, Cmd.none)
		Gateway gw -> ({ model | bifrost = Maybe.withDefault ariaNet (Dict.get gw gateways) }, Cmd.none)

view : Model -> Html Msg
view model =
	let matrixId = jidToMatrix model.bifrost model.jabberId
	in
	div [] [
		select [onInput Gateway] (List.map (\gateway ->
			option [] [text gateway]
		) (Dict.keys gateways)),
		input [
			type_ "text",
			placeholder "Jabber ID",
			value model.jabberId,
			onInput JabberID
		] [],
		input [
			type_ "tel",
			placeholder "SMS",
			onInput SMS
		] [],
		input [
			type_ "email",
			placeholder "Email",
			onInput Email
		] [],
		input [
			type_ "text",
			placeholder "SIP",
			onInput SIP
		] [],
		a [target "_blank", href ("https://matrix.to/#/" ++ matrixId)] [text matrixId]
	]

type alias Bifrost = {
		domain : String,
		prefix : String
	}

gateways = Dict.fromList [("aria-net.org", ariaNet), ("matrix.org", matrixOrg)]
ariaNet = Bifrost "aria-net.org" "bifrost"
matrixOrg = Bifrost "matrix.org" "xmpp"

jidToMatrix : Bifrost -> String -> String
jidToMatrix gateway jid =
	if jid == "" then
		""
	else
		"@_" ++ gateway.prefix ++ "_" ++
		String.replace "%" "=" (String.toLower (percentEncode jid)) ++
		":" ++ gateway.domain

emailToJid : String -> String
emailToJid email =
	if email == "" then
		""
	else
		escapeJid email ++ "@smtp.cheogram.com"

sipToJid : String -> String
sipToJid sip =
	if sip == "" then
		""
	else if String.startsWith "sip:" sip then
		(escapeJid (String.dropLeft 4 sip)) ++ "@sip.cheogram.com"
	else
		(escapeJid sip) ++ "@sip.cheogram.com"

telToJid : String -> String
telToJid telish =
	let digits = String.filter Char.isDigit telish
	in
	if String.startsWith "+" telish then
		"+" ++ digits ++ "@cheogram.com"
	else if String.startsWith "1" digits && String.length digits == 11 then
		"+" ++ digits ++ "@cheogram.com"
	else if String.length digits == 10 then
		"+1" ++ digits ++ "@cheogram.com"
	else
		""

aRegex : String -> Regex.Regex
aRegex str = Maybe.withDefault Regex.never <| Regex.fromString str

escapeJid : String -> String
escapeJid str =
	Regex.replace
		(aRegex "[ \"&'/:<>@]")
		(\match -> String.replace "%" "\\" (percentEncode match.match))
	<|
	Regex.replace
		(aRegex "\\\\(20|22|26|27|2f|3a|3c|3e|40|5c)")
		(\match -> "\\5c" ++ String.dropLeft 1 match.match)
		str