]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/server/clientcommands.qc
oops... inverted logic in VoteCheckNasty
[divverent/nexuiz.git] / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 string 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         if(!teamsay)
15                 if(substring(msgin, 0, 1) == " ")
16                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
17
18         msgin = formatmessage(msgin);
19
20         if(msgin == "")
21                 return;
22
23         if(source.classname != "player")
24                 colorstr = "^0"; // black for spectators
25         else if(teams_matter)
26                 colorstr = Team_ColorCode(source.team);
27         else
28                 teamsay = FALSE;
29
30         if(intermission_running)
31                 teamsay = FALSE;
32
33         /*
34          * using bprint solves this... me stupid
35         // how can we prevent the message from appearing in a listen server?
36         // for now, just give "say" back and only handle say_team
37         if(!teamsay)
38         {
39                 clientcommand(self, strcat("say ", msgin));
40                 return;
41         }
42         */
43
44         if(cvar("g_chat_teamcolors"))
45                 namestr = playername(source);
46         else
47                 namestr = source.netname;
48         if(teamsay)
49         {
50                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin, "\n"));
51                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", wordwrap(msgin, 50));
52         }
53         else
54                 msgstr = strzone(strcat("\{1}", namestr, "^7: ", msgin, "\n"));
55
56         // FLOOD CONTROL
57         flood = 0;
58         {
59                 float flood_spl;
60                 float flood_burst;
61                 float flood_lmax;
62                 var .float flood_field;
63                 float lines;
64                 if(teamsay)
65                 {
66                         flood_spl = cvar("g_chat_flood_spl_team");
67                         flood_burst = cvar("g_chat_flood_burst_team");
68                         flood_lmax = cvar("g_chat_flood_lmax_team");
69                         flood_field = floodcontrol_team;
70                 }
71                 else
72                 {
73                         flood_spl = cvar("g_chat_flood_spl");
74                         flood_burst = cvar("g_chat_flood_burst");
75                         flood_lmax = cvar("g_chat_flood_lmax");
76                         flood_field = floodcontrol_chat;
77                 }
78                 flood_burst = max(0, flood_burst - 1);
79                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
80                 lines = ceil(strlennocol(msgstr) / 75);
81                 if(flood_lmax && lines > flood_lmax)
82                         flood = 2;
83                 else if(time >= self.flood_field)
84                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
85                 else
86                         flood = 1;
87         }
88
89         if(flood)
90         {
91                 if(cvar("g_chat_flood_notify_flooder"))
92                 {
93                         if(flood == 1)
94                                 sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
95                         else if(flood == 2)
96                                 sprint(self, "^3FLOOD CONTROL: ^7message too long\n");
97                 }
98                 else
99                         sprint(self, msgstr);
100                 ServerConsoleEcho(strcat("NOTE: ", playername(self), "^7 is flooding."), TRUE);
101         }
102         else if(teamsay)
103         {
104                 if(source.classname == "player")
105                 {
106                         FOR_EACH_REALPLAYER(head)
107                         {
108                                 if(head.team == source.team)
109                                 {
110                                         sprint(head, msgstr);
111                                         centerprint(head, cmsgstr);
112                                 }
113                         }
114                 }
115                 else
116                 {
117                         FOR_EACH_REALCLIENT(head) if(head.classname != "player")
118                         {
119                                 sprint(head, msgstr);
120                                 centerprint(head, cmsgstr);
121                         }
122                 }
123         }
124         else
125         {
126                 bprint(msgstr);
127                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
128         }
129
130         strunzone(msgstr);
131 }
132
133 float VoteCheckNasty(string cmd)
134 {
135         if(strstrofs(cmd, ";", 0) >= 0)
136                 return TRUE;
137         if(strstrofs(cmd, "\n", 0) >= 0)
138                 return TRUE;
139         if(strstrofs(cmd, "\r", 0) >= 0)
140                 return TRUE;
141         return FALSE;
142 }
143
144 string GetKickVoteVictim_newcommand;
145 entity GetKickVoteVictim(string vote, string cmd)
146 {
147         float tokens;
148         float i, n;
149         string ns;
150         entity e;
151
152         tokens = tokenize(vote);
153         ns = "";
154
155         if(tokens == 2)
156                 if(substring(argv(1), 0, 1) == "#")
157                         ns = substring(argv(1), 1, 999);
158         
159         if(tokens == 3)
160                 if(argv(1) == "#")
161                         ns = argv(2);
162
163         if(ns != "")
164         {
165                 n = stof(ns);
166                 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
167                 {
168                         for((e = world), (i = 0); i < n; ++i, (e = nextent(e)))
169                                 ;
170                         if(clienttype(e) == CLIENTTYPE_REAL)
171                         {
172                                 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
173                                 return e;
174                         }
175                 }
176         }
177
178         sprint(self, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
179         return world;
180 }
181
182 void SV_ParseClientCommand(string s) {
183         local string cmd;
184         local entity e;
185         local float i;
186
187         tokenize(s);
188
189         if(argv(0) == "vote") {
190                 if(argv(1) == "help") {
191                         local string vmasterdis;
192                         if(!cvar("sv_vote_master")) {
193                                 vmasterdis = " ^1(disabled)";
194                         }
195                         local string vcalldis;
196                         if(!cvar("sv_vote_call")) {
197                                 vcalldis = " ^1(disabled)";
198                         }
199                         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");
200                         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");
201                         sprint(self, "^7\"^2help^7\" shows this info.\n");
202                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
203                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
204                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
205                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
206                         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");
207                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
208                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
209                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
210                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
211                         sprint(self, "^7You can call a vote for or execute these commands:\n");
212                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
213                 } else if(argv(1) == "status") {
214                         if(votecalled) {
215                                 sprint(self, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", votecaller.netname, "^7.\n"));
216                         } else {
217                                 sprint(self, "^1No vote called.\n");
218                         }
219                 } else if(argv(1) == "call") {
220                         if(cvar("sv_vote_call")) {
221                                 if(votecalled) {
222                                         sprint(self, "^1There is already a vote called.\n");
223                                 } else {
224                                         local string vote;
225                                         vote = VoteParse();
226                                         if(vote == "") {
227                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
228                                         } else if(time < self.vote_next) {
229                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
230                                         } else if(VoteCheckNasty(vote)) {
231                                                 sprint(self, "Syntax error in command.\n");
232                                         } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
233                                                 // remap chmap to gotomap (forces intermission)
234                                                 if(vote == "chmap" || vote == "gotomap") // won't work without arguments
235                                                         return;
236                                                 if(substring(vote, 0, 6) == "chmap ")
237                                                         vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
238                                                 if(substring(vote, 0, 8) == "gotomap ")
239                                                 {
240                                                         if(!(vote = ValidateMap(substring(vote, 8, strlen(vote) - 8))))
241                                                                 return;
242                                                         vote = strcat("gotomap ", vote);
243                                                 }
244
245                                                 // make kick and kickban votes a bit nicer (and reject them if formatted badly)
246                                                 if(substring(vote, 0, 5) == "kick " || substring(vote, 0, 8) == "kickban ")
247                                                 {
248                                                         if(!(e = GetKickVoteVictim(vote, "vcall")))
249                                                                 return;
250                                                         votecalledvote_display = strzone(strcat("^1", vote, " (^7", e.netname, "^1)"));
251                                                         vote = GetKickVoteVictim_newcommand;
252                                                 }
253                                                 else
254                                                 {
255                                                         votecalledvote_display = strzone(strcat("^1", vote));
256                                                 }
257                                                 votecalledvote = strzone(vote);
258                                                 votecalled = TRUE;
259                                                 votecalledmaster = FALSE;
260                                                 votecaller = self; // remember who called the vote
261                                                 votefinished = time + cvar("sv_vote_timeout");
262                                                 votecaller.vote_vote = 1; // of course you vote yes
263                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
264                                                 bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ", votecalledvote_display, "\n");
265                                                 VoteCount(); // needed if you are the only one
266                                         } else {
267                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
268                                         }
269                                 }
270                         } else {
271                                 sprint(self, "^1Vote calling is NOT allowed.\n");
272                         }
273                 } else if(argv(1) == "stop") {
274                         if(!votecalled) {
275                                 sprint(self, "^1No vote called.\n");
276                         } else if(self == votecaller) { // the votecaller can stop a vote
277                                 VoteStop(self);
278                         } else if(self.vote_master) { // masters can, too
279                                 VoteStop(self);
280                         } else {
281                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
282                         }
283                 } else if(argv(1) == "master") {
284                         if(cvar("sv_vote_master")) {
285                                 if(votecalled) {
286                                         sprint(self, "^1There is already a vote called.\n");
287                                 } else {
288                                         votecalled = TRUE;
289                                         votecalledmaster = TRUE;
290                                         votecalledvote = strzone("XXX");
291                                         votecalledvote_display = strzone("^3master");
292                                         votecaller = self; // remember who called the vote
293                                         votefinished = time + cvar("sv_vote_timeout");
294                                         votecaller.vote_vote = 1; // of course you vote yes
295                                         votecaller.vote_next = time + cvar("sv_vote_wait");
296                                         bprint("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n");
297                                         VoteCount(); // needed if you are the only one
298                                 }
299                         } else {
300                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
301                         }
302                 } else if(argv(1) == "do") {
303                         if(argv(2) == "login") {
304                                 local string masterpwd;
305                                 masterpwd = cvar_string("sv_vote_master_password");
306                                 if(masterpwd != "") {
307                                         self.vote_master = (masterpwd == argv(3));
308                                         if(self.vote_master) {
309                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
310                                                 bprint("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n");
311                                         }
312                                         else
313                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
314                                 }
315                                 else
316                                         sprint(self, "^1You are NOT a master.\n");
317                         } else if(self.vote_master) {
318                                 local string dovote, dovote_display;
319                                 dovote = VoteParse();
320                                 if(dovote == "") {
321                                         sprint(self, "^1Your command was empty. See help for more info.\n");
322                                 } else if(VoteCheckNasty(dovote)) {
323                                         sprint(self, "Syntax error in command.\n");
324                                 } else if(VoteAllowed(strcat1(argv(2)))) { // strcat seems to be necessary
325                                         if(dovote == "chmap" || dovote == "gotomap") // won't work without arguments
326                                                 return;
327                                         if(substring(dovote, 0, 6) == "chmap ")
328                                                 dovote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
329                                         if(substring(dovote, 0, 8) == "gotomap ")
330                                         {
331                                                 if(!(dovote = ValidateMap(substring(dovote, 8, strlen(dovote) - 8))))
332                                                         return;
333                                                 dovote = strcat("gotomap ", dovote);
334                                         }
335
336                                         dovote_display = dovote;
337                                         if(substring(dovote, 0, 5) == "kick " || substring(dovote, 0, 8) == "kickban ")
338                                         {
339                                                 if(!(e = GetKickVoteVictim(dovote, "vdo")))
340                                                         return;
341                                                 dovote_display = strzone(strcat("^1", dovote, " (^7", e.netname, "^1)"));
342                                                 dovote = GetKickVoteVictim_newcommand;
343                                         }
344                                         bprint("\{1}^2* ^3", self.netname, "^2 used his ^3master^2 status to do \"^2", dovote_display, "^2\".\n");
345                                         localcmd(strcat(dovote, "\n"));
346                                 } else {
347                                         sprint(self, "^1This command is not ok. See help for more info.\n");
348                                 }
349                         } else {
350                                 sprint(self, "^1You are NOT a master.\n");
351                         }
352                 } else if(argv(1) == "yes") {
353                         if(!votecalled) {
354                                 sprint(self, "^1No vote called.\n");
355                         } else if(self.vote_vote == 0
356                                   || cvar("sv_vote_change")) {
357                                 sprint(self, "^1You accepted the vote.\n");
358                                 self.vote_vote = 1;
359                                 if(!cvar("sv_vote_singlecount")) {
360                                         VoteCount();
361                                 }
362                         } else {
363                                 sprint(self, "^1You have already voted.\n");
364                         }
365                 } else if(argv(1) == "no") {
366                         if(!votecalled) {
367                                 sprint(self, "^1No vote called.\n");
368                         } else if(self.vote_vote == 0
369                                   || cvar("sv_vote_change")) {
370                                 sprint(self, "^1You rejected the vote.\n");
371                                 self.vote_vote = -1;
372                                 if(!cvar("sv_vote_singlecount")) {
373                                         VoteCount();
374                                 }
375                         } else {
376                                 sprint(self, "^1You have already voted.\n");
377                         }
378                 } else {
379                         // ignore this?
380                         sprint(self, "^1Unknown vote command.\n");
381                 }
382         } else if(argv(0) == "autoswitch") {
383                 // be backwards compatible with older clients (enabled)
384                 self.autoswitch = ("0" != argv(1));
385                 local string autoswitchmsg;
386                 if (self.autoswitch) {
387                         autoswitchmsg = "on";
388                 } else {
389                         autoswitchmsg = "off";
390                 }
391                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
392         } else if(argv(0) == "clientversion") {
393                 if (argv(1) == "$gameversion") {
394                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
395                         // either that or someone wants to be funny
396                         self.version = 1;
397                 } else {
398                         self.version = stof(argv(1));
399                 }
400                 if(self.version != cvar("gameversion")) 
401                 {
402                         self.classname = "observer";
403                         self.frags = -2;
404                         PutClientInServer();
405                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
406                         //JoinBestTeam(self, 0);
407                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
408                         self.classname = "observer";
409                         stuffcmd(self,"menu_showteamselect\n");
410                 }
411         } else if(argv(0) == "reportcvar") { // old system
412                 GetCvars(1);
413         } else if(argv(0) == "sentcvar") { // new system
414                 GetCvars(1);
415         } else if(argv(0) == "spectate") {
416                 if(g_lms || g_arena)
417                         return; // don't allow spectating in lms, unless player runs out of lives
418                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
419                         if(self.flagcarried)
420                                 DropFlag(self.flagcarried);
421                         kh_Key_DropAll(self, TRUE);
422                         WaypointSprite_PlayerDead();
423                         DistributeFragsAmongTeam(self, self.team, 1.0);
424                         self.classname = "observer";
425                         PutClientInServer();
426                 }
427         } else if(argv(0) == "join") {
428                 if(!g_arena)
429                 if (self.classname != "player")
430                 {
431                         self.classname = "player";
432                         self.frags = 0;
433                         bprint ("^4", self.netname, "^4 is playing now\n");
434                         PutClientInServer();
435                 }
436         } else if( argv(0) == "selectteam" ) {
437                 if( !cvar("teamplay") ) {
438                         sprint( self, "selecteam can only be used in teamgames\n");
439                 } else if(cvar("g_campaign")) {
440                         //JoinBestTeam(self, 0);
441                 } else if( argv(1) == "none" ) {
442                         SV_ChangeTeam( 0 );
443                 } else if( argv(1) == "red" ) {
444                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
445                 } else if( argv(1) == "blue" ) {
446                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
447                 } else if( argv(1) == "yellow" ) {
448                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
449                 } else if( argv(1) == "pink" ) {
450                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
451                 } else if( argv(1) == "auto" ) {
452                         self.team = -1;
453                         JoinBestTeam( self, 0 );
454                 } else {
455                         sprint( self, strcat( "selectteam none/red/blue/yellow/pink/auto - \"", argv(1), "\" not recognised\n" ) );
456                 }
457         } else if(argv(0) == "ready") {
458                 if(cvar("sv_ready_restart"))
459                 {
460                         if(!restart_countdown)
461                         {
462                                 self.ready = TRUE;
463                                 bprint(self.netname, "^2 is ready\n");
464                                 ReadyCount();
465                         } else {
466                                 sprint(self, "^1game has already been restarted\n");
467                         }
468                 }
469         } else if(argv(0) == "maplist") {
470                 local float n;
471                 local string col;
472                 n = tokenize(cvar_string("g_maplist"));
473                 sprint(self, "^7Maps in list: ");
474                 for(i = 0; i < n; ++i)
475                 {
476                         if(mod(i, 2))
477                                 col = "^2";
478                         else
479                                 col = "^3";
480                         sprint(self, strcat(col, argv(i), " "));
481                 }
482                 sprint(self, "\n");
483 #ifdef MAPINFO
484         } else if(argv(0) == "lsmaps") {
485                 sprint(self, "^7Maps available: ");
486                 for(i = 0; i < MapInfo_count; ++i)
487                 {
488                         if(mod(i, 2))
489                                 col = "^2";
490                         else
491                                 col = "^3";
492                         sprint(self, strcat(col, MapInfo_BSPName_ByID(i), " "));
493                 }
494                 sprint(self, "\n");
495 #endif
496         } else if(argv(0) == "teamstatus") {
497                 PrintScoreboard(self);
498         } else if(argv(0) == "voice") {
499                 VoiceMessage(argv(1));
500         } else if(argv(0) == "say") {
501                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
502                 //clientcommand(self, formatmessage(s));
503         } else if(argv(0) == "say_team") {
504                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
505                 //clientcommand(self, formatmessage(s));
506         } else if(argv(0) == "info") {
507                 cmd = cvar_string(strcat("sv_info_", argv(1)));
508                 if(cmd == "")
509                         sprint(self, "ERROR: unsupported info command\n");
510                 else
511                         wordwrap_sprint(cmd, 1111);
512         } else if(argv(0) == "suggestmap") {
513                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
514         } else {
515                 cmd = argv(0);
516                 /* checks not needed any more since DP has separated clientcommands and regular commands
517                 if(cmd != "status")
518                 if(cmd != "name")
519                 //if(cmd != "say") // handled above
520                 //if(cmd != "say_team") // handled above
521                 if(cmd != "tell")
522                 if(cmd != "color")
523                 if(cmd != "kill")
524                 if(cmd != "pause")
525                 if(cmd != "kick")
526                 if(cmd != "ping")
527                 if(cmd != "pings")
528                 if(cmd != "ban")
529                 if(cmd != "pmodel")
530                 if(cmd != "rate")
531                 if(cmd != "playermodel")
532                 if(cmd != "playerskin")
533                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
534                 {
535                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
536                         return;
537                 }
538                 */
539                 clientcommand(self,s);
540         }
541 }
542
543 string ValidateMap(string m)
544 {
545 #ifdef MAPINFO
546         m = MapInfo_FixName(m);
547         if(!m)
548         {
549                 sprint(self, "This map is not available on this server.\n");
550                 return string_null;
551         }
552 #endif
553         if(!cvar("sv_vote_change_gametype"))
554                 if(!IsSameGametype(m))
555                 {
556                         sprint(self, "This server does not allow changing the game type by map votes.\n");
557                         return string_null;
558                 }
559         if(!cvar("sv_vote_override_mostrecent"))
560                 if(Map_IsRecent(m))
561                 {
562                         sprint(self, "This server does not allow for recent maps to be played again. Please be patient for some rounds.\n");
563                         return string_null;
564                 }
565 #ifdef MAPINFO
566         if(!MapInfo_CheckMap(m))
567         {
568                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode.\n"));
569                 return string_null;
570         }
571 #else
572         if(!TryFile(strcat("maps/", m, ".mapcfg")))
573         {
574                 sprint(self, strcat("^1Invalid mapname, \"^3", m, "^1\" does not exist on this server.\n"));
575                 return string_null;
576         }
577 #endif
578
579         return m;
580 }
581
582
583 void VoteThink() {
584         if(votefinished > 0 // a vote was called
585             && time > votefinished) // time is up
586         {
587                 VoteCount();
588         }
589 }
590
591 string VoteParse() {
592         local float index;
593         index = 3;
594         local string vote;
595         vote = argv(2);
596         while(argv(index) != "") {
597                 vote = strcat(vote, " ", argv(index));
598                 index++;
599         }
600
601         // necessary for some of the string operations
602         vote = strzone(vote);
603
604         // now we remove some things that could be misused
605         index = 0;
606         local float found;
607         found = FALSE;
608         local float votelength;
609         votelength = strlen(vote);
610         while(!found && index < votelength)
611         {
612                 local string badchar;
613                 badchar = substring(vote, index, 1);
614                 if(badchar == ";"
615                    || badchar == "\r"
616                    || badchar == "\n")
617                 {
618                         found = TRUE;
619                 } else {
620                         index++;
621                 }
622         }
623         return substring(vote, 0, index);
624 }
625
626 float VoteAllowed(string votecommand) {
627         tokenize(cvar_string("sv_vote_commands"));
628         local float index;
629         index = 0;
630         while(argv(index) != "") {
631                 local string allowed;
632                 allowed = argv(index);
633                 if(votecommand == allowed) {
634                         return TRUE;
635                 }
636                 index++;
637         }
638         return FALSE;
639 }
640
641 void VoteReset() {
642         local entity player;
643
644         FOR_EACH_CLIENT(player)
645         {
646                 player.vote_vote = 0;
647                 centerprint_expire(player, CENTERPRIO_VOTE);
648         }
649
650         if(votecalled)
651         {
652                 strunzone(votecalledvote);
653                 strunzone(votecalledvote_display);
654         }
655
656         votecalled = FALSE;
657         votecalledmaster = FALSE;
658         votefinished = 0;
659 }
660
661 void VoteAccept() {
662         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
663         if(votecalledmaster)
664         {
665                 votecaller.vote_master = 1;
666         } else {
667                 localcmd(strcat(votecalledvote, "\n"));
668         }
669         votecaller.vote_next = 0; // people like your votes, no wait for next vote
670         VoteReset();
671 }
672
673 void VoteReject() {
674         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
675         VoteReset();
676 }
677
678 void VoteTimeout() {
679         bprint("\{1}^2* ^3", votecaller.netname, "^2's vote for ", votecalledvote_display, "^2 timed out\n");
680         VoteReset();
681 }
682
683 void VoteStop(entity stopper) {
684         bprint("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n");
685         if(stopper == votecaller) {
686                 // no wait for next vote so you can correct your vote
687                 votecaller.vote_next = 0;
688         }
689         VoteReset();
690 }
691
692 void VoteNag() {
693         if(votecalled)
694                 if(self.vote_vote == 0)
695                         centerprint_atprio(self, CENTERPRIO_VOTE, strcat("^7^3", votecaller.netname, "^2 called a vote for ", votecalledvote_display, "\n\n^2You have not voted yet!\n^2HINT: By default, F1 is yes and F2 is no."));
696 }
697
698 void VoteCount() {
699         local float playercount;
700         playercount = 0;
701         local float yescount;
702         yescount = 0;
703         local float nocount;
704         nocount = 0;
705         local entity player;
706
707         FOR_EACH_REALCLIENT(player)
708         {
709                 if(player.vote_vote < 0) {
710                         nocount++;
711                 } else if(player.vote_vote > 0) {
712                         yescount++;
713                 }
714                 playercount++;
715         }
716
717         if((playercount == 1) && votecalledmaster) {
718                 // if only one player is on the server becoming vote
719                 // master is not allowed.  This could be used for
720                 // trolling or worse. 'self' is the user who has
721                 // called the vote because this function is called
722                 // by SV_ParseClientCommand. Maybe all voting should
723                 // be disabled for a single player?
724                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
725                 votecaller.vote_next = 0;
726                 VoteReset();
727         } else if((playercount / 2) < yescount) { // vote accepted
728                 VoteAccept();
729         } else if((playercount / 2) < nocount) { // vote rejected
730                 VoteReject();
731         } else if(time > votefinished) { // vote timedout
732                 if(cvar("sv_vote_simple_majority"))
733                         if(yescount > nocount)
734                                 VoteAccept();
735                         else if(nocount > yescount)
736                                 VoteReject();
737                         else
738                                 VoteTimeout();
739                 else
740                         VoteTimeout();
741         } // else still running
742 }
743
744 float timelimit_orig;
745
746 void ReadyCount()
747 {
748         local entity e;
749         local float r, p;
750
751         FOR_EACH_REALPLAYER(e)
752         {
753                 p += 1;
754                 if(e.ready)
755                         r += 1;
756         }
757
758         if(!p || r < p)
759                 return;
760
761         bprint("^1Server is restarting...\n");
762         
763         // no arena, assault & onslaught support yet...
764         if(g_arena | g_assault | g_onslaught | gameover | intermission_running)
765                 localcmd("restart\n");
766
767         restart_countdown = time + RESTART_COUNTDOWN;
768         if(0<cvar("timelimit"))
769         {
770                 // remember original timelimit on first restart
771                 if(!timelimit_orig)
772                         timelimit_orig = cvar("timelimit");
773                 cvar_set("timelimit", ftos(timelimit_orig + ceil(restart_countdown)/60));
774         }
775
776         reset_map();
777         
778         if(cvar("sv_eventlog"))
779                 GameLogEcho(":restart", FALSE);
780 }