console_cmds.c
changeset 841 8f0a68c6f43b
parent 785 e9ca2bcc9c8f
child 885 c9509db963ac
--- a/console_cmds.c	Sat Jan 01 19:18:48 2005 +0000
+++ b/console_cmds.c	Sun Jan 02 12:03:43 2005 +0000
@@ -144,6 +144,87 @@
 // ********************************* //
 #ifdef ENABLE_NETWORK
 
+DEF_CONSOLE_CMD(ConBan)
+{
+	NetworkClientInfo *ci;
+
+	if (argc == 2) {
+		uint32 index = atoi(argv[1]);
+		if (index == NETWORK_SERVER_INDEX && !_network_dedicated) {
+			IConsolePrint(_iconsole_color_default, "Silly boy, you can not ban yourself!");
+			return NULL;
+		}
+		if (index == 0) {
+			IConsoleError("Invalid Client-ID");
+			return NULL;
+		}
+
+		ci = NetworkFindClientInfoFromIndex(index);
+
+		if (ci != NULL) {
+			uint i;
+			/* Add user to ban-list */
+			for (i = 0; i < lengthof(_network_ban_list); i++) {
+				if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0') {
+					_network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ci->client_ip));
+					break;
+				}
+			}
+
+			SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(index), NETWORK_ERROR_KICKED);
+			return NULL;
+		} else {
+			IConsoleError("Client-ID not found");
+			return NULL;
+		}
+	}
+
+	IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: ban <client-id>. For client-ids, see 'clients'.");
+
+	return NULL;
+}
+
+DEF_CONSOLE_CMD(ConUnBan)
+{
+	if (argc == 2) {
+		uint i;
+		for (i = 0; i < lengthof(_network_ban_list); i++) {
+			if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0')
+				continue;
+
+			if (strncmp(_network_ban_list[i], argv[1], strlen(_network_ban_list[i])) == 0) {
+				_network_ban_list[i][0] = '\0';
+				IConsolePrint(_iconsole_color_default, "IP unbanned.");
+				return NULL;
+			}
+		}
+
+		IConsolePrint(_iconsole_color_default, "IP not in ban-list.");
+
+		return NULL;
+	}
+
+	IConsolePrint(_iconsole_color_default, "Unknown usage. Usage: unban <ip>.");
+
+	return NULL;
+}
+
+DEF_CONSOLE_CMD(ConBanList)
+{
+	uint i;
+
+	IConsolePrint(_iconsole_color_default, "Banlist: ");
+
+	for (i = 0; i < lengthof(_network_ban_list); i++) {
+		if (_network_ban_list[i] == NULL || _network_ban_list[i][0] == '\0')
+			continue;
+
+		IConsolePrintF(_iconsole_color_default, "  %d) %s", i + 1, _network_ban_list[i]);
+	}
+
+	return NULL;
+}
+
 DEF_CONSOLE_CMD(ConStatus)
 {
 	const char *status;
@@ -978,6 +1059,13 @@
 	IConsoleCmdHook("status", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
 	IConsoleCmdHook("resetengines", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork);
 
+	IConsoleCmdRegister("ban",   ConBan);
+	IConsoleCmdHook("ban", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
+	IConsoleCmdRegister("unban",   ConUnBan);
+	IConsoleCmdHook("unban", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
+	IConsoleCmdRegister("banlist",   ConBanList);
+	IConsoleCmdHook("banlist", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetClient);
+
 	IConsoleAliasRegister("clean_company",		"reset_company %A");
 
 	IConsoleVarRegister("net_frame_freq", &_network_frame_freq, ICONSOLE_VAR_UINT8);