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