forked from cuberite/cuberite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupManager.cpp
139 lines (95 loc) · 3.29 KB
/
GroupManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "GroupManager.h"
#include "Group.h"
#include "inifile/iniFile.h"
#include "ChatColor.h"
#include "Root.h"
typedef std::map< AString, cGroup* > GroupMap;
struct cGroupManager::sGroupManagerState
{
GroupMap Groups;
};
cGroupManager::~cGroupManager()
{
for( GroupMap::iterator itr = m_pState->Groups.begin(); itr != m_pState->Groups.end(); ++itr )
{
delete itr->second;
}
m_pState->Groups.clear();
delete m_pState;
}
cGroupManager::cGroupManager()
: m_pState( new sGroupManagerState )
{
LOGD("-- Loading Groups --");
cIniFile IniFile;
if (!IniFile.ReadFile("groups.ini"))
{
LOGWARNING("Regenerating groups.ini, all groups will be reset");
IniFile.AddHeaderComment(" This is the MCServer permissions manager groups file");
IniFile.AddHeaderComment(" It stores all defined groups such as Administrators, Players, or Moderators");
IniFile.SetValue("Owner", "Permissions", "*", true);
IniFile.SetValue("Owner", "Color", "2", true);
IniFile.SetValue("Moderator", "Permissions", "core.time,core.item,core.teleport,core.ban,core.unban,core.save-all,core.toggledownfall");
IniFile.SetValue("Moderator", "Color", "2", true);
IniFile.SetValue("Moderator", "Inherits", "Player", true);
IniFile.SetValue("Player", "Permissions", "core.build", true);
IniFile.SetValue("Player", "Color", "f", true);
IniFile.SetValue("Player", "Inherits", "Default", true);
IniFile.SetValue("Default", "Permissions", "core.help,core.playerlist,core.pluginlist,core.spawn,core.listworlds,core.back,core.motd,core.gotoworld,core.coords,core.viewdistance", true);
IniFile.SetValue("Default", "Color", "f", true);
IniFile.WriteFile("groups.ini");
}
unsigned int NumKeys = IniFile.GetNumKeys();
for( unsigned int i = 0; i < NumKeys; i++ )
{
std::string KeyName = IniFile.GetKeyName( i );
cGroup* Group = GetGroup( KeyName.c_str() );
LOGD("Loading group: %s", KeyName.c_str() );
Group->SetName( KeyName );
char Color = IniFile.GetValue( KeyName, "Color", "-" )[0];
if( Color != '-' )
Group->SetColor( cChatColor::MakeColor(Color) );
else
Group->SetColor( cChatColor::White );
std::string Commands = IniFile.GetValue( KeyName, "Commands", "" );
if( Commands.size() > 0 )
{
AStringVector Split = StringSplit( Commands, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->AddCommand( Split[i] );
}
}
std::string Permissions = IniFile.GetValue( KeyName, "Permissions", "" );
if( Permissions.size() > 0 )
{
AStringVector Split = StringSplit( Permissions, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->AddPermission( Split[i] );
}
}
std::string Groups = IniFile.GetValue( KeyName, "Inherits", "" );
if( Groups.size() > 0 )
{
AStringVector Split = StringSplit( Groups, "," );
for( unsigned int i = 0; i < Split.size(); i++)
{
Group->InheritFrom( GetGroup( Split[i].c_str() ) );
}
}
}
LOGD("-- Groups Successfully Loaded --");
}
cGroup* cGroupManager::GetGroup( const AString & a_Name )
{
GroupMap::iterator itr = m_pState->Groups.find( a_Name );
if( itr != m_pState->Groups.end() )
{
return itr->second;
}
cGroup* Group = new cGroup();
m_pState->Groups[a_Name] = Group;
return Group;
}