diff --git a/src/github.com/matrix-org/dendrite/appservice/routing/user.go b/src/github.com/matrix-org/dendrite/appservice/routing/user.go index d2e02e94e..4f9a8e863 100644 --- a/src/github.com/matrix-org/dendrite/appservice/routing/user.go +++ b/src/github.com/matrix-org/dendrite/appservice/routing/user.go @@ -3,9 +3,11 @@ package routing import ( "github.com/matrix-org/util" "net/http" + "strings" + "unicode" ) -// represents response to an AppService URI to User Id +// URIToUIDResponse represents response to an AppService URI to User Id // (/_matrix/app/r0/user?uri={url_encoded_uri}) request type URIToUIDResponse struct { UserID string `json:"user_id"` @@ -15,9 +17,9 @@ type URIToUIDResponse struct { // enables users to contact appservice users directly by taking an encoded // URI and turning it into a Matrix ID on the homeserver. // https://matrix.org/docs/spec/application_service/unstable.html#user-ids - +// tel://123.1234 -> @tel_//123.1234:matrix.org +// mailto:test@matrix.org -> @mailto_test_matrix.org:matrix.org func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse { - // TODO: Implement homeserver := cfg.Matrix.ServerName uri := req.URL.Query().Get("uri") if uri == "" { @@ -26,8 +28,25 @@ func URIToUID(req *http.Request, cfg config.Dendrite) util.JSONResponse { JSON: nil, } } + // V1 just replaces the illegal characters (from + // https://matrix.org/docs/spec/appendices.html#id12) with _ + // TODO: Come up with a better way to turn URIs into User IDs + w := strings.FieldsFunc(strings.ToLower(uri), func(r rune) bool { + if unicode.IsDigit(r) || unicode.IsLetter(r) { + return false + } + switch r { + case '.', '_', '=', '-', '/': + return false + } + return true + }) + + // compile user ID and return + userID := "@" + strings.Join(w, "_") + ":" + homeserver + return util.JSONResponse{ Code: http.StatusOK, - JSON: nil, + JSON: URIToUIDResponse{UserID: userID}, } }