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