registration.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. falseVal := false
  50. registration.RateLimited = &falseVal
  51. registration.SenderLocalpart = config.AppService.Bot.Username
  52. registration.EphemeralEvents = config.AppService.EphemeralEvents
  53. userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$",
  54. config.Bridge.FormatUsername("[0-9]+"),
  55. config.Homeserver.Domain))
  56. if err != nil {
  57. return err
  58. }
  59. registration.Namespaces.RegisterUserIDs(userIDRegex, true)
  60. return nil
  61. }