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