appservice.go 1.5 KB

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