]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/gamec/clientcommands.c
more elegant checking to see if stuffcmds are safe
[divverent/nexuiz.git] / data / qcsrc / gamec / clientcommands.c
1 void SV_ParseClientCommand(string s) {
2         local float index;
3
4         tokenize(s);
5
6         if(argv(0) == "clogin") {
7                 if(cvar("sv_clientcommands")) {
8                         if(self.adminstatus < -5) {
9                                 sprint(self, "Too many unsuccessful tries.\n");
10                         } else if(argv(1) == cvar_string("sv_clientcommands_password")) {
11                                 self.adminstatus = 1;
12                                 sprint(self, "You now have remote admin status.\n");
13                         } else {
14                                 sprint(self, "Wrong password.\n");
15                                 // use of -- produces compiler warning in the if() line???
16                                 self.adminstatus = self.adminstatus - 1;
17                                 if(self.adminstatus == 0)
18                                         sprint(self, "You lost remote admin status.\n");
19                         }
20                 } else {
21                         sprint(self, "Clientside commands NOT allowed.\n");
22                 }
23         } else if(argv(0) == "ccmd") {
24                 if(cvar("sv_clientcommands")) {
25                         if(self.adminstatus > 0) {
26                                 local string command;
27                                 command = argv(1);
28                                 index = 2;
29                                 while(argv(index) != "") {
30                                         command = strcat(command, " ", argv(index));
31                                         index++;
32                                 }
33                                 localcmd(command);
34                         } else
35                                 sprint(self, "You don't have remote admin status.\n");
36                 } else {
37                         sprint(self, "Clientside commands NOT allowed.\n");
38                 }
39         } else if(argv(0) == "vote") {
40                 if(argv(1) == "help") {
41                         sprint(self, "^7You can use voting with \"^2cmd vote help^7\" \"^2cmd vote status^7\" \"^2cmd vote call ^3COMMAND ARGUMENTS^7\" \"^2cmd vote stop^7\" \"^2cmd vote master^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\".\n");
42                         sprint(self, "^7Or if your version is up to date you can use these aliases \"^2vhelp^7\" \"^2vstatus^7\" \"^2vcall ^3COMMAND ARGUMENTS^7\" \"^2vstop^7\" \"^2vmaster^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\".\n");
43                         sprint(self, "^7\"^2help^7\" shows this info.\n");
44                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
45                         sprint(self, "^7\"^2call^7\" is used to call a vote. See the list of allowed commands.\n");
46                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
47                         sprint(self, "^7\"^2master^7\" is used to call a vote to become a master.\n");
48                         sprint(self, "^7\"^2do^7\" If you are a master you can execute a command without a vote. See the list of allowed commands.\n");
49                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
50                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
51                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
52                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
53                         sprint(self, "^7You can call a vote for or execute these commands:\n");
54                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
55                 } else if(argv(1) == "status") {
56                         if(votecalled) {
57                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
58                         } else {
59                                 sprint(self, "^1No vote called.\n");
60                         }
61                 } else if(argv(1) == "call") {
62                         if(cvar("sv_vote_call")) {
63                                 if(votecalled) {
64                                         sprint(self, "^1There is already a vote called.\n");
65                                 } else {
66                                         local string vote;
67                                         vote = VoteParse();
68                                         if(vote == "") {
69                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
70                                         } else if(time < self.vote_next) {
71                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
72                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
73                                                 votecalled = TRUE;
74                                                 votecalledmaster = FALSE;
75                                                 votecalledvote = strzone(vote);
76                                                 votecaller = self; // remember who called the vote
77                                                 votecaller.vote_vote = 1; // of course you vote yes
78                                                 votecaller.vote_finished = time + cvar("sv_vote_timeout");
79                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
80                                                 bprint(strcat("^3Vote for \"^1", votecalledvote, "^3\" called by \"^7", votecaller.netname, "^3\".\n"));
81                                                 VoteCount(); // needed if you are the only one
82                                         } else {
83                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
84                                         }
85                                 }
86                         } else {
87                                 sprint(self, "^1Vote calling is NOT allowed.\n");
88                         }
89                 } else if(argv(1) == "stop") {
90                         if(!votecalled) {
91                                 sprint(self, "^1No vote called.\n");
92                         } else if(self == votecaller
93                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
94                                 VoteStop(self);
95                         } else {
96                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
97                         }
98                 } else if(argv(1) == "master") {
99                         if(cvar("sv_vote_master")) {
100                                 if(votecalled) {
101                                         sprint(self, "^1There is already a vote called.\n");
102                                 } else {
103                                         votecalled = TRUE;
104                                         votecalledmaster = TRUE;
105                                         votecalledvote = strzone("^3master");
106                                         votecaller = self; // remember who called the vote
107                                         votecaller.vote_vote = 1; // of course you vote yes
108                                         votecaller.vote_finished = time + cvar("sv_vote_timeout");
109                                         votecaller.vote_next = time + cvar("sv_vote_wait");
110                                         bprint(strcat("\"^7", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
111                                         VoteCount(); // needed if you are the only one
112                                 }
113                         } else {
114                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
115                         }
116                 } else if(argv(1) == "do") {
117                         if(self.vote_master) {
118                                 local string dovote;
119                                 dovote = VoteParse();
120                                 if(dovote == "") {
121                                         sprint(self, "^1Your command was empty. See help for more info.\n");
122                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
123                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
124                                         localcmd(dovote);
125                                 } else {
126                                         sprint(self, "^1This command is not ok. See help for more info.\n");
127                                 }
128                         } else {
129                                 sprint(self, "^1You are NOT a master.\n");
130                         }
131                 } else if(argv(1) == "yes") {
132                         if(!votecalled) {
133                                 sprint(self, "^1No vote called.\n");
134                         } else if(self.vote_vote == 0
135                                   || cvar("sv_vote_change")) {
136                                 sprint(self, "^1You accepted the vote.\n");
137                                 self.vote_vote = 1;
138                                 if(!cvar("sv_vote_singlecount")) {
139                                         VoteCount();
140                                 }
141                         } else {
142                                 sprint(self, "^1You have already voted.\n");
143                         }
144                 } else if(argv(1) == "no") {
145                         if(!votecalled) {
146                                 sprint(self, "^1No vote called.\n");
147                         } else if(self.vote_vote == 0
148                                   || cvar("sv_vote_change")) {
149                                 sprint(self, "^1You rejected the vote.\n");
150                                 self.vote_vote = -1;
151                                 if(!cvar("sv_vote_singlecount")) {
152                                         VoteCount();
153                                 }
154                         } else {
155                                 sprint(self, "^1You have already voted.\n");
156                         }
157                 } else {
158                         // ignore this?
159                         sprint(self, "^1Unknown vote command.\n");
160                 }
161         } else if(argv(0) == "autoswitch") {
162                 // be backwards compatible with older clients (enabled)
163                 self.autoswitch = ("0" != argv(1));
164                 local string autoswitchmsg;
165                 if (self.autoswitch) {
166                         autoswitchmsg = "on";
167                 } else {
168                         autoswitchmsg = "off";
169                 }
170                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
171         } else if(argv(0) == "clientversion") {
172                 local string versionmsg;
173                 if (argv(1) == "$g_nexuizversion_major") {
174                         versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
175                         // either that or someone wants to be funny
176                 } else if (stof(argv(1)) != cvar("g_nexuizversion_major")) {
177                         if(stof(argv(1)) < cvar("g_nexuizversion_major")) {
178                                 versionmsg = "^3Your client version is outdated.\nPlease update!!!^8";
179                         } else {
180                                 versionmsg = "^3This server is using an outdated Nexuiz version.\nThis can lead to problems.^8";
181                         }
182
183                 } else {
184                         versionmsg = "^2client version and server version are the same.^8";
185                 }
186                 self.versionmessage = strzone(versionmsg);
187         } else if(argv(0) == "spectate") {
188                 if(cvar("g_lms"))
189                         return; // don't allow spectating in lms, unless player runs out of lives
190                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
191                         self.classname = "observer";
192                         PutClientInServer();
193                 }
194         } else if(argv(0) == "join") {
195                 self.classname = "player";
196                 self.frags = 0;
197                 // TODO: I have no idea whether this is needed or not
198                 if(!cvar("g_lms")) {
199                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
200                 }
201                 PutClientInServer();
202         } else if( argv(0) == "selectteam" ) {
203                 if( argv(1) == "none" ) {
204                         SV_ChangeTeam( 0 );
205                 } else if( argv(1) == "red" ) {
206                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
207                 } else if( argv(1) == "blue" ) {
208                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
209                 } else if( argv(1) == "pink" ) {
210                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
211                 } else if( argv(1) == "yellow" ) {
212                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
213                 } else if( argv(1) == "auto" ) {
214                         JoinBestTeam( self, 0 );
215                 } else {
216                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
217                 }
218         } else {
219                 clientcommand(self,s);
220         }
221 }
222
223 void VoteThink() {
224         if(self.vote_finished > 0 // this player has called a vote
225             && time > self.vote_finished) // time is up
226         {
227                 VoteCount();
228         }
229 }
230
231 string VoteParse() {
232         local float index;
233         index = 3;
234         local string vote;
235         vote = argv(2);
236         while(argv(index) != "") {
237                 vote = strcat(vote, " ", argv(index));
238                 index++;
239         }
240
241         // necessary for some of the string operations
242         vote = strzone(vote);
243
244         // now we remove some things that could be misused
245         index = 0;
246         local float found;
247         found = FALSE;
248         local float votelength;
249         votelength = strlen(vote);
250         while(!found && index < votelength)
251         {
252                 local string badchar;
253                 badchar = substring(vote, index, 1);
254                 if(badchar == ";"
255                    || badchar == "\n")
256                 {
257                         found = TRUE;
258                 } else {
259                         index++;
260                 }
261         }
262         return substring(vote, 0, index);
263 }
264
265 float VoteAllowed(string votecommand) {
266         tokenize(cvar_string("sv_vote_commands"));
267         local float index;
268         index = 0;
269         while(argv(index) != "") {
270                 local string allowed;
271                 allowed = argv(index);
272                 if(votecommand == allowed) {
273                         return TRUE;
274                 }
275                 index++;
276         }
277         return FALSE;
278 }
279
280 void VoteReset() {
281         local string searchclass;
282         searchclass = "player";
283
284         while (TRUE)
285         {
286                 local entity player;
287                 player = find(player, classname, searchclass);
288                 while(player)
289                 {
290                         player.vote_vote = 0;
291                         player.vote_finished = 0;
292                         player = find(player, classname, searchclass);
293                 }
294
295                 if("player" == searchclass) {
296                         searchclass = "observer";
297                 } else if("observer" == searchclass) {
298                         searchclass = "spectator";
299                 } else {
300                         break;
301                 }
302         }
303
304         votecalled = FALSE;
305         votecalledmaster = FALSE;
306 }
307
308 void VoteAccept() {
309         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
310         if(votecalledmaster)
311         {
312                 votecaller.vote_master = 1;
313         } else {
314                 localcmd(votecalledvote);
315         }
316         votecaller.vote_next = 0; // people like your votes, no wait for next vote
317         VoteReset();
318 }
319
320 void VoteReject() {
321         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
322         VoteReset();
323 }
324
325 void VoteTimeout() {
326         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
327         VoteReset();
328 }
329
330 void VoteStop(entity stopper) {
331         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
332         if(stopper == votecaller) {
333                 // no wait for next vote so you can correct your vote
334                 votecaller.vote_next = 0;
335         }
336         VoteReset();
337 }
338
339 void VoteCount() {
340         local float playercount;
341         playercount = 0;
342         local float yescount;
343         yescount = 0;
344         local float nocount;
345         nocount = 0;
346         local string searchclass;
347         searchclass = "player";
348
349         while (TRUE)
350         {
351                 local entity player;
352                 player = find(player, classname, searchclass);
353
354                 while(player)
355                 {
356                         if(clienttype(player) != CLIENTTYPE_BOT) {
357                                 if(player.vote_vote < 0) {
358                                         nocount++;
359                                 } else if(player.vote_vote > 0) {
360                                         yescount++;
361                                 }
362                                 playercount++;
363                         }
364                         player = find(player, classname, searchclass);
365                 }
366
367                 if("player" == searchclass) {
368                         searchclass = "observer";
369                 } else if("observer" == searchclass) {
370                         searchclass = "specator";
371                 } else {
372                         break;
373                 }
374         }
375
376         if((playercount / 2) < yescount) { // vote accepted
377                 VoteAccept();
378         } else if((playercount / 2) < nocount) { // vote rejected
379                 VoteReject();
380         } else if(time > votecaller.vote_finished) { // vote timedout
381                 VoteTimeout();
382         } // else still running
383 }