appservice.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. ASToken string `yaml:"as_token"`
  12. HSToken string `yaml:"hs_token"`
  13. }
  14. func (a *appservice) validate() error {
  15. if a.ID == "" {
  16. a.ID = "discord"
  17. }
  18. if a.Address == "" {
  19. a.Address = "http://localhost:29350"
  20. }
  21. if a.Hostname == "" {
  22. a.Hostname = "0.0.0.0"
  23. }
  24. if a.Port == 0 {
  25. a.Port = 29350
  26. }
  27. if err := a.Bot.validate(); err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. func (a *appservice) UnmarshalYAML(unmarshal func(interface{}) error) error {
  33. type rawAppservice appservice
  34. raw := rawAppservice{}
  35. if err := unmarshal(&raw); err != nil {
  36. return err
  37. }
  38. *a = appservice(raw)
  39. return a.validate()
  40. }
  41. func (cfg *Config) CreateAppService() (*as.AppService, error) {
  42. appservice := as.Create()
  43. appservice.HomeserverURL = cfg.Homeserver.Address
  44. appservice.HomeserverDomain = cfg.Homeserver.Domain
  45. appservice.Host.Hostname = cfg.Appservice.Hostname
  46. appservice.Host.Port = cfg.Appservice.Port
  47. appservice.DefaultHTTPRetries = 4
  48. return appservice, nil
  49. }