registration.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "strings"
  21. "maunium.net/go/mautrix/appservice"
  22. )
  23. func (config *Config) NewRegistration() (*appservice.Registration, error) {
  24. registration := appservice.CreateRegistration()
  25. err := config.copyToRegistration(registration)
  26. if err != nil {
  27. return nil, err
  28. }
  29. config.AppService.ASToken = registration.AppToken
  30. config.AppService.HSToken = registration.ServerToken
  31. // Workaround for https://github.com/matrix-org/synapse/pull/5758
  32. registration.SenderLocalpart = appservice.RandomString(32)
  33. botRegex := regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
  34. regexp.QuoteMeta(config.AppService.Bot.Username),
  35. regexp.QuoteMeta(config.Homeserver.Domain)))
  36. registration.Namespaces.RegisterUserIDs(botRegex, true)
  37. return registration, nil
  38. }
  39. func (config *Config) GetRegistration() (*appservice.Registration, error) {
  40. registration := appservice.CreateRegistration()
  41. err := config.copyToRegistration(registration)
  42. if err != nil {
  43. return nil, err
  44. }
  45. registration.AppToken = config.AppService.ASToken
  46. registration.ServerToken = config.AppService.HSToken
  47. return registration, nil
  48. }
  49. func (config *Config) copyToRegistration(registration *appservice.Registration) error {
  50. registration.ID = config.AppService.ID
  51. registration.URL = config.AppService.Address
  52. falseVal := false
  53. registration.RateLimited = &falseVal
  54. registration.SenderLocalpart = config.AppService.Bot.Username
  55. registration.EphemeralEvents = config.AppService.EphemeralEvents
  56. usernamePlaceholder := appservice.RandomString(16)
  57. usernameTemplate := fmt.Sprintf("@%s:%s",
  58. config.Bridge.FormatUsername(usernamePlaceholder),
  59. config.Homeserver.Domain)
  60. usernameTemplate = regexp.QuoteMeta(usernameTemplate)
  61. usernameTemplate = strings.Replace(usernameTemplate, usernamePlaceholder, "[0-9]+", 1)
  62. usernameTemplate = fmt.Sprintf("^%s$", usernameTemplate)
  63. userIDRegex, err := regexp.Compile(usernameTemplate)
  64. if err != nil {
  65. return err
  66. }
  67. registration.Namespaces.RegisterUserIDs(userIDRegex, true)
  68. return nil
  69. }