vote.qc 37 KB
Newer Older
TimePath's avatar
TimePath committed
1
#include "vote.qh"
TimePath's avatar
TimePath committed
2

TimePath's avatar
TimePath committed
3
#include <common/command/_mod.qh>
TimePath's avatar
TimePath committed
4
#include <common/constants.qh>
5
#include <common/gamemodes/_mod.qh>
TimePath's avatar
TimePath committed
6
#include <common/mapinfo.qh>
7
#include <common/net_linked.qh>
TimePath's avatar
TimePath committed
8
#include <common/notifications/all.qh>
TimePath's avatar
TimePath committed
9
#include <common/playerstats.qh>
10
#include <common/stats.qh>
TimePath's avatar
TimePath committed
11
#include <common/util.qh>
12 13
#include <common/weapons/_all.qh>
#include <server/client.qh>
14
#include <server/command/banning.qh>
15 16 17 18 19 20 21 22 23 24 25
#include <server/command/common.qh>
#include <server/command/vote.qh>
#include <server/damage.qh>
#include <server/gamelog.qh>
#include <server/intermission.qh>
#include <server/mutators/_mod.qh>
#include <server/race.qh>
#include <server/round_handler.qh>
#include <server/scores.qh>
#include <server/teamplay.qh>
#include <server/world.qh>
26

27 28
// =============================================
//  Server side voting code, reworked by Samual
29
//  Last updated: December 27th, 2011
30 31
// =============================================

32
//  Nagger for players to know status of voting
TimePath's avatar
TimePath committed
33
bool Nagger_SendEntity(entity this, entity to, float sendflags)
34
{
TimePath's avatar
TimePath committed
35
	int nags, i, f, b;
36
	entity e;
TimePath's avatar
TimePath committed
37
	WriteHeader(MSG_ENTITY, ENT_CLIENT_NAGGER);
38 39 40 41 42 43 44 45 46 47 48 49

	// bits:
	//   1 = ready
	//   2 = player needs to ready up
	//   4 = vote
	//   8 = player needs to vote
	//  16 = warmup
	// sendflags:
	//  64 = vote counts
	// 128 = vote string

	nags = 0;
TimePath's avatar
TimePath committed
50
	if (readycount)
51
	{
TimePath's avatar
TimePath committed
52
		nags |= BIT(0);
TimePath's avatar
TimePath committed
53
		if (to.ready == 0) nags |= BIT(1);
54
	}
TimePath's avatar
TimePath committed
55
	if (vote_called)
56
	{
TimePath's avatar
TimePath committed
57
		nags |= BIT(2);
TimePath's avatar
TimePath committed
58
		if (to.vote_selection == 0) nags |= BIT(3);
59
	}
TimePath's avatar
TimePath committed
60
	if (warmup_stage) nags |= BIT(4);
61

TimePath's avatar
TimePath committed
62
	if (sendflags & BIT(6)) nags |= BIT(6);
63

TimePath's avatar
TimePath committed
64
	if (sendflags & BIT(7)) nags |= BIT(7);
65

TimePath's avatar
TimePath committed
66
	if (!(nags & 4))  // no vote called? send no string
TimePath's avatar
TimePath committed
67
		nags &= ~(BIT(6) | BIT(7));
68 69 70

	WriteByte(MSG_ENTITY, nags);

TimePath's avatar
TimePath committed
71
	if (nags & BIT(6))
72
	{
Samual's avatar
Samual committed
73 74
		WriteByte(MSG_ENTITY, vote_accept_count);
		WriteByte(MSG_ENTITY, vote_reject_count);
75
		WriteByte(MSG_ENTITY, vote_needed_overall);
76
		WriteChar(MSG_ENTITY, to.vote_selection);
77 78
	}

TimePath's avatar
TimePath committed
79
	if (nags & BIT(7)) WriteString(MSG_ENTITY, vote_called_display);
80

TimePath's avatar
TimePath committed
81
	if (nags & 1)
82
	{
TimePath's avatar
TimePath committed
83
		for (i = 1; i <= maxclients; i += 8)
84
		{
terencehill's avatar
terencehill committed
85 86 87
			for (f = 0, e = edict_num(i), b = BIT(0); b < BIT(8); b <<= 1, e = nextent(e))
				if (!IS_REAL_CLIENT(e) || e.ready)
					f |= b;
88 89 90 91
			WriteByte(MSG_ENTITY, f);
		}
	}

TimePath's avatar
TimePath committed
92
	return true;
93
}
94

