From d47e87e390b018ac5510bb2143eb0e66f6bc2eeb Mon Sep 17 00:00:00 2001
From: Diego Martinez <kaeza@users.sf.net>
Date: Wed, 21 May 2014 07:07:37 -0300
Subject: Allow importing old databases.

Currently:
- Minetest (`ipban.txt')
- xban v1 (`players.iplist')
---
 dbimport.lua           | 37 +++++++++++++++++++++++++++++++++++++
 importers/minetest.lua | 29 +++++++++++++++++++++++++++++
 importers/v1.lua       | 33 +++++++++++++++++++++++++++++++++
 init.lua               |  8 ++++----
 4 files changed, 103 insertions(+), 4 deletions(-)
 create mode 100644 dbimport.lua
 create mode 100644 importers/minetest.lua
 create mode 100644 importers/v1.lua

diff --git a/dbimport.lua b/dbimport.lua
new file mode 100644
index 0000000..22fef27
--- /dev/null
+++ b/dbimport.lua
@@ -0,0 +1,37 @@
+
+xban.importers = { }
+
+dofile(xban.MP.."/importers/minetest.lua")
+dofile(xban.MP.."/importers/v1.lua")
+
+minetest.register_chatcommand("xban_dbi", {
+	description = "Import old databases",
+	params = "<importer>",
+	privs = { server=true },
+	func = function(name, params)
+		if params == "--list" then
+			local names = { }
+			for name in pairs(xban.importers) do
+				table.insert(names, name)
+			end
+			minetest.chat_send_player(name,
+			  ("[xban] Known importers: %s"):format(
+			  table.concat(names, ", ")))
+			return
+		elseif not xban.importers[params] then
+			minetest.chat_send_player(name,
+			  ("[xban] Unknown importer `%s'"):format(params))
+			minetest.chat_send_player(name, "[xban] Try `--list'")
+			return
+		end
+		local f = xban.importers[params]
+		local ok, err = f()
+		if ok then
+			minetest.chat_send_player(name,
+			  "[xban] Import successfull")
+		else
+			minetest.chat_send_player(name,
+			  ("[xban] Import failed: %s"):format(err))
+		end
+	end,
+})
diff --git a/importers/minetest.lua b/importers/minetest.lua
new file mode 100644
index 0000000..e19c3c6
--- /dev/null
+++ b/importers/minetest.lua
@@ -0,0 +1,29 @@
+
+function xban.importers.minetest()
+	local f, e = io.open(minetest.get_worldpath().."/ipban.txt")
+	if not f then
+		return false, "Unable to open `ipban.txt': "..e
+	end
+	for line in f:lines() do
+		local ip, name = line:match("([^|]+)%|(.+)")
+		if ip and name then
+			local entry
+			entry = xban.find_entry(ip, true)
+			entry.banned = true
+			entry.reason = "Banned in `ipban.txt'"
+			entry.names[name] = true
+			entry.names[ip] = true
+			entry.time = os.time()
+			entry.expires = nil
+			entry.source = "xban:importer_minetest"
+			table.insert(entry.record, {
+				source = entry.source,
+				reason = entry.reason,
+				time = entry.time,
+				expires = nil,
+			})
+		end
+	end
+	f:close()
+	return true
+end
diff --git a/importers/v1.lua b/importers/v1.lua
new file mode 100644
index 0000000..a3722c7
--- /dev/null
+++ b/importers/v1.lua
@@ -0,0 +1,33 @@
+
+function xban.importers.v1()
+	local f, e = io.open(minetest.get_worldpath().."/players.iplist")
+	if not f then
+		return false, "Unable to open `players.iplist': "..e
+	end
+	for line in f:lines() do
+		local list = line:split("|")
+		if #list >= 2 then
+			local banned = (list[1]:sub(1, 1) == "!")
+			local entry
+			entry = xban.find_entry(list[1], true)
+			entry.banned = banned
+			for _, name in ipairs(list) do
+				entry.names[name] = true
+			end
+			if banned then
+				entry.reason = "Banned in `players.iplist'"
+				entry.time = os.time()
+				entry.expires = nil
+				entry.source = "xban:importer_v1"
+				table.insert(entry.record, {
+					source = entry.source,
+					reason = entry.reason,
+					time = entry.time,
+					expires = nil,
+				})
+			end
+		end
+	end
+	f:close()
+	return true
+end
diff --git a/init.lua b/init.lua
index f255283..603d060 100644
--- a/init.lua
+++ b/init.lua
@@ -1,9 +1,7 @@
 
-xban = { }
+xban = { MP = minetest.get_modpath(minetest.get_current_modname()) }
 
-local MP = minetest.get_modpath(minetest.get_current_modname())
-
-dofile(MP.."/serialize.lua")
+dofile(xban.MP.."/serialize.lua")
 
 local db = { }
 local tempbans = { }
@@ -262,3 +260,5 @@ minetest.register_on_shutdown(save_db)
 minetest.after(SAVE_INTERVAL, save_db)
 load_db()
 xban.db = db
+
+dofile(xban.MP.."/dbimport.lua")
-- 
cgit v1.2.3