eula.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func initEULA(serverFolder string) []ServerConfig {
  11. properties := []ServerConfig{}
  12. b, err := ioutil.ReadFile(serverFolder + "/eula.txt") // just pass the file name
  13. if err != nil {
  14. fmt.Print(err)
  15. }
  16. str := string(b) // convert content to a 'string'
  17. lines := strings.Split(str, "\n")
  18. for _, line := range lines {
  19. regex := regexp.MustCompile("^(.*)=(.*[^=]*)$")
  20. regexSubmatch := regex.FindStringSubmatch(line)
  21. if len(regexSubmatch) > 0 {
  22. item := ServerConfig{
  23. Key: strings.TrimSpace(regexSubmatch[1]),
  24. Value: strings.TrimSpace(regexSubmatch[2]),
  25. }
  26. properties = append(properties, item)
  27. }
  28. }
  29. return properties
  30. }
  31. //ReadEULA should exported.
  32. func (mch *Handler) ReadEULA() bool {
  33. b, _ := strconv.ParseBool(mch.eula[0].Value)
  34. return b
  35. }
  36. //WriteEULA should exported.
  37. func (mch *Handler) WriteEULA(accept bool) bool {
  38. mch.reloadEULA()
  39. mch.eula[0].Value = strconv.FormatBool(accept)
  40. mch.SaveEULA()
  41. return true
  42. }
  43. //SaveEULA is exported function
  44. func (mch *Handler) SaveEULA() bool {
  45. TXT := "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).\n"
  46. TXT += "#" + time.Now().Format("Mon Jan 02 15:04:05 MST 2006") + "\n"
  47. for _, line := range mch.eula {
  48. TXT += line.Key + "=" + line.Value + "\n"
  49. }
  50. err := ioutil.WriteFile(mch.serverFolder+"/eula.txt", []byte(TXT), 0777)
  51. if err != nil {
  52. fmt.Println(err)
  53. }
  54. return true
  55. }
  56. //there is no remove cuz EULA shouldn't be removed.