[Include] [WIP] Ban system
#1

Hey,

I've been working on this ban system on and off for a while now, and I think it's reached a stable testing state.

Worth mentioning is IPs are dealt with as integers, not strings. All IPs are tagged with IP:.

Another thing that's probably worth mentioning is it doesn't require any configuration what so ever, and it uses SQLite.

Functions
GetDotDecimalIP(IP:ip) Returns a string containing the IP in human-readable format (i.e. 127.0.0.1).
IP:GetIntegerIP(ip[]) Convert a dot-decimal IP string to an integer IP.
IP:GetPlayerIP Returns the integer IP for a player.
IsIPInRange(IP:ip, range)
IsIPInRange(ip[], range)
Returns true if the IP is within the specified IP range. The 1st argument can be an integer IP or a string.
GetDurationFromString(duration[]) Convert a human-readable duration into seconds. Examples of valid input:
  • "2 weeks 5 days"
  • "1 month and 3 weeks"
  • "1 year -2 months" (gives 10 months)
  • "1y2m"
  • "1hr20min"
GetStringFromDuration(duration) Opposite of above (i.e. 9000 => 2 hours 30 minutes).
GetUnixTimestamp() Returns the current UNIX timestamp.
BanIP(IP:ip, duration = DURATION_PERMANENT)
BanIP(ip[], duration = DURATION_PERMANENT)
Ban an IP and, optionally, set the duration of that ban. The duration is in seconds.
See GetDurationFromString for an easy way to generate a duration.
Returns the ban index.
BanIPRange(IPRange:range, duration = DURATION_PERMANENT)
Same as above, but for IP ranges.
Returns the ban index.
BanPlayer(iPlayer, iDuration = DURATION_PERMANENT)
Ban a player by their IP.
Returns the ban index.
LiftBan(ban_index) Remove a ban.
GetIPBanIndex(IP:ip)
GetIPBanIndex(ip[])
Returns the ban index for the first active ban found on that IP, 0 if no bans.
GetPlayerBanIndex(playerid) Same as above, but for player ID.
GetBanType(ban_index) Returns the ban type for a ban. Possible return values:
  • BAN_TYPE_NONE - Probably invalid ban index
  • BAN_TYPE_SINGLE - IP is banned
  • BAN_TYPE_RANGE - IP is inside banned range
GetIPRange(ip[], ip[])
GetIPRange(IP:ip, IP:ip)
GetIPRange(ip_range[])
Returns an IP range either between argument 1 and 2, or generated by wildcards (*) in argument 1.
Example: GetIPRange("192.168.0.0", "192.168.255.255") is the same as GetIPRange("192.168.*.*").
IsIPInRange(IP:ip, IPRange:range)
IsIPInRange(ip[], IPRange:range)
Returns true if the IP (1st argument) is inside the IP range (2nd argument).
The IP passed to the function can be either a string or a cell.
See GetIPRange or the IP_RANGE macro to get IP ranges.
SetBanInfoInt(ban_index, key[], value)
SetBanInfoFloat(ban_index, key[], Float:value)
SetBanInfoString(ban_index, key[], value[])
Set values associated with ban indexes (for example, reason, user id, etc.).
GetBanInfoInt(ban_index, key[])
GetBanInfoFloat(ban_index, key[])
GetBanInfoString(ban_index, key[])
GetBanInfoStringCopy(ban_index, key[], destination[])
Read values stored by SetBanInfo*. Note that the max string size for GetBanInfoString is 256. For larger strings, use GetBanInfoStringCopy.
GetIPBanIndices(ip[], destination[])
GetIPBanIndices(IP:ip, destination[])
Fill the destination array with all active ban indices (indexes) and return the number of indices found.
GetPlayerBanIndices(playerid, destination[])
Same as above, but for player ID.
Macros
IP(x.x.x.x)
Gives an integer IP.
Example: IP(127.0.0.1)
IP_RANGE(x.x.x.x - x.x.x.x)
Gives an IP range.
Example: IP_RANGE(192.168.0.0 - 192.168.0.255)
IP_OCTET(ip, position)
Gives the value of a single octet in an IP.
Example: IP_OCTET(IP(127.0.0.1), 1) will give 127.
IPRange<name> Used when declating IP range variables (IP ranges are enums).
DURATION(x)
Like GetDurationFromString, but during compile-time. Has to follow the syntax below.
Example: DURATION(1 hour, 20 minutes) gives 3620.
LOOP_IP_BANS(ban_index : IP:ip)
LOOP_IP_BANS(ban_index : ip[])
LOOP_IP_BANS(new ban_index : IP:ip)
LOOP_IP_BANS(new ban_index : ip[])
Loop all bans for a specific IP. I designed it to resemble foreach, for consistency.
Example: LOOP_IP_BANS(new ban : "127.0.0.1") printf("Ban index: %d", ban);
LOOP_PLAYER_BANS(ban_index : playerid)
LOOP_PLAYER_BANS(new_ban_index_:_playerid)
Loop all bans for a specific IP. I designed it to resemble foreach, for consistency.
Example: LOOP_PLAYER_BANS(new ban : playerid) printf("Ban index: %d", ban);
Examples
Ban a player
pawn Код:
new ban = BanPlayer(playerid, DURATION(2 hours, 30 minutes));

