Add a generate-registration command
This commit is contained in:
@@ -5,6 +5,10 @@ type appservice struct {
|
||||
Hostname string `yaml:"hostname"`
|
||||
Port uint16 `yaml:"port"`
|
||||
|
||||
ID string `yaml:"id"`
|
||||
|
||||
Bot bot `yaml:"bot"`
|
||||
|
||||
ASToken string `yaml:"as_token"`
|
||||
HSToken string `yaml:"hs_token"`
|
||||
}
|
||||
|
||||
7
config/bot.go
Normal file
7
config/bot.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
type bot struct {
|
||||
Username string `yaml:"username"`
|
||||
Displayname string `yaml:"displayname"`
|
||||
Avatar string `yaml:"avatar"`
|
||||
}
|
||||
40
config/bridge.go
Normal file
40
config/bridge.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type bridge struct {
|
||||
UsernameTemplate string `yaml:"username_template"`
|
||||
|
||||
usernameTemplate *template.Template `yaml:"-"`
|
||||
}
|
||||
|
||||
func (b *bridge) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
type rawBridge bridge
|
||||
|
||||
raw := rawBridge{}
|
||||
|
||||
err := unmarshal(&raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
raw.usernameTemplate, err = template.New("username").Parse(raw.UsernameTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*b = bridge(raw)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b bridge) FormatUsername(userid string) string {
|
||||
var buffer bytes.Buffer
|
||||
|
||||
b.usernameTemplate.Execute(&buffer, userid)
|
||||
|
||||
return buffer.String()
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
type Config struct {
|
||||
Homeserver homeserver `yaml:"homeserver"`
|
||||
Appservice appservice `yaml:"appservice"`
|
||||
Bridge bridge `yaml:"bridge"`
|
||||
}
|
||||
|
||||
func FromBytes(data []byte) (*Config, error) {
|
||||
@@ -33,3 +34,12 @@ func FromFile(filename string) (*Config, error) {
|
||||
|
||||
return FromBytes(data)
|
||||
}
|
||||
|
||||
func (cfg *Config) Save(filename string) error {
|
||||
data, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(filename, data, 0600)
|
||||
}
|
||||
|
||||
33
config/registration.go
Normal file
33
config/registration.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
as "maunium.net/go/mautrix/appservice"
|
||||
)
|
||||
|
||||
func (cfg *Config) CopyToRegistration(registration *as.Registration) error {
|
||||
registration.ID = cfg.Appservice.ID
|
||||
registration.URL = cfg.Appservice.Address
|
||||
|
||||
falseVal := false
|
||||
registration.RateLimited = &falseVal
|
||||
|
||||
registration.SenderLocalpart = cfg.Appservice.Bot.Username
|
||||
|
||||
pattern := fmt.Sprintf(
|
||||
"^@%s:%s$",
|
||||
cfg.Bridge.FormatUsername("[0-9]+"),
|
||||
cfg.Homeserver.Domain,
|
||||
)
|
||||
|
||||
userIDRegex, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registration.Namespaces.RegisterUserIDs(userIDRegex, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user