From 9dfa0f1f15c413540e932a04e94419a8a3b37dcf Mon Sep 17 00:00:00 2001 From: faketruth Date: Mon, 30 Jan 2012 16:47:26 +0000 Subject: Added a WebAdmin interface to view users their groups, and the permissions of groups. cChunk::Tick did an assertion in cCSLock, I used a cCSUnlock to fix it, but not sure if this is correct. git-svn-id: http://mc-server.googlecode.com/svn/trunk@194 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- Plugins/Core/main.lua | 3 +- Plugins/Core/web_permissions.lua | 79 ++++++++++++++++++++++++++++++++++++++++ Plugins/Core/web_playerlist.lua | 17 +++++---- settings.ini | 2 +- source/Bindings.cpp | 37 +++++++++++++++++-- source/Bindings.h | 2 +- source/ManualBindings.cpp | 22 +++++++++++ source/cChunk.cpp | 2 +- source/cChunkMap.cpp | 2 +- source/cPlayer.cpp | 23 +++++++++++- source/cPlayer.h | 3 ++ source/cWebPlugin_Lua.cpp | 11 ++++-- 12 files changed, 183 insertions(+), 20 deletions(-) create mode 100644 Plugins/Core/web_permissions.lua diff --git a/Plugins/Core/main.lua b/Plugins/Core/main.lua index 90f86938f..d8fadaad9 100644 --- a/Plugins/Core/main.lua +++ b/Plugins/Core/main.lua @@ -129,9 +129,10 @@ function Initialize( Plugin ) local WebPlugin = Plugin:CreateWebPlugin() WebPlugin:SetName( Plugin:GetName() ) + WebPlugin:AddTab( "Playerlist", HandleRequest_PlayerList ) WebPlugin:AddTab( "Whitelist", HandleRequest_WhiteList ) WebPlugin:AddTab( "Reload", HandleRequest_Reload ) - WebPlugin:AddTab( "Playerlist", HandleRequest_PlayerList ) + WebPlugin:AddTab( "Permissions", HandleRequest_Permissions ) LOG( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() ) return true diff --git a/Plugins/Core/web_permissions.lua b/Plugins/Core/web_permissions.lua new file mode 100644 index 000000000..5c7527056 --- /dev/null +++ b/Plugins/Core/web_permissions.lua @@ -0,0 +1,79 @@ +local function ShowUsersTable() + local Content = "

Users

" + + local UsersIni = cIniFile("users.ini") + if( UsersIni:ReadFile() == false ) then + return "Could not read users.ini!" + end + + local NumUsers = UsersIni:GetNumKeys() + + Content = Content .. "" + + if( NumUsers > 0 ) then + Content = Content .. "" + + for i=0, NumUsers-1 do + local UserName = UsersIni:GetKeyName( i ) + + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + end + else + Content = Content .. "" + end + Content = Content .. "
UserGroups
" .. i .. "." .. UserName .. "" + Content = Content .. UsersIni:GetValue( UserName, "Groups", "-" ) + Content = Content .. "
None
" + + + return Content +end + +local function ShowGroupsTable() + local Content = "

Groups

