gui.lua 8.3 KB

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