95 96
void Nagger_Init()
{
TimePath's avatar
TimePath committed
97
	Net_LinkEntity(nagger = new_pure(nagger), false, 0, Nagger_SendEntity);
98
}
99

100 101
void Nagger_VoteChanged()
{
TimePath's avatar
TimePath committed
102
	if (nagger) nagger.SendFlags |= BIT(7);
103
}
104

105 106
void Nagger_VoteCountChanged()
{
TimePath's avatar
TimePath committed
107
	if (nagger) nagger.SendFlags |= BIT(6);
108
}
109

110 111
void Nagger_ReadyCounted()
{
TimePath's avatar
TimePath committed
112
	if (nagger) nagger.SendFlags |= BIT(0);
113 114
}

115 116 117
// If the vote_caller is still here, return their name, otherwise vote_caller_name
string OriginalCallerName()
{
118
	if (IS_REAL_CLIENT(vote_caller)) return playername(vote_caller.netname, vote_caller.team, false);
119 120
	return vote_caller_name;
}
121

122 123 124
// =======================
//  Game logic for voting
// =======================
125

126
void VoteReset()
127
{
TimePath's avatar
TimePath committed
128
	FOREACH_CLIENT(true, { it.vote_selection = 0; });
129

TimePath's avatar
TimePath committed
130
	if (vote_called)
131
	{
132 133 134
		strfree(vote_called_command);
		strfree(vote_called_display);
		strfree(vote_caller_name);
135 136
	}

137
	vote_called = VOTE_NULL;
TimePath's avatar
TimePath committed
138
	vote_caller = NULL;
139
	vote_endtime = 0;
140

141 142 143 144 145 146
	vote_parsed_command = string_null;
	vote_parsed_display = string_null;

	Nagger_VoteChanged();
}

147
void VoteStop(entity stopper)
148
{
149
	bprint("\{1}^2* ^3", GetCallerName(stopper), "^2 stopped ^3", OriginalCallerName(), "^2's vote\n");
TimePath's avatar
TimePath committed
150
	if (autocvar_sv_eventlog)   GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
151
	// Don't force them to wait for next vote, this way they can e.g. correct their vote.
TimePath's avatar
TimePath committed
152
	if ((vote_caller) && (stopper == vote_caller))   vote_caller.vote_waittime = time + autocvar_sv_vote_stop;
153 154
	VoteReset();
}
155

156
void VoteAccept()
157
{
158
	bprint("\{1}^2* ^3", OriginalCallerName(), "^2's vote for ^1", vote_called_display, "^2 was accepted\n");
159

TimePath's avatar
TimePath committed
160 161
	if ((vote_called == VOTE_MASTER) && vote_caller) vote_caller.vote_master = 1;
	else localcmd(strcat(vote_called_command, "\n"));
162

TimePath's avatar
TimePath committed
163
	if (vote_caller)   vote_caller.vote_waittime = 0;  // people like your votes, you don't need to wait to vote again
164 165

	VoteReset();
TimePath's avatar
TimePath committed
166
	Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_VOTE_ACCEPT);
167 168
}

169
void VoteReject()
170
{
171
	bprint("\{1}^2* ^3", OriginalCallerName(), "^2's vote for ", vote_called_display, "^2 was rejected\n");
172
	VoteReset();
TimePath's avatar
TimePath committed
173
	Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_VOTE_FAIL);
174 175
}

176
void VoteTimeout()
Samual's avatar
Samual committed
177
{
178
	bprint("\{1}^2* ^3", OriginalCallerName(), "^2's vote for ", vote_called_display, "^2 timed out\n");
179
	VoteReset();
TimePath's avatar
TimePath committed
180
	Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_VOTE_FAIL);
Samual's avatar
Samual committed
181 182
}

