eula.go 1.5 KB

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