cmd.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package registration
  2. import (
  3. "fmt"
  4. "regexp"
  5. "maunium.net/go/mautrix/appservice"
  6. "gitlab.com/beeper/discord/config"
  7. "gitlab.com/beeper/discord/globals"
  8. )
  9. type Cmd struct {
  10. Filename string `kong:"flag,help='The filename to store the registration into',name='REGISTRATION',short='r',default='registration.yaml'"`
  11. }
  12. func (c *Cmd) Run(g *globals.Globals) error {
  13. cfg, err := config.FromFile(g.Config)
  14. if err != nil {
  15. return err
  16. }
  17. registration := appservice.CreateRegistration()
  18. // Load existing values from the config into the registration.
  19. if err := cfg.CopyToRegistration(registration); err != nil {
  20. return err
  21. }
  22. // Save the new App and Server tokens in the config.
  23. cfg.Appservice.ASToken = registration.AppToken
  24. cfg.Appservice.HSToken = registration.ServerToken
  25. // Workaround for https://github.com/matrix-org/synapse/pull/5758
  26. registration.SenderLocalpart = appservice.RandomString(32)
  27. // Register the bot's user.
  28. pattern := fmt.Sprintf(
  29. "^@%s:%s$",
  30. cfg.Appservice.Bot.Username,
  31. cfg.Homeserver.Domain,
  32. )
  33. botRegex, err := regexp.Compile(pattern)
  34. if err != nil {
  35. return err
  36. }
  37. registration.Namespaces.RegisterUserIDs(botRegex, true)
  38. // Finally save the registration and the updated config file.
  39. if err := registration.Save(c.Filename); err != nil {
  40. return err
  41. }
  42. if err := cfg.Save(g.Config); err != nil {
  43. return err
  44. }
  45. return nil
  46. }