registration.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "fmt"
  19. "regexp"
  20. "maunium.net/go/mautrix/appservice"
  21. )
  22. func (config *Config) NewRegistration() (*appservice.Registration, error) {
  23. registration := appservice.CreateRegistration()
  24. err := config.copyToRegistration(registration)
  25. if err != nil {
  26. return nil, err
  27. }
  28. config.AppService.ASToken = registration.AppToken
  29. config.AppService.HSToken = registration.ServerToken
  30. // Workaround for https://github.com/matrix-org/synapse/pull/5758
  31. registration.SenderLocalpart = appservice.RandomString(32)
  32. botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$", config.AppService.Bot.Username, config.Homeserver.Domain))
  33. registration.Namespaces.RegisterUserIDs(botRegex, true)
  34. return registration, nil
  35. }
  36. func (config *Config) GetRegistration() (*appservice.Registration, error) {
  37. registration := appservice.CreateRegistration()
  38. err := config.copyToRegistration(registration)
  39. if err != nil {
  40. return nil, err
  41. }
  42. registration.AppToken = config.AppService.ASToken
  43. registration.ServerToken = config.AppService.HSToken
  44. return registration, nil
  45. }
  46. func (config *Config) copyToRegistration(registration *appservice.Registration) error {
  47. registration.ID = config.AppService.ID
  48. registration.URL = config.AppService.Address
  49. registration.RateLimited = false
  50. registration.SenderLocalpart = config.AppService.Bot.Username
  51. userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$",
  52. config.Bridge.FormatUsername("[0-9]+"),
  53. config.Homeserver.Domain))
  54. if err != nil {
  55. return err
  56. }
  57. registration.Namespaces.RegisterUserIDs(userIDRegex, true)
  58. return nil
  59. }