183
void VoteSpam(float notvoters, float mincount, string result)
184
{
Samual's avatar
Samual committed
185 186 187 188 189
	bprint(strcat(
		strcat("\{1}^2* vote results: ^1", ftos(vote_accept_count)),
		strcat("^2:^1", ftos(vote_reject_count)),
		((mincount >= 0) ? strcat("^2 (^1", ftos(mincount), "^2 needed)") : "^2"),
		strcat(", ^1", ftos(vote_abstain_count), "^2 didn't care"),
190
		strcat(", ^1", ftos(notvoters), strcat("^2 didn't ", ((mincount >= 0) ? "" : "have to "), "vote\n"))));
191

TimePath's avatar
TimePath committed
192
	if (autocvar_sv_eventlog)
193
	{
Samual's avatar
Samual committed
194 195 196 197 198 199
		GameLogEcho(strcat(
			strcat(":vote:v", result, ":", ftos(vote_accept_count)),
			strcat(":", ftos(vote_reject_count)),
			strcat(":", ftos(vote_abstain_count)),
			strcat(":", ftos(notvoters)),
			strcat(":", ftos(mincount))));
200 201 202
	}
}

203 204
#define spectators_allowed (!autocvar_sv_vote_nospectators || (autocvar_sv_vote_nospectators == 1 && (warmup_stage || intermission_running)))

205
void VoteCount(float first_count)
206
{
207 208
	// declarations
	vote_accept_count = vote_reject_count = vote_abstain_count = 0;
209

210
	float vote_player_count = 0, notvoters = 0;
211 212
	float vote_real_player_count = 0, vote_real_accept_count = 0;
	float vote_real_reject_count = 0, vote_real_abstain_count = 0;
213 214
	float vote_needed_of_voted, final_needed_votes;
	float vote_factor_overall, vote_factor_of_voted;
215

216
	Nagger_VoteCountChanged();
217

218
	// add up all the votes from each connected client
TimePath's avatar
TimePath committed
219
	FOREACH_CLIENT(IS_REAL_CLIENT(it) && IS_CLIENT(it), {
220
		++vote_player_count;
221 222
		if (IS_PLAYER(it))   ++vote_real_player_count;
		switch (it.vote_selection)
223
		{
TimePath's avatar
TimePath committed
224 225
			case VOTE_SELECT_REJECT:
			{ ++vote_reject_count;
226
			  { if (IS_PLAYER(it)) ++vote_real_reject_count; } break;
TimePath's avatar
TimePath committed
227 228 229
			}
			case VOTE_SELECT_ACCEPT:
			{ ++vote_accept_count;
230
			  { if (IS_PLAYER(it)) ++vote_real_accept_count; } break;
TimePath's avatar
TimePath committed
231 232 233
			}
			case VOTE_SELECT_ABSTAIN:
			{ ++vote_abstain_count;
234
			  { if (IS_PLAYER(it)) ++vote_real_abstain_count; } break;
TimePath's avatar
TimePath committed
235
			}
236
			default: break;
237
		}
TimePath's avatar
TimePath committed
238
	});
239

240
	// Check to see if there are enough players on the server to allow master voting... otherwise, vote master could be used for evil.
TimePath's avatar
TimePath committed
241
	if ((vote_called == VOTE_MASTER) && autocvar_sv_vote_master_playerlimit > vote_player_count)
242
	{
TimePath's avatar
TimePath committed
243
		if (vote_caller)   vote_caller.vote_waittime = 0;
244
		print_to(vote_caller, "^1There are not enough players on this server to allow you to become vote master.");
245 246
		VoteReset();
		return;
247
	}
248 249

	// if spectators aren't allowed to vote and there are players in a match, then only count the players in the vote and ignore spectators.
TimePath's avatar
TimePath committed
250
	if (!spectators_allowed && (vote_real_player_count > 0))
251
	{
252 253 254 255
		vote_accept_count = vote_real_accept_count;
		vote_reject_count = vote_real_reject_count;
		vote_abstain_count = vote_real_abstain_count;
		vote_player_count = vote_real_player_count;
256
	}
257

258 259
	// people who have no opinion in any way :D
	notvoters = (vote_player_count - vote_accept_count - vote_reject_count - vote_abstain_count);
260

261 262 263
	// determine the goal for the vote to be passed or rejected normally
	vote_factor_overall = bound(0.5, autocvar_sv_vote_majority_factor, 0.999);
	vote_needed_overall = floor((vote_player_count - vote_abstain_count) * vote_factor_overall) + 1;
264

265 266 267
	// if the vote times out, determine the amount of votes needed of the people who actually already voted
	vote_factor_of_voted = bound(0.5, autocvar_sv_vote_majority_factor_of_voted, 0.999);
	vote_needed_of_voted = floor((vote_accept_count + vote_reject_count) * vote_factor_of_voted) + 1;
268

269
	// are there any players at all on the server? it could be an admin vote
TimePath's avatar
TimePath committed
270
	if (vote_player_count == 0 && first_count)
271
	{
TimePath's avatar
TimePath committed
272
		VoteSpam(0, -1, "yes");  // no players at all, just accept it
273 274 275
		VoteAccept();
		return;
	}
276 277

	// since there ARE players, finally calculate the result of the vote
TimePath's avatar
TimePath committed
278
	if (vote_accept_count >= vote_needed_overall)
279
	{
TimePath's avatar
TimePath committed
280
		VoteSpam(notvoters, -1, "yes");  // there is enough acceptions to pass the vote
281 282 283
		VoteAccept();
		return;
	}
284

TimePath's avatar
TimePath committed
285
	if (vote_reject_count > vote_player_count - vote_abstain_count - vote_needed_overall)
286
	{
TimePath's avatar
TimePath committed
287
		VoteSpam(notvoters, -1, "no");  // there is enough rejections to deny the vote
288 289 290
		VoteReject();
		return;
	}
291

292
	// there is not enough votes in either direction, now lets just calculate what the voters have said
TimePath's avatar
TimePath committed
293
	if (time > vote_endtime)
294 295
	{
		final_needed_votes = vote_needed_overall;
296

TimePath's avatar
TimePath committed
297
		if (autocvar_sv_vote_majority_factor_of_voted)
298
		{
TimePath's avatar
TimePath committed
299
			if (vote_accept_count >= vote_needed_of_voted)
300
			{
301 302 303
				VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "yes");
				VoteAccept();
				return;
304
			}
305

TimePath's avatar
TimePath committed
306
			if (vote_accept_count + vote_reject_count > 0)
307
			{
308 309 310
				VoteSpam(notvoters, min(vote_needed_overall, vote_needed_of_voted), "no");
				VoteReject();
				return;
311
			}
312

313
			final_needed_votes = min(vote_needed_overall, vote_needed_of_voted);
314
		}
315

316
		// it didn't pass or fail, so not enough votes to even make a decision.
317 318
		VoteSpam(notvoters, final_needed_votes, "timeout");
		VoteTimeout();
319 320 321
	}
}

