You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package irc
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "gopkg.in/yaml.v2"
  7. )
  8. type PassConfig struct {
  9. Password string
  10. }
  11. func (conf *PassConfig) PasswordBytes() []byte {
  12. bytes, err := DecodePassword(conf.Password)
  13. if err != nil {
  14. log.Fatal("decode password error: ", err)
  15. }
  16. return bytes
  17. }
  18. type Config struct {
  19. Network struct {
  20. Name string
  21. }
  22. Server struct {
  23. PassConfig
  24. Name string
  25. Database string
  26. Listen []string
  27. Wslisten string
  28. Log string
  29. MOTD string
  30. }
  31. Operator map[string]*PassConfig
  32. Theater map[string]*PassConfig
  33. }
  34. func (conf *Config) Operators() map[Name][]byte {
  35. operators := make(map[Name][]byte)
  36. for name, opConf := range conf.Operator {
  37. operators[NewName(name)] = opConf.PasswordBytes()
  38. }
  39. return operators
  40. }
  41. func (conf *Config) Theaters() map[Name][]byte {
  42. theaters := make(map[Name][]byte)
  43. for s, theaterConf := range conf.Theater {
  44. name := NewName(s)
  45. if !name.IsChannel() {
  46. log.Fatal("config uses a non-channel for a theater!")
  47. }
  48. theaters[name] = theaterConf.PasswordBytes()
  49. }
  50. return theaters
  51. }
  52. func LoadConfig(filename string) (config *Config, err error) {
  53. data, err := ioutil.ReadFile(filename)
  54. if err != nil {
  55. return nil, err
  56. }
  57. err = yaml.Unmarshal(data, &config)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if config.Network.Name == "" {
  62. return nil, errors.New("Network name missing")
  63. }
  64. if config.Server.Name == "" {
  65. return nil, errors.New("Server name missing")
  66. }
  67. if config.Server.Database == "" {
  68. return nil, errors.New("Server database missing")
  69. }
  70. if len(config.Server.Listen) == 0 {
  71. return nil, errors.New("Server listening addresses missing")
  72. }
  73. return config, nil
  74. }