gui.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. "field_close_on_enter[filter;false]",
  108. }
  109. local fsn = #fs
  110. -- Translate internal player names to display names.
  111. local nlist = {}
  112. for k, v in ipairs(list) do
  113. local dn = rename.gpn(v)
  114. local rn = rename.grn(v)
  115. local ts = ac.get_total_suspicion(rn)
  116. if dn ~= rn then
  117. if ts > 0 then
  118. nlist[k] = ESC(dn .. " [" .. rn .. "] (" .. ts .. ")")
  119. else
  120. nlist[k] = ESC(dn .. " [" .. rn .. "]")
  121. end
  122. else
  123. if ts > 0 then
  124. nlist[k] = ESC(rn .. " (" .. ts .. ")")
  125. else
  126. nlist[k] = ESC(rn)
  127. end
  128. end
  129. end
  130. fsn=fsn+1 fs[fsn] = format("textlist[0,1.8;4,8;player;%s;%d;0]",
  131. table.concat(nlist, ","), pli)
  132. local record_name = list[pli]
  133. if record_name then
  134. local e, strings, gotten = get_record_simple(record_name)
  135. for i, r in ipairs(strings) do
  136. strings[i] = ESC(r)
  137. end
  138. -- Element field name changes based on whether we got a real set of ban records.
  139. fsn=fsn+1 fs[fsn] = format(
  140. "textlist[4.2,1.8;11.6,6;" .. (gotten and "entry" or "err") .. ";%s;%d;0]",
  141. table.concat(strings, ","), ei)
  142. local rec = e.record[ei]
  143. if #e.record > 0 then
  144. -- Ensure a valid record is selected.
  145. if not rec then
  146. rec = e.record[1]
  147. state.entry_index = 1
  148. ei = 1
  149. end
  150. fsn=fsn+1 fs[fsn] = format("label[0,10.3;%s]",
  151. ESC("Source: "..(rec.source or "<none>")
  152. .."\nDate: "..os.date("%c", rec.time)
  153. .."\n"..(rec.expires and os.date("Expires: %c", rec.expires) or "")
  154. .."\n"..(e.banned and "Status: Banned!" or "Player is not banned.")),
  155. pli) -- End format.
  156. else
  157. -- No ban records?
  158. fsn=fsn+1 fs[fsn] = format("label[0,10.3;%s]",
  159. ESC("Player <" .. rename.gpn(record_name) .. "> has no ban records.")
  160. ) -- End format.
  161. end
  162. -- Obtain all alternate names/IPs for this record.
  163. local names = {}
  164. local ips = {}
  165. for k, v in pairs(e.names) do
  166. if not k:find("[%.%:]") then
  167. names[#names+1] = rename.gpn(k)
  168. else
  169. ips[#ips+1] = k -- Is an IP address.
  170. end
  171. end
  172. local infomsg = {}
  173. infomsg[#infomsg+1] = "Other names (" .. #names .. "): {"..table.concat(names, ", ").."}"
  174. if #ips <= 5 then
  175. infomsg[#infomsg+1] = "IPs used (" .. #ips .. "): ["..table.concat(ips, " | ").."]"
  176. else
  177. infomsg[#infomsg+1] = "IPs used (" .. #ips .. "): DYNAMIC"
  178. end
  179. -- last_pos and last_seen are per name, not per record-entry.
  180. if type(e.last_pos) == "table" and e.last_pos[record_name] then
  181. infomsg[#infomsg+1] = "User was last seen at " ..
  182. rc.pos_to_namestr(vector_round(e.last_pos[record_name])) .. "."
  183. -- We can also add a button to allow the formspec user to jump to this
  184. -- location.
  185. fsn=fsn+1 fs[fsn] = "button[13,10.3;3,1;jump;Jump To Last Pos]"
  186. end
  187. if type(e.last_seen) == "table" and e.last_seen[record_name] then
  188. infomsg[#infomsg+1] = "Last login: " ..
  189. os.date("!%Y/%m/%d, %H:%M:%S UTC", e.last_seen[record_name]) .. "."
  190. end
  191. if sheriff.is_cheater(record_name) then
  192. infomsg[#infomsg+1] = "Player is a registered cheater/hacker."
  193. elseif sheriff.is_suspected_cheater(record_name) then
  194. infomsg[#infomsg+1] = "Player is a suspected cheater!"
  195. end
  196. for k, v in ipairs(infomsg) do
  197. infomsg[k] = ESC(v)
  198. end
  199. fsn=fsn+1 fs[fsn] = "textlist[4.2,8.0;11.6,1.8;info;"..table.concat(infomsg, ",")..";0]"
  200. else
  201. local e = "No entry matches the query."
  202. fsn=fsn+1 fs[fsn] = "textlist[4.2,1.8;11.6,6;err;"..ESC(e)..";0]"
  203. fsn=fsn+1 fs[fsn] = "textlist[4.2,8.0;11.6,1.8;info;;0]"
  204. fsn=fsn+1 fs[fsn] = "label[0,10.3;"..ESC(e).."]"
  205. end
  206. return table.concat(fs)
  207. end
  208. function xban.gui.on_receive_fields(player, formname, fields)
  209. if formname ~= FORMNAME then return end
  210. local name = player:get_player_name()
  211. if not minetest.check_player_privs(name, { ban=true }) then
  212. minetest.log("warning", "[xban2] Received fields from unauthorized user: " .. name)
  213. return true
  214. end
  215. local state = get_state(name)
  216. if fields.player then
  217. local t = minetest.explode_textlist_event(fields.player)
  218. if (t.type == "CHG") or (t.type == "DCL") then
  219. state.player_index = t.index
  220. minetest.show_formspec(name, FORMNAME, make_fs(name))
  221. end
  222. return true
  223. end
  224. if fields.entry then
  225. local t = minetest.explode_textlist_event(fields.entry)
  226. if (t.type == "CHG") or (t.type == "DCL") then
  227. state.entry_index = t.index
  228. minetest.show_formspec(name, FORMNAME, make_fs(name))
  229. end
  230. return true
  231. end
  232. if fields.key_enter_field == "filter" or fields.search then
  233. local filter = fields.filter or ""
  234. state.filter = filter
  235. state.list = make_list(filter)
  236. minetest.show_formspec(name, FORMNAME, make_fs(name))
  237. end
  238. if fields.jump then
  239. local list = state.list
  240. local pli = state.player_index or 1
  241. if pli > #list then
  242. pli = #list
  243. end
  244. local record_name = list[pli]
  245. if record_name then
  246. local e, strings, gotten = get_record_simple(record_name)
  247. if type(e.last_pos) == "table" and e.last_pos[record_name] then
  248. local pos = vector_round(table.copy(e.last_pos[record_name]))
  249. minetest.chat_send_player(name,
  250. "# Server: Teleporting to <" .. rename.gpn(record_name) ..
  251. ">'s last known exit position at " .. rc.pos_to_namestr(pos) .. ".")
  252. player:set_pos(pos)
  253. rc.notify_realm_update(name, pos)
  254. end
  255. end
  256. end
  257. return true
  258. end
  259. function xban.gui.chatcommand(name, params)
  260. minetest.show_formspec(name, FORMNAME, make_fs(name))
  261. end
  262. if not xban.gui.registered then
  263. minetest.register_on_player_receive_fields(function(...)
  264. return xban.gui.on_receive_fields(...)
  265. end)
  266. minetest.register_chatcommand("xban_gui", {
  267. description = "Show XBan GUI.",
  268. params = "",
  269. privs = { ban=true, },
  270. func = function(...)
  271. return xban.gui.chatcommand(...)
  272. end,
  273. })
  274. xban.gui.registered = true
  275. end