The new logbot, not committed from the wrong terminal window this time.
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.

68 lines
1.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "sync"
  7. )
  8. // Config represents the logbot's configuration.
  9. type Config struct {
  10. Bot struct {
  11. Nicks []string `json:"nicks"`
  12. User string `json:"user"`
  13. RealName string `json:"realName"`
  14. } `json:"bot"`
  15. Debug struct {
  16. Enabled bool `json:"enabled"`
  17. Listen string `json:"listen"`
  18. } `json:"debug"`
  19. Server struct {
  20. Address string `json:"server"`
  21. SSL bool `json:"ssl"`
  22. } `json:"server"`
  23. API struct {
  24. Endpoint string `json:"endpoint"`
  25. Username string `json:"username"`
  26. Key struct {
  27. ID string `json:"id"`
  28. Secret string `json:"secret"`
  29. } `json:"key"`
  30. } `json:"api"`
  31. Commands struct {
  32. OnJoinOp []string `json:"onJoinOp"`
  33. OnReady []string `json:"onReady"`
  34. } `json:"commands"`
  35. Names struct {
  36. ChanServ string `json:"chanserv"`
  37. } `json:"names"`
  38. }
  39. var mutex sync.Mutex
  40. var config *Config
  41. // Get lazy-loads the configuration in a thread-safe manner.
  42. func Get() Config {
  43. mutex.Lock()
  44. defer mutex.Unlock()
  45. if config == nil {
  46. config = &Config{}
  47. file, err := os.Open("/etc/aiterp/logbot3.json")
  48. if err != nil {
  49. fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
  50. os.Exit(1)
  51. }
  52. defer file.Close()
  53. err = json.NewDecoder(file).Decode(&config)
  54. if err != nil {
  55. fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
  56. os.Exit(1)
  57. }
  58. }
  59. return *config
  60. }