]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/banning.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / banning.qc
1 // =====================================================
2 //  Banning and kicking command code, written by Samual
3 //  Last updated: December 29th, 2011
4 // =====================================================
5
6 void BanCommand_ban(float request, float argc, string command)
7 {
8         switch(request)
9         {
10                 case CMD_REQUEST_COMMAND:
11                 {
12                         if(argc >= 2)
13                         {
14                                 string ip = argv(1);
15                                 float reason_arg, bantime;
16                                 string reason;
17                                 
18                                 reason_arg = 2; 
19                                 
20                                 GET_BAN_ARG(bantime, autocvar_g_ban_default_bantime);
21                                 GET_BAN_REASON(reason, "No reason provided");
22
23                                 Ban_Insert(ip, bantime, reason, 1);
24                                 return;
25                         }
26                 }
27                         
28                 default:
29                         print("Incorrect parameters for ^2ban^7\n");
30                 case CMD_REQUEST_USAGE:
31                 {
32                         print("\nUsage:^3 sv_cmd ban address [bantime] [reason]\n");
33                         print("  'address' is the IP address or range of the player to ban,\n");
34                         print("  'bantime' is the amount of time that the ban is active (default if not provided),\n");
35                         print("  and 'reason' is the string to label the ban with as reason for banning.\n");
36                         print("See also: ^2banlist, kickban, unban^7\n");
37                         return;
38                 }
39         }
40 }
41
42 void BanCommand_banlist(float request)
43 {
44         switch(request)
45         {
46                 case CMD_REQUEST_COMMAND:
47                 {
48                         Ban_View();
49                         return;
50                 }
51                         
52                 default:
53                 case CMD_REQUEST_USAGE:
54                 {
55                         print("\nUsage:^3 sv_cmd banlist\n");
56                         print("  No arguments required.\n");
57                         print("See also: ^2ban, kickban, unban^7\n");
58                         return;
59                 }
60         }
61 }
62
63 void BanCommand_kickban(float request, float argc, string command)
64 {
65         switch(request)
66         {
67                 case CMD_REQUEST_COMMAND:
68                 {
69                         if(argc >= 2)
70                         {
71                                 entity client = GetIndexedEntity(argc, 1);
72                                 float accepted = VerifyClientEntity(client, TRUE, FALSE);
73                                 float reason_arg, bantime, masksize;
74                                 string reason;
75                                 
76                                 if(accepted > 0) 
77                                 {
78                                         reason_arg = next_token; 
79
80                                         GET_BAN_ARG(bantime, autocvar_g_ban_default_bantime);
81                                         GET_BAN_ARG(masksize, autocvar_g_ban_default_masksize);
82                                         GET_BAN_REASON(reason, "No reason provided");
83
84                                         Ban_KickBanClient(client, bantime, masksize, reason);
85                                         
86                                         return;
87                                 }
88                                 else
89                                 {
90                                         print("kickban: ", GetClientErrorString(accepted, argv(1)), ".\n"); 
91                                 }
92                         }
93                 }
94                         
95                 default:
96                         print("Incorrect parameters for ^2kickban^7\n");
97                 case CMD_REQUEST_USAGE:
98                 {
99                         print("\nUsage:^3 sv_cmd kickban client [bantime] [masksize] [reason]\n");
100                         print("  'client' is the entity number or name of the player to ban,\n");
101                         print("  'bantime' is the amount of time that the ban is active (default if not provided),\n");
102                         print("  'masksize' is the range of the IP address (1-thru-4, default if not provided),\n");
103                         print("  and 'reason' is the string to label the ban with as reason for banning.\n");
104                         print("See also: ^2ban, banlist, unban^7\n");
105                         return;
106                 }
107         }
108 }
109
110 void BanCommand_unban(float request, float argc)
111 {
112         switch(request)
113         {
114                 case CMD_REQUEST_COMMAND:
115                 {
116                         if(argv(1))
117                         {
118                                 Ban_Delete(stof(argv(1)));
119                                 return;
120                         }
121                 }
122                         
123                 default:
124                 case CMD_REQUEST_USAGE:
125                 {
126                         print("\nUsage:^3 sv_cmd unban banid\n");
127                         print("  Where 'banid' is the ID of the ban of which to remove.\n");
128                         print("See also: ^2ban, banlist, kickban^7\n");
129                         return;
130                 }
131         }
132 }
133
134 /* use this when creating a new command, making sure to place it in alphabetical order... also,
135 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
136 void BanCommand_(float request)
137 {
138         switch(request)
139         {
140                 case CMD_REQUEST_COMMAND:
141                 {
142                         
143                         return;
144                 }
145                         
146                 default:
147                 case CMD_REQUEST_USAGE:
148                 {
149                         print("\nUsage:^3 sv_cmd \n");
150                         print("  No arguments required.\n");
151                         return;
152                 }
153         }
154 }
155 */
156
157
158 // ==================================
159 //  Macro system for server commands
160 // ==================================
161
162 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
163 #define BAN_COMMANDS(request,arguments,command) \
164         BAN_COMMAND("ban", BanCommand_ban(request, arguments, command), "Ban an IP address or a range of addresses (like 1.2.3)") \
165         BAN_COMMAND("banlist", BanCommand_banlist(request), "List all existing bans") \
166         BAN_COMMAND("kickban", BanCommand_kickban(request, arguments, command), "Disconnect a client and ban it at the same time") \
167         BAN_COMMAND("unban", BanCommand_unban(request, arguments), "Remove an existing ban") \
168         /* nothing */
169
170 void BanCommand_macro_help()
171 {
172         #define BAN_COMMAND(name,function,description) \
173                 { print("  ^2", name, "^7: ", description, "\n"); }
174                 
175         BAN_COMMANDS(0, 0, "")
176         #undef BAN_COMMAND
177         
178         return;
179 }
180
181 float BanCommand_macro_command(float argc, string command)
182 {
183         #define BAN_COMMAND(name,function,description) \
184                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
185                 
186         BAN_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
187         #undef BAN_COMMAND
188         
189         return FALSE;
190 }
191
192 float BanCommand_macro_usage(float argc)
193 {
194         #define BAN_COMMAND(name,function,description) \
195                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
196                 
197         BAN_COMMANDS(CMD_REQUEST_USAGE, argc, "")
198         #undef BAN_COMMAND
199         
200         return FALSE;
201 }
202
203 void BanCommand_macro_write_aliases(float fh)
204 {
205         #define BAN_COMMAND(name,function,description) \
206                 { CMD_Write_Alias("qc_cmd_sv", name, description); }
207         
208         BAN_COMMANDS(0, 0, "")
209         #undef BAN_COMMAND
210         
211         return;
212 }
213
214 float BanCommand(string command)
215 {
216         float argc = tokenize_console(command);
217         
218         // Guide for working with argc arguments by example:
219         // argc:   1    - 2      - 3     - 4
220         // argv:   0    - 1      - 2     - 3 
221         // cmd     vote - master - login - password
222
223         if(BanCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
224         {
225                 return TRUE; // handled by one of the above GenericCommand_* functions
226         }
227         
228         return FALSE;
229 }