appservice.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. reg, err := cfg.getRegistration()
  49. if err != nil {
  50. return nil, err
  51. }
  52. appservice.Registration = reg
  53. return appservice, nil
  54. }