322
void VoteThink()
323
{
TimePath's avatar
TimePath committed
324
	if (vote_endtime > 0)        // a vote was called
325
	{
TimePath's avatar
TimePath committed
326 327
		if (time > vote_endtime) // time is up
			VoteCount(false);
328 329 330
	}
}

331 332 333 334 335

// =======================
//  Game logic for warmup
// =======================

336
// Resets the state of all clients, items, weapons, waypoints, ... of the map.
337
void reset_map(bool dorespawn)
TimePath's avatar
TimePath committed
338
{
339 340
	if (time <= game_starttime)
	{
341
		if (game_stopped)
342 343 344 345
			return;
		if (round_handler_IsActive())
			round_handler_Reset(game_starttime);
	}
346

347 348 349 350 351
	if (shuffleteams_on_reset_map)
	{
		shuffleteams();
		shuffleteams_on_reset_map = false;
	}
352
	MUTATOR_CALLHOOK(reset_map_global);
353

354
	FOREACH_ENTITY_FLOAT_ORDERED(pure_data, false,
355 356 357
	{
		if(IS_CLIENT(it))
			continue;
TimePath's avatar
TimePath committed
358
		if (it.reset)
359
		{
TimePath's avatar
TimePath committed
360
			it.reset(it);
TimePath's avatar
TimePath committed
361
			continue;
362
		}
TimePath's avatar
TimePath committed
363
		if (it.team_saved) it.team = it.team_saved;
TimePath's avatar
TimePath committed
364
		if (it.flags & FL_PROJECTILE) delete(it);  // remove any projectiles left
TimePath's avatar
TimePath committed
365
	});
366 367

	// Waypoints and assault start come LAST
TimePath's avatar
TimePath committed
368
	FOREACH_ENTITY_ORDERED(IS_NOT_A_CLIENT(it), {
Mario's avatar
Mario committed
369
		if (it.reset2) it.reset2(it);
TimePath's avatar
TimePath committed
370
	});
371

372
	FOREACH_CLIENT(IS_PLAYER(it) && STAT(FROZEN, it), { Unfreeze(it, false); });
Mario's avatar
Mario committed
373

374 375
	// Moving the player reset code here since the player-reset depends
	// on spawnpoint entities which have to be reset first --blub
TimePath's avatar
TimePath committed
376
	if (dorespawn)
377
	{
TimePath's avatar
TimePath committed
378
		if (!MUTATOR_CALLHOOK(reset_map_players))
379
		{
380 381
			if (restart_mapalreadyrestarted || (time < game_starttime))
			{
382
				FOREACH_CLIENT(IS_PLAYER(it),
TimePath's avatar
TimePath committed
383
				{
384 385 386 387 388 389
					/*
					only reset players if a restart countdown is active
					this can either be due to cvar sv_ready_restart_after_countdown having set
					restart_mapalreadyrestarted to 1 after the countdown ended or when
					sv_ready_restart_after_countdown is not used and countdown is still running
					*/
TimePath's avatar
TimePath committed
390
					// NEW: changed behaviour so that it prevents that previous spectators/observers suddenly spawn as players
391
					// PlayerScore_Clear(it);
Mario's avatar
Mario committed
392
					CS(it).killcount = 0;
393 394 395
					// stop the player from moving so that he stands still once he gets respawned
					it.velocity = '0 0 0';
					it.avelocity = '0 0 0';
396
					CS(it).movement = '0 0 0';
397 398
					PutClientInServer(it);
				});
399
			}
400 401 402 403
		}
	}
}

