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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* The ManaVerse Client
* Copyright (C) 2016-2019 The ManaPlus Developers
*
* This file is part of The ManaVerse Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "resources/db/groupdb.h"
#include "configuration.h"
#include "being/localplayer.h"
#include "utils/checkutils.h"
#ifdef TMWA_SUPPORT
#include "net/net.h"
#endif // TMWA_SUPPORT
#include "resources/beingcommon.h"
#include "resources/groupinfo.h"
#include "debug.h"
namespace
{
GroupDb::GroupInfos mGroups;
const GroupInfo mEmptyGroup;
bool mLoaded = false;
} // namespace
void GroupDb::load()
{
if (mLoaded)
unload();
logger->log1("Initializing group database...");
loadXmlFile(paths.getStringValue("groupsFile"), SkipError_false);
loadXmlFile(paths.getStringValue("groupsPatchFile"), SkipError_true);
loadXmlDir("groupsPatchDir", loadXmlFile)
mLoaded = true;
}
#define servercommandFirst(name) \
if (str == #name) \
return ServerCommandType::name; \
else
#define servercommand(name) \
if (str == #name) \
return ServerCommandType::name; \
else
#define servercommand2(name1, name2) \
if (str == #name2) \
return ServerCommandType::name1; \
else
static ServerCommandTypeT parseCommand(const std::string &str,
const int id)
{
#include "resources/servercommands.inc"
{
reportAlways("GroupsDb: unknown command name: '%s' in group id '%d'.",
str.c_str(),
id)
}
return ServerCommandType::Max;
}
SERVERCOMMANDS_VOID
#undef servercommandFirst
#undef servercommand
#undef servercommand2
#define serverpermissionFirst(name) \
if (str == #name) \
return ServerPermissionType::name; \
else
#define serverpermission(name) \
if (str == #name) \
return ServerPermissionType::name; \
else
static ServerPermissionTypeT parsePermission(const std::string &str,
const int id)
{
#include "resources/serverpermissions.inc"
{
reportAlways("GroupsDb: unknown permission name: "
"'%s' in group id '%d'.",
str.c_str(),
id)
}
return ServerPermissionType::Max;
}
SERVERPERMISSION_VOID
#undef serverpermissionFirst
#undef serverpermission
static ServerCommandEnable::Type parseUseFlag(const std::string &str,
const int id)
{
if (str == "self")
{
return ServerCommandEnable::Self;
}
else if (str == "other")
{
return ServerCommandEnable::Other;
}
else if (str == "both")
{
return ServerCommandEnable::Both;
}
else if (str == "false")
{
return ServerCommandEnable::No;
}
else
{
reportAlways("GroupsDb: unknown use flag: '%s' in group id '%d'."
"Possible values 'self', 'other', 'both'.",
str.c_str(),
id)
return ServerCommandEnable::No;
}
}
static void loadCommands(XmlNodePtr rootNode,
const int id,
GroupInfo *groupInfo) A_NONNULL(3);
static void loadCommands(XmlNodePtr rootNode,
const int id,
GroupInfo *groupInfo)
{
for_each_xml_child_node(node, rootNode)
{
if (xmlNameEqual(node, "command"))
{
const std::string nameStr = XML::getProperty(node,
"name",
"");
const std::string useStr = XML::getProperty(node,
"use",
"");
ServerCommandTypeT name = parseCommand(nameStr, id);
if (name == ServerCommandType::Max)
continue;
ServerCommandEnable::Type useFlag = parseUseFlag(useStr, id);
if (useFlag == ServerCommandEnable::No)
continue;
groupInfo->mCommands[CAST_SIZE(name)] = useFlag;
}
}
}
static void loadPermissions(XmlNodePtr rootNode,
const int id,
GroupInfo *groupInfo) A_NONNULL(3);
static void loadPermissions(XmlNodePtr rootNode,
const int id,
GroupInfo *groupInfo)
{
for_each_xml_child_node(node, rootNode)
{
if (xmlNameEqual(node, "permission"))
{
const std::string nameStr = XML::getProperty(node,
"name",
"");
ServerPermissionTypeT perm = parsePermission(nameStr, id);
if (perm == ServerPermissionType::Max)
continue;
if (!XML::getBoolProperty(node,
"enable",
true))
{
continue;
}
groupInfo->mPermissions[CAST_SIZE(perm)] = Enable_true;
}
}
}
static void loadSubNodes(XmlNodePtr groupNode,
const int id,
GroupInfo *groupInfo) A_NONNULL(3);
static void loadSubNodes(XmlNodePtr groupNode,
const int id,
GroupInfo *groupInfo)
{
for_each_xml_child_node(node, groupNode)
{
if (xmlNameEqual(node, "commands"))
loadCommands(node, id, groupInfo);
if (xmlNameEqual(node, "permissions"))
loadPermissions(node, id, groupInfo);
}
}
static void parseInherit(XmlNodePtr groupNode,
const int id,
GroupInfo *groupInfo) A_NONNULL(3);
static void parseInherit(XmlNodePtr groupNode,
const int id,
GroupInfo *groupInfo)
{
const std::string inherit = XML::langProperty(groupNode,
"inherit",
"");
STD_VECTOR<int> parts;
splitToIntVector(parts, inherit, ',');
FOR_EACH (STD_VECTOR<int>::const_iterator, it, parts)
{
const int id2 = *it;
GroupDb::GroupInfos::const_iterator it2 = mGroups.find(id2);
if (it2 == mGroups.end())
{
reportAlways("Unknown inherit group id '%d' in group '%d'",
id2,
id)
continue;
}
GroupInfo *const groupInfo2 = (*it2).second;
for (size_t f = 0; f < CAST_SIZE(ServerCommandType::Max); f ++)
{
ServerCommandEnable::Type enable = groupInfo2->mCommands[f];
if (enable != ServerCommandEnable::No)
groupInfo->mCommands[f] = enable;
}
for (size_t f = 0; f < CAST_SIZE(ServerPermissionType::Max); f ++)
{
if (groupInfo2->mPermissions[f] == Enable_true)
groupInfo->mPermissions[f] = Enable_true;
}
}
}
void GroupDb::loadXmlFile(const std::string &fileName,
const SkipError skipError)
{
XML::Document doc(fileName,
UseVirtFs_true,
skipError);
XmlNodeConstPtrConst rootNode = doc.rootNode();
if (rootNode == nullptr ||
!xmlNameEqual(rootNode, "groups"))
{
if (skipError == SkipError_true)
{
logger->log("GroupsDb: Error while loading %s!",
fileName.c_str());
}
else
{
reportAlways("GroupsDb: Error while loading %s!",
fileName.c_str())
}
return;
}
for_each_xml_child_node(node, rootNode)
{
if (xmlNameEqual(node, "include"))
{
const std::string name = XML::getProperty(node, "name", "");
if (!name.empty())
loadXmlFile(name, skipError);
continue;
}
if (xmlNameEqual(node, "group"))
{
const int id = XML::getProperty(node,
"id",
-1);
if (id < 0)
{
reportAlways("Empty id field in GroupDb")
continue;
}
GroupInfosIter it = mGroups.find(id);
GroupInfo *group = nullptr;
if (it != mGroups.end())
{
reportAlways("GroupDb: group with id %d already added",
id)
group = (*it).second;
}
else
{
group = new GroupInfo;
mGroups[id] = group;
}
group->name = XML::langProperty(node,
"name",
"");
group->longName = XML::langProperty(node,
"longName",
"");
group->badge = XML::langProperty(node,
"badge",
paths.getStringValue("gmbadge"));
group->showBadge = XML::getBoolProperty(node,
"showBadge",
false);
group->highlightName = XML::getBoolProperty(node,
"highlightName",
false);
loadSubNodes(node, id, group);
parseInherit(node, id, group);
}
}
}
void GroupDb::unload()
{
logger->log1("Unloading group database...");
FOR_EACH (GroupInfosIter, it, mGroups)
{
delete (*it).second;
}
mGroups.clear();
mLoaded = false;
}
const std::string &GroupDb::getName(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return mEmptyGroup.name;
}
return (*it).second->name;
}
const std::string &GroupDb::getLongName(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return mEmptyGroup.longName;
}
return (*it).second->longName;
}
bool GroupDb::getShowBadge(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return mEmptyGroup.showBadge;
}
return (*it).second->showBadge;
}
bool GroupDb::getHighlightName(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return mEmptyGroup.highlightName;
}
return (*it).second->highlightName;
}
const std::string &GroupDb::getBadge(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return mEmptyGroup.badge;
}
return (*it).second->badge;
}
const GroupInfo *GroupDb::getGroup(const int id)
{
GroupInfos::const_iterator it = mGroups.find(id);
if (it == mGroups.end())
{
reportAlways("Unknown group id requested: %d", id)
return &mEmptyGroup;
}
return (*it).second;
}
bool GroupDb::isAllowCommand(const ServerCommandTypeT command)
{
if (localPlayer == nullptr)
return false;
const int groupId = localPlayer->getGroupId();
const GroupInfo *const group = GroupDb::getGroup(groupId);
#ifdef TMWA_SUPPORT
// allow any commands for legacy if group > 0
if (Net::getNetworkType() == ServerType::TMWATHENA &&
localPlayer->isGM())
{
return true;
}
#endif
if (group->mPermissions[CAST_SIZE(ServerPermissionType::all_commands)] ==
Enable_true)
{
return true;
}
const ServerCommandEnable::Type enabled =
group->mCommands[CAST_SIZE(command)];
return (enabled & ServerCommandEnable::Self) != 0;
}
#ifdef UNITTESTS
GroupDb::GroupInfos &GroupDb::getGroups()
{
return mGroups;
}
#endif // UNITTESTS
|