|
| 1 | +// Copyright (c) 2024 Tulir Asokan |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +package bridgeconfig |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "html/template" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "go.mau.fi/util/exerrors" |
| 16 | + "go.mau.fi/util/random" |
| 17 | + |
| 18 | + "maunium.net/go/mautrix/appservice" |
| 19 | + "maunium.net/go/mautrix/id" |
| 20 | +) |
| 21 | + |
| 22 | +type AppserviceConfig struct { |
| 23 | + Address string `yaml:"address"` |
| 24 | + Hostname string `yaml:"hostname"` |
| 25 | + Port uint16 `yaml:"port"` |
| 26 | + |
| 27 | + ID string `yaml:"id"` |
| 28 | + Bot BotUserConfig `yaml:"bot"` |
| 29 | + |
| 30 | + ASToken string `yaml:"as_token"` |
| 31 | + HSToken string `yaml:"hs_token"` |
| 32 | + |
| 33 | + EphemeralEvents bool `yaml:"ephemeral_events"` |
| 34 | + AsyncTransactions bool `yaml:"async_transactions"` |
| 35 | + |
| 36 | + UsernameTemplate string `yaml:"username_template"` |
| 37 | + usernameTemplate *template.Template `yaml:"-"` |
| 38 | +} |
| 39 | + |
| 40 | +func (asc *AppserviceConfig) FormatUsername(username string) string { |
| 41 | + if asc.usernameTemplate == nil { |
| 42 | + asc.usernameTemplate = exerrors.Must(template.New("username").Parse(asc.UsernameTemplate)) |
| 43 | + } |
| 44 | + var buf strings.Builder |
| 45 | + _ = asc.usernameTemplate.Execute(&buf, username) |
| 46 | + return buf.String() |
| 47 | +} |
| 48 | + |
| 49 | +func (config *Config) MakeUserIDRegex(matcher string) *regexp.Regexp { |
| 50 | + usernamePlaceholder := strings.ToLower(random.String(16)) |
| 51 | + usernameTemplate := fmt.Sprintf("@%s:%s", |
| 52 | + config.AppService.FormatUsername(usernamePlaceholder), |
| 53 | + config.Homeserver.Domain) |
| 54 | + usernameTemplate = regexp.QuoteMeta(usernameTemplate) |
| 55 | + usernameTemplate = strings.Replace(usernameTemplate, usernamePlaceholder, matcher, 1) |
| 56 | + usernameTemplate = fmt.Sprintf("^%s$", usernameTemplate) |
| 57 | + return regexp.MustCompile(usernameTemplate) |
| 58 | +} |
| 59 | + |
| 60 | +// GetRegistration copies the data from the bridge config into an *appservice.Registration struct. |
| 61 | +// This can't be used with the homeserver, see GenerateRegistration for generating files for the homeserver. |
| 62 | +func (asc *AppserviceConfig) GetRegistration() *appservice.Registration { |
| 63 | + reg := &appservice.Registration{} |
| 64 | + asc.copyToRegistration(reg) |
| 65 | + reg.SenderLocalpart = asc.Bot.Username |
| 66 | + reg.ServerToken = asc.HSToken |
| 67 | + reg.AppToken = asc.ASToken |
| 68 | + return reg |
| 69 | +} |
| 70 | + |
| 71 | +func (asc *AppserviceConfig) copyToRegistration(registration *appservice.Registration) { |
| 72 | + registration.ID = asc.ID |
| 73 | + registration.URL = asc.Address |
| 74 | + falseVal := false |
| 75 | + registration.RateLimited = &falseVal |
| 76 | + registration.EphemeralEvents = asc.EphemeralEvents |
| 77 | + registration.SoruEphemeralEvents = asc.EphemeralEvents |
| 78 | +} |
| 79 | + |
| 80 | +// GenerateRegistration generates a registration file for the homeserver. |
| 81 | +func (config *Config) GenerateRegistration() *appservice.Registration { |
| 82 | + registration := appservice.CreateRegistration() |
| 83 | + config.AppService.HSToken = registration.ServerToken |
| 84 | + config.AppService.ASToken = registration.AppToken |
| 85 | + config.AppService.copyToRegistration(registration) |
| 86 | + |
| 87 | + registration.SenderLocalpart = random.String(32) |
| 88 | + botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", |
| 89 | + regexp.QuoteMeta(config.AppService.Bot.Username), |
| 90 | + regexp.QuoteMeta(config.Homeserver.Domain))) |
| 91 | + registration.Namespaces.UserIDs.Register(botRegex, true) |
| 92 | + registration.Namespaces.UserIDs.Register(config.MakeUserIDRegex(".*"), true) |
| 93 | + |
| 94 | + return registration |
| 95 | +} |
| 96 | + |
| 97 | +func (config *Config) MakeAppService() *appservice.AppService { |
| 98 | + as := appservice.Create() |
| 99 | + as.HomeserverDomain = config.Homeserver.Domain |
| 100 | + _ = as.SetHomeserverURL(config.Homeserver.Address) |
| 101 | + as.Host.Hostname = config.AppService.Hostname |
| 102 | + as.Host.Port = config.AppService.Port |
| 103 | + as.Registration = config.AppService.GetRegistration() |
| 104 | + return as |
| 105 | +} |
| 106 | + |
| 107 | +type BotUserConfig struct { |
| 108 | + Username string `yaml:"username"` |
| 109 | + Displayname string `yaml:"displayname"` |
| 110 | + Avatar string `yaml:"avatar"` |
| 111 | + |
| 112 | + ParsedAvatar id.ContentURI `yaml:"-"` |
| 113 | +} |
| 114 | + |
| 115 | +type serializableBUC BotUserConfig |
| 116 | + |
| 117 | +func (buc *BotUserConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 118 | + var sbuc serializableBUC |
| 119 | + err := unmarshal(&sbuc) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + *buc = (BotUserConfig)(sbuc) |
| 124 | + if buc.Avatar != "" && buc.Avatar != "remove" { |
| 125 | + buc.ParsedAvatar, err = id.ParseContentURI(buc.Avatar) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("%w in bot avatar", err) |
| 128 | + } |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
0 commit comments