404
// Restarts the map after the countdown is over (and cvar sv_ready_restart_after_countdown is set)
405
void ReadyRestart_think(entity this)
TimePath's avatar
TimePath committed
406
{
407
	restart_mapalreadyrestarted = true;
TimePath's avatar
TimePath committed
408
	reset_map(true);
409
	Score_ClearAll();
TimePath's avatar
TimePath committed
410
	delete(this);
411 412 413 414 415
}

// Forces a restart of the game without actually reloading the map // this is a mess...
void ReadyRestart_force()
{
416
	if (time <= game_starttime && game_stopped)
417 418
		return;

419 420 421 422 423
	bprint("^1Server is restarting...\n");

	VoteReset();

	// clear overtime, we have to decrease timelimit to its original value again.
424 425
	if (checkrules_overtimesadded > 0 && g_race_qualifying != 2)
		cvar_set("timelimit", ftos(autocvar_timelimit - (checkrules_overtimesadded * autocvar_timelimit_overtime)));
426 427
	checkrules_suddendeathend = checkrules_overtimesadded = checkrules_suddendeathwarning = 0;

428
	readyrestart_happened = true;
429
	game_starttime = time + RESTART_COUNTDOWN;
430

Samual's avatar
Samual committed
431
	// clear player attributes
TimePath's avatar
TimePath committed
432
	FOREACH_CLIENT(IS_PLAYER(it), {
433
		it.alivetime = 0;
Mario's avatar
Mario committed
434
		CS(it).killcount = 0;
terencehill's avatar
terencehill committed
435 436
		float val = PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_ALIVETIME, 0);
		PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_ALIVETIME, -val);
TimePath's avatar
TimePath committed
437
	});
438

439
	restart_mapalreadyrestarted = false; // reset this var, needed when cvar sv_ready_restart_repeatable is in use
440 441

	// disable the warmup global for the server
442 443
	if(warmup_stage)
		localcmd("\nsv_hook_warmupend\n");
TimePath's avatar
TimePath committed
444
	warmup_stage = 0;                // once the game is restarted the game is in match stage
445 446

	// reset the .ready status of all players (also spectators)
TimePath's avatar
TimePath committed
447
	FOREACH_CLIENT(IS_REAL_CLIENT(it), { it.ready = false; });
448
	readycount = 0;
TimePath's avatar
TimePath committed
449
	Nagger_ReadyCounted();  // NOTE: this causes a resend of that entity, and will also turn off warmup state on the client
450 451

	// lock teams with lockonrestart
