config.go 2.2 KB

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