serverlistmgr.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. --Minetest
  2. --Copyright (C) 2020 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. serverlistmgr = {}
  18. --------------------------------------------------------------------------------
  19. local function order_server_list(list)
  20. local res = {}
  21. --orders the favorite list after support
  22. for i = 1, #list do
  23. local fav = list[i]
  24. if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
  25. res[#res + 1] = fav
  26. end
  27. end
  28. for i = 1, #list do
  29. local fav = list[i]
  30. if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
  31. res[#res + 1] = fav
  32. end
  33. end
  34. return res
  35. end
  36. local public_downloading = false
  37. --------------------------------------------------------------------------------
  38. function serverlistmgr.sync()
  39. if not serverlistmgr.servers then
  40. serverlistmgr.servers = {{
  41. name = fgettext("Loading..."),
  42. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  43. }}
  44. end
  45. local serverlist_url = core.settings:get("serverlist_url") or ""
  46. if not core.get_http_api or serverlist_url == "" then
  47. serverlistmgr.servers = {{
  48. name = fgettext("Public server list is disabled"),
  49. description = ""
  50. }}
  51. return
  52. end
  53. if public_downloading then
  54. return
  55. end
  56. public_downloading = true
  57. core.handle_async(
  58. function(param)
  59. local http = core.get_http_api()
  60. local url = ("%s/list?proto_version_min=%d&proto_version_max=%d"):format(
  61. core.settings:get("serverlist_url"),
  62. core.get_min_supp_proto(),
  63. core.get_max_supp_proto())
  64. local response = http.fetch_sync({ url = url })
  65. if not response.succeeded then
  66. return {}
  67. end
  68. local retval = core.parse_json(response.data)
  69. return retval and retval.list or {}
  70. end,
  71. nil,
  72. function(result)
  73. public_downloading = nil
  74. local favs = order_server_list(result)
  75. if favs[1] then
  76. serverlistmgr.servers = favs
  77. end
  78. core.event_handler("Refresh")
  79. end
  80. )
  81. end
  82. --------------------------------------------------------------------------------
  83. local function get_favorites_path()
  84. local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM
  85. return base .. core.settings:get("serverlist_file")
  86. end
  87. --------------------------------------------------------------------------------
  88. local function save_favorites(favorites)
  89. local filename = core.settings:get("serverlist_file")
  90. -- If setting specifies legacy format change the filename to the new one
  91. if filename:sub(#filename - 3):lower() == ".txt" then
  92. core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json")
  93. end
  94. local path = get_favorites_path()
  95. core.create_dir(path)
  96. core.safe_file_write(path, core.write_json(favorites))
  97. end
  98. --------------------------------------------------------------------------------
  99. function serverlistmgr.read_legacy_favorites(path)
  100. local file = io.open(path, "r")
  101. if not file then
  102. return nil
  103. end
  104. local lines = {}
  105. for line in file:lines() do
  106. lines[#lines + 1] = line
  107. end
  108. file:close()
  109. local favorites = {}
  110. local i = 1
  111. while i < #lines do
  112. local function pop()
  113. local line = lines[i]
  114. i = i + 1
  115. return line and line:trim()
  116. end
  117. if pop():lower() == "[server]" then
  118. local name = pop()
  119. local address = pop()
  120. local port = tonumber(pop())
  121. local description = pop()
  122. if name == "" then
  123. name = nil
  124. end
  125. if description == "" then
  126. description = nil
  127. end
  128. if not address or #address < 3 then
  129. core.log("warning", "Malformed favorites file, missing address at line " .. i)
  130. elseif not port or port < 1 or port > 65535 then
  131. core.log("warning", "Malformed favorites file, missing port at line " .. i)
  132. elseif (name and name:upper() == "[SERVER]") or
  133. (address and address:upper() == "[SERVER]") or
  134. (description and description:upper() == "[SERVER]") then
  135. core.log("warning", "Potentially malformed favorites file, overran at line " .. i)
  136. else
  137. favorites[#favorites + 1] = {
  138. name = name,
  139. address = address,
  140. port = port,
  141. description = description
  142. }
  143. end
  144. end
  145. end
  146. return favorites
  147. end
  148. --------------------------------------------------------------------------------
  149. local function read_favorites()
  150. local path = get_favorites_path()
  151. -- If new format configured fall back to reading the legacy file
  152. if path:sub(#path - 4):lower() == ".json" then
  153. local file = io.open(path, "r")
  154. if file then
  155. local json = file:read("*all")
  156. file:close()
  157. return core.parse_json(json)
  158. end
  159. path = path:sub(1, #path - 5) .. ".txt"
  160. end
  161. local favs = serverlistmgr.read_legacy_favorites(path)
  162. if favs then
  163. save_favorites(favs)
  164. os.remove(path)
  165. end
  166. return favs
  167. end
  168. --------------------------------------------------------------------------------
  169. local function delete_favorite(favorites, del_favorite)
  170. for i=1, #favorites do
  171. local fav = favorites[i]
  172. if fav.address == del_favorite.address and fav.port == del_favorite.port then
  173. table.remove(favorites, i)
  174. return
  175. end
  176. end
  177. end
  178. --------------------------------------------------------------------------------
  179. function serverlistmgr.get_favorites()
  180. if serverlistmgr.favorites then
  181. return serverlistmgr.favorites
  182. end
  183. serverlistmgr.favorites = {}
  184. -- Add favorites, removing duplicates
  185. local seen = {}
  186. for _, fav in ipairs(read_favorites() or {}) do
  187. local key = ("%s:%d"):format(fav.address:lower(), fav.port)
  188. if not seen[key] then
  189. seen[key] = true
  190. serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav
  191. end
  192. end
  193. return serverlistmgr.favorites
  194. end
  195. --------------------------------------------------------------------------------
  196. function serverlistmgr.add_favorite(new_favorite)
  197. assert(type(new_favorite.port) == "number")
  198. -- Whitelist favorite keys
  199. new_favorite = {
  200. name = new_favorite.name,
  201. address = new_favorite.address,
  202. port = new_favorite.port,
  203. description = new_favorite.description,
  204. }
  205. local favorites = serverlistmgr.get_favorites()
  206. delete_favorite(favorites, new_favorite)
  207. table.insert(favorites, 1, new_favorite)
  208. save_favorites(favorites)
  209. end
  210. --------------------------------------------------------------------------------
  211. function serverlistmgr.delete_favorite(del_favorite)
  212. local favorites = serverlistmgr.get_favorites()
  213. delete_favorite(favorites, del_favorite)
  214. save_favorites(favorites)
  215. end