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