serverlistmgr.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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(folder)
  84. local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM
  85. if folder then
  86. return base
  87. end
  88. return base .. core.settings:get("serverlist_file")
  89. end
  90. --------------------------------------------------------------------------------
  91. local function save_favorites(favorites)
  92. local filename = core.settings:get("serverlist_file")
  93. -- If setting specifies legacy format change the filename to the new one
  94. if filename:sub(#filename - 3):lower() == ".txt" then
  95. core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json")
  96. end
  97. assert(core.create_dir(get_favorites_path(true)))
  98. core.safe_file_write(get_favorites_path(), core.write_json(favorites))
  99. end
  100. --------------------------------------------------------------------------------
  101. function serverlistmgr.read_legacy_favorites(path)
  102. local file = io.open(path, "r")
  103. if not file then
  104. return nil
  105. end
  106. local lines = {}
  107. for line in file:lines() do
  108. lines[#lines + 1] = line
  109. end
  110. file:close()
  111. local favorites = {}
  112. local i = 1
  113. while i < #lines do
  114. local function pop()
  115. local line = lines[i]
  116. i = i + 1
  117. return line and line:trim()
  118. end
  119. if pop():lower() == "[server]" then
  120. local name = pop()
  121. local address = pop()
  122. local port = tonumber(pop())
  123. local description = pop()
  124. if name == "" then
  125. name = nil
  126. end
  127. if description == "" then
  128. description = nil
  129. end
  130. if not address or #address < 3 then
  131. core.log("warning", "Malformed favorites file, missing address at line " .. i)
  132. elseif not port or port < 1 or port > 65535 then
  133. core.log("warning", "Malformed favorites file, missing port at line " .. i)
  134. elseif (name and name:upper() == "[SERVER]") or
  135. (address and address:upper() == "[SERVER]") or
  136. (description and description:upper() == "[SERVER]") then
  137. core.log("warning", "Potentially malformed favorites file, overran at line " .. i)
  138. else
  139. favorites[#favorites + 1] = {
  140. name = name,
  141. address = address,
  142. port = port,
  143. description = description
  144. }
  145. end
  146. end
  147. end
  148. return favorites
  149. end
  150. --------------------------------------------------------------------------------
  151. local function read_favorites()
  152. local path = get_favorites_path()
  153. -- If new format configured fall back to reading the legacy file
  154. if path:sub(#path - 4):lower() == ".json" then
  155. local file = io.open(path, "r")
  156. if file then
  157. local json = file:read("*all")
  158. file:close()
  159. return core.parse_json(json)
  160. end
  161. path = path:sub(1, #path - 5) .. ".txt"
  162. end
  163. local favs = serverlistmgr.read_legacy_favorites(path)
  164. if favs then
  165. save_favorites(favs)
  166. os.remove(path)
  167. end
  168. return favs
  169. end
  170. --------------------------------------------------------------------------------
  171. local function delete_favorite(favorites, del_favorite)
  172. for i=1, #favorites do
  173. local fav = favorites[i]
  174. if fav.address == del_favorite.address and fav.port == del_favorite.port then
  175. table.remove(favorites, i)
  176. return
  177. end
  178. end
  179. end
  180. --------------------------------------------------------------------------------
  181. function serverlistmgr.get_favorites()
  182. if serverlistmgr.favorites then
  183. return serverlistmgr.favorites
  184. end
  185. serverlistmgr.favorites = {}
  186. -- Add favorites, removing duplicates
  187. local seen = {}
  188. for _, fav in ipairs(read_favorites() or {}) do
  189. local key = ("%s:%d"):format(fav.address:lower(), fav.port)
  190. if not seen[key] then
  191. seen[key] = true
  192. serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav
  193. end
  194. end
  195. return serverlistmgr.favorites
  196. end
  197. --------------------------------------------------------------------------------
  198. function serverlistmgr.add_favorite(new_favorite)
  199. assert(type(new_favorite.port) == "number")
  200. -- Whitelist favorite keys
  201. new_favorite = {
  202. name = new_favorite.name,
  203. address = new_favorite.address,
  204. port = new_favorite.port,
  205. description = new_favorite.description,
  206. }
  207. local favorites = serverlistmgr.get_favorites()
  208. delete_favorite(favorites, new_favorite)
  209. table.insert(favorites, 1, new_favorite)
  210. save_favorites(favorites)
  211. end
  212. --------------------------------------------------------------------------------
  213. function serverlistmgr.delete_favorite(del_favorite)
  214. local favorites = serverlistmgr.get_favorites()
  215. delete_favorite(favorites, del_favorite)
  216. save_favorites(favorites)
  217. end