File: superUser.cpp

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (81 lines) | stat: -rw-r--r-- 2,098 bytes parent folder | download | duplicates (4)
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
// superUser.cpp : Defines the entry point for the DLL application.
//

#include "bzfsAPI.h"
#include "plugin_utils.h"
#include <algorithm>

class SuperUser : public bz_Plugin
{
public:
    virtual const char* Name ()
    {
        return "SuperUser";
    }
    virtual void Init ( const char* config );

    virtual void Event ( bz_EventData * /* eventData */ );

protected:
    std::vector<std::string> GetUserInfo(const char* bzID );

    PluginConfig Users;
};

BZ_PLUGIN(SuperUser)

void SuperUser::Init ( const char* commandLine )
{
    if (commandLine == NULL || strlen(commandLine) == 0)
        bz_debugMessage(0,"SuperUser plugin needs a user file to work from");
    else
        Users.read(commandLine);

    Register(bz_eGetPlayerInfoEvent);
    Register(bz_ePlayerJoinEvent);
}

std::vector<std::string> SuperUser::GetUserInfo(const char* bzID )
{
    std::vector<std::string> perms;

    std::string info = Users.item("Users",bzID);

    if (info.size() > 0)
        perms = tokenize(info,std::string(","),0,true);

    return perms;
}

void SuperUser::Event ( bz_EventData * eventData )
{
    if (eventData->eventType == bz_eGetPlayerInfoEvent)
    {
        bz_GetPlayerInfoEventData_V1* playerInfoData = (bz_GetPlayerInfoEventData_V1*)eventData;
        bz_BasePlayerRecord* pr = bz_getPlayerByIndex(playerInfoData->playerID);

        std::vector<std::string> perms = GetUserInfo(pr->bzID.c_str());

        if (std::find(perms.begin(), perms.end(), "ban") != perms.end())
            playerInfoData->admin = true;

        bz_freePlayerRecord(pr);
    }
    else if (eventData->eventType == bz_ePlayerJoinEvent)
    {
        bz_PlayerJoinPartEventData_V1 *joinData = (bz_PlayerJoinPartEventData_V1*)eventData;

        std::vector<std::string> perms = GetUserInfo(joinData->record->bzID.c_str());

        for (size_t i = 0; i < perms.size(); i++)
            bz_grantPerm(joinData->playerID,perms[i].c_str());
    }
}

// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4