]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/server/clientcommands.qc
colors
[divverent/nexuiz.git] / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 float ValidateMap(string vote);
3 void(entity e) DropFlag;
4
5 void Say(entity source, float teamsay, string msgin)
6 {
7         string msgstr, colorstr, cmsgstr;
8         entity head;
9
10         msgin = formatmessage(msgin);
11
12         if(msgin == "")
13                 return;
14
15         colorstr = Team_ColorCode(source.team);
16
17         if(!teams_matter)
18                 teamsay = FALSE;
19
20         if(source.classname != "player") // observers can't
21                 teamsay = FALSE;
22
23         // how can we prevent the message from appearing in a listen server?
24         // for now, just give "say" back and only handle say_team
25         if(!teamsay)
26         {
27                 clientcommand(self, strcat("say ", msgin));
28                 return;
29         }
30
31         if(teamsay)
32         {
33                 msgstr = strzone(strcat("\{1}", colorstr, "(^3", source.netname, colorstr, ") ^7", msgin, "\n"));
34                 cmsgstr = strcat(colorstr, "(^3", source.netname, colorstr, ")\n^7", wordwrap(msgin, 50));
35         }
36         else
37                 msgstr = strzone(strcat("\{1}^3", source.netname, "^7: ", msgin, "\n"));
38
39         head = find(world, classname, "player");
40         while(head)
41         {
42                 if(clienttype(head) == CLIENTTYPE_REAL)
43                         if(!teamsay || (head.team == source.team))
44                         {
45                                 sprint(head, msgstr);
46                                 if(teamsay)
47                                         centermsg_setfor(head, CENTERMSG_TEAMSAY, cmsgstr);
48                                 //stuffcmd(head, "play2 misc/talk.wav\n");
49                         }
50                 head = find(head, classname, "player");
51         }
52
53         if(!teamsay)
54         {
55                 head = find(world, classname, "observer");
56                 while(head)
57                 {
58                         if(clienttype(head) == CLIENTTYPE_REAL)
59                         {
60                                 sprint(head, msgstr);
61                                 //stuffcmd(head, "play2 misc/talk.wav\n");
62                         }
63                         head = find(head, classname, "observer");
64                 }
65                 head = find(world, classname, "spectator");
66                 while(head)
67                 {
68                         if(clienttype(head) == CLIENTTYPE_REAL)
69                         {
70                                 sprint(head, msgstr);
71                                 //stuffcmd(head, "play2 misc/talk.wav\n");
72                         }
73                         head = find(head, classname, "spectator");
74                 }
75                 ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
76         }
77
78         strunzone(msgstr);
79 }
80
81 void SV_ParseClientCommand(string s) {
82         local float index;
83
84         tokenize(s);
85
86         if(argv(0) == "clogin") {
87                 if(cvar("sv_clientcommands")) {
88                         if(self.adminstatus < -5) {
89                                 sprint(self, "Too many unsuccessful tries.\n");
90                         } else if(argv(1) == cvar_string("sv_clientcommands_password")) {
91                                 self.adminstatus = 1;
92                                 sprint(self, "You now have remote admin status.\n");
93                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " received admin status"), TRUE);
94                         } else {
95                                 sprint(self, "Wrong password.\n");
96                                 // use of -- produces compiler warning in the if() line???
97                                 self.adminstatus = self.adminstatus - 1;
98                                 if(self.adminstatus == 0)
99                                 {
100                                         sprint(self, "You lost remote admin status.\n");
101                                         ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " lost admin status"), TRUE);
102                                 }
103                         }
104                 } else {
105                         sprint(self, "Clientside commands NOT allowed.\n");
106                 }
107         } else if(argv(0) == "ccmd") {
108                 if(cvar("sv_clientcommands")) {
109                         if(self.adminstatus > 0) {
110                                 local string command;
111                                 command = argv(1);
112                                 index = 2;
113                                 while(argv(index) != "") {
114                                         command = strcat(command, " ", argv(index));
115                                         index++;
116                                 }
117                                 command = strzone(command);
118                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " issued command '", command, "'"), TRUE);
119                                 localcmd(strcat(command, "\n"));
120                                 strunzone(command);
121                         } else
122                                 sprint(self, "You don't have remote admin status.\n");
123                 } else {
124                         sprint(self, "Clientside commands NOT allowed.\n");
125                 }
126         } else if(argv(0) == "vote") {
127                 if(argv(1) == "help") {
128                         local string vmasterdis;
129                         if(!cvar("sv_vote_master")) {
130                                 vmasterdis = " ^1(disabled)";
131                         }
132                         local string vcalldis;
133                         if(!cvar("sv_vote_call")) {
134                                 vcalldis = " ^1(disabled)";
135                         }
136                         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");
137                         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");
138                         sprint(self, "^7\"^2help^7\" shows this info.\n");
139                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
140                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
141                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
142                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
143                         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");
144                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
145                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
146                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
147                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
148                         sprint(self, "^7You can call a vote for or execute these commands:\n");
149                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
150                 } else if(argv(1) == "status") {
151                         if(votecalled) {
152                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
153                         } else {
154                                 sprint(self, "^1No vote called.\n");
155                         }
156                 } else if(argv(1) == "call") {
157                         if(cvar("sv_vote_call")) {
158                                 if(votecalled) {
159                                         sprint(self, "^1There is already a vote called.\n");
160                                 } else {
161                                         local string vote;
162                                         vote = VoteParse();
163                                         if(vote == "") {
164                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
165                                         } else if(time < self.vote_next) {
166                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
167                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
168                                                 if(!ValidateMap(vote))
169                                                         return;
170                                                 votecalled = TRUE;
171                                                 votecalledmaster = FALSE;
172                                                 // remap chmap to gomap (forces intermission)
173                                                 if(strlen(vote) >= 6)
174                                                         if(substring(vote, 0, 6) == "chmap ")
175                                                                 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
176                                                 votecalledvote = strzone(vote);
177                                                 votecaller = self; // remember who called the vote
178                                                 votefinished = time + cvar("sv_vote_timeout");
179                                                 votecaller.vote_vote = 1; // of course you vote yes
180                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
181                                                 bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ^1", votecalledvote, "\n"));
182                                                 VoteCount(); // needed if you are the only one
183                                         } else {
184                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
185                                         }
186                                 }
187                         } else {
188                                 sprint(self, "^1Vote calling is NOT allowed.\n");
189                         }
190                 } else if(argv(1) == "stop") {
191                         if(!votecalled) {
192                                 sprint(self, "^1No vote called.\n");
193                         } else if(self == votecaller
194                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
195                                 VoteStop(self);
196                         } else {
197                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
198                         }
199                 } else if(argv(1) == "master") {
200                         if(cvar("sv_vote_master")) {
201                                 if(votecalled) {
202                                         sprint(self, "^1There is already a vote called.\n");
203                                 } else {
204                                         votecalled = TRUE;
205                                         votecalledmaster = TRUE;
206                                         votecalledvote = strzone("^3master");
207                                         votecaller = self; // remember who called the vote
208                                         votefinished = time + cvar("sv_vote_timeout");
209                                         votecaller.vote_vote = 1; // of course you vote yes
210                                         votecaller.vote_next = time + cvar("sv_vote_wait");
211                                         bprint(strcat("\"^3", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
212                                         VoteCount(); // needed if you are the only one
213                                 }
214                         } else {
215                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
216                         }
217                 } else if(argv(1) == "do") {
218                         if(self.vote_master) {
219                                 local string dovote;
220                                 dovote = VoteParse();
221                                 if(dovote == "") {
222                                         sprint(self, "^1Your command was empty. See help for more info.\n");
223                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
224                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
225                                         localcmd(strcat(dovote, "\n"));
226                                 } else {
227                                         sprint(self, "^1This command is not ok. See help for more info.\n");
228                                 }
229                         } else {
230                                 sprint(self, "^1You are NOT a master.\n");
231                         }
232                 } else if(argv(1) == "yes") {
233                         if(!votecalled) {
234                                 sprint(self, "^1No vote called.\n");
235                         } else if(self.vote_vote == 0
236                                   || cvar("sv_vote_change")) {
237                                 sprint(self, "^1You accepted the vote.\n");
238                                 self.vote_vote = 1;
239                                 if(!cvar("sv_vote_singlecount")) {
240                                         VoteCount();
241                                 }
242                         } else {
243                                 sprint(self, "^1You have already voted.\n");
244                         }
245                 } else if(argv(1) == "no") {
246                         if(!votecalled) {
247                                 sprint(self, "^1No vote called.\n");
248                         } else if(self.vote_vote == 0
249                                   || cvar("sv_vote_change")) {
250                                 sprint(self, "^1You rejected the vote.\n");
251                                 self.vote_vote = -1;
252                                 if(!cvar("sv_vote_singlecount")) {
253                                         VoteCount();
254                                 }
255                         } else {
256                                 sprint(self, "^1You have already voted.\n");
257                         }
258                 } else {
259                         // ignore this?
260                         sprint(self, "^1Unknown vote command.\n");
261                 }
262         } else if(argv(0) == "autoswitch") {
263                 // be backwards compatible with older clients (enabled)
264                 self.autoswitch = ("0" != argv(1));
265                 local string autoswitchmsg;
266                 if (self.autoswitch) {
267                         autoswitchmsg = "on";
268                 } else {
269                         autoswitchmsg = "off";
270                 }
271                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
272         } else if(argv(0) == "clientversion") {
273                 if (argv(1) == "$gameversion") {
274                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
275                         // either that or someone wants to be funny
276                         self.version = 1;
277                 } else {
278                         self.version = stof(argv(1));
279                 }
280                 if(self.version != cvar("gameversion")) 
281                 {
282                         self.classname = "observer";
283                         self.frags = -2;
284                         PutClientInServer();
285                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
286                         //JoinBestTeam(self, 0);
287                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
288                         self.classname = "observer";
289                         stuffcmd(self,"menu_showteamselect\n");
290                 }
291         } else if(argv(0) == "reportcvar") {
292                 GetCvars(1);
293         } else if(argv(0) == "spectate") {
294                 if(cvar("g_lms") || cvar("g_arena"))
295                         return; // don't allow spectating in lms, unless player runs out of lives
296                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
297                         if(self.flagcarried)
298                                 DropFlag(self.flagcarried);
299                         DistributeFragsAmongTeam(self, self.team, 1.0);
300                         self.classname = "observer";
301                         PutClientInServer();
302                 }
303         } else if(argv(0) == "join") {
304                 if(!cvar("g_arena"))
305                 if (self.classname != "player")
306                 {
307                         self.classname = "player";
308                         self.frags = 0;
309                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
310                         PutClientInServer();
311                 }
312         } else if( argv(0) == "selectteam" ) {
313                 if( !cvar("teamplay") ) {
314                         sprint( self, "selecteam can only be used in teamgames\n");
315                 } else if(cvar("g_campaign")) {
316                         //JoinBestTeam(self, 0);
317                 } else if( argv(1) == "none" ) {
318                         SV_ChangeTeam( 0 );
319                 } else if( argv(1) == "red" ) {
320                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
321                 } else if( argv(1) == "blue" ) {
322                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
323                 } else if( argv(1) == "pink" ) {
324                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
325                 } else if( argv(1) == "yellow" ) {
326                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
327                 } else if( argv(1) == "auto" ) {
328                         self.team = -1;
329                         JoinBestTeam( self, 0 );
330                 } else {
331                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
332                 }
333         } else if(argv(0) == "ready") {
334                 if(cvar("sv_ready_restart"))
335                 {
336                         self.ready = TRUE;
337                         bprint(self.netname, "^2 is ready\n");
338                         ReadyCount();
339                 }
340         } else if(argv(0) == "maplist") {
341                 local float i, n;
342                 local string col;
343                 n = tokenize(cvar_string("g_maplist"));
344                 sprint(self, "^7Maps in list: ");
345                 for(i = 0; i < n; ++i)
346                 {
347                         if(math_mod(i, 2))
348                                 col = "^2";
349                         else
350                                 col = "^3";
351                         sprint(self, strcat(col, argv(i), " "));
352                 }
353                 sprint(self, "\n");
354         } else if(argv(0) == "say") {
355                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
356                 //clientcommand(self, formatmessage(s));
357         } else if(argv(0) == "say_team") {
358                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
359                 //clientcommand(self, formatmessage(s));
360         } else {
361                 string cmd;
362                 cmd = argv(0);
363                 if(cmd != "status")
364                 if(cmd != "name")
365                 //if(cmd != "say") // handled above
366                 //if(cmd != "say_team") // handled above
367                 if(cmd != "tell")
368                 if(cmd != "color")
369                 if(cmd != "kill")
370                 if(cmd != "pause")
371                 if(cmd != "kick")
372                 if(cmd != "ping")
373                 if(cmd != "pings")
374                 if(cmd != "ban")
375                 if(cmd != "pmodel")
376                 if(cmd != "rate")
377                 if(cmd != "playermodel")
378                 if(cmd != "playerskin")
379                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
380                 {
381                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
382                         return;
383                 }
384                 clientcommand(self,s);
385         }
386 }
387
388 float ValidateMap(string vote)
389 {
390         string ext;
391         
392         tokenize(vote);
393         if(argv(0) == "map" || argv(0) == "changelevel")
394                 ext = ".bsp";
395         else if(argv(0) == "chmap")
396                 ext = ".mapcfg";
397         else if(argv(0) == "gotomap")
398                 ext = ".mapcfg";
399         else
400                 return TRUE;
401
402         if(!TryFile(strcat("maps/", argv(1), ext)))
403         {
404                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
405                 return FALSE;
406         }
407         return TRUE;
408 }
409
410
411 void VoteThink() {
412         if(votefinished > 0 // a vote was called
413             && time > votefinished) // time is up
414         {
415                 VoteCount();
416         }
417 }
418
419 string VoteParse() {
420         local float index;
421         index = 3;
422         local string vote;
423         vote = argv(2);
424         while(argv(index) != "") {
425                 vote = strcat(vote, " ", argv(index));
426                 index++;
427         }
428
429         // necessary for some of the string operations
430         vote = strzone(vote);
431
432         // now we remove some things that could be misused
433         index = 0;
434         local float found;
435         found = FALSE;
436         local float votelength;
437         votelength = strlen(vote);
438         while(!found && index < votelength)
439         {
440                 local string badchar;
441                 badchar = substring(vote, index, 1);
442                 if(badchar == ";"
443                    || badchar == "\r"
444                    || badchar == "\n")
445                 {
446                         found = TRUE;
447                 } else {
448                         index++;
449                 }
450         }
451         return substring(vote, 0, index);
452 }
453
454 float VoteAllowed(string votecommand) {
455         tokenize(cvar_string("sv_vote_commands"));
456         local float index;
457         index = 0;
458         while(argv(index) != "") {
459                 local string allowed;
460                 allowed = argv(index);
461                 if(votecommand == allowed) {
462                         return TRUE;
463                 }
464                 index++;
465         }
466         return FALSE;
467 }
468
469 void VoteReset() {
470         local string searchclass;
471         searchclass = "player";
472
473         while (TRUE)
474         {
475                 local entity player;
476                 player = find(player, classname, searchclass);
477                 while(player)
478                 {
479                         player.vote_vote = 0;
480                         player = find(player, classname, searchclass);
481                 }
482
483                 if("player" == searchclass) {
484                         searchclass = "observer";
485                 } else if("observer" == searchclass) {
486                         searchclass = "spectator";
487                 } else {
488                         break;
489                 }
490         }
491
492         votecalled = FALSE;
493         votecalledmaster = FALSE;
494         votefinished = 0;
495 }
496
497 void VoteAccept() {
498         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was accepted\n"));
499         if(votecalledmaster)
500         {
501                 votecaller.vote_master = 1;
502         } else {
503                 localcmd(strcat(votecalledvote, "\n"));
504         }
505         votecaller.vote_next = 0; // people like your votes, no wait for next vote
506         VoteReset();
507 }
508
509 void VoteReject() {
510         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was rejected\n"));
511         VoteReset();
512 }
513
514 void VoteTimeout() {
515         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 timed out\n"));
516         VoteReset();
517 }
518
519 void VoteStop(entity stopper) {
520         bprint(strcat("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n"));
521         if(stopper == votecaller) {
522                 // no wait for next vote so you can correct your vote
523                 votecaller.vote_next = 0;
524         }
525         VoteReset();
526 }
527
528 void VoteCount() {
529         local float playercount;
530         playercount = 0;
531         local float yescount;
532         yescount = 0;
533         local float nocount;
534         nocount = 0;
535         local string searchclass;
536         searchclass = "player";
537
538         while (TRUE)
539         {
540                 local entity player;
541                 player = find(player, classname, searchclass);
542
543                 while(player)
544                 {
545                         if(clienttype(player) != CLIENTTYPE_BOT) {
546                                 if(player.vote_vote < 0) {
547                                         nocount++;
548                                 } else if(player.vote_vote > 0) {
549                                         yescount++;
550                                 }
551                                 playercount++;
552                         }
553                         player = find(player, classname, searchclass);
554                 }
555
556                 if("player" == searchclass) {
557                         searchclass = "observer";
558                 } else if("observer" == searchclass) {
559                         searchclass = "specator";
560                 } else {
561                         break;
562                 }
563         }
564
565         if((playercount == 1) && votecalledmaster) {
566                 // if only one player is on the server becoming vote
567                 // master is not allowed.  This could be used for
568                 // trolling or worse. 'self' is the user who has
569                 // called the vote because this function is called
570                 // by SV_ParseClientCommand. Maybe all voting should
571                 // be disabled for a single player?
572                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
573                 votecaller.vote_next = 0;
574                 VoteReset();
575         } else if((playercount / 2) < yescount) { // vote accepted
576                 VoteAccept();
577         } else if((playercount / 2) < nocount) { // vote rejected
578                 VoteReject();
579         } else if(time > votefinished) { // vote timedout
580                 VoteTimeout();
581         } // else still running
582 }
583
584
585 void ReadyCount()
586 {
587         local entity e;
588         local float r, p;
589
590         e = find(world, classname, "player");
591
592         while(e)
593         {
594                 if(clienttype(e) == CLIENTTYPE_REAL)
595                 {
596                         p += 1;
597                         if(e.ready) r += 1;
598                 }
599                 e = find(e, classname, "player");
600         }
601
602         if(p && r == p)
603         {
604                 bprint("^1Server is restarting...\n");
605                 localcmd("restart\n");
606         }
607 }