config.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 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. "io/ioutil"
  19. "gopkg.in/yaml.v2"
  20. "maunium.net/go/mautrix-appservice"
  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 struct {
  32. Type string `yaml:"type"`
  33. URI string `yaml:"uri"`
  34. } `yaml:"database"`
  35. StateStore string `yaml:"state_store_path"`
  36. ID string `yaml:"id"`
  37. Bot struct {
  38. Username string `yaml:"username"`
  39. Displayname string `yaml:"displayname"`
  40. Avatar string `yaml:"avatar"`
  41. } `yaml:"bot"`
  42. ASToken string `yaml:"as_token"`
  43. HSToken string `yaml:"hs_token"`
  44. } `yaml:"appservice"`
  45. Bridge BridgeConfig `yaml:"bridge"`
  46. Logging appservice.LogConfig `yaml:"logging"`
  47. }
  48. func Load(path string) (*Config, error) {
  49. data, err := ioutil.ReadFile(path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. var config = &Config{}
  54. err = yaml.Unmarshal(data, config)
  55. return config, err
  56. }
  57. func (config *Config) Save(path string) error {
  58. data, err := yaml.Marshal(config)
  59. if err != nil {
  60. return err
  61. }
  62. return ioutil.WriteFile(path, data, 0600)
  63. }
  64. func (config *Config) MakeAppService() (*appservice.AppService, error) {
  65. as := appservice.Create()
  66. as.HomeserverDomain = config.Homeserver.Domain
  67. as.HomeserverURL = config.Homeserver.Address
  68. as.Host.Hostname = config.AppService.Hostname
  69. as.Host.Port = config.AppService.Port
  70. var err error
  71. as.Registration, err = config.GetRegistration()
  72. return as, err
  73. }