appservice.go 1.4 KB

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