registration.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 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. "maunium.net/go/mautrix-appservice"
  19. "regexp"
  20. )
  21. func (config *Config) NewRegistration() (*appservice.Registration, error) {
  22. registration := appservice.CreateRegistration("mautrix-whatsapp")
  23. err := config.copyToRegistration(registration)
  24. if err != nil {
  25. return nil, err
  26. }
  27. config.AppService.ASToken = registration.AppToken
  28. config.AppService.HSToken = registration.ServerToken
  29. return registration, nil
  30. }
  31. func (config *Config) GetRegistration() (*appservice.Registration, error) {
  32. registration := appservice.CreateRegistration("mautrix-whatsapp")
  33. err := config.copyToRegistration(registration)
  34. if err != nil {
  35. return nil, err
  36. }
  37. registration.AppToken = config.AppService.ASToken
  38. registration.ServerToken = config.AppService.HSToken
  39. return registration, nil
  40. }
  41. func (config *Config) copyToRegistration(registration *appservice.Registration) error {
  42. registration.ID = config.AppService.ID
  43. registration.URL = config.AppService.Address
  44. registration.RateLimited = false
  45. registration.SenderLocalpart = config.AppService.Bot.Username
  46. userIDRegex, err := regexp.Compile(config.Bridge.FormatUsername("[0-9]+", "[0-9]+"))
  47. if err != nil {
  48. return err
  49. }
  50. registration.Namespaces.RegisterUserIDs(userIDRegex, true)
  51. return nil
  52. }