void SV_RemoteCmdSetAdmin(int uid, char* guid, int power)
{

    adminPower_t *admin;
    adminPower_t *this;

    if(uid < 1){
        Com_Printf("No such player or player without uid\n");
        return;
    }


    for(admin = adminpower ; admin ; admin = admin->next){
            if(admin->uid == uid){
                if(admin->power != power){
                    admin->power = power;

                    Com_Printf( "Admin power changed for: uid: %i to level: %i\n", uid, power);
                    SV_PrintAdministrativeLog( "changed power of admin with uid: %i to new power: %i", uid, power);
                }
                return;
            }
    }

    this = Z_Malloc(sizeof(adminPower_t));
    if(this){
            this->uid = uid;
            this->power = power;
            this->next = adminpower;
            adminpower = this;
            Com_Printf( "Admin added: uid: %i level: %i\n", uid, power);
            SV_PrintAdministrativeLog( "added a new admin with uid: %i and power: %i", uid, power);
    }

}
/*
============
Cmd_RemoteUnsetAdmin_f
============
*/
void SV_RemoteCmdUnsetAdmin(int uid, char* guid)
{

    adminPower_t *admin, **this;

    if(SV_UseUids()){

        if(uid < 1){
            Com_Printf("No such player\n");
            return;
        }

        NV_ProcessBegin();

        for(this = &adminpower, admin = *this; admin ; admin = *this)
        {

            if(admin->uid == uid){
                *this = admin->next;
                Z_Free(admin);
                NV_ProcessEnd();
                Com_Printf( "User removed: uid: %i\n", uid);
                SV_PrintAdministrativeLog( "removed admin with uid: %i", uid);
                return;
            }
            this = &admin->next;
        }

    }else{

        if(guid && strlen(guid) == 32)
        {
            guid += 24;
        }
        if(!guid || strlen(guid) != 8)
        {
                Com_Printf("Error: No such player\n");
                return;
        }

        NV_ProcessBegin();
        for(this = &adminpower, admin = *this; admin ; admin = *this)
        {

            if(!Q_stricmp(admin->guid, guid)){
                *this = admin->next;
                Z_Free(admin);
                NV_ProcessEnd();
                Com_Printf( "User removed: guid: %s\n", guid);
                SV_PrintAdministrativeLog( "removed admin with guid: %s", guid);
                return;
            }
            this = &admin->next;
        }

    }

    Com_Printf( "Error: No such user in database\n");
    NV_ProcessEnd();
}
/*
============
Cmd_RemoteUnsetAdmin_f
============
*/
void SV_RemoteCmdUnsetAdmin(int uid, char* guid)
{

    adminPower_t *admin, **this;

    if(uid < 1){
            Com_Printf("No such player\n");
            return;
    }

    NV_ProcessBegin();

    for(this = &adminpower, admin = *this; admin ; admin = *this)
    {

        if(admin->uid == uid){
                *this = admin->next;
                Z_Free(admin);
                NV_ProcessEnd();
                Com_Printf( "User removed: uid: %i\n", uid);
                SV_PrintAdministrativeLog( "removed admin with uid: %i", uid);
                return;
        }
        this = &admin->next;
    }

    Com_Printf( "Error: No such user in database\n");
    NV_ProcessEnd();
}
/*
============
Cmd_RemoteSetPermission
Changes minimum-PowerLevel of a command
============
*/
void SV_RemoteCmdSetPermission(char* command, int power)
{


    NV_ProcessBegin();
    if(Cmd_SetPower(command, power))
    {
        SV_PrintAdministrativeLog("changed required power of cmd: %s to new power: %i", command, power);
        Com_Printf("changed required power of cmd: %s to new power: %i\n", command, power);
    }else{
        Com_Printf("Failed to change power of cmd: %s Maybe this is not a valid command.\n", command);
    }
    NV_ProcessEnd();
}
void SV_RemoteCmdSetAdmin(int uid, char* guid, int power)
{

    adminPower_t *admin;
    adminPower_t *this;

    if(SV_UseUids()){

        if(uid < 1){
            Com_Printf("No such player\n");
            return;
        }

        NV_ProcessBegin();

        for(admin = adminpower ; admin ; admin = admin->next){
            if(admin->uid == uid){
                if(admin->power != power){
                    admin->power = power;

                    Com_Printf( "Admin power changed for: uid: %i to level: %i\n", uid, power);
                    SV_PrintAdministrativeLog( "changed power of admin with uid: %i to new power: %i", uid, power);
                }
                NV_ProcessEnd();
                return;
            }
        }

        this = Z_Malloc(sizeof(adminPower_t));
        if(this){
            this->uid = uid;
            this->power = power;
            this->next = adminpower;
            adminpower = this;
            Com_Printf( "Admin added: uid: %i level: %i\n", uid, power);
            SV_PrintAdministrativeLog( "added a new admin with uid: %i and power: %i", uid, power);
        }

    }else{

        if(guid && strlen(guid) == 32)
        {
            guid += 24;
        }
        if(!guid || strlen(guid) != 8)
        {
                Com_Printf("Error: No such player\n");
                return;
        }

        NV_ProcessBegin();

        for(admin = adminpower ; admin ; admin = admin->next)
        {
            if(!Q_stricmp(admin->guid, guid)){
                if(admin->power != power){
                    admin->power = power;

                    Com_Printf( "Admin power changed for: guid: %s to level: %i\n", guid, power);
                    SV_PrintAdministrativeLog( "changed power of admin with guid: %s to new power: %i", guid, power);
                }
                NV_ProcessEnd();
                return;
            }
        }

        this = Z_Malloc(sizeof(adminPower_t));
        if(this)
        {
            Q_strncpyz(this->guid, guid, sizeof(this->guid));
            this->power = power;
            this->next = adminpower;
            adminpower = this;
            Com_Printf( "Admin added: guid: %s level: %i\n", guid, power);
            SV_PrintAdministrativeLog( "added a new admin with guid: %s and power: %i", guid, power);
        }
    }

    NV_ProcessEnd();
}
P_P_F void Plugin_BanClient( unsigned int clientnum, int duration, int invokerid, char *banreason )
{
    
	client_t *cl;
	char* guid;
	time_t expire;
	char* temp;
    time_t aclock;
	char endtime[32];
    char dropmsg[MAX_STRING_CHARS];

	if(clientnum > sv_maxclients->integer)
		return;
	
	cl = &svs.clients[clientnum];

	time(&aclock);
	
	if(duration == -1)
	{
		expire = duration;
		Q_strncpyz(endtime, "never", sizeof(endtime));
	}
	else
	{
		expire = (aclock+(time_t)(duration*60));
		temp = ctime(&expire);
		temp[strlen(temp)-1] = 0;
		Q_strncpyz(endtime, temp, sizeof(endtime));
	
	}
	
	if(strlen(cl->pbguid) == 32)
	{
		guid = &cl->pbguid[24];
	}
	else if(cl->uid < 1)
	{
		Com_Printf("Error: This player has no valid ID and got banned by IP only\n");
		SV_DropClient(cl, "Invalid ID\n");
		SV_PlayerAddBanByip(&cl->netchan.remoteAddress, "INVALID USER", 0, "INVALID", 0, expire);
		return;
	}
	
	if(banreason == NULL)
	{
		banreason = "N/A";
	}
	
	SV_AddBan(cl->uid, invokerid, guid, cl->name, expire, banreason);

	if( cl->uid > 0 )
	{
		Com_Printf( "Banrecord added for player: %s uid: %i\n", cl->name, cl->uid);
		SV_PrintAdministrativeLog( "Banned player: %s uid: %i until %s with the following reason: %s", cl->name, cl->uid, endtime, banreason);
		Com_sprintf(dropmsg, sizeof(dropmsg), "You have been banned from this server\nYour ban will expire on: %s\nYour UID is: %i    Banning admin UID is: %i\nReason for this ban:\n%s",
			endtime, cl->uid, invokerid, banreason);

	}else{
		Com_Printf( "Banrecord added for player: %s guid: %s\n", cl->name, cl->pbguid);
		SV_PrintAdministrativeLog( "Banned player: %s guid: %s until %s with the following reason: %s", cl->name, cl->pbguid, endtime, banreason);
		Com_sprintf(dropmsg, sizeof(dropmsg), "You have been banned from this server\nYour ban will expire on: %s\nYour GUID is: %s    Banning admin UID is: %i\nReason for this ban:\n%s",
			endtime, cl->pbguid, invokerid, banreason);

		if(cl->authentication < 1)
		{
			SV_PlayerAddBanByip(&cl->netchan.remoteAddress, banreason, 0, cl->pbguid, 0, expire);
		}
	}
	SV_DropClient(cl, dropmsg);
}
qboolean SV_RemoveBan(int uid, char* guid, char* name){

    banList_t *this;
    int i;
    int type;
    qboolean succ = qfalse;
    char* printguid;
    char* banreason;
    char* printnick;

    if(uid > 0){
        type = 0;
    }else if(strlen(guid) == 8){
        type = 1;
    }else if(strlen(name) > 2){
        type = 2;
    }else{
        return qfalse;
    }

    this = banlist;
    if(!this)
        return qfalse;

    if(!SV_ReloadBanlist())
        return qfalse;

    for(i = 0 ; i < current_banindex; this++, i++)
    {
        switch(type)
        {
                case 0:
                    if(uid != this->playeruid)
                        continue;

                    break;
                case 1:
                    if(Q_stricmp(guid, this->pbguid))
                        continue;

                    break;
                case 2:
                    if(!Q_stricmp(name, this->playername))
                        continue;
        }
        this->expire = (time_t) 0;
        SV_RemoveBanByip(NULL, this->playeruid, this->pbguid);
        succ = qtrue;

        if(!*this->pbguid){
            printguid = "N/A";
        }else{
            printguid = this->pbguid;
        }

        if(!*this->reason){
            banreason = "N/A";
        }else{
            banreason = this->reason;
        }

        if(!*this->playername){
            printnick = "N/A";
        }else{
            printnick = this->playername;
        }

        Com_Printf("Removing ban for Nick: %s, UID: %i, GUID: %s, Banreason: %s\n", printnick, this->playeruid, printguid, banreason);
        SV_PrintAdministrativeLog("Removing ban for Nick: %s, UID: %i, GUID: %s, Banreason: %s\n", printnick, this->playeruid, printguid, banreason);
    }
    if(succ)
        SV_WriteBanlist();

    return succ;
}
qboolean SV_AddBan(int uid, int auid, char* guid, char* name, time_t expire, char* banreason){

    if(!SV_OversizeBanlistAlign())
        return qfalse;

    banList_t *this;
    int i;


    this = banlist;
    if(!this)
        return qfalse;

    int type;

    if(uid > 0){
        type = 0;
    }else if(guid && strlen(guid) == 8){
        type = 1;
    }else{
        return qfalse;
    }

    if(!SV_ReloadBanlist())
        return qfalse;

    for(i = 0 ; i < current_banindex; this++, i++){
        switch(type)
        {
            case 0:
                if(uid != this->playeruid)
                    continue;

            break;
            case 1:
                if(Q_stricmp(guid, this->pbguid))
                    continue;
        }
        break;
    }
    if(i == current_banindex){
        current_banindex++; //Rise the array index

    }else{
        if(type == 0){
            Com_Printf( "Modifying banrecord for player uid: %i\n", uid);
            SV_PrintAdministrativeLog( "modified banrecord of player uid: %i:", uid);
        }else{
            Com_Printf( "Modifying banrecord for player guid: %s\n", guid);
            SV_PrintAdministrativeLog( "modified banrecord of player guid: %s:", guid);
        }
    }

    this->playeruid = uid;
    this->adminuid = auid;
    this->expire = expire;

    if(banreason)
        Q_strncpyz(this->reason, banreason, sizeof(this->reason));
    else
        *this->reason = 0;

    if(guid && type)
        Q_strncpyz(this->pbguid, guid, sizeof(this->pbguid));
    else
        *this->pbguid = 0;

    if(name)
        Q_strncpyz(this->playername, name, sizeof(this->playername));
    else
        *this->playername = 0;

    SV_WriteBanlist();
    return qtrue;
}