eula.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.reloadEULA()
  38. mch.eula[0].Value = strconv.FormatBool(accept)
  39. mch.SaveEULA()
  40. return true
  41. }
  42. //SaveEULA is exported function
  43. func (mch *Handler) SaveEULA() bool {
  44. TXT := "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).\n"
  45. for _, line := range mch.eula {
  46. TXT += line.Key + "=" + line.Value + "\n"
  47. }
  48. err := ioutil.WriteFile(mch.serverFolder+"/eula.txt", []byte(TXT), 0777)
  49. if err != nil {
  50. fmt.Println(err)
  51. }
  52. return true
  53. }
  54. //there is no remove cuz EULA shouldn't be removed.