registration.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package appservice
  2. import (
  3. "io/ioutil"
  4. "regexp"
  5. "gopkg.in/yaml.v2"
  6. )
  7. // Registration contains the data in a Matrix appservice registration.
  8. // See https://matrix.org/docs/spec/application_service/unstable.html#registration
  9. type Registration struct {
  10. ID string `yaml:"id"`
  11. URL string `yaml:"url"`
  12. AppToken string `yaml:"as_token"`
  13. ServerToken string `yaml:"hs_token"`
  14. SenderLocalpart string `yaml:"sender_localpart"`
  15. RateLimited bool `yaml:"rate_limited"`
  16. Namespaces Namespaces `yaml:"namespaces"`
  17. }
  18. // CreateRegistration creates a Registration with random appservice and homeserver tokens.
  19. func CreateRegistration() *Registration {
  20. return &Registration{
  21. AppToken: RandomString(64),
  22. ServerToken: RandomString(64),
  23. }
  24. }
  25. // LoadRegistration loads a YAML file and turns it into a Registration.
  26. func LoadRegistration(path string) (*Registration, error) {
  27. data, err := ioutil.ReadFile(path)
  28. if err != nil {
  29. return nil, err
  30. }
  31. reg := &Registration{}
  32. err = yaml.Unmarshal(data, reg)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return reg, nil
  37. }
  38. // Save saves this Registration into a file at the given path.
  39. func (reg *Registration) Save(path string) error {
  40. data, err := yaml.Marshal(reg)
  41. if err != nil {
  42. return err
  43. }
  44. return ioutil.WriteFile(path, data, 0600)
  45. }
  46. // YAML returns the registration in YAML format.
  47. func (reg *Registration) YAML() (string, error) {
  48. data, err := yaml.Marshal(reg)
  49. if err != nil {
  50. return "", err
  51. }
  52. return string(data), nil
  53. }
  54. // Namespaces contains the three areas that appservices can reserve parts of.
  55. type Namespaces struct {
  56. UserIDs []Namespace `yaml:"users,omitempty"`
  57. RoomAliases []Namespace `yaml:"aliases,omitempty"`
  58. RoomIDs []Namespace `yaml:"rooms,omitempty"`
  59. }
  60. // Namespace is a reserved namespace in any area.
  61. type Namespace struct {
  62. Regex string `yaml:"regex"`
  63. Exclusive bool `yaml:"exclusive"`
  64. }
  65. // RegisterUserIDs creates an user ID namespace registration.
  66. func (nslist *Namespaces) RegisterUserIDs(regex *regexp.Regexp, exclusive bool) {
  67. nslist.UserIDs = append(nslist.UserIDs, Namespace{
  68. Regex: regex.String(),
  69. Exclusive: exclusive,
  70. })
  71. }
  72. // RegisterRoomAliases creates an room alias namespace registration.
  73. func (nslist *Namespaces) RegisterRoomAliases(regex *regexp.Regexp, exclusive bool) {
  74. nslist.RoomAliases = append(nslist.RoomAliases, Namespace{
  75. Regex: regex.String(),
  76. Exclusive: exclusive,
  77. })
  78. }
  79. // RegisterRoomIDs creates an room ID namespace registration.
  80. func (nslist *Namespaces) RegisterRoomIDs(regex *regexp.Regexp, exclusive bool) {
  81. nslist.RoomIDs = append(nslist.RoomIDs, Namespace{
  82. Regex: regex.String(),
  83. Exclusive: exclusive,
  84. })
  85. }