gui.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. xban.gui = xban.gui or {}
  2. xban.gui.states = xban.gui.states or {}
  3. -- Localize for performance.
  4. local vector_round = vector.round
  5. local FORMNAME = "xban2:main"
  6. local MAXLISTSIZE = 1000
  7. local strfind, format = string.find, string.format
  8. local ESC = minetest.formspec_escape
  9. -- Get records of all registered players.
  10. local function make_list(filter)
  11. filter = filter and filter:split(",") or {}
  12. local list, dropped = { }, false
  13. -- Trim whitespace from filters.
  14. for k, v in ipairs(filter) do
  15. filter[k] = v:trim()
  16. end
  17. -- If a filter is chosen, only return filtered items.
  18. if #filter > 0 then
  19. for _, data in ipairs(xban.db) do
  20. for name, _ in pairs(data.names) do
  21. if not name:find("[%.%:]") then -- No IP addresses.
  22. for _, fname in ipairs(filter) do
  23. -- Plaintext search.
  24. if fname ~= "" then -- Don't search empty filters.
  25. -- Match real name or alias.
  26. if strfind(name, fname, 1, true) or strfind(rename.gpn(name), fname, 1, true) then
  27. if #list > MAXLISTSIZE then
  28. dropped = true
  29. goto done
  30. end
  31. list[#list+1] = name -- Insert real name.
  32. end
  33. end
  34. end
  35. end
  36. end
  37. end
  38. else
  39. for _, data in ipairs(xban.db) do
  40. for name, _ in pairs(data.names) do
  41. if not name:find("[%.%:]") then -- No IP addresses.
  42. if #list > MAXLISTSIZE then
  43. dropped = true
  44. goto done
  45. end
  46. list[#list+1] = name
  47. end
  48. end
  49. end
  50. end
  51. ::done::
  52. -- If filter has more than one entry, remove duplicates in the list.
  53. if #filter > 1 then
  54. local klist = {}
  55. for k, v in ipairs(list) do
  56. klist[v] = true
  57. end
  58. list = {}
  59. for k, v in pairs(klist) do
  60. list[#list+1] = k
  61. end
  62. end
  63. table.sort(list)
  64. return list, dropped
  65. end
  66. local states = xban.gui.states
  67. local function get_state(name)
  68. local state = states[name]
  69. if not state then
  70. state = { index=1, filter="" }
  71. states[name] = state
  72. state.list, state.dropped = make_list()
  73. end
  74. return state
  75. end
  76. local function get_record_simple(name)
  77. local e = xban.find_entry(name)
  78. if not e then
  79. return nil, {("No entry found for <%s>."):format(rename.gpn(name))}, false
  80. elseif (not e.record) or (#e.record == 0) then
  81. return e, {("Player <%s> has no ban records."):format(rename.gpn(name))}, false
  82. end
  83. local strings = {}
  84. -- Assemble ban record strings.
  85. for _, rec in ipairs(e.record) do
  86. local msg = (os.date("%Y-%m-%d %H:%M:%S", rec.time).." | "
  87. ..(rec.reason or "No reason given."))
  88. table.insert(strings, msg)
  89. end
  90. return e, strings, true
  91. end
  92. local function make_fs(name)
  93. local state = get_state(name)
  94. local list, filter = state.list, state.filter
  95. local pli, ei = state.player_index or 1, state.entry_index or 0
  96. if pli > #list then
  97. pli = #list
  98. end
  99. local fs = {
  100. "size[16,12]",
  101. default.gui_bg,
  102. default.gui_bg_img,
  103. default.gui_slots,
  104. "label[0,0.02;Filter]",
  105. "field[1.5,0.33;12.8,1;filter;;"..ESC(filter).."]",
  106. "button[14,0;2,1;search;Search]",
  107. }
  108. local fsn = #fs
  109. -- Translate internal player names to display names.
  110. local nlist = {}
  111. for k, v in ipairs(list) do
  112. local dn = rename.gpn(v)
  113. local rn = rename.grn(v)
  114. local ts = ac.get_total_suspicion(rn)
  115. if dn ~= rn then
  116. if ts > 0 then
  117. nlist[k] = ESC(dn .. " [" .. rn .. "] (" .. ts .. ")")
  118. else
  119. nlist[k] = ESC(dn .. " [" .. rn .. "]")
  120. end
  121. else
  122. if ts > 0 then
  123. nlist[k] = ESC(rn .. " (" .. ts .. ")")
  124. else
  125. nlist[k] = ESC(rn)
  126. end
  127. end
  128. end
  129. fsn=fsn+1 fs[fsn] = format("textlist[0,1.8;4,8;player;%s;%d;0]",
  130. table.concat(nlist, ","), pli)
  131. local record_name = list[pli]
  132. if record_name then
  133. local e, strings, gotten = get_record_simple(record_name)
  134. for i, r in ipairs(strings) do
  135. strings[i] = ESC(r)
  136. end
  137. -- Element field name changes based on whether we got a real set of ban records.
  138. fsn=fsn+1 fs[fsn] = format(
  139. "textlist[4.2,1.8;11.6,6;" .. (gotten and "entry" or "err") .. ";%s;%d;0]",
  140. table.concat(strings, ","), ei)
  141. local rec = e.record[ei]
  142. if #e.record > 0 then
  143. -- Ensure a valid record is selected.
  144. if not rec then
  145. rec = e.record[1]
  146. state.entry_index = 1
  147. ei = 1
  148. end
  149. fsn=fsn+1 fs[fsn] = format("label[0,10.3;%s]",
  150. ESC("Source: "..(rec.source or "<none>")
  151. .."\nDate: "..os.date("%c", rec.time)
  152. .."\n"..(rec.expires and os.date("Expires: %c", rec.expires) or "")
  153. .."\n"..(e.banned and "Status: Banned!" or "Player is not banned.")),
  154. pli) -- End format.
  155. else
  156. -- No ban records?
  157. fsn=fsn+1 fs[fsn] = format("label[0,10.3;%s]",
  158. ESC("Player <" .. rename.gpn(record_name) .. "> has no ban records.")
  159. ) -- End format.
  160. end
  161. -- Obtain all alternate names/IPs for this record.
  162. local names = {}
  163. local ips = {}
  164. for k, v in pairs(e.names) do
  165. if not k:find("[%.%:]") then
  166. names[#names+1] = rename.gpn(k)
  167. else
  168. ips[#ips+1] = k -- Is an IP address.
  169. end
  170. end
  171. local infomsg = {}
  172. infomsg[#infomsg+1] = "Other names (" .. #names .. "): {"..table.concat(names, ", ").."}"
  173. if #ips <= 5 then
  174. infomsg[#infomsg+1] = "IPs used (" .. #ips .. "): ["..table.concat(ips, " | ").."]"
  175. else
  176. infomsg[#infomsg+1] = "IPs used (" .. #ips .. "): DYNAMIC"
  177. end
  178. -- last_pos and last_seen are per name, not per record-entry.
  179. if type(e.last_pos) == "table" and e.last_pos[record_name] then
  180. infomsg[#infomsg+1] = "User was last seen at " ..
  181. rc.pos_to_namestr(vector_round(e.last_pos[record_name])) .. "."
  182. -- We can also add a button to allow the formspec user to jump to this
  183. -- location.
  184. fsn=fsn+1 fs[fsn] = "button[13,10.3;3,1;jump;Jump To Last Pos]"
  185. end
  186. if type(e.last_seen) == "table" and e.last_seen[record_name] then
  187. infomsg[#infomsg+1] = "Last login: " ..
  188. os.date("!%Y/%m/%d, %H:%M:%S UTC", e.last_seen[record_name]) .. "."
  189. end
  190. if sheriff.is_cheater(record_name) then
  191. infomsg[#infomsg+1] = "Player is a registered cheater/hacker."
  192. elseif sheriff.is_suspected_cheater(record_name) then
  193. infomsg[#infomsg+1] = "Player is a suspected cheater!"
  194. end
  195. for k, v in ipairs(infomsg) do
  196. infomsg[k] = ESC(v)
  197. end
  198. fsn=fsn+1 fs[fsn] = "textlist[4.2,8.0;11.6,1.8;info;"..table.concat(infomsg, ",")..";0]"
  199. else
  200. local e = "No entry matches the query."
  201. fsn=fsn+1 fs[fsn] = "textlist[4.2,1.8;11.6,6;err;"..ESC(e)..";0]"
  202. fsn=fsn+1 fs[fsn] = "textlist[4.2,8.0;11.6,1.8;info;;0]"
  203. fsn=fsn+1 fs[fsn] = "label[0,10.3;"..ESC(e).."]"
  204. end
  205. return table.concat(fs)
  206. end
  207. function xban.gui.on_receive_fields(player, formname, fields)
  208. if formname ~= FORMNAME then return end
  209. local name = player:get_player_name()
  210. if not minetest.check_player_privs(name, { ban=true }) then
  211. minetest.log("warning", "[xban2] Received fields from unauthorized user: " .. name)
  212. return true
  213. end
  214. local state = get_state(name)
  215. if fields.player then
  216. local t = minetest.explode_textlist_event(fields.player)
  217. if (t.type == "CHG") or (t.type == "DCL") then
  218. state.player_index = t.index
  219. minetest.show_formspec(name, FORMNAME, make_fs(name))
  220. end
  221. return true
  222. end
  223. if fields.entry then
  224. local t = minetest.explode_textlist_event(fields.entry)
  225. if (t.type == "CHG") or (t.type == "DCL") then
  226. state.entry_index = t.index
  227. minetest.show_formspec(name, FORMNAME, make_fs(name))
  228. end
  229. return true
  230. end
  231. if fields.search then
  232. local filter = fields.filter or ""
  233. state.filter = filter
  234. state.list = make_list(filter)
  235. minetest.show_formspec(name, FORMNAME, make_fs(name))
  236. end
  237. if fields.jump then
  238. local list = state.list
  239. local pli = state.player_index or 1
  240. if pli > #list then
  241. pli = #list
  242. end
  243. local record_name = list[pli]
  244. if record_name then
  245. local e, strings, gotten = get_record_simple(record_name)
  246. if type(e.last_pos) == "table" and e.last_pos[record_name] then
  247. local pos = vector_round(table.copy(e.last_pos[record_name]))
  248. minetest.chat_send_player(name,
  249. "# Server: Teleporting to <" .. rename.gpn(record_name) ..
  250. ">'s last known exit position at " .. rc.pos_to_namestr(pos) .. ".")
  251. player:set_pos(pos)
  252. rc.notify_realm_update(name, pos)
  253. end
  254. end
  255. end
  256. return true
  257. end
  258. function xban.gui.chatcommand(name, params)
  259. minetest.show_formspec(name, FORMNAME, make_fs(name))
  260. end
  261. if not xban.gui.registered then
  262. minetest.register_on_player_receive_fields(function(...)
  263. return xban.gui.on_receive_fields(...)
  264. end)
  265. minetest.register_chatcommand("xban_gui", {
  266. description = "Show XBan GUI.",
  267. params = "",
  268. privs = { ban=true, },
  269. func = function(...)
  270. return xban.gui.chatcommand(...)
  271. end,
  272. })
  273. xban.gui.registered = true
  274. end