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