SetBanInfoString(ban, "reason", "Auto-ban for doing whatever you're not supposed to..");
Check if a player is banned
pawn Код:
public OnPlayerConnect(playerid) {
    new ban = GetPlayerBanIndex(playerid);
   
    if (ban) {
        new message[128];
       
        if (GetBanType(ban) == BAN_TYPE_RANGE)
            format(message, 128, "* Your IP is inside a range-ban, reason: %s", GetBanInfoString(ban, "reason"));
        else
            format(message, 128, "* You are banned, reason: %s", GetBanInfoString(ban, "reason"));
       
        SendClientMessage(playerid, 0xCC0000FF, message);
       
        // This will output something like: "* You may come back in 1 day and 13 hours"
        format(message, 128, "* You may come back in %s.", GetStringFromDuration(GetBanInfoInt(ban, "time_left")));
        SendClientMessage(playerid, 0xCC0000FF, message);
       
        Kick(playerid);
       
        return 0;
    }
   
    return 1;
}
Changes
2011-11-25
  • Fixed a bug causing BanIP to fail.
2011-11-18
  • Minor bug fixes.
  • BanIPRange now accepts both strings IP ranges (IPRange<>).
  • New functions:
    • GetBanIPRange
    • GetIPString
    • GetIPRangeString
    • ClearBanDB
  • To facilitate DB upgrades, tables will now be created according to gs_Tables, and missing fields (if any) will be searched for & created from gs_Columns.
2011-11-08
  • Added automatic table creation.
  • The database will have VACUUM done on initialization.
  • GetStringFromDuration now puts commas and, if applicable, it puts "and" before the last part.
  • Packed a bunch of print and db_query statements to save space.
  • Rewrote EscapeSQLiteString to be approx. 160% faster.
  • Added Get(IP/Player)BanIndices.
  • Added LOOP_IP_BANS and LOOP_PLAYER_BANS.
  • Fixed a few bugs related to improper buffer usage.
Download

http://pastebin.com/R12ZpSjc
Reply


Messages In This Thread
Ban system - by Slice - 07.11.2011, 20:41
Re: [WIP] Ban system - by Pinguinn - 07.11.2011, 20:42
Respuesta: [WIP] Ban system - by Manuel7284 - 07.11.2011, 21:34
Re: [WIP] Ban system - by Fool - 08.11.2011, 03:09
Re: [WIP] Ban system - by RBTDM - 08.11.2011, 07:41
Re: [WIP] Ban system - by iPLEOMAX - 08.11.2011, 08:16
Re: [WIP] Ban system - by Slice - 08.11.2011, 08:36
Re: [WIP] Ban system - by Simon - 08.11.2011, 08:40
Re: [WIP] Ban system - by Slice - 08.11.2011, 08:52
Re: [WIP] Ban system - by Darius - 08.11.2011, 09:18
Re: [WIP] Ban system - by Simon - 08.11.2011, 09:58
Re: [WIP] Ban system - by Slice - 08.11.2011, 13:52
Re: [WIP] Ban system - by Mr_Scripter - 08.11.2011, 14:15
Re: [WIP] Ban system - by Slice - 10.11.2011, 17:52
Re: [WIP] Ban system - by AndreT - 10.11.2011, 18:10
Re : [WIP] Ban system - by Habdel - 10.11.2011, 18:33
Re: [WIP] Ban system - by Slice - 10.11.2011, 19:01
Re: [WIP] Ban system - by TheArcher - 10.11.2011, 19:13
Re: [WIP] Ban system - by AndreT - 10.11.2011, 20:45
Re: [WIP] Ban system - by Slice - 11.11.2011, 07:02
Re: [WIP] Ban system - by Calgon - 11.11.2011, 14:00
Re: [WIP] Ban system - by Slice - 15.11.2011, 13:33
Re: [WIP] Ban system - by thiaZ_ - 15.11.2011, 15:31
Re: [WIP] Ban system - by Pinguinn - 15.11.2011, 16:09
Re: [WIP] Ban system - by Slice - 15.11.2011, 16:22
Re: [WIP] Ban system - by Chris# - 15.11.2011, 16:54
Re: [WIP] Ban system - by Astralis - 15.11.2011, 19:28
Re: [WIP] Ban system - by Scenario - 16.11.2011, 02:41
Re: [WIP] Ban system - by Slice - 16.11.2011, 05:30
Re: [WIP] Ban system - by Slice - 16.11.2011, 18:58
Re: [WIP] Ban system - by TheArcher - 05.09.2012, 18:25
Re: [WIP] Ban system - by Ada32 - 06.01.2014, 12:44
Re: [WIP] Ban system - by Sojo12 - 11.01.2014, 08:59

Forum Jump:


Users browsing this thread: 1 Guest(s)