Autocheck if a person was banned previously
#1

Hello to all. Today I wanna implement a function in my admin tool filterscript. As I'm new on INI and similars and never saved text to .txt files, I would like to know how to do few things.

- OnPlayerConnect:

Save name of the player on a newline in a txt file (/n playername)
Save ip of the previous guy in another txt file


OnPlayerUpdate (maybe I'm wrong but I think it's correct)

If he got banned when next login, this system check into the previous 2 files if in him account name and IP there is a ban. If so, get kicked with date, him details and the reason.


I dunno if for this is better MySQL or anything else. As I've never used it, I'm totally on Yours hand. I will give a big thanx to who will help me out with this!
Reply
#2

This should work. For banning check the functions BanIp and BanName.

pawn Код:
public OnPlyerConnect(playerid)
{
    if(IsPlayerNameBanned(playerid))
    {
        SendClientMessage(playerid, -1, "Your name is banned");
        Kick(playerid);
    }
    else if(IsPlayerIpBanned(playerid))
    {
        SendClientMessage(playerid, -1, "Your ip is banned");
        Kick(playerid);
    }
}

IsPlayerNameBanned(playerid)
{
    new name[ MAX_PLAYER_NAME +1 ],
        File:file = fopen("bans_name.txt", io_read),
        buffer[256];

    GetPlayerName(playerid, name, sizeof(name));
    if(file)
    {
        while(fread(file, buffer)) // this will read 1 line.
        {
            StripNewLine(buffer); // This removes the symbols \r and \n
            if(!strcmp(buffer, name))
            {
                fclose(file);
                return true;
            }
        }
        fclose(file);
    }
    return false;
}

IsPlayerNameBanned(playerid)
{
    new ip[ 16 ],
        File:file = fopen("bans_ip.txt", io_read),
        buffer[256];

    GetPlayerIp(playerid, ip, sizeof(ip));
    if(file)
    {
        while(fread(file, buffer)) // this will read 1 line.
        {
            StripNewLine(buffer); // This removes the symbols \r and \n
            if(!strcmp(buffer, ip))
            {
                fclose(file);
                return true;
            }
        }
        fclose(file);
    }
    return false;
}


