IP/Account ban using MySQL
#1

Hey guys, I use MySQL. But I still do not have a clear idea on how to ban the players IP/Account and store the information in the database. For example in the database, the columns in the ban table would be:

- Player Name
- Reason
- Banned by
- Ban date
- Unban date
- Ban length

I don't have a clear idea on how to actually do that. Can you explain using my anti - jetpack ban.

pawn Код:
public OnPlayerUpdate(playerid)
{
    if(GetPlayerSpecialAction(playerid) == SPECIAL_ACTION_USEJETPACK)
    {
        for(new i = 0; i < 50; i++) SendClientMessage(playerid, COLOR_WHITE," ");
        new str[128],
            name[MAX_PLAYER_NAME];
        GetPlayerName(playerid,name,MAX_PLAYER_NAME);
        format(str, sizeof (str), "[AUTO - BAN] %s has been automatically banned from the server. Reason: Spawning a jetpack!", name);
        SendClientMessageToAll(RED, str);
        new string[952], pName[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pName, sizeof(pName));
        format(string, sizeof string, "{FF0000}Name: %s\n", pName);
        strcat(string, "{FF0000}Ban reason: Spawning a jetpack\n");
        strcat(string, "{FF0000}Banned by: Anti - Cheat\n");
        strcat(string, "{FF0000}Ban length: Permenant\n");
        strcat(string, "{FF0000}Date of unban: Never\n");
        strcat(string, "\n");
        strcat(string, "{FF0000}If you think this ban is unfair, please take a screenshot by pressing F8, and make an unban appeal at our forums\n");
        strcat(string, "\n");
        strcat(string, "{FF0000}Note - We do not unban cheaters or people who lie in their application!\n");
        ShowPlayerDialog(playerid, DIALOG_BAN, DIALOG_STYLE_MSGBOX, "{FFD000}Banned!", string, "Close", "");
        pInfo[playerid][pBanned] = 1;
    }
    return 1;
}
Please help - appreciated

Thanks!
Reply
#2

Can someone please help me?
Reply
#3

Create a new table named 'bans', then check when the player connects, before you show the login dialog, if the row with the playername exists in the table 'bans'. If yes, kick him again.
Reply
#4

I know that, but I need help inputting the players ban data into the ban table!
Reply
#5

It's exactly the same as the code you use when you register a player, the only difference is the things you send to the database, and the name of the table.

EDIT: this is the code I use to send the player's name to the database
pawn Код:
format(Query,sizeof(Query),"INSERT INTO `bans` (Name) VALUES ('%s')",GetPlayerNameEx(tid));//tid is the id of the player that's getting banned.
Reply
#6

Use the gettime() function to obtain the current timestamp. You'll want to keep this as the "bantime" in the database. Then, add the amount of milliseconds you want the player to be banned for (remember, 1000ms = 1 second; 60000ms = 1 minute; 60000*60 = 1 hour; 60000*60*24 = 1 day). Save that as the unban time.

It might be a good idea to have a ban type. So for example, temp bans and perm bans. If perm ban = 1, they will never be unbanned.

When the player connects, check their username/IP against the ban table. If gettime() is >= the unban time, unban the player and remove the data from the ban table. If gettime() is < the unban time, don't unban the player.

If you need more help, __ (which is a username here) made a tutorial about timestamps and how to use them!
Reply
#7

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
Use the gettime() function to obtain the current timestamp. You'll want to keep this as the "bantime" in the database. Then, add the amount of milliseconds you want the player to be banned for (remember, 1000ms = 1 second; 60000ms = 1 minute; 60000*60 = 1 hour; 60000*60*24 = 1 day). Save that as the unban time.

It might be a good idea to have a ban type. So for example, temp bans and perm bans. If perm ban = 1, they will never be unbanned.

When the player connects, check their username/IP against the ban table. If gettime() is >= the unban time, unban the player and remove the data from the ban table. If gettime() is < the unban time, don't unban the player.

If you need more help, __ (which is a username here) made a tutorial about timestamps and how to use them!
I get what you mean, but can you make an example?
Reply
#8

Sure! This is what I have in one of my GM's.

Under OnPlayerConnect, I have this:

pawn Код:
new
        szQuery[128]
    ;
   
    format(szQuery, sizeof(szQuery), "SELECT * FROM `Bans` WHERE `Username` = '%s' OR `ipAddress` = '%s'", getEscName(playerid), szPlayerIP[playerid]);
    mysql_function_query(dbHandle, szQuery, true, "initConnection", "d", playerid);
The function which handles the response:

