From 49b63089f50d87e701947b0af866b7ea09e90a35 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Mon, 16 Jul 2018 05:30:04 -0700 Subject: [PATCH] Consolidate AS interest checking (#539) * Methods for checking if an AS is interested in events * Look through rooms namespace for matching room IDs --- .../appservice/consumers/roomserver.go | 21 ++------ .../dendrite/clientapi/routing/register.go | 12 ++--- .../dendrite/common/config/appservice.go | 48 +++++++++++++++++++ 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/appservice/consumers/roomserver.go b/src/github.com/matrix-org/dendrite/appservice/consumers/roomserver.go index bc1d3bf2..b3584dfb 100644 --- a/src/github.com/matrix-org/dendrite/appservice/consumers/roomserver.go +++ b/src/github.com/matrix-org/dendrite/appservice/consumers/roomserver.go @@ -185,18 +185,9 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont return false } - // Check sender of the event - for _, userNamespace := range appservice.NamespaceMap["users"] { - if userNamespace.RegexpObject.MatchString(event.Sender()) { - return true - } - } - - // Check room id of the event - for _, roomNamespace := range appservice.NamespaceMap["rooms"] { - if roomNamespace.RegexpObject.MatchString(event.RoomID()) { - return true - } + if appservice.IsInterestedInUserID(event.Sender()) || + appservice.IsInterestedInRoomID(event.RoomID()) { + return true } // Check all known room aliases of the room the event came from @@ -204,10 +195,8 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont var queryRes api.GetAliasesForRoomIDResponse if err := s.alias.GetAliasesForRoomID(ctx, &queryReq, &queryRes); err == nil { for _, alias := range queryRes.Aliases { - for _, aliasNamespace := range appservice.NamespaceMap["aliases"] { - if aliasNamespace.RegexpObject.MatchString(alias) { - return true - } + if appservice.IsInterestedInRoomAlias(alias) { + return true } } } else { diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go index 76a696f9..813c4b3f 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/register.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/register.go @@ -337,17 +337,15 @@ func UsernameMatchesMultipleExclusiveNamespaces( ) bool { // Check namespaces and see if more than one match matchCount := 0 + userID := userutil.MakeUserID(username, cfg.Matrix.ServerName) for _, appservice := range cfg.Derived.ApplicationServices { - for _, namespaceSlice := range appservice.NamespaceMap { - for _, namespace := range namespaceSlice { - // Check if we have a match on this username - if namespace.RegexpObject.MatchString(username) { - matchCount++ - } + if appservice.IsInterestedInUserID(userID) { + if matchCount++; matchCount > 1 { + return true } } } - return matchCount > 1 + return false } // validateApplicationService checks if a provided application service token diff --git a/src/github.com/matrix-org/dendrite/common/config/appservice.go b/src/github.com/matrix-org/dendrite/common/config/appservice.go index 178788e3..2b47eb58 100644 --- a/src/github.com/matrix-org/dendrite/common/config/appservice.go +++ b/src/github.com/matrix-org/dendrite/common/config/appservice.go @@ -66,6 +66,54 @@ type ApplicationService struct { Protocols []string `yaml:"protocols"` } +// IsInterestedInRoomID returns a bool on whether an application service's +// namespace includes the given room ID +func (a *ApplicationService) IsInterestedInRoomID( + roomID string, +) bool { + if namespaceSlice, ok := a.NamespaceMap["rooms"]; ok { + for _, namespace := range namespaceSlice { + if namespace.RegexpObject.MatchString(roomID) { + return true + } + } + } + + return false +} + +// IsInterestedInUserID returns a bool on whether an application service's +// namespace includes the given user ID +func (a *ApplicationService) IsInterestedInUserID( + userID string, +) bool { + if namespaceSlice, ok := a.NamespaceMap["users"]; ok { + for _, namespace := range namespaceSlice { + if namespace.RegexpObject.MatchString(userID) { + return true + } + } + } + + return false +} + +// IsInterestedInRoomAlias returns a bool on whether an application service's +// namespace includes the given room alias +func (a *ApplicationService) IsInterestedInRoomAlias( + roomAlias string, +) bool { + if namespaceSlice, ok := a.NamespaceMap["aliases"]; ok { + for _, namespace := range namespaceSlice { + if namespace.RegexpObject.MatchString(roomAlias) { + return true + } + } + } + + return false +} + // loadAppservices iterates through all application service config files // and loads their data into the config object for later access. func loadAppservices(config *Dendrite) error {