init.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. xban = { MP = minetest.get_modpath(minetest.get_current_modname()) }
  2. dofile(xban.MP.."/serialize.lua")
  3. local db = { }
  4. local tempbans = { }
  5. local DEF_SAVE_INTERVAL = 300 -- 5 minutes
  6. local DEF_DB_FILENAME = minetest.get_worldpath().."/xban.db"
  7. local DB_FILENAME = minetest.settings:get("xban.db_filename")
  8. local SAVE_INTERVAL = tonumber(
  9. minetest.settings:get("xban.db_save_interval")) or DEF_SAVE_INTERVAL
  10. if (not DB_FILENAME) or (DB_FILENAME == "") then
  11. DB_FILENAME = DEF_DB_FILENAME
  12. end
  13. local function make_logger(level)
  14. return function(text, ...)
  15. minetest.log(level, "[xban] "..text:format(...))
  16. end
  17. end
  18. local ACTION = make_logger("action")
  19. local WARNING = make_logger("warning")
  20. local unit_to_secs = {
  21. s = 1, m = 60, h = 3600,
  22. D = 86400, W = 604800, M = 2592000, Y = 31104000,
  23. [""] = 1,
  24. }
  25. local function parse_time(t) --> secs
  26. local secs = 0
  27. for num, unit in t:gmatch("(%d+)([smhDWMY]?)") do
  28. secs = secs + (tonumber(num) * (unit_to_secs[unit] or 1))
  29. end
  30. return secs
  31. end
  32. local function concat_keys(t, sep)
  33. local keys = {}
  34. for k, _ in pairs(t) do
  35. keys[#keys + 1] = k
  36. end
  37. return table.concat(keys, sep)
  38. end
  39. function xban.find_entry(player, create) --> entry, index
  40. for index, e in ipairs(db) do
  41. for name in pairs(e.names) do
  42. if name == player then
  43. return e, index
  44. end
  45. end
  46. end
  47. if create then
  48. print(("Created new entry for `%s'"):format(player))
  49. local e = {
  50. names = { [player]=true },
  51. banned = false,
  52. record = { },
  53. }
  54. table.insert(db, e)
  55. return e, #db
  56. end
  57. return nil
  58. end
  59. function xban.get_info(player) --> ip_name_list, banned, last_record
  60. local e = xban.find_entry(player)
  61. if not e then
  62. return nil, "No such entry"
  63. end
  64. return e.names, e.banned, e.record[#e.record]
  65. end
  66. function xban.ban_player(player, source, expires, reason) --> bool, err
  67. if xban.get_whitelist(player) then
  68. return nil, "Player is whitelisted; remove from whitelist first"
  69. end
  70. local e = xban.find_entry(player, true)
  71. if e.banned then
  72. return nil, "Already banned"
  73. end
  74. local rec = {
  75. source = source,
  76. time = os.time(),
  77. expires = expires,
  78. reason = reason,
  79. }
  80. table.insert(e.record, rec)
  81. e.names[player] = true
  82. local pl = minetest.get_player_by_name(player)
  83. if pl then
  84. local ip = minetest.get_player_ip(player)
  85. if ip then
  86. e.names[ip] = true
  87. end
  88. e.last_pos = pl:getpos()
  89. end
  90. e.reason = reason
  91. e.time = rec.time
  92. e.expires = expires
  93. e.banned = true
  94. local msg
  95. local date = (expires and os.date("%c", expires)
  96. or "the end of time")
  97. if expires then
  98. table.insert(tempbans, e)
  99. msg = ("Banned: Expires: %s, Reason: %s"):format(date, reason)
  100. else
  101. msg = ("Banned: Reason: %s"):format(reason)
  102. end
  103. for nm in pairs(e.names) do
  104. minetest.kick_player(nm, msg)
  105. end
  106. ACTION("%s bans %s until %s for reason: %s", source, player,
  107. date, reason)
  108. ACTION("Banned Names/IPs: %s", concat_keys(e.names, ", "))
  109. return true
  110. end
  111. function xban.unban_player(player, source) --> bool, err
  112. local e = xban.find_entry(player)
  113. if not e then
  114. return nil, "No such entry"
  115. end
  116. local rec = {
  117. source = source,
  118. time = os.time(),
  119. reason = "Unbanned",
  120. }
  121. table.insert(e.record, rec)
  122. e.banned = false
  123. e.reason = nil
  124. e.expires = nil
  125. e.time = nil
  126. ACTION("%s unbans %s", source, player)
  127. ACTION("Unbanned Names/IPs: %s", concat_keys(e.names, ", "))
  128. return true
  129. end
  130. function xban.get_whitelist(name_or_ip)
  131. return db.whitelist and db.whitelist[name_or_ip]
  132. end
  133. function xban.remove_whitelist(name_or_ip)
  134. if db.whitelist then
  135. db.whitelist[name_or_ip] = nil
  136. end
  137. end
  138. function xban.add_whitelist(name_or_ip, source)
  139. local wl = db.whitelist
  140. if not wl then
  141. wl = { }
  142. db.whitelist = wl
  143. end
  144. wl[name_or_ip] = {
  145. source=source,
  146. }
  147. return true
  148. end
  149. function xban.get_record(player)
  150. local e = xban.find_entry(player)
  151. if not e then
  152. return nil, ("No entry for `%s'"):format(player)
  153. elseif (not e.record) or (#e.record == 0) then
  154. return nil, ("`%s' has no ban records"):format(player)
  155. end
  156. local record = { }
  157. for _, rec in ipairs(e.record) do
  158. local msg = rec.reason or "No reason given."
  159. if rec.expires then
  160. msg = msg..(", Expires: %s"):format(os.date("%c", e.expires))
  161. end
  162. if rec.source then
  163. msg = msg..", Source: "..rec.source
  164. end
  165. table.insert(record, ("[%s]: %s"):format(os.date("%c", e.time), msg))
  166. end
  167. local last_pos
  168. if e.last_pos then
  169. last_pos = ("User was last seen at %s"):format(
  170. minetest.pos_to_string(e.last_pos))
  171. end
  172. return record, last_pos
  173. end
  174. minetest.register_on_prejoinplayer(function(name, ip)
  175. local wl = db.whitelist or { }
  176. if wl[name] or wl[ip] then return end
  177. local e = xban.find_entry(name) or xban.find_entry(ip)
  178. if not e then return end
  179. if e.banned then
  180. local date = (e.expires and os.date("%c", e.expires)
  181. or "the end of time")
  182. return ("Banned: Expires: %s, Reason: %s"):format(
  183. date, e.reason)
  184. end
  185. end)
  186. minetest.register_on_joinplayer(function(player)
  187. local name = player:get_player_name()
  188. local e = xban.find_entry(name)
  189. local ip = minetest.get_player_ip(name)
  190. if not e then
  191. if ip then
  192. e = xban.find_entry(ip, true)
  193. else
  194. return
  195. end
  196. end
  197. e.names[name] = true
  198. if ip then
  199. e.names[ip] = true
  200. end
  201. e.last_seen = os.time()
  202. end)
  203. minetest.register_chatcommand("xban", {
  204. description = "XBan a player",
  205. params = "<player> <reason>",
  206. privs = { ban=true },
  207. func = function(name, params)
  208. local plname, reason = params:match("(%S+)%s+(.+)")
  209. if not (plname and reason) then
  210. return false, "Usage: /xban <player> <reason>"
  211. end
  212. local ok, e = xban.ban_player(plname, name, nil, reason)
  213. return ok, ok and ("Banned %s."):format(plname) or e
  214. end,
  215. })
  216. minetest.register_chatcommand("xtempban", {
  217. description = "XBan a player temporarily",
  218. params = "<player> <time> <reason>",
  219. privs = { ban=true },
  220. func = function(name, params)
  221. local plname, time, reason = params:match("(%S+)%s+(%S+)%s+(.+)")
  222. if not (plname and time and reason) then
  223. return false, "Usage: /xtempban <player> <time> <reason>"
  224. end
  225. time = parse_time(time)
  226. if time < 60 then
  227. return false, "You must ban for at least 60 seconds."
  228. end
  229. local expires = os.time() + time
  230. local ok, e = xban.ban_player(plname, name, expires, reason)
  231. return ok, (ok and ("Banned %s until %s."):format(
  232. plname, os.date("%c", expires)) or e)
  233. end,
  234. })
  235. minetest.register_chatcommand("xunban", {
  236. description = "XUnBan a player",
  237. params = "<player_or_ip>",
  238. privs = { ban=true },
  239. func = function(name, params)
  240. local plname = params:match("%S+")
  241. if not plname then
  242. minetest.chat_send_player(name,
  243. "Usage: /xunban <player_or_ip>")
  244. return
  245. end
  246. local ok, e = xban.unban_player(plname, name)
  247. return ok, ok and ("Unbanned %s."):format(plname) or e
  248. end,
  249. })
  250. minetest.register_chatcommand("xban_record", {
  251. description = "Show the ban records of a player",
  252. params = "<player_or_ip>",
  253. privs = { ban=true },
  254. func = function(name, params)
  255. local plname = params:match("%S+")
  256. if not plname then
  257. return false, "Usage: /xban_record <player_or_ip>"
  258. end
  259. local record, last_pos = xban.get_record(plname)
  260. if not record then
  261. local err = last_pos
  262. minetest.chat_send_player(name, "[xban] "..err)
  263. return
  264. end
  265. for _, e in ipairs(record) do
  266. minetest.chat_send_player(name, "[xban] "..e)
  267. end
  268. if last_pos then
  269. minetest.chat_send_player(name, "[xban] "..last_pos)
  270. end
  271. return true, "Record listed."
  272. end,
  273. })
  274. minetest.register_chatcommand("xban_wl", {
  275. description = "Manages the whitelist",
  276. params = "(add|del|get) <name_or_ip>",
  277. privs = { ban=true },
  278. func = function(name, params)
  279. local cmd, plname = params:match("%s*(%S+)%s*(%S+)")
  280. if cmd == "add" then
  281. xban.add_whitelist(plname, name)
  282. ACTION("%s adds %s to whitelist", name, plname)
  283. return true, "Added to whitelist: "..plname
  284. elseif cmd == "del" then
  285. xban.remove_whitelist(plname)
  286. ACTION("%s removes %s to whitelist", name, plname)
  287. return true, "Removed from whitelist: "..plname
  288. elseif cmd == "get" then
  289. local e = xban.get_whitelist(plname)
  290. if e then
  291. return true, "Source: "..(e.source or "Unknown")
  292. else
  293. return true, "No whitelist for: "..plname
  294. end
  295. end
  296. end,
  297. })
  298. local function check_temp_bans()
  299. minetest.after(60, check_temp_bans)
  300. local to_rm = { }
  301. local now = os.time()
  302. for i, e in ipairs(tempbans) do
  303. if e.expires and (e.expires <= now) then
  304. table.insert(to_rm, i)
  305. e.banned = false
  306. e.expires = nil
  307. e.reason = nil
  308. e.time = nil
  309. end
  310. end
  311. for _, i in ipairs(to_rm) do
  312. table.remove(tempbans, i)
  313. end
  314. end
  315. local function save_db()
  316. minetest.after(SAVE_INTERVAL, save_db)
  317. local f, e = io.open(DB_FILENAME, "wt")
  318. db.timestamp = os.time()
  319. if f then
  320. local ok, err = f:write(xban.serialize(db))
  321. if not ok then
  322. WARNING("Unable to save database: %s", err)
  323. end
  324. else
  325. WARNING("Unable to save database: %s", e)
  326. end
  327. if f then f:close() end
  328. return
  329. end
  330. local function load_db()
  331. local f, e = io.open(DB_FILENAME, "rt")
  332. if not f then
  333. WARNING("Unable to load database: %s", e)
  334. return
  335. end
  336. local cont = f:read("*a")
  337. if not cont then
  338. WARNING("Unable to load database: %s", "Read failed")
  339. return
  340. end
  341. local t, e2 = minetest.deserialize(cont)
  342. if not t then
  343. WARNING("Unable to load database: %s",
  344. "Deserialization failed: "..(e2 or "unknown error"))
  345. return
  346. end
  347. db = t
  348. tempbans = { }
  349. for _, entry in ipairs(db) do
  350. if entry.banned and entry.expires then
  351. table.insert(tempbans, entry)
  352. end
  353. end
  354. end
  355. minetest.register_chatcommand("xban_cleanup", {
  356. description = "Removes all non-banned entries from the xban db",
  357. privs = { server=true },
  358. func = function(name, params)
  359. local old_count = #db
  360. local i = 1
  361. while i <= #db do
  362. if not db[i].banned then
  363. -- not banned, remove from db
  364. table.remove(db, i)
  365. else
  366. -- banned, hold entry back
  367. i = i + 1
  368. end
  369. end
  370. -- save immediately
  371. save_db()
  372. return true, "Removed " .. (old_count - #db) .. " entries, new db entry-count: " .. #db
  373. end,
  374. })
  375. minetest.register_on_shutdown(save_db)
  376. minetest.after(SAVE_INTERVAL, save_db)
  377. load_db()
  378. xban.db = db
  379. minetest.after(1, check_temp_bans)
  380. dofile(xban.MP.."/dbimport.lua")
  381. dofile(xban.MP.."/gui.lua")