]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/banning.qc
Merge remote-tracking branch 'origin/master' into samual/lightning_gun
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / banning.qc
1 // =====================================================
2 //  Banning and kicking command code, written by Samual
3 //  Last updated: December 29th, 2011
4 // =====================================================
5
6 void BanCommand_ban(float request, float argc, string command)
7 {
8         switch(request)
9         {
10                 case CMD_REQUEST_COMMAND:
11                 {
12                         if(argc >= 2)
13                         {
14                                 string ip = argv(1);
15                                 float reason_arg, bantime;
16                                 string reason;
17                                 
18                                 reason_arg = 2; 
19                                 
20                                 GET_BAN_ARG(bantime, autocvar_g_ban_default_bantime);
21                                 GET_BAN_REASON(reason, "No reason provided");
22
23                                 Ban_Insert(ip, bantime, reason, 1);
24                                 return;
25                         }
26                 }
27                         
28                 default:
29                         print("Incorrect parameters for ^2ban^7\n");
30                 case CMD_REQUEST_USAGE:
31                 {
32                         print("\nUsage:^3 sv_cmd ban address [bantime] [reason]\n");
33                         print("  'address' is the IP address or range of the player to ban,\n");
34                         print("  'bantime' is the amount of time that the ban is active (default if not provided),\n");
35                         print("  and 'reason' is the string to label the ban with as reason for banning.\n");
36                         print("See also: ^2banlist, kickban, unban^7\n");
37                         return;
38                 }
39         }
40 }
41
42 void BanCommand_banlist(float request)
43 {
44         switch(request)
45         {
46                 case CMD_REQUEST_COMMAND:
47                 {
48                         Ban_View();
49                         return;
50                 }
51                         
52                 default:
53                 case CMD_REQUEST_USAGE:
54                 {
55                         print("\nUsage:^3 sv_cmd banlist\n");
56                         print("  No arguments required.\n");
57                         print("See also: ^2ban, kickban, unban^7\n");
58                         return;
59                 }
60         }
61 }
62
63 void BanCommand_kickban(float request, float argc, string command)
64 {
65         switch(request)
66         {
67                 case CMD_REQUEST_COMMAND:
68                 {
69                         if(argc >= 2)
70                         {
71                                 entity client = GetIndexedEntity(argc, 1);
72                                 float accepted = VerifyClientEntity(client, TRUE, FALSE);
73                                 float reason_arg, bantime, masksize;
74                                 string reason;
75                                 
76                                 if(accepted > 0) 
77                                 {
78                                         reason_arg = next_token; 
79
80                                         GET_BAN_ARG(bantime, autocvar_g_ban_default_bantime);
81                                         GET_BAN_ARG(masksize, autocvar_g_ban_default_masksize);
82                                         GET_BAN_REASON(reason, "No reason provided");
83
84                                         Ban_KickBanClient(client, bantime, masksize, reason);
85                                         
86                                         return;
87                                 }
88                                 else
89                                 {
90                                         print("kickban: ", GetClientErrorString(accepted, argv(1)), ".\n"); 
91                                 }
92                         }
93                 }
94                         
95                 default:
96                         print("Incorrect parameters for ^2kickban^7\n");
97                 case CMD_REQUEST_USAGE:
98                 {
99                         print("\nUsage:^3 sv_cmd kickban client [bantime] [masksize] [reason]\n");
100                         print("  'client' is the entity number or name of the player to ban,\n");
101                         print("  'bantime' is the amount of time that the ban is active (default if not provided),\n");
102                         print("  'masksize' is the range of the IP address (1-thru-4, default if not provided),\n");
103                         print("  and 'reason' is the string to label the ban with as reason for banning.\n");
104                         print("See also: ^2ban, banlist, unban^7\n");
105                         return;
106                 }
107         }
108 }
109
110 void BanCommand_mute(float request, float argc, string command) // TODO: Add a sort of mute-"ban" which allows players to be muted based on IP/cryptokey
111 {
112         switch(request)
113         {
114                 case CMD_REQUEST_COMMAND:
115                 {
116                         if(argc >= 2)
117                         {
118                                 entity client = GetFilteredEntity(argv(1));
119                                 float accepted = VerifyClientEntity(client, TRUE, FALSE);
120                                 
121                                 if(accepted > 0) 
122                                 {
123                                         client.muted = TRUE;
124                                         return;
125                                 }
126                                 else
127                                 {
128                                         print("mute: ", GetClientErrorString(accepted, argv(1)), ".\n"); 
129                                 }
130                         }
131                 }
132                         
133                 default:
134                         print("Incorrect parameters for ^2mute^7\n");
135                 case CMD_REQUEST_USAGE:
136                 {
137                         print("\nUsage:^3 sv_cmd mute client\n");
138                         print("  'client' is the entity number or name of the player to mute.\n");
139                         print("See also: ^2unmute^7\n");
140                         return;
141                 }
142         }
143 }
144
145 void BanCommand_unban(float request, float argc)
146 {
147         switch(request)
148         {
149                 case CMD_REQUEST_COMMAND:
150                 {       
151                         if(argv(1))
152                         {
153                                 float tmp_number = -1;
154                                 string tmp_string;
155                                 
156                                 if(substring(argv(1), 0, 1) == "#")
157                                 {
158                                         tmp_string = substring(argv(1), 1, -1);
159                                         
160                                         if(tmp_string != "") // is it all one token? like #1
161                                         {
162                                                 tmp_number = stof(tmp_string);
163                                         }
164                                         else if(argc > 2) // no, it's two tokens? # 1
165                                         {
166                                                 tmp_number = stof(argv(2));
167                                         }
168                                         else
169                                                 tmp_number = -1;
170                                 }
171                                 else // maybe it's ONLY a number?
172                                 {
173                                         tmp_number = stof(argv(1));
174                                         
175                                         if((tmp_number == 0) && (argv(1) != "0"))
176                                                 { tmp_number = -1; }
177                                 }
178
179                                 if(tmp_number >= 0)
180                                 {
181                                         Ban_Delete(tmp_number);
182                                         return;
183                                 }
184                         }
185                 }
186                         
187                 default:
188                 case CMD_REQUEST_USAGE:
189                 {
190                         print("\nUsage:^3 sv_cmd unban banid\n");
191                         print("  Where 'banid' is the ID of the ban of which to remove.\n");
192                         print("See also: ^2ban, banlist, kickban^7\n");
193                         return;
194                 }
195         }
196 }
197
198 void BanCommand_unmute(float request, float argc)
199 {
200         switch(request)
201         {
202                 case CMD_REQUEST_COMMAND:
203                 {
204                         if(argc >= 2)
205                         {
206                                 entity client = GetFilteredEntity(argv(1));
207                                 float accepted = VerifyClientEntity(client, TRUE, FALSE);
208                                 
209                                 if(accepted > 0) 
210                                 {
211                                         client.muted = FALSE;
212                                         return;
213                                 }
214                                 else
215                                 {
216                                         print("unmute: ", GetClientErrorString(accepted, argv(1)), ".\n"); 
217                                 }
218                         }
219                 }
220                         
221                 default:
222                         print("Incorrect parameters for ^2mute^7\n");
223                 case CMD_REQUEST_USAGE:
224                 {
225                         print("\nUsage:^3 sv_cmd unmute client\n");
226                         print("  'client' is the entity number or name of the player to unmute.\n");
227                         print("See also: ^2mute^7\n");
228                         return;
229                 }
230         }
231 }
232
233 /* use this when creating a new command, making sure to place it in alphabetical order... also,
234 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
235 void BanCommand_(float request)
236 {
237         switch(request)
238         {
239                 case CMD_REQUEST_COMMAND:
240                 {
241                         
242                         return;
243                 }
244                         
245                 default:
246                 case CMD_REQUEST_USAGE:
247                 {
248                         print("\nUsage:^3 sv_cmd \n");
249                         print("  No arguments required.\n");
250                         return;
251                 }
252         }
253 }
254 */
255
256
257 // ==================================
258 //  Macro system for server commands
259 // ==================================
260
261 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
262 #define BAN_COMMANDS(request,arguments,command) \
263         BAN_COMMAND("ban", BanCommand_ban(request, arguments, command), "Ban an IP address or a range of addresses (like 1.2.3)") \
264         BAN_COMMAND("banlist", BanCommand_banlist(request), "List all existing bans") \
265         BAN_COMMAND("kickban", BanCommand_kickban(request, arguments, command), "Disconnect a client and ban it at the same time") \
266         BAN_COMMAND("mute", BanCommand_mute(request, arguments, command), "Disallow a client from talking by muting them") \
267         BAN_COMMAND("unban", BanCommand_unban(request, arguments), "Remove an existing ban") \
268         BAN_COMMAND("unmute", BanCommand_unmute(request, arguments), "Unmute a client") \
269         /* nothing */
270
271 void BanCommand_macro_help()
272 {
273         #define BAN_COMMAND(name,function,description) \
274                 { if(strtolower(description) != "") { print("  ^2", name, "^7: ", description, "\n"); } }
275                 
276         BAN_COMMANDS(0, 0, "")
277         #undef BAN_COMMAND
278         
279         return;
280 }
281
282 float BanCommand_macro_command(float argc, string command)
283 {
284         #define BAN_COMMAND(name,function,description) \
285                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
286                 
287         BAN_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
288         #undef BAN_COMMAND
289         
290         return FALSE;
291 }
292
293 float BanCommand_macro_usage(float argc)
294 {
295         #define BAN_COMMAND(name,function,description) \
296                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
297                 
298         BAN_COMMANDS(CMD_REQUEST_USAGE, argc, "")
299         #undef BAN_COMMAND
300         
301         return FALSE;
302 }
303
304 void BanCommand_macro_write_aliases(float fh)
305 {
306         #define BAN_COMMAND(name,function,description) \
307                 { if(strtolower(description) != "") { CMD_Write_Alias("qc_cmd_sv", name, description); } }
308         
309         BAN_COMMANDS(0, 0, "")
310         #undef BAN_COMMAND
311         
312         return;
313 }
314
315 float BanCommand(string command)
316 {
317         float argc = tokenize_console(command);
318         
319         // Guide for working with argc arguments by example:
320         // argc:   1    - 2      - 3     - 4
321         // argv:   0    - 1      - 2     - 3 
322         // cmd     vote - master - login - password
323
324         if(BanCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
325         {
326                 return TRUE; // handled by one of the above GenericCommand_* functions
327         }
328         
329         return FALSE;
330 }