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