소스 검색

Add support for weird user ID templates when generating registration

Fixes #492
Tulir Asokan 3 년 전
부모
커밋
b6ec252fcd
1개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 12 4
      config/registration.go

+ 12 - 4
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
 	}