From b6ec252fcd9a0023c93b93db37a68e60356cf078 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 15 May 2022 11:35:18 +0300 Subject: [PATCH] Add support for weird user ID templates when generating registration Fixes #492 --- config/registration.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/config/registration.go b/config/registration.go index a982279..8fdcef0 100644 --- a/config/registration.go +++ b/config/registration.go @@ -19,6 +19,7 @@ package config import ( "fmt" "regexp" + "strings" "maunium.net/go/mautrix/appservice" ) @@ -36,7 +37,9 @@ func (config *Config) NewRegistration() (*appservice.Registration, error) { // Workaround for https://github.com/matrix-org/synapse/pull/5758 registration.SenderLocalpart = appservice.RandomString(32) - botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", config.AppService.Bot.Username, config.Homeserver.Domain)) + botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", + regexp.QuoteMeta(config.AppService.Bot.Username), + regexp.QuoteMeta(config.Homeserver.Domain))) registration.Namespaces.RegisterUserIDs(botRegex, true) return registration, nil @@ -63,9 +66,14 @@ func (config *Config) copyToRegistration(registration *appservice.Registration) registration.SenderLocalpart = config.AppService.Bot.Username registration.EphemeralEvents = config.AppService.EphemeralEvents - userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$", - config.Bridge.FormatUsername("[0-9]+"), - config.Homeserver.Domain)) + usernamePlaceholder := appservice.RandomString(16) + usernameTemplate := fmt.Sprintf("@%s:%s", + config.Bridge.FormatUsername(usernamePlaceholder), + config.Homeserver.Domain) + usernameTemplate = regexp.QuoteMeta(usernameTemplate) + usernameTemplate = strings.Replace(usernameTemplate, usernamePlaceholder, "[0-9]+", 1) + usernameTemplate = fmt.Sprintf("^%s$", usernameTemplate) + userIDRegex, err := regexp.Compile(usernameTemplate) if err != nil { return err }