pawn Код:
forward initConnection(playerid);
public initConnection(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows == 1)
    {
        new szIP[16], szUsername[26], szAdminName[26], szReason[60], temp[20];
        cache_get_row(0, 0, temp);
        new iBanID = strval(temp);
        cache_get_row(0, 1, szIP);
        cache_get_row(0, 2, szUsername);
        cache_get_row(0, 3, szAdminName);
        cache_get_row(0, 4, szReason);
        cache_get_row(0, 5, temp);
        new iBannedTimestamp = strval(temp);
        cache_get_row(0, 6, temp);
        new iUnbannedTimestamp = strval(temp);

        if(gettime() >= iUnbannedTimestamp) // checking if the timestamps are allowing the player to be unbanned
        {
            new szQuery[128];
           
            // remove the ban from the bans table
            format(szQuery, sizeof(szQuery), "DELETE FROM `Bans` WHERE `banID` = %d", iBanID);
            mysql_function_query(dbHandle, szQuery, false, "databaseCallback", "");

            // check if an account for this username exists
            format(szQuery, sizeof(szQuery), "SELECT * FROM `Accounts` WHERE `Username` = '%s'", getEscName(playerid));
            mysql_function_query(dbHandle, szQuery, true, "accountCheck", "d", playerid);
        }
        else // the player is still banned
        {
            clearPlayerChat(playerid);

            new szString[128];
            SendClientMessage(playerid, COLOR_RED, "You are banned from this server.");
            SendClientMessage(playerid, -1, " ");
            format(szString, sizeof(szString), "You were banned by {E70000}%s {FFFFFF}on %s.", szAdminName, date(iBannedTimestamp, 2));
            SendClientMessage(playerid, COLOR_WHITE, szString);
            format(szString, sizeof(szString), "Reason: %s", szReason);
            SendClientMessage(playerid, COLOR_WHITE, szString);
            SendClientMessage(playerid, -1, " ");
            format(szString, sizeof(szString), "You will be automatically unbanned on %s.", date(iUnbannedTimestamp, 2));
            SendClientMessage(playerid, COLOR_WHITE, szString);
            KickEx(playerid);
        }
    }
    else if(rows == 0) // no bans exist, continue to check if an account exists
    {
        new szQuery[128];
        format(szQuery, sizeof(szQuery), "SELECT * FROM `Accounts` WHERE `Username` = '%s'", getEscName(playerid));
        mysql_function_query(dbHandle, szQuery, true, "accountCheck", "d", playerid);
    }
    else // more rows were returned than necessary, something is wrong
    {
        clearPlayerChat(playerid);
        SendClientMessage(playerid, COLOR_RED, "There was an error during ban-checks. If it continues, please contact the development team.");
        KickEx(playerid);
    }
    return 1;
}
The /ban command:

pawn Код:
CMD:ban(playerid, params[])
{
    new iID = -1, szReason[51], iDays = 0, iMonths = 0, iYears = 0;
    if(pStats[playerid][pAdmin] < 1)
        return 1;
    if(sscanf(params, "uddds[50]", iID, iYears, iMonths, iDays, szReason))
        return SendClientMessage(playerid, COLOR_WHITE, "SYNTAX: /ban [nick/id] [years] [months] [days] [reason] "),
        SendClientMessage(playerid, COLOR_WHITE, "HINT: If you enter 999 for years the ban will be permanent.");
    if(playerid == iID)
        return SendClientMessage(playerid, COLOR_RED, "For security reasons, you cannot ban yourself.");
    if(!IsPlayerConnected(iID))
        return SendClientMessage(playerid, COLOR_RED, "Invalid nickname/ID.");
    if(iDays < 0 || iMonths < 0 || iYears < 0)
        return SendClientMessage(playerid, COLOR_RED, "You have entered an invalid value for days, months, and/or years.");
    if(pStats[playerid][pAdmin] <= pStats[iID][pAdmin])
        return SendClientMessage(playerid, COLOR_RED, "ERROR: You cannot ban an administrator with an equal or greater administrative rank than you.");    
       
    new szEscReason[52];
    mysql_real_escape_string(szReason, szEscReason, 1);

    new iFinalCalculation = gettime() + mktime(0, 0, 0, iDays, iMonths, iYears);

    new szQuery[300];
    format(szQuery, sizeof(szQuery), "INSERT INTO `Bans` (`ipAddress`, `Username`, `Administrator`, `Reason`, `banTimestamp`, `unbanTimestamp`) VALUES('%s', '%s', '%s', '%s', %d, %d)", szPlayerIP[iID], getEscName(iID), getEscName(playerid), szEscReason, gettime(), iFinalCalculation);
    mysql_function_query(dbHandle, szQuery, false, "databaseCallback", "");

    if(iYears == 999)
    {
        new szString[128];
        format(szString, sizeof(szString), "ADMIN: %s has been permanently banned by %s. Reason: %s", getName(iID), getName(playerid), szReason);
        sendGlobalMessage(COLOR_LIGHTRED, szString);
   
        clearPlayerChat(iID);
        SendClientMessage(iID, COLOR_RED, "You are banned from this server.");
        SendClientMessage(iID, -1, " ");
        format(szString, sizeof(szString), "You were banned by {E70000}%s {FFFFFF}on %s.", getName(playerid), date(gettime(), 2));
        SendClientMessage(iID, COLOR_WHITE, szString);
        format(szString, sizeof(szString), "Reason: %s", szReason);
        SendClientMessage(iID, COLOR_WHITE, szString);
        SendClientMessage(iID, -1, " ");
        SendClientMessage(iID, COLOR_WHITE, "This ban is permanent and will not expire.");
        KickEx(iID);
    }
    else
    {
        new szString[128];
        format(szString, sizeof(szString), "ADMIN: %s has been banned by %s until %s. Reason: %s", getName(iID), getName(playerid), date(iFinalCalculation, 2), szReason);
        sendGlobalMessage(COLOR_LIGHTRED, szString);
   
        clearPlayerChat(iID);
        SendClientMessage(iID, COLOR_RED, "You are banned from this server.");
        SendClientMessage(iID, -1, " ");
        format(szString, sizeof(szString), "You were banned by {E70000}%s {FFFFFF}on %s.", getName(playerid), date(gettime(), 2));
        SendClientMessage(iID, COLOR_WHITE, szString);
        format(szString, sizeof(szString), "Reason: %s", szReason);
        SendClientMessage(iID, COLOR_WHITE, szString);
        SendClientMessage(iID, -1, " ");
        format(szString, sizeof(szString), "You will be automatically unbanned on %s.", date(iFinalCalculation, 2));
        SendClientMessage(iID, COLOR_WHITE, szString);
        KickEx(iID);
    }
    return 1;
}
Feel free to use any of it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)