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