TimePath's avatar
TimePath committed
452
	if (autocvar_teamplay_lockonrestart && teamplay)
453
	{
454
		lockteams = true;
455 456 457
		bprint("^1The teams are now locked.\n");
	}

TimePath's avatar
TimePath committed
458
	// initiate the restart-countdown-announcer entity
459
	if (sv_ready_restart_after_countdown)
460
	{
TimePath's avatar
TimePath committed
461
		entity restart_timer = new_pure(restart_timer);
TimePath's avatar
TimePath committed
462
		setthink(restart_timer, ReadyRestart_think);
463 464 465 466
		restart_timer.nextthink = game_starttime;
	}

	// after a restart every players number of allowed timeouts gets reset, too
TimePath's avatar
TimePath committed
467 468
	if (autocvar_sv_timeout)
	{
TimePath's avatar
TimePath committed
469
		FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), { CS(it).allowed_timeouts = autocvar_sv_timeout_number; });
470
	}
471 472

	if (!sv_ready_restart_after_countdown) reset_map(true);
473 474
	if (autocvar_sv_eventlog) GameLogEcho(":restart");
}
475 476 477

void ReadyRestart()
{
478
	if (MUTATOR_CALLHOOK(ReadyRestart_Deny) || intermission_running || race_completing) localcmd("restart\n");
479
	else localcmd("\nsv_hook_readyrestart\n");
480 481 482

	// Reset ALL scores, but only do that at the beginning of the countdown if sv_ready_restart_after_countdown is off!
	// Otherwise scores could be manipulated during the countdown.
483
	if (!sv_ready_restart_after_countdown) Score_ClearAll();
484 485 486
	ReadyRestart_force();
}

Samual's avatar
Samual committed
487
// Count the players who are ready and determine whether or not to restart the match
488 489
void ReadyCount()
{
490
	float ready_needed_factor, ready_needed_count;
491
	float t_ready = 0, t_players = 0;
492

TimePath's avatar
TimePath committed
493
	FOREACH_CLIENT(IS_REAL_CLIENT(it) && (IS_PLAYER(it) || it.caplayer == 1), {
494 495
		++t_players;
		if (it.ready) ++t_ready;
TimePath's avatar
TimePath committed
496
	});
497 498 499 500 501

	readycount = t_ready;

	Nagger_ReadyCounted();

502 503
	ready_needed_factor = bound(0.5, cvar("g_warmup_majority_factor"), 0.999);
	ready_needed_count = floor(t_players * ready_needed_factor) + 1;
504

TimePath's avatar
TimePath committed
505
	if (readycount >= ready_needed_count) ReadyRestart();
506 507 508 509 510 511 512 513 514 515
}


// ======================================
//  Supporting functions for VoteCommand
// ======================================

float Votecommand_check_assignment(entity caller, float assignment)
{
	float from_server = (!caller);
516

TimePath's avatar
TimePath committed
517 518 519
	if ((assignment == VC_ASGNMNT_BOTH)
	    || ((!from_server && assignment == VC_ASGNMNT_CLIENTONLY)
	    || (from_server && assignment == VC_ASGNMNT_SERVERONLY))) return true;
520

TimePath's avatar
TimePath committed
521
	return false;
522 523
}

Daniel Ivanescu's avatar
Daniel Ivanescu committed
524
string VoteCommand_extractcommand(string input, float startpos, int argc)
525 526
{
	string output;
527

TimePath's avatar
TimePath committed
528 529
	if ((argc - 1) < startpos) output = "";
	else output = substring(input, argv_start_index(startpos), argv_end_index(-1) - argv_start_index(startpos));
530

531 532 533 534 535
	return output;
}

float VoteCommand_checknasty(string vote_command)
{
TimePath's avatar
TimePath committed
536 537 538 539
	if ((strstrofs(vote_command, ";", 0) >= 0)
	    || (strstrofs(vote_command, "\n", 0) >= 0)
	    || (strstrofs(vote_command, "\r", 0) >= 0)
	    || (strstrofs(vote_command, "$", 0) >= 0)) return false;
540

TimePath's avatar
TimePath committed
541
	return true;
542 543
}

