From 02d10842fe2728792405b0f519dfa5896515e33a Mon Sep 17 00:00:00 2001 From: Ryan Newell Date: Mon, 20 Oct 2014 13:26:30 -0700 Subject: [PATCH] Fixed compatibility with older auth.txts, changed variable name, and changed how the last_login time is set --- builtin/game/auth.lua | 40 ++++++++++++++++++++++++++------- builtin/game/misc.lua | 1 + src/script/cpp_api/s_server.cpp | 17 -------------- src/script/cpp_api/s_server.h | 2 -- src/server.cpp | 5 ----- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/builtin/game/auth.lua b/builtin/game/auth.lua index b62e54059..d99062988 100644 --- a/builtin/game/auth.lua +++ b/builtin/game/auth.lua @@ -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_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 newtable = {} local file, errmsg = io.open(core.auth_file_path, 'rb') @@ -41,12 +60,17 @@ local function read_auth_file() end for line in file:lines() do if line ~= "" then - local name, password, privilegestring, lastlogin = string.match(line, "([^:]*):([^:]*):([^:]*):([^:]*)") - if not name or not password or not privilegestring or not lastlogin then + --local name, password, privilegestring, last_login = string.match(line, "([^:]*):([^:]*):([^:]*):([^:]*)") + 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)) end 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 io.close(file) @@ -63,7 +87,7 @@ local function save_auth_file() assert(type(stuff) == "table") assert(type(stuff.password) == "string") assert(type(stuff.privileges) == "table") - assert(type(stuff.lastlogin) == "number") + assert(type(stuff.last_login) == "number") end local file, errmsg = io.open(core.auth_file_path, 'w+b') if not file then @@ -71,7 +95,7 @@ local function save_auth_file() end for name, stuff in pairs(core.auth_table) do 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 io.close(file) end @@ -112,7 +136,7 @@ core.builtin_auth_handler = { return { password = core.auth_table[name].password, privileges = privileges, - lastlogin = core.auth_table[name].lastlogin, + last_login = core.auth_table[name].last_login, } end, create_auth = function(name, password) @@ -122,7 +146,7 @@ core.builtin_auth_handler = { core.auth_table[name] = { password = password, privileges = core.string_to_privs(core.setting_get("default_privs")), - lastlogin = 0, + last_login = os.time(), } save_auth_file() 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.auth_table[name].privileges = core.string_to_privs(core.setting_get("default_privs")) end - core.auth_table[name].lastlogin = logintime + core.auth_table[name].last_login = logintime save_auth_file() end, } diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index a392386f1..2a9169254 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -50,6 +50,7 @@ local player_list = {} core.register_on_joinplayer(function(player) player_list[player:get_player_name()] = player + core.set_last_login(player:get_player_name(), os.time()) end) core.register_on_leaveplayer(function(player) diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp index 0a17990e1..21fe164aa 100644 --- a/src/script/cpp_api/s_server.cpp +++ b/src/script/cpp_api/s_server.cpp @@ -125,23 +125,6 @@ bool ScriptApiServer::setPassword(const std::string &playername, 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, const std::string &message) { diff --git a/src/script/cpp_api/s_server.h b/src/script/cpp_api/s_server.h index 5ad6b1f1a..a63e36320 100644 --- a/src/script/cpp_api/s_server.h +++ b/src/script/cpp_api/s_server.h @@ -42,8 +42,6 @@ public: const std::string &password); bool setPassword(const std::string &playername, const std::string &password); - bool set_login_time(const std::string &playername, - int logintime); private: void getAuthHandler(); void readPrivileges(int index, std::set &result); diff --git a/src/server.cpp b/src/server.cpp index 0f8f807eb..6afe600ed 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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"a minute."); } - - time_t logtim; - logtim = time(NULL); - - m_script->set_login_time(playername, (int) logtim); m_clients.setPlayerName(peer_id,playername);