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