3 string ban_ip[BAN_MAX];
4 float ban_expire[BAN_MAX];
22 for(i = 0; i < ban_count; ++i)
24 if(time > ban_expire[i])
26 out = strcat(out, " ", ban_ip[i]);
27 out = strcat(out, " ", ftos(ban_expire[i] - time));
29 if(strlen(out) <= 1) // no real entries
30 cvar_set("g_banned_list", "");
32 cvar_set("g_banned_list", out);
35 float Ban_Delete(float i)
41 if(ban_expire[i] == 0)
54 for(i = 0; i < ban_count; ++i)
58 n = tokenize_sane(cvar_string("g_banned_list"));
59 if(stof(argv(0)) == 1)
61 ban_count = (n - 1) / 2;
62 for(i = 0; i < ban_count; ++i)
64 ban_ip[i] = strzone(argv(2*i+1));
65 ban_expire[i] = time + stof(argv(2*i+2));
74 for(i = 0; i < ban_count; ++i)
76 if(time > ban_expire[i])
78 msg = strcat("#", ftos(i), ": ");
79 msg = strcat(msg, ban_ip[i], " is still banned for ");
80 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
85 float Ban_GetClientIP(entity client)
88 n = tokenizebyseparator(client.netaddress, ".");
91 ban_ip1 = strcat1(argv(0));
92 ban_ip2 = strcat(ban_ip1, ".", argv(1));
93 ban_ip3 = strcat(ban_ip2, ".", argv(2));
94 ban_ip4 = strcat(ban_ip3, ".", argv(3));
98 float Ban_IsClientBanned(entity client)
103 if(!Ban_GetClientIP(client))
105 for(i = 0; i < ban_count; ++i)
108 if(time > ban_expire[i])
111 if(ban_ip1 == s) return TRUE;
112 if(ban_ip2 == s) return TRUE;
113 if(ban_ip3 == s) return TRUE;
114 if(ban_ip4 == s) return TRUE;
119 float Ban_MaybeEnforceBan(entity client)
121 if(Ban_IsClientBanned(client))
124 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
132 float Ban_Insert(string ip, float bantime)
138 for(i = 0; i < ban_count; ++i)
141 // do we have a free slot?
142 for(i = 0; i < ban_count; ++i)
143 if(time > ban_expire[i])
145 // no free slot? Then look for the one who would get unbanned next
149 bestscore = ban_expire[i];
150 for(j = 1; j < ban_count; ++j)
152 if(ban_expire[j] < bestscore)
155 bestscore = ban_expire[i];
159 // if we replace someone, will we be banned longer than him (so long-term
160 // bans never get overridden by short-term bans)
161 if(ban_expire[i] > time + bantime)
163 // okay, insert our new victim as i
165 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
166 ban_expire[i] = time + bantime;
167 ban_ip[i] = strzone(ip);
168 ban_count = max(ban_count, i + 1);
175 void Ban_KickBanClient(entity client, float bantime, float masksize)
177 if(!Ban_GetClientIP(client))
186 Ban_Insert(ban_ip1, bantime);
189 Ban_Insert(ban_ip2, bantime);
192 Ban_Insert(ban_ip3, bantime);
195 Ban_Insert(ban_ip4, bantime);
202 float GameCommand_Ban(string command)
210 argc = tokenize_sane(command);
211 if(argv(0) == "help")
213 print(" kickban # n m p - kickban player n for m seconds, using mask size p (1 to 4)\n");
214 print(" ban ip m - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
215 print(" bans - list all existing bans\n");
216 print(" unban n - delete the entry #n from the bans list\n");
219 if(argv(0) == "kickban")
223 entno = stof(argv(2));
224 if(entno > maxclients || entno < 1)
226 client = edict_num(entno);
228 bantime = stof(argv(3));
230 bantime = cvar("g_ban_default_bantime");
232 masksize = stof(argv(4));
234 masksize = cvar("g_ban_default_masksize");
235 Ban_KickBanClient(client, bantime, masksize);
239 else if(argv(0) == "ban")
246 bantime = stof(argv(2));
248 bantime = cvar("g_ban_default_bantime");
249 Ban_Insert(ip, bantime);
253 else if(argv(0) == "bans")
258 else if(argv(0) == "unban")