]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
chat width is 100% again; voting in chat area
[divverent/nexuiz.git] / data / 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("^3Vote for \"^1", votecalledvote, "^3\" called by \"^7", votecaller.netname, "^3\".\n"));
182                                                 bprint(strcat("\{1}^3* ^7", votecaller.netname, "^3 calls a vote for ^1", votecalledvote, "\n"));
183                                                 VoteCount(); // needed if you are the only one
184                                         } else {
185                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
186                                         }
187                                 }
188                         } else {
189                                 sprint(self, "^1Vote calling is NOT allowed.\n");
190                         }
191                 } else if(argv(1) == "stop") {
192                         if(!votecalled) {
193                                 sprint(self, "^1No vote called.\n");
194                         } else if(self == votecaller
195                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
196                                 VoteStop(self);
197                         } else {
198                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
199                         }
200                 } else if(argv(1) == "master") {
201                         if(cvar("sv_vote_master")) {
202                                 if(votecalled) {
203                                         sprint(self, "^1There is already a vote called.\n");
204                                 } else {
205                                         votecalled = TRUE;
206                                         votecalledmaster = TRUE;
207                                         votecalledvote = strzone("^3master");
208                                         votecaller = self; // remember who called the vote
209                                         votefinished = time + cvar("sv_vote_timeout");
210                                         votecaller.vote_vote = 1; // of course you vote yes
211                                         votecaller.vote_next = time + cvar("sv_vote_wait");
212                                         bprint(strcat("\"^3", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
213                                         VoteCount(); // needed if you are the only one
214                                 }
215                         } else {
216                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
217                         }
218                 } else if(argv(1) == "do") {
219                         if(self.vote_master) {
220                                 local string dovote;
221                                 dovote = VoteParse();
222                                 if(dovote == "") {
223                                         sprint(self, "^1Your command was empty. See help for more info.\n");
224                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
225                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
226                                         localcmd(strcat(dovote, "\n"));
227                                 } else {
228                                         sprint(self, "^1This command is not ok. See help for more info.\n");
229                                 }
230                         } else {
231                                 sprint(self, "^1You are NOT a master.\n");
232                         }
233                 } else if(argv(1) == "yes") {
234                         if(!votecalled) {
235                                 sprint(self, "^1No vote called.\n");
236                         } else if(self.vote_vote == 0
237                                   || cvar("sv_vote_change")) {
238                                 sprint(self, "^1You accepted the vote.\n");
239                                 self.vote_vote = 1;
240                                 if(!cvar("sv_vote_singlecount")) {
241                                         VoteCount();
242                                 }
243                         } else {
244                                 sprint(self, "^1You have already voted.\n");
245                         }
246                 } else if(argv(1) == "no") {
247                         if(!votecalled) {
248                                 sprint(self, "^1No vote called.\n");
249                         } else if(self.vote_vote == 0
250                                   || cvar("sv_vote_change")) {
251                                 sprint(self, "^1You rejected the vote.\n");
252                                 self.vote_vote = -1;
253                                 if(!cvar("sv_vote_singlecount")) {
254                                         VoteCount();
255                                 }
256                         } else {
257                                 sprint(self, "^1You have already voted.\n");
258                         }
259                 } else {
260                         // ignore this?
261                         sprint(self, "^1Unknown vote command.\n");
262                 }
263         } else if(argv(0) == "autoswitch") {
264                 // be backwards compatible with older clients (enabled)
265                 self.autoswitch = ("0" != argv(1));
266                 local string autoswitchmsg;
267                 if (self.autoswitch) {
268                         autoswitchmsg = "on";
269                 } else {
270                         autoswitchmsg = "off";
271                 }
272                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
273         } else if(argv(0) == "clientversion") {
274                 if (argv(1) == "$gameversion") {
275                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
276                         // either that or someone wants to be funny
277                         self.version = 1;
278                 } else {
279                         self.version = stof(argv(1));
280                 }
281                 if(self.version != cvar("gameversion")) 
282                 {
283                         self.classname = "observer";
284                         self.frags = -2;
285                         PutClientInServer();
286                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
287                         //JoinBestTeam(self, 0);
288                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
289                         self.classname = "observer";
290                         stuffcmd(self,"menu_showteamselect\n");
291                 }
292         } else if(argv(0) == "reportcvar") {
293                 GetCvars(1);
294         } else if(argv(0) == "spectate") {
295                 if(cvar("g_lms") || cvar("g_arena"))
296                         return; // don't allow spectating in lms, unless player runs out of lives
297                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
298                         if(self.flagcarried)
299                                 DropFlag(self.flagcarried);
300                         DistributeFragsAmongTeam(self, self.team, 1.0);
301                         self.classname = "observer";
302                         PutClientInServer();
303                 }
304         } else if(argv(0) == "join") {
305                 if(!cvar("g_arena"))
306                 if (self.classname != "player")
307                 {
308                         self.classname = "player";
309                         self.frags = 0;
310                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
311                         PutClientInServer();
312                 }
313         } else if( argv(0) == "selectteam" ) {
314                 if( !cvar("teamplay") ) {
315                         sprint( self, "selecteam can only be used in teamgames\n");
316                 } else if(cvar("g_campaign")) {
317                         //JoinBestTeam(self, 0);
318                 } else if( argv(1) == "none" ) {
319                         SV_ChangeTeam( 0 );
320                 } else if( argv(1) == "red" ) {
321                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
322                 } else if( argv(1) == "blue" ) {
323                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
324                 } else if( argv(1) == "pink" ) {
325                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
326                 } else if( argv(1) == "yellow" ) {
327                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
328                 } else if( argv(1) == "auto" ) {
329                         self.team = -1;
330                         JoinBestTeam( self, 0 );
331                 } else {
332                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
333                 }
334         } else if(argv(0) == "ready") {
335                 if(cvar("sv_ready_restart"))
336                 {
337                         self.ready = TRUE;
338                         bprint(self.netname, "^2 is ready\n");
339                         ReadyCount();
340                 }
341         } else if(argv(0) == "maplist") {
342                 local float i, n;
343                 local string col;
344                 n = tokenize(cvar_string("g_maplist"));
345                 sprint(self, "^7Maps in list: ");
346                 for(i = 0; i < n; ++i)
347                 {
348                         if(math_mod(i, 2))
349                                 col = "^2";
350                         else
351                                 col = "^3";
352                         sprint(self, strcat(col, argv(i), " "));
353                 }
354                 sprint(self, "\n");
355         } else if(argv(0) == "say") {
356                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
357                 //clientcommand(self, formatmessage(s));
358         } else if(argv(0) == "say_team") {
359                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
360                 //clientcommand(self, formatmessage(s));
361         } else {
362                 string cmd;
363                 cmd = argv(0);
364                 if(cmd != "status")
365                 if(cmd != "name")
366                 //if(cmd != "say") // handled above
367                 //if(cmd != "say_team") // handled above
368                 if(cmd != "tell")
369                 if(cmd != "color")
370                 if(cmd != "kill")
371                 if(cmd != "pause")
372                 if(cmd != "kick")
373                 if(cmd != "ping")
374                 if(cmd != "pings")
375                 if(cmd != "ban")
376                 if(cmd != "pmodel")
377                 if(cmd != "rate")
378                 if(cmd != "playermodel")
379                 if(cmd != "playerskin")
380                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
381                 {
382                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
383                         return;
384                 }
385                 clientcommand(self,s);
386         }
387 }
388
389 float ValidateMap(string vote)
390 {
391         string ext;
392         
393         tokenize(vote);
394         if(argv(0) == "map" || argv(0) == "changelevel")
395                 ext = ".bsp";
396         else if(argv(0) == "chmap")
397                 ext = ".mapcfg";
398         else if(argv(0) == "gotomap")
399                 ext = ".mapcfg";
400         else
401                 return TRUE;
402
403         if(!TryFile(strcat("maps/", argv(1), ext)))
404         {
405                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
406                 return FALSE;
407         }
408         return TRUE;
409 }
410
411
412 void VoteThink() {
413         if(votefinished > 0 // a vote was called
414             && time > votefinished) // time is up
415         {
416                 VoteCount();
417         }
418 }
419
420 string VoteParse() {
421         local float index;
422         index = 3;
423         local string vote;
424         vote = argv(2);
425         while(argv(index) != "") {
426                 vote = strcat(vote, " ", argv(index));
427                 index++;
428         }
429
430         // necessary for some of the string operations
431         vote = strzone(vote);
432
433         // now we remove some things that could be misused
434         index = 0;
435         local float found;
436         found = FALSE;
437         local float votelength;
438         votelength = strlen(vote);
439         while(!found && index < votelength)
440         {
441                 local string badchar;
442                 badchar = substring(vote, index, 1);
443                 if(badchar == ";"
444                    || badchar == "\r"
445                    || badchar == "\n")
446                 {
447                         found = TRUE;
448                 } else {
449                         index++;
450                 }
451         }
452         return substring(vote, 0, index);
453 }
454
455 float VoteAllowed(string votecommand) {
456         tokenize(cvar_string("sv_vote_commands"));
457         local float index;
458         index = 0;
459         while(argv(index) != "") {
460                 local string allowed;
461                 allowed = argv(index);
462                 if(votecommand == allowed) {
463                         return TRUE;
464                 }
465                 index++;
466         }
467         return FALSE;
468 }
469
470 void VoteReset() {
471         local string searchclass;
472         searchclass = "player";
473
474         while (TRUE)
475         {
476                 local entity player;
477                 player = find(player, classname, searchclass);
478                 while(player)
479                 {
480                         player.vote_vote = 0;
481                         player = find(player, classname, searchclass);
482                 }
483
484                 if("player" == searchclass) {
485                         searchclass = "observer";
486                 } else if("observer" == searchclass) {
487                         searchclass = "spectator";
488                 } else {
489                         break;
490                 }
491         }
492
493         votecalled = FALSE;
494         votecalledmaster = FALSE;
495         votefinished = 0;
496 }
497
498 void VoteAccept() {
499         // bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
500         bprint(strcat("\{1}^3* ^7", votecaller.netname, "^3's vote for ^1", votecalledvote, "^3 was accepted\n"));
501         if(votecalledmaster)
502         {
503                 votecaller.vote_master = 1;
504         } else {
505                 localcmd(strcat(votecalledvote, "\n"));
506         }
507         votecaller.vote_next = 0; // people like your votes, no wait for next vote
508         VoteReset();
509 }
510
511 void VoteReject() {
512         // bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
513         bprint(strcat("\{1}^3* ^7", votecaller.netname, "^3's vote for ^1", votecalledvote, "^3 was rejected\n"));
514         VoteReset();
515 }
516
517 void VoteTimeout() {
518         // bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
519         bprint(strcat("\{1}^3* ^7", votecaller.netname, "^3's vote for ^1", votecalledvote, "^3 timed out\n"));
520         VoteReset();
521 }
522
523 void VoteStop(entity stopper) {
524         // bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
525         bprint(strcat("\{1}^3* ^7", stopper.netname, "^3 stopped ^7", votecaller.netname, "^3's vote\n"));
526         if(stopper == votecaller) {
527                 // no wait for next vote so you can correct your vote
528                 votecaller.vote_next = 0;
529         }
530         VoteReset();
531 }
532
533 void VoteCount() {
534         local float playercount;
535         playercount = 0;
536         local float yescount;
537         yescount = 0;
538         local float nocount;
539         nocount = 0;
540         local string searchclass;
541         searchclass = "player";
542
543         while (TRUE)
544         {
545                 local entity player;
546                 player = find(player, classname, searchclass);
547
548                 while(player)
549                 {
550                         if(clienttype(player) != CLIENTTYPE_BOT) {
551                                 if(player.vote_vote < 0) {
552                                         nocount++;
553                                 } else if(player.vote_vote > 0) {
554                                         yescount++;
555                                 }
556                                 playercount++;
557                         }
558                         player = find(player, classname, searchclass);
559                 }
560
561                 if("player" == searchclass) {
562                         searchclass = "observer";
563                 } else if("observer" == searchclass) {
564                         searchclass = "specator";
565                 } else {
566                         break;
567                 }
568         }
569
570         if((playercount == 1) && votecalledmaster) {
571                 // if only one player is on the server becoming vote
572                 // master is not allowed.  This could be used for
573                 // trolling or worse. 'self' is the user who has
574                 // called the vote because this function is called
575                 // by SV_ParseClientCommand. Maybe all voting should
576                 // be disabled for a single player?
577                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
578                 votecaller.vote_next = 0;
579                 VoteReset();
580         } else if((playercount / 2) < yescount) { // vote accepted
581                 VoteAccept();
582         } else if((playercount / 2) < nocount) { // vote rejected
583                 VoteReject();
584         } else if(time > votefinished) { // vote timedout
585                 VoteTimeout();
586         } // else still running
587 }
588
589
590 void ReadyCount()
591 {
592         local entity e;
593         local float r, p;
594
595         e = find(world, classname, "player");
596
597         while(e)
598         {
599                 if(clienttype(e) == CLIENTTYPE_REAL)
600                 {
601                         p += 1;
602                         if(e.ready) r += 1;
603                 }
604                 e = find(e, classname, "player");
605         }
606
607         if(p && r == p)
608         {
609                 bprint("^1Server is restarting...\n");
610                 localcmd("restart\n");
611         }
612 }