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.

64 lines
1.2 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. Server struct {
  16. Address string `json:"server"`
  17. SSL bool `json:"ssl"`
  18. }
  19. API struct {
  20. Endpoint string `json:"endpoint"`
  21. Username string `json:"username"`
  22. Key struct {
  23. ID string `json:"id"`
  24. Secret string `json:"secret"`
  25. } `json:"key"`
  26. } `json:"api"`
  27. Commands struct {
  28. OnJoinOp []string `json:"onJoinOp"`
  29. OnReady []string `json:"onReady"`
  30. } `json:"commands"`
  31. Names struct {
  32. ChanServ string `json:"chanserv"`
  33. }
  34. }
  35. var mutex sync.Mutex
  36. var config *Config
  37. // Get lazy-loads the configuration in a thread-safe manner.
  38. func Get() Config {
  39. mutex.Lock()
  40. defer mutex.Unlock()
  41. if config == nil {
  42. config = &Config{}
  43. file, err := os.Open("/etc/aiterp/logbot3.json")
  44. if err != nil {
  45. fmt.Fprintf(os.Stderr, "Failed to load config: %s\n", err)
  46. os.Exit(1)
  47. }
  48. defer file.Close()
  49. err = json.NewDecoder(file).Decode(&config)
  50. if err != nil {
  51. fmt.Fprintf(os.Stderr, "Failed to parse config: %s\n", err)
  52. os.Exit(1)
  53. }
  54. }
  55. return *config
  56. }