appservice.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package config
  2. import (
  3. as "maunium.net/go/mautrix/appservice"
  4. )
  5. type appservice struct {
  6. Address string `yaml:"address"`
  7. Hostname string `yaml:"hostname"`
  8. Port uint16 `yaml:"port"`
  9. ID string `yaml:"id"`
  10. Bot bot `yaml:"bot"`
  11. Provisioning provisioning `yaml:"provisioning"`
  12. Database database `yaml:"database"`
  13. ASToken string `yaml:"as_token"`
  14. HSToken string `yaml:"hs_token"`
  15. }
  16. func (a *appservice) validate() error {
  17. if a.ID == "" {
  18. a.ID = "discord"
  19. }
  20. if a.Address == "" {
  21. a.Address = "http://localhost:29350"
  22. }
  23. if a.Hostname == "" {
  24. a.Hostname = "0.0.0.0"
  25. }
  26. if a.Port == 0 {
  27. a.Port = 29350
  28. }
  29. if err := a.Database.validate(); err != nil {
  30. return err
  31. }
  32. if err := a.Bot.validate(); err != nil {
  33. return err
  34. }
  35. return nil
  36. }
  37. func (a *appservice) UnmarshalYAML(unmarshal func(interface{}) error) error {
  38. type rawAppservice appservice
  39. raw := rawAppservice{}
  40. if err := unmarshal(&raw); err != nil {
  41. return err
  42. }
  43. *a = appservice(raw)
  44. return a.validate()
  45. }
  46. func (cfg *Config) CreateAppService() (*as.AppService, error) {
  47. appservice := as.Create()
  48. appservice.HomeserverURL = cfg.Homeserver.Address
  49. appservice.HomeserverDomain = cfg.Homeserver.Domain
  50. appservice.Host.Hostname = cfg.Appservice.Hostname
  51. appservice.Host.Port = cfg.Appservice.Port
  52. appservice.DefaultHTTPRetries = 4
  53. reg, err := cfg.getRegistration()
  54. if err != nil {
  55. return nil, err
  56. }
  57. appservice.Registration = reg
  58. return appservice, nil
  59. }