BanIp(playerid, name[])
{
    new File:file = fopen("bans_name.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, name);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

BanName(playerid, ip[])
{
    new File:file = fopen("bans_ip.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, ip);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

StripNewLine(string[])
{
    new len = strlen(string);
    if(!string[0])
        return;

    if((string[len - 1] == '\n') || (string[len - 1] == '\r'))
    {
        string[len - 1] = 0;
        if(string[0]==0) return ;
        if((string[len - 2] == '\n') || (string[len - 2] == '\r'))
            string[len - 2] = 0;
    }
}
Reply
#3

Quote:
Originally Posted by dusk
Посмотреть сообщение
This should work. For banning check the functions BanIp and BanName.

pawn Код:
public OnPlyerConnect(playerid)
{
    if(IsPlayerNameBanned(playerid))
    {
        SendClientMessage(playerid, -1, "Your name is banned");
        Kick(playerid);
    }
    else if(IsPlayerIpBanned(playerid))
    {
        SendClientMessage(playerid, -1, "Your ip is banned");
        Kick(playerid);
    }
}

IsPlayerNameBanned(playerid)
{
    new name[ MAX_PLAYER_NAME +1 ],
        File:file = fopen("bans_name.txt", io_read),
        buffer[256];

    GetPlayerName(playerid, name, sizeof(name));
    if(file)
    {
        while(fread(file, buffer)) // this will read 1 line.
        {
            StripNewLine(buffer); // This removes the symbols \r and \n
            if(!strcmp(buffer, name))
            {
                fclose(file);
                return true;
            }
        }
        fclose(file);
    }
    return false;
}

IsPlayerNameBanned(playerid)
{
    new ip[ 16 ],
        File:file = fopen("bans_ip.txt", io_read),
        buffer[256];

    GetPlayerIp(playerid, ip, sizeof(ip));
    if(file)
    {
        while(fread(file, buffer)) // this will read 1 line.
        {
            StripNewLine(buffer); // This removes the symbols \r and \n
            if(!strcmp(buffer, ip))
            {
                fclose(file);
                return true;
            }
        }
        fclose(file);
    }
    return false;
}


BanIp(playerid, name[])
{
    new File:file = fopen("bans_name.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, name);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

BanName(playerid, ip[])
{
    new File:file = fopen("bans_ip.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, ip);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

StripNewLine(string[])
{
    new len = strlen(string);
    if(!string[0])
        return;

    if((string[len - 1] == '\n') || (string[len - 1] == '\r'))
    {
        string[len - 1] = 0;
        if(string[0]==0) return ;
        if((string[len - 2] == '\n') || (string[len - 2] == '\r'))
            string[len - 2] = 0;
    }
}
Very appreciated help!

i got 2 errors:

error 017: undefined symbol "IsPlayerIpBanned"
error 021: symbol already defined: "IsPlayerNameBanned"


Do You know how to solve them?


This is my filterscript:

HERE

But if i ban someone, him details will automatically get saved or i need to modify my ban CMD?
Reply
#4

You got the IsPlayerNameBanned TWICE!! Just rename the second one on IsPlayerIpBanned
Reply
#5

Quote:
Originally Posted by Matess
Посмотреть сообщение
You got the IsPlayerNameBanned TWICE!! Just rename the second one on IsPlayerIpBanned
You are right, I've not noticed it my bad.

Now i have this:

warning 203: symbol is never used: "BanIp"
warning 203: symbol is never used: "BanName"

As i have mentioned above, to auto update the ban name and ban ip txts i need to modify in some way my ban CMD or there is a way to call that functions when i ban a guy?
Reply
#6

These two functions are for you to use when you want to ban someone.

It may be a command:
pawn Код:
CMD:banplayername(playerid, params[])
{
    if(isnull(params))
       SendClientMessage(playerid, -1, "Usage /banplayername [ name ]");
   // You should also validate if the user used a valid player name.
    BanName(params);
    return 1;
}
Also, I added a useless parameter to those functions, so change them to:

pawn Код:
BanIp(name[])
{
    new File:file = fopen("bans_name.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, name);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

BanName(ip[])
{
    new File:file = fopen("bans_ip.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, ip);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}
Reply
#7

Quote:
Originally Posted by dusk
Посмотреть сообщение
These two functions are for you to use when you want to ban someone.

It may be a command:
pawn Код:
CMD:banplayername(playerid, params[])
{
    if(isnull(params))
       SendClientMessage(playerid, -1, "Usage /banplayername [ name ]");
   // You should also validate if the user used a valid player name.
    BanName(params);
    return 1;
}
Also, I added a useless parameter to those functions, so change them to:

pawn Код:
BanIp(name[])
{
    new File:file = fopen("bans_name.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, name);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

BanName(ip[])
{
    new File:file = fopen("bans_ip.txt", io_append); // io_append will create the file it does not exist
    if(file)
    {
        fwrite(file, ip);
        fwrite(file, "\r\n"); // a new line
    }
    fclose(file);
}

Код:
CMD:kick(playerid, params[])
{
	new id, reason[128];
	if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
	else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /kick [id/name][reason]");
	else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick yourself!");
	else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not kick another admin!");
 	else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
 	else {
 	    new Name[MAX_PLAYER_NAME], KickMessage[128];
		new Name2[MAX_PLAYER_NAME];
		GetPlayerName(playerid, Name, sizeof(Name));
		GetPlayerName(id, Name2, sizeof(Name2));
		format(KickMessage, sizeof(KickMessage), "%s(%d) has kicked player %s(%d). Reason: %s", Name, playerid, Name2, id);
		SendClientMessageToAll(COLOR_WHITE, KickMessage);
		Kick(id);
	}
	return 1;
}

CMD:ban(playerid, params[])
{
	new id, reason[128];
	if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
	else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /ban [id/name][reason]");
	else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban yourself!");
	else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban another admin!");
 	else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
 	else {
 	    new Name3[MAX_PLAYER_NAME], BanMessage[128];
		new Name4[MAX_PLAYER_NAME];
		GetPlayerName(playerid, Name3, sizeof(Name3));
		GetPlayerName(id, Name4, sizeof(Name4));
		format(BanMessage, sizeof(BanMessage), "%s(%d) has Banned player %s(%d). Reason: %s", Name3, playerid, Name4, id);
		SendClientMessageToAll(COLOR_WHITE, BanMessage);
		Ban(id);
	}
	return 1;
}
I already have my ban and kick code, how to implement yours to mine?
Reply
#8

Well I don't know what does the /kick command have to do with anything here but in /ban just change the native Ban() to BanIp if you want to ban his IP or too BanIp AND BanName to ban both:

pawn Код:
CMD:ban(playerid, params[])
{
    new id, reason[128];
    if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
    else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /ban [id/name][reason]");
    else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban yourself!");
    else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban another admin!");
    else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
    else {
        new Name3[MAX_PLAYER_NAME], BanMessage[128];
        new Name4[MAX_PLAYER_NAME];
        GetPlayerName(playerid, Name3, sizeof(Name3));
        GetPlayerName(id, Name4, sizeof(Name4));
        format(BanMessage, sizeof(BanMessage), "%s(%d) has Banned player %s(%d). Reason: %s", Name3, playerid, Name4, id);
        SendClientMessageToAll(COLOR_WHITE, BanMessage);
        BanName(Name4);
                GetPlayerIp(id, Name4, sizeof(Name4));
                BanIp(Name4);
    }
    return 1;
}
This code will ban both by name AND by ip.
Reply
#9

Quote:
Originally Posted by dusk
Посмотреть сообщение
Well I don't know what does the /kick command have to do with anything here but in /ban just change the native Ban() to BanIp if you want to ban his IP or too BanIp AND BanName to ban both:

pawn Код:
CMD:ban(playerid, params[])
{
    new id, reason[128];
    if(!IsPlayerAdmin(playerid))return SendClientMessage(playerid, COLOR_WHITE, "You need to be a admin to use that command!");
    else if(sscanf(params, "us", id, reason))SendClientMessage(playerid, COLOR_WHITE, "Usage: /ban [id/name][reason]");
    else if(id==playerid)SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban yourself!");
    else if(IsPlayerAdmin(id))SendClientMessage(playerid,COLOR_WHITE,"Error: You can not ban another admin!");
    else if (id==INVALID_PLAYER_ID)SendClientMessage(playerid,COLOR_WHITE,"Error: Player is not connected!");
    else {
        new Name3[MAX_PLAYER_NAME], BanMessage[128];
        new Name4[MAX_PLAYER_NAME];
        GetPlayerName(playerid, Name3, sizeof(Name3));
        GetPlayerName(id, Name4, sizeof(Name4));
        format(BanMessage, sizeof(BanMessage), "%s(%d) has Banned player %s(%d). Reason: %s", Name3, playerid, Name4, id);
        SendClientMessageToAll(COLOR_WHITE, BanMessage);
        BanName(Name4);
                GetPlayerIp(id, Name4, sizeof(Name4));
                BanIp(Name4);
    }
    return 1;
}
This code will ban both by name AND by ip.
The kick command is a copy paste of my code, You can fully ignore it.
Ok, thanks for the completion but in this wai it will save the user dato on the txt file right?
Reply
#10

Yes, the player name will appear in "bans_name.txt" file and player ip will be in "bans_ip.txt", both of which will be in your scriptfiles directory.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)