Makefile: use `-I.`
[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                                                 tmp_number = stof(tmp_string);
172                                         else if (argc > 2)    // no, it's two tokens? # 1
173                                                 tmp_number = stof(argv(2));
174                                         else tmp_number = -1;
175                                 }
176                                 else  // maybe it's ONLY a number?
177                                 {
178                                         tmp_number = stof(argv(1));
179
180                                         if ((tmp_number == 0) && (argv(1) != "0")) tmp_number = -1; }
181
182                                 if (tmp_number >= 0)
183                                 {
184                                         Ban_Delete(tmp_number);
185                                         return;
186                                 }
187                         }
188                 }
189
190                 default:
191                 case CMD_REQUEST_USAGE:
192                 {
193                         LOG_INFO("\nUsage:^3 sv_cmd unban banid\n");
194                         LOG_INFO("  Where 'banid' is the ID of the ban of which to remove.\n");
195                         LOG_INFO("See also: ^2ban, banlist, kickban^7\n");
196                         return;
197                 }
198         }
199 }
200
201 void BanCommand_unmute(float request, float argc)
202 {
203         switch (request)
204         {
205                 case CMD_REQUEST_COMMAND:
206                 {
207                         if (argc >= 2)
208                         {
209                                 entity client = GetFilteredEntity(argv(1));
210                                 float accepted = VerifyClientEntity(client, true, false);
211
212                                 if (accepted > 0)
213                                 {
214                                         client.muted = false;
215                                         return;
216                                 }
217                                 else
218                                 {
219                                         LOG_INFO("unmute: ", GetClientErrorString(accepted, argv(1)), ".\n");
220                                 }
221                         }
222                 }
223
224                 default:
225                         LOG_INFO("Incorrect parameters for ^2mute^7\n");
226                 case CMD_REQUEST_USAGE:
227                 {
228                         LOG_INFO("\nUsage:^3 sv_cmd unmute client\n");
229                         LOG_INFO("  'client' is the entity number or name of the player to unmute.\n");
230                         LOG_INFO("See also: ^2mute^7\n");
231                         return;
232                 }
233         }
234 }
235
236 /* use this when creating a new command, making sure to place it in alphabetical order... also,
237 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
238 void BanCommand_(float request)
239 {
240     switch(request)
241     {
242         case CMD_REQUEST_COMMAND:
243         {
244
245             return;
246         }
247
248         default:
249         case CMD_REQUEST_USAGE:
250         {
251             print("\nUsage:^3 sv_cmd \n");
252             print("  No arguments required.\n");
253             return;
254         }
255     }
256 }
257 */
258
259
260 // ==================================
261 //  Macro system for server commands
262 // ==================================
263
264 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
265 #define BAN_COMMANDS(request, arguments, command) \
266         BAN_COMMAND("ban", BanCommand_ban(request, arguments, command), "Ban an IP address or a range of addresses (like 1.2.3)") \
267         BAN_COMMAND("banlist", BanCommand_banlist(request), "List all existing bans") \
268         BAN_COMMAND("kickban", BanCommand_kickban(request, arguments, command), "Disconnect a client and ban it at the same time") \
269         BAN_COMMAND("mute", BanCommand_mute(request, arguments, command), "Disallow a client from talking by muting them") \
270         BAN_COMMAND("unban", BanCommand_unban(request, arguments), "Remove an existing ban") \
271         BAN_COMMAND("unmute", BanCommand_unmute(request, arguments), "Unmute a client") \
272         /* nothing */
273
274 void BanCommand_macro_help()
275 {
276         #define BAN_COMMAND(name, function, description) \
277                 { if (strtolower(description) != "") { LOG_INFO("  ^2", name, "^7: ", description, "\n"); } }
278
279         BAN_COMMANDS(0, 0, "");
280 #undef BAN_COMMAND
281 }
282
283 float BanCommand_macro_command(float argc, string command)
284 {
285         #define BAN_COMMAND(name, function, description) \
286                 { if (name == strtolower(argv(0))) { function; return true; } }
287
288         BAN_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
289 #undef BAN_COMMAND
290
291         return false;
292 }
293
294 float BanCommand_macro_usage(float argc)
295 {
296         #define BAN_COMMAND(name, function, description) \
297                 { if (name == strtolower(argv(1))) { function; return true; } }
298
299         BAN_COMMANDS(CMD_REQUEST_USAGE, argc, "");
300 #undef BAN_COMMAND
301
302         return false;
303 }
304
305 void BanCommand_macro_write_aliases(float fh)
306 {
307         #define BAN_COMMAND(name, function, description) \
308                 { if (strtolower(description) != "") { CMD_Write_Alias("qc_cmd_sv", name, description); } }
309
310         BAN_COMMANDS(0, 0, "");
311 #undef BAN_COMMAND
312 }
313
314 float BanCommand(string command)
315 {
316         float argc = tokenize_console(command);
317
318         // Guide for working with argc arguments by example:
319         // argc:   1    - 2      - 3     - 4
320         // argv:   0    - 1      - 2     - 3
321         // cmd     vote - master - login - password
322
323         if (BanCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
324                 return true;                             // handled by one of the above GenericCommand_* functions
325
326         return false;
327 }