config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // mautrix-whatsapp - A Matrix-Whatsapp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "maunium.net/go/mautrix-appservice"
  19. "io/ioutil"
  20. "gopkg.in/yaml.v2"
  21. )
  22. type Config struct {
  23. Homeserver struct {
  24. Address string `yaml:"address"`
  25. Domain string `yaml:"domain"`
  26. } `yaml:"homeserver"`
  27. AppService struct {
  28. Address string `yaml:"address"`
  29. Hostname string `yaml:"hostname"`
  30. Port uint16 `yaml:"port"`
  31. Database string `yaml:"database"`
  32. ID string `yaml:"id"`
  33. Bot struct {
  34. Username string `yaml:"username"`
  35. Displayname string `yaml:"displayname"`
  36. Avatar string `yaml:"avatar"`
  37. } `yaml:"bot"`
  38. ASToken string `yaml:"as_token"`
  39. HSToken string `yaml:"hs_token"`
  40. } `yaml:"appservice"`
  41. Bridge BridgeConfig `yaml:"bridge"`
  42. Logging appservice.LogConfig `yaml:"logging"`
  43. }
  44. func Load(path string) (*Config, error) {
  45. data, err := ioutil.ReadFile(path)
  46. if err != nil {
  47. return nil, err
  48. }
  49. var config = &Config{}
  50. err = yaml.Unmarshal(data, config)
  51. return config, err
  52. }
  53. func (config *Config) Save(path string) error {
  54. data, err := yaml.Marshal(config)
  55. if err != nil {
  56. return err
  57. }
  58. return ioutil.WriteFile(path, data, 0600)
  59. }
  60. func (config *Config) Appservice() (*appservice.Config, error) {
  61. as := appservice.Create()
  62. as.LogConfig = config.Logging
  63. as.HomeserverDomain = config.Homeserver.Domain
  64. as.HomeserverURL = config.Homeserver.Address
  65. var err error
  66. as.Registration, err = config.GetRegistration()
  67. return as, err
  68. }