544 545 546 547 548 549 550 551 552 553
// NOTE: requires input to be surrounded by spaces
string VoteCommand_checkreplacements(string input)
{
	string output = input;
	// allow gotomap replacements
	output = strreplace(" map ", " gotomap ", output);
	output = strreplace(" chmap ", " gotomap ", output);
	return output;
}

554 555
float VoteCommand_checkinlist(string vote_command, string list)
{
556
	string l = VoteCommand_checkreplacements(strcat(" ", list, " "));
557

558
	if (strstrofs(l, VoteCommand_checkreplacements(strcat(" ", vote_command, " ")), 0) >= 0) return true;
559

TimePath's avatar
TimePath committed
560
	return false;
561 562 563 564 565
}

string ValidateMap(string validated_map, entity caller)
{
	validated_map = MapInfo_FixName(validated_map);
566

Rudolf Polzer's avatar
Rudolf Polzer committed
567
	if (!validated_map)
568 569 570 571
	{
		print_to(caller, "This map is not available on this server.");
		return string_null;
	}
572

TimePath's avatar
TimePath committed
573
	if (!autocvar_sv_vote_override_mostrecent && caller)
574
	{
TimePath's avatar
TimePath committed
575
		if (Map_IsRecent(validated_map))
576 577 578 579 580
		{
			print_to(caller, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
			return string_null;
		}
	}
581

TimePath's avatar
TimePath committed
582
	if (!MapInfo_CheckMap(validated_map))
583 584 585 586 587 588 589 590
	{
		print_to(caller, strcat("^1Invalid mapname, \"^3", validated_map, "^1\" does not support the current game mode."));
		return string_null;
	}

	return validated_map;
}

Daniel Ivanescu's avatar
Daniel Ivanescu committed
591
float VoteCommand_checkargs(float startpos, int argc)
592
{
593
	float p, q, check, minargs;
Rudolf Polzer's avatar
Rudolf Polzer committed
594
	string cvarname = strcat("sv_vote_command_restriction_", argv(startpos));
595
	string cmdrestriction = "";  // No we don't.
596 597
	string charlist, arg;
	float checkmate;
598

599 600 601
	if(cvar_type(cvarname) & CVAR_TYPEFLAG_EXISTS)
		cmdrestriction = cvar_string(cvarname);
	else
TimePath's avatar
TimePath committed
602
		LOG_INFO("NOTE: ", cvarname, " does not exist, no restrictions will be applied.");
603

TimePath's avatar
TimePath committed
604
	if (cmdrestriction == "") return true;
605

TimePath's avatar
TimePath committed
606
	++startpos;  // skip command name
607 608 609 610 611 612 613

	// check minimum arg count

	// 0 args: argc == startpos
	// 1 args: argc == startpos + 1
	// ...

614
	minargs = stof(cmdrestriction);
TimePath's avatar
TimePath committed
615
	if (argc - startpos < minargs) return false;
616

TimePath's avatar
TimePath committed
617
	p = strstrofs(cmdrestriction, ";", 0);  // find first semicolon
618

TimePath's avatar
TimePath committed
619
	for ( ; ; )
620
	{
621 622 623 624 625
		// we know that at any time, startpos <= argc - minargs
		// so this means: argc-minargs >= startpos >= argc, thus
		// argc-minargs >= argc, thus minargs <= 0, thus all minargs
		// have been seen already

TimePath's avatar
TimePath committed
626
		if (startpos >= argc) // all args checked? GOOD
627
			break;
628

TimePath's avatar
TimePath committed
629
		if (p < 0)            // no more args? FAIL
630
		{
631
			// exception: exactly minargs left, this one included
TimePath's avatar
TimePath committed
632
			if (argc - startpos == minargs) break;
633 634

			// otherwise fail
TimePath's avatar
TimePath committed
635
			return false;
636
		}
637 638

		// cut to next semicolon
TimePath's avatar
TimePath committed
639 640 641
		q = strstrofs(cmdrestriction, ";", p + 1);  // find next semicolon
		if (q < 0) charlist = substring(cmdrestriction, p + 1, -1);
		else charlist = substring(cmdrestriction, p + 1, q - (p + 1));
642 643 644 645

		// in case we ever want to allow semicolons in VoteCommand_checknasty
		// charlist = strreplace("^^", ";", charlist);

TimePath's avatar
TimePath committed
646
		if (charlist != "")
647
		{
648 649 650
			// verify the arg only contains allowed chars
			arg = argv(startpos);
			checkmate = strlen(arg);
TimePath's avatar
TimePath committed
651 652 653
			for (check = 0; check < checkmate; ++check)
				if (strstrofs(charlist, substring(arg, check, 1), 0) < 0) return false;
			// not allowed character
654
			// all characters are allowed. FINE.
655
		}
656 657

		++startpos;
658
		--minargs;
659
		p = q;
660
	}
661

TimePath's avatar
TimePath committed
662
	return true;
663 664
}

Daniel Ivanescu's avatar
Daniel Ivanescu committed
665
int VoteCommand_parse(entity caller, string vote_command, string vote_list, float startpos, int argc)
666
{
667
	string first_command = argv(startpos);
668
	int missing_chars = argv_start_index(startpos);
669

670 671
	if (autocvar_sv_vote_limit > 0 && strlen(vote_command) > autocvar_sv_vote_limit)
		return 0;
672

673
	if (!VoteCommand_checkinlist(first_command, vote_list)) return 0;
674

675
	if (!VoteCommand_checkargs(startpos, argc)) return 0;
676

Mario's avatar
Mario committed
677 678 679 680 681 682 683 684
	switch (MUTATOR_CALLHOOK(VoteCommand_Parse, caller, first_command, vote_command, startpos, argc))
	{
		case MUT_VOTEPARSE_CONTINUE: { break; }
		case MUT_VOTEPARSE_SUCCESS: { return 1; }
		case MUT_VOTEPARSE_INVALID: { return -1; }
		case MUT_VOTEPARSE_UNACCEPTABLE: { return 0; }
	}

TimePath's avatar
TimePath committed
685
	switch (first_command) // now go through and parse the proper commands to adjust as needed.
686 687
	{
		case "kick":
TimePath's avatar
TimePath committed
688
		case "kickban":    // catch all kick/kickban commands
689
		{
Samual's avatar
Samual committed
690
			entity victim = GetIndexedEntity(argc, (startpos + 1));
TimePath's avatar
TimePath committed
691
			float accepted = VerifyClientEntity(victim, true, false);
692

TimePath's avatar
TimePath committed
693
			if (accepted > 0)
Samual's avatar
Samual committed
694
			{
695 696 697
				string reason = "No reason provided";
				if(argc > next_token)
					reason = substring(vote_command, argv_start_index(next_token) - missing_chars, -1);
698

699 700 701
				string command_arguments = reason;
				if (first_command == "kickban")
					command_arguments = strcat(ftos(autocvar_g_ban_default_bantime), " ", ftos(autocvar_g_ban_default_masksize), " ~");
702

TimePath's avatar
TimePath committed
703
				vote_parsed_command = strcat(first_command, " # ", ftos(etof(victim)), " ", command_arguments);
704
				vote_parsed_display = sprintf("^1%s #%d ^7%s^1 %s", first_command, etof(victim), victim.netname, reason);
Samual's avatar
Samual committed
705
			}
706
			else { print_to(caller, strcat("vcall: ", GetClientErrorString(accepted, argv(startpos + 1)), ".\n")); return 0; }
707

708 709
			break;
		}
710

711 712
		case "map":
		case "chmap":
TimePath's avatar
TimePath committed
713
		case "gotomap":  // re-direct all map selection commands to gotomap
714
		{
Samual's avatar
Samual committed
715
			vote_command = ValidateMap(argv(startpos + 1), caller);
716
			if (!vote_command)  return -1;
717 718
			vote_parsed_command = strcat("gotomap ", vote_command);
			vote_parsed_display = strzone(strcat("^1", vote_parsed_command));
719

720 721
			break;
		}
722

723 724 725
		// TODO: replicate the old behaviour of being able to vote for maps from different modes on multimode servers (possibly support it in gotomap too)
		// maybe fallback instead of aborting if map name is invalid?
		case "nextmap":
Mario's avatar
Mario committed
726 727 728 729 730 731 732 733
		{
			vote_command = ValidateMap(argv(startpos + 1), caller);
			if (!vote_command)  return -1;
			vote_parsed_command = strcat("nextmap ", vote_command);
			vote_parsed_display = strzone(strcat("^1", vote_parsed_command));

			break;
		}
734

735 736