2017-06-27 06:37:25 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-11 12:16:53 -05:00
|
|
|
package routing
|
2017-06-27 06:37:25 -05:00
|
|
|
|
|
|
|
import (
|
2017-07-07 08:11:32 -05:00
|
|
|
"net/http"
|
|
|
|
|
2017-06-27 06:37:25 -05:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2017-07-28 05:31:43 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-06-27 06:37:25 -05:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirectoryRoom looks up a room alias
|
|
|
|
func DirectoryRoom(
|
|
|
|
req *http.Request,
|
|
|
|
roomAlias string,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
cfg *config.Dendrite,
|
2017-07-28 05:31:43 -05:00
|
|
|
aliasAPI api.RoomserverAliasAPI,
|
2017-06-27 06:37:25 -05:00
|
|
|
) util.JSONResponse {
|
2017-07-07 08:11:32 -05:00
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
2017-06-27 06:37:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-06-27 06:37:25 -05:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:31:43 -05:00
|
|
|
var resp gomatrixserverlib.RespDirectory
|
|
|
|
|
2017-06-27 06:37:25 -05:00
|
|
|
if domain == cfg.Matrix.ServerName {
|
2018-05-30 07:43:13 -05:00
|
|
|
queryReq := api.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
|
|
var queryRes api.GetRoomIDForAliasResponse
|
|
|
|
if err = aliasAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 05:31:43 -05:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(queryRes.RoomID) > 0 {
|
|
|
|
// TODO: List servers that are aware of this room alias
|
|
|
|
resp = gomatrixserverlib.RespDirectory{
|
|
|
|
RoomID: queryRes.RoomID,
|
|
|
|
Servers: []gomatrixserverlib.ServerName{},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the response doesn't contain a non-empty string, return an error
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusNotFound,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.NotFound("Room alias " + roomAlias + " not found."),
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 06:37:25 -05:00
|
|
|
} else {
|
2017-09-13 05:03:41 -05:00
|
|
|
resp, err = federation.LookupRoomAlias(req.Context(), domain, roomAlias)
|
2017-06-27 06:37:25 -05:00
|
|
|
if err != nil {
|
|
|
|
switch x := err.(type) {
|
|
|
|
case gomatrix.HTTPError:
|
2018-03-13 10:55:45 -05:00
|
|
|
if x.Code == http.StatusNotFound {
|
2017-06-27 06:37:25 -05:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusNotFound,
|
2017-06-27 06:37:25 -05:00
|
|
|
JSON: jsonerror.NotFound("Room alias not found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Return 502 if the remote server errored.
|
|
|
|
// TODO: Return 504 if the remote server timed out.
|
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
2017-07-28 05:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
|
|
|
// TODO: Check if the user has the power level to set an alias
|
|
|
|
func SetLocalAlias(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
alias string,
|
|
|
|
cfg *config.Dendrite,
|
|
|
|
aliasAPI api.RoomserverAliasAPI,
|
|
|
|
) util.JSONResponse {
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', alias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain != cfg.Matrix.ServerName {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusForbidden,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.Forbidden("Alias must be on local homeserver"),
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 06:37:25 -05:00
|
|
|
|
2018-06-29 06:09:00 -05:00
|
|
|
// Check that the alias does not fall within an exclusive namespace of an
|
|
|
|
// application service
|
|
|
|
for _, appservice := range cfg.Derived.ApplicationServices {
|
|
|
|
if userNamespaces, ok := appservice.NamespaceMap["users"]; ok {
|
|
|
|
for _, namespace := range userNamespaces {
|
|
|
|
if namespace.Exclusive && namespace.RegexpObject.MatchString(alias) {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: jsonerror.ASExclusive("Alias is reserved by an application service"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:31:43 -05:00
|
|
|
var r struct {
|
|
|
|
RoomID string `json:"room_id"`
|
|
|
|
}
|
|
|
|
if resErr := httputil.UnmarshalJSONRequest(req, &r); resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
|
|
|
|
queryReq := api.SetRoomAliasRequest{
|
|
|
|
UserID: device.UserID,
|
|
|
|
RoomID: r.RoomID,
|
|
|
|
Alias: alias,
|
|
|
|
}
|
|
|
|
var queryRes api.SetRoomAliasResponse
|
2017-09-13 07:37:50 -05:00
|
|
|
if err := aliasAPI.SetRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 05:31:43 -05:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if queryRes.AliasExists {
|
2017-06-27 06:37:25 -05:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusConflict,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: jsonerror.Unknown("The alias " + alias + " already exists."),
|
2017-06-27 06:37:25 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 05:31:43 -05:00
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLocalAlias implements DELETE /directory/room/{roomAlias}
|
|
|
|
// TODO: Check if the user has the power level to remove an alias
|
|
|
|
func RemoveLocalAlias(
|
|
|
|
req *http.Request,
|
|
|
|
device *authtypes.Device,
|
|
|
|
alias string,
|
|
|
|
aliasAPI api.RoomserverAliasAPI,
|
|
|
|
) util.JSONResponse {
|
|
|
|
queryReq := api.RemoveRoomAliasRequest{
|
|
|
|
Alias: alias,
|
|
|
|
UserID: device.UserID,
|
|
|
|
}
|
|
|
|
var queryRes api.RemoveRoomAliasResponse
|
2017-09-13 07:37:50 -05:00
|
|
|
if err := aliasAPI.RemoveRoomAlias(req.Context(), &queryReq, &queryRes); err != nil {
|
2017-07-28 05:31:43 -05:00
|
|
|
return httputil.LogThenError(req, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-07-28 05:31:43 -05:00
|
|
|
JSON: struct{}{},
|
|
|
|
}
|
2017-06-27 06:37:25 -05:00
|
|
|
}
|