
/* AMX Mod script.
*
* Slots reservation by IP (reserveslotsip.sma)
*
* (c) Copyright 2002, SYZo
* This file is provided as is (no warranties).
- 
* Slots reservation by IP (reserveslotsip.amx) working only with Slots reservation (adminslots.amx)
* 
* Usage
* Create file cstrike/addons/amx/ips.ini
* ips.ini file format:
* xxx.xxx.xxx.xxx/yyy.yyy.yyy.yyy
* where xxx.xxx.xxx.xxx - The address of the network
*       yyy.yyy.yyy.yyy - Mask of a subnet
* For example:
* 192.168.224.0/255.255.224.0
* 192.168.176.0/255.255.240.0
* 192.168.76.97/255.255.255.255
* WARNING:
* Correct plugin.ini
* insert "reserveslotsip.amx" before adminslots.amx
* For example:
* welcome.amx
* admin.amx
* adminslay2.amx
* reserveslotsip.amx
* adminslots.amx
* adminvote.amx
* amx_atac.amx
* 
*/

#include <amxmod>
#include <string>

ip_to_number(userip[16]) 
{
    new ipb1[12]
    new ipb2[12]
    new ipb3[12]
    new ipb4[12]
    new ip
    new nipb1
    new nipb2
    new nipb3
    new nipb4
    new uip[16]
    copy(uip, 16, userip)
    while(replace(uip, 16, ".", " ")){}
    parse(uip, ipb1, 12, ipb2, 12, ipb3, 12, ipb4, 12)
    nipb1 = strtonum(ipb1)
    nipb2 = strtonum(ipb2)
    nipb3 = strtonum(ipb3)
    nipb4 = strtonum(ipb4)
    ip = ((((nipb1 * 256) + nipb2) * 256) + nipb3) + ((((((nipb1 * 256) + nipb2) * 256) + nipb3) * 255) + nipb4)    
    return ip
}

net_hi_num(addr, mask) 
{
    return addr+4294967296-mask
}

net_belongs(ipaddr, ipmask, ipaddrin, ipmaskin) 
{
    if (ipaddr >= ipaddrin && net_hi_num(ipaddr, ipmask) <= net_hi_num(ipaddrin, ipmaskin))  {
	return 1
    }
    return 0
}

public client_connect(id){
    if (file_exists("addons\amx\ips.ini")) {
	new readdata[50]
	new sipaddr[16]
	new sipmask[16]
	new len
	new userip[16]
	get_user_ip(id,userip,16,1)
	for(new i=0; i < 100 && read_file("addons\amx\ips.ini",i,readdata,50,len) != 0; i += 1) {
	    read_file("addons\amx\ips.ini",i,readdata,50,len)
	    replace(readdata, 50, "/", " ")
	    parse(readdata, sipaddr, 16, sipmask, 16)
	    if (net_belongs(ip_to_number(userip), ip_to_number("255.255.255.255"), ip_to_number(sipaddr), ip_to_number(sipmask)) == 1) {
		set_user_flags(id,read_flags("b"))
	    }
	}
    }
    return PLUGIN_CONTINUE
}

public plugin_init() {
    register_plugin("Slots reservation by IP","0.1","SYZo")
    return PLUGIN_CONTINUE
}

