Fixed compatibility with older auth.txts, changed variable name, and changed how the last_login time is set

This commit is contained in:
Ryan Newell 2014-10-20 13:26:30 -07:00
parent 2d01df9b45
commit 02d10842fe
5 changed files with 33 additions and 32 deletions

View File

@ -32,6 +32,25 @@ assert(core.privs_to_string({a=true,b=true}) == "a,b")
core.auth_file_path = core.get_worldpath().."/auth.txt" core.auth_file_path = core.get_worldpath().."/auth.txt"
core.auth_table = {} core.auth_table = {}
local function split(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s~= 1 or cap ~= "" then
table.insert(t, cap)
end
last_end = e + 1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local function read_auth_file() local function read_auth_file()
local newtable = {} local newtable = {}
local file, errmsg = io.open(core.auth_file_path, 'rb') local file, errmsg = io.open(core.auth_file_path, 'rb')
@ -41,12 +60,17 @@ local function read_auth_file()
end end
for line in file:lines() do for line in file:lines() do
if line ~= "" then if line ~= "" then
local name, password, privilegestring, lastlogin = string.match(line, "([^:]*):([^:]*):([^:]*):([^:]*)") --local name, password, privilegestring, last_login = string.match(line, "([^:]*):([^:]*):([^:]*):([^:]*)")
if not name or not password or not privilegestring or not lastlogin then local t = split(line, ":")
local name, password, privilegestring, last_login = unpack(t)
if not last_login then
last_login = os.time()
end
if not name or not password or not privilegestring then
error("Invalid line in auth.txt: "..dump(line)) error("Invalid line in auth.txt: "..dump(line))
end end
local privileges = core.string_to_privs(privilegestring) local privileges = core.string_to_privs(privilegestring)
newtable[name] = {password=password, privileges=privileges, lastlogin=lastlogin} newtable[name] = {password=password, privileges=privileges, last_login=last_login}
end end
end end
io.close(file) io.close(file)
@ -63,7 +87,7 @@ local function save_auth_file()
assert(type(stuff) == "table") assert(type(stuff) == "table")
assert(type(stuff.password) == "string") assert(type(stuff.password) == "string")
assert(type(stuff.privileges) == "table") assert(type(stuff.privileges) == "table")
assert(type(stuff.lastlogin) == "number") assert(type(stuff.last_login) == "number")
end end
local file, errmsg = io.open(core.auth_file_path, 'w+b') local file, errmsg = io.open(core.auth_file_path, 'w+b')
if not file then if not file then
@ -71,7 +95,7 @@ local function save_auth_file()
end end
for name, stuff in pairs(core.auth_table) do for name, stuff in pairs(core.auth_table) do
local privstring = core.privs_to_string(stuff.privileges) local privstring = core.privs_to_string(stuff.privileges)
file:write(name..":"..stuff.password..":"..privstring..":"..stuff.lastlogin..'\n') file:write(name..":"..stuff.password..":"..privstring..":"..stuff.last_login..'\n')
end end
io.close(file) io.close(file)
end end
@ -112,7 +136,7 @@ core.builtin_auth_handler = {
return { return {
password = core.auth_table[name].password, password = core.auth_table[name].password,
privileges = privileges, privileges = privileges,
lastlogin = core.auth_table[name].lastlogin, last_login = core.auth_table[name].last_login,
} }
end, end,
create_auth = function(name, password) create_auth = function(name, password)
@ -122,7 +146,7 @@ core.builtin_auth_handler = {
core.auth_table[name] = { core.auth_table[name] = {
password = password, password = password,
privileges = core.string_to_privs(core.setting_get("default_privs")), privileges = core.string_to_privs(core.setting_get("default_privs")),
lastlogin = 0, last_login = os.time(),
} }
save_auth_file() save_auth_file()
end, end,
@ -159,7 +183,7 @@ core.builtin_auth_handler = {
core.builtin_auth_handler.create_auth(name, core.get_password_hash(name, core.setting_get("default_password"))) core.builtin_auth_handler.create_auth(name, core.get_password_hash(name, core.setting_get("default_password")))
core.auth_table[name].privileges = core.string_to_privs(core.setting_get("default_privs")) core.auth_table[name].privileges = core.string_to_privs(core.setting_get("default_privs"))
end end
core.auth_table[name].lastlogin = logintime core.auth_table[name].last_login = logintime
save_auth_file() save_auth_file()
end, end,
} }

View File

@ -50,6 +50,7 @@ local player_list = {}
core.register_on_joinplayer(function(player) core.register_on_joinplayer(function(player)
player_list[player:get_player_name()] = player player_list[player:get_player_name()] = player
core.set_last_login(player:get_player_name(), os.time())
end) end)
core.register_on_leaveplayer(function(player) core.register_on_leaveplayer(function(player)

View File

@ -125,23 +125,6 @@ bool ScriptApiServer::setPassword(const std::string &playername,
return lua_toboolean(L, -1); return lua_toboolean(L, -1);
} }
bool ScriptApiServer::set_login_time(const std::string &playername,
int logintime)
{
SCRIPTAPI_PRECHECKHEADER
getAuthHandler();
lua_getfield(L, -1, "set_login_time");
lua_remove(L, -2);
if(lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError("Authentication handler missing set_login_time");
lua_pushstring(L, playername.c_str());
lua_pushnumber(L, logintime);
if(lua_pcall(L, 2, 1, m_errorhandler))
scriptError();
return lua_toboolean(L, -1);
}
bool ScriptApiServer::on_chat_message(const std::string &name, bool ScriptApiServer::on_chat_message(const std::string &name,
const std::string &message) const std::string &message)
{ {

View File

@ -42,8 +42,6 @@ public:
const std::string &password); const std::string &password);
bool setPassword(const std::string &playername, bool setPassword(const std::string &playername,
const std::string &password); const std::string &password);
bool set_login_time(const std::string &playername,
int logintime);
private: private:
void getAuthHandler(); void getAuthHandler();
void readPrivileges(int index, std::set<std::string> &result); void readPrivileges(int index, std::set<std::string> &result);

View File

@ -1600,11 +1600,6 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
L"name. If your client closed unexpectedly, try again in " L"name. If your client closed unexpectedly, try again in "
L"a minute."); L"a minute.");
} }
time_t logtim;
logtim = time(NULL);
m_script->set_login_time(playername, (int) logtim);
m_clients.setPlayerName(peer_id,playername); m_clients.setPlayerName(peer_id,playername);