" + + local GroupsIni = cIniFile("groups.ini") + if( GroupsIni:ReadFile() == false ) then + return "Could not read groups.ini!" + end + + local NumGroups = GroupsIni:GetNumKeys() + + Content = Content .. "" + if( NumGroups > 0 ) then + Content = Content .. "" + + for i=0, NumGroups-1 do + local GroupName = GroupsIni:GetKeyName( i ) + + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + Content = Content .. "" + end + else + Content = Content .. "" + end + Content = Content .. "
NamePermissionsColor
" .. i .. "." .. GroupName .. "" + Content = Content .. GroupsIni:GetValue( GroupName, "Permissions", "-" ) + Content = Content .. "" + Content = Content .. GroupsIni:GetValue( GroupName, "Color", "-" ) + Content = Content .. "
None
" + + return Content +end + +function HandleRequest_Permissions( Request ) + local Content = "" + + Content = Content .. ShowGroupsTable() + Content = Content .. ShowUsersTable() + + return Content +end \ No newline at end of file diff --git a/Plugins/Core/web_playerlist.lua b/Plugins/Core/web_playerlist.lua index eeb9369c1..205306ba8 100644 --- a/Plugins/Core/web_playerlist.lua +++ b/Plugins/Core/web_playerlist.lua @@ -18,14 +18,17 @@ function HandleRequest_PlayerList( Request ) local PlayerList = World:GetAllPlayers() - for i, Player in ipairs( PlayerList ) do - Content = Content .. "" - Content = Content .. "" .. i .. "." - Content = Content .. "" .. Player:GetName() .. "" - Content = Content .. "Kick" - Content = Content .. "" + if( #PlayerList > 0 ) then + for i, Player in ipairs( PlayerList ) do + Content = Content .. "" + Content = Content .. "" .. i .. "." + Content = Content .. "" .. Player:GetName() .. "" + Content = Content .. "Kick" + Content = Content .. "" + end + else + Content = Content .. "None" end - Content = Content .. "" Content = Content .. "
" return Content diff --git a/settings.ini b/settings.ini index a281b9ba3..14c36e4d9 100644 --- a/settings.ini +++ b/settings.ini @@ -15,7 +15,7 @@ NewPlugin=Core ShowPluginNames=1 [Physics] -Water=1 +Water=0 [Monsters] AnimalsOn=0 diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 51342db62..8d484fa1f 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,10 +1,8 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 01/27/12 00:53:11. +** Generated automatically by tolua++-1.0.92 on 01/30/12 17:26:14. */ -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - #ifndef __cplusplus #include "stdlib.h" #endif @@ -15,6 +13,7 @@ /* Exported function */ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S); +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "tolua_base.h" #include "cTorch.h" #include "cStairs.h" @@ -5897,6 +5896,37 @@ static int tolua_AllToLua_cPlayer_MoveToWorld00(lua_State* tolua_S) return 0; #endif } +#endif //#ifndef TOLUA_DISABLE + +/* method: LoadPermissionsFromDisk of class cPlayer */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_cPlayer_LoadPermissionsFromDisk00 +static int tolua_AllToLua_cPlayer_LoadPermissionsFromDisk00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cPlayer",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'LoadPermissionsFromDisk'", NULL); +#endif + { + self->LoadPermissionsFromDisk(); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'LoadPermissionsFromDisk'.",&tolua_err); + return 0; +#endif +} #endif //#ifndef TOLUA_DISABLE class Lua__cPlayer : public cPlayer, public ToluaBase { @@ -16785,6 +16815,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"SetVisible",tolua_AllToLua_cPlayer_SetVisible00); tolua_function(tolua_S,"IsVisible",tolua_AllToLua_cPlayer_IsVisible00); tolua_function(tolua_S,"MoveToWorld",tolua_AllToLua_cPlayer_MoveToWorld00); + tolua_function(tolua_S,"LoadPermissionsFromDisk",tolua_AllToLua_cPlayer_LoadPermissionsFromDisk00); tolua_endmodule(tolua_S); tolua_cclass(tolua_S,"Lua__cPlayer","Lua__cPlayer","cPlayer",NULL); tolua_beginmodule(tolua_S,"Lua__cPlayer"); diff --git a/source/Bindings.h b/source/Bindings.h index fc3d0cb62..b2ff3c1c8 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 01/27/12 00:53:12. +** Generated automatically by tolua++-1.0.92 on 01/30/12 17:26:15. */ /* Exported function */ diff --git a/source/ManualBindings.cpp b/source/ManualBindings.cpp index 45fae285d..06a148b91 100644 --- a/source/ManualBindings.cpp +++ b/source/ManualBindings.cpp @@ -140,6 +140,27 @@ static int tolua_cPlayer_GetGroups(lua_State* tolua_S) return 1; } +static int tolua_cPlayer_GetResolvedPermissions(lua_State* tolua_S) +{ + cPlayer* self = (cPlayer*) tolua_tousertype(tolua_S,1,0); + + cPlayer::StringList AllPermissions = self->GetResolvedPermissions(); + + lua_createtable(tolua_S, AllPermissions.size(), 0); + int newTable = lua_gettop(tolua_S); + int index = 1; + cPlayer::StringList::iterator iter = AllPermissions.begin(); + while(iter != AllPermissions.end()) + { + std::string& Permission = *iter; + tolua_pushstring( tolua_S, Permission.c_str() ); + lua_rawseti(tolua_S, newTable, index); + ++iter; + ++index; + } + return 1; +} + static int tolua_cPlugin_BindCommand(lua_State* tolua_S) { cPlugin* self = (cPlugin*) tolua_tousertype(tolua_S,1,0); @@ -267,6 +288,7 @@ void ManualBindings::Bind( lua_State* tolua_S ) tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S,"cPlayer"); tolua_function(tolua_S,"GetGroups",tolua_cPlayer_GetGroups); + tolua_function(tolua_S,"GetResolvedPermissions",tolua_cPlayer_GetResolvedPermissions); tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S,"cWebPlugin_Lua"); tolua_function(tolua_S,"AddTab",tolua_cWebPlugin_Lua_AddTab); diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 7d93b4ba9..5e9c44a18 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -249,7 +249,7 @@ void cChunk::Tick(float a_Dt) } m_pState->PendingSendBlocks.clear(); } - Lock.Unlock(); + cCSUnlock Unlock( Lock ); while( !m_pState->UnloadQuery.empty() ) { diff --git a/source/cChunkMap.cpp b/source/cChunkMap.cpp index d815e311a..d3dba0510 100644 --- a/source/cChunkMap.cpp +++ b/source/cChunkMap.cpp @@ -380,7 +380,7 @@ void cChunkMap::UnloadUnusedChunks() cChunk* Chunk = Layer.m_Chunks[i].m_LiveChunk; if( Chunk && Chunk->GetClients().size() == 0 && Chunk->GetReferenceCount() <= 0 ) { - Chunk->SaveToDisk(); + //Chunk->SaveToDisk(); World->RemoveSpread( ptr_cChunk( Chunk ) ); RemoveChunk( Chunk ); delete Chunk; diff --git a/source/cPlayer.cpp b/source/cPlayer.cpp index c7ef8e56d..c3ba32840 100644 --- a/source/cPlayer.cpp +++ b/source/cPlayer.cpp @@ -732,8 +732,11 @@ bool cPlayer::MoveToWorld( const char* a_WorldName ) return false; } -bool cPlayer::LoadFromDisk() // TODO - This should also get/set/whatever the correct world for this player +void cPlayer::LoadPermissionsFromDisk() { + m_pState->Groups.clear(); + m_pState->Permissions.clear(); + cIniFile IniFile("users.ini"); if( IniFile.ReadFile() ) { @@ -759,6 +762,11 @@ bool cPlayer::LoadFromDisk() // TODO - This should also get/set/whatever the cor AddToGroup("Default"); } ResolvePermissions(); +} + +bool cPlayer::LoadFromDisk() // TODO - This should also get/set/whatever the correct world for this player +{ + LoadPermissionsFromDisk(); // Log player permissions, cause it's what the cool kids do LOGINFO("Player %s has permissions:", m_pState->PlayerName.c_str() ); @@ -891,6 +899,19 @@ const cPlayer::GroupList & cPlayer::GetGroups() return m_pState->Groups; } +cPlayer::StringList cPlayer::GetResolvedPermissions() +{ + StringList Permissions; + + const PermissionMap& ResolvedPermissions = m_pState->ResolvedPermissions; + for( PermissionMap::const_iterator itr = ResolvedPermissions.begin(); itr != ResolvedPermissions.end(); ++itr ) + { + if( itr->second ) Permissions.push_back( itr->first ); + } + + return Permissions; +} + const char* cPlayer::GetLoadedWorldName() { return m_pState->LoadedWorldName.c_str(); diff --git a/source/cPlayer.h b/source/cPlayer.h index 3793443dd..c05600f80 100644 --- a/source/cPlayer.h +++ b/source/cPlayer.h @@ -63,10 +63,12 @@ public: void SetName( const char* a_Name ); //tolua_export typedef std::list< cGroup* > GroupList; + typedef std::list< std::string > StringList; void AddToGroup( const char* a_GroupName ); //tolua_export bool CanUseCommand( const char* a_Command ); //tolua_export bool HasPermission( const char* a_Permission ); //tolua_export const GroupList & GetGroups(); // >> EXPORTED IN MANUALBINDINGS << + StringList GetResolvedPermissions(); // >> EXPORTED IN MANUALBINDINGS << bool IsInGroup( const char* a_Group ); //tolua_export std::string GetColor(); //tolua_export @@ -86,6 +88,7 @@ public: bool SaveToDisk(); bool LoadFromDisk(); + void LoadPermissionsFromDisk(); //tolua_export const char* GetLoadedWorldName(); diff --git a/source/cWebPlugin_Lua.cpp b/source/cWebPlugin_Lua.cpp index f92cb1690..40e8fcb76 100644 --- a/source/cWebPlugin_Lua.cpp +++ b/source/cWebPlugin_Lua.cpp @@ -84,18 +84,21 @@ std::string cWebPlugin_Lua::HandleRequest( HTTPRequest* a_Request ) tolua_pushusertype( LuaState, a_Request, "HTTPRequest" ); LOGINFO("Calling bound function! :D"); int s = lua_pcall( LuaState, 1, 1, 0); - if( report_errors( LuaState, s ) ) + + if ( s != 0 ) { + std::string err = lua_tostring(LuaState, -1); + LOGERROR("-- %s", err.c_str() ); + lua_pop(LuaState, 1); LOGINFO("error. Stack size: %i", lua_gettop(LuaState) ); - return false; + return err; // Show the error message in the web page, looks cool } - if( !lua_isstring( LuaState, -1 ) ) { LOGWARN("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str() ); lua_pop(LuaState, 1); // Pop return value - return ""; + return std::string("WARNING: WebPlugin tab '") + Tab->Title + std::string("' did not return a string!"); } RetVal += tolua_tostring(LuaState, -1, 0); -- cgit v1.2.3