summaryrefslogtreecommitdiff
path: root/src/console_cmds.cpp
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2010-05-13 16:00:50 +0000
committersmatz <smatz@openttd.org>2010-05-13 16:00:50 +0000
commit80fd67a314fa233f873b6d953856160d3ed54acf (patch)
treebb83b8398c87d97090593a5fe62ea946d1e07ea1 /src/console_cmds.cpp
parent241dd9a9f59208f5c9d025f0481dc00dac35989d (diff)
downloadopenttd-80fd67a314fa233f873b6d953856160d3ed54acf.tar.xz
(svn r19818) -Fix [FS#3784](r16004): kicking clients by IP didn't work
Diffstat (limited to 'src/console_cmds.cpp')
-rw-r--r--src/console_cmds.cpp119
1 files changed, 47 insertions, 72 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp
index df57479e0..7cb96d3d1 100644
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -408,54 +408,69 @@ DEF_CONSOLE_CMD(ConClearBuffer)
**********************************/
#ifdef ENABLE_NETWORK
-DEF_CONSOLE_CMD(ConBan)
+static bool ConKickOrBan(const char *argv, bool ban)
{
- NetworkClientInfo *ci;
- const char *banip = NULL;
- ClientID client_id;
+ const char *ip = argv;
- if (argc == 0) {
- IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'");
- IConsoleHelp("For client-id's, see the command 'clients'");
- IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
- return true;
- }
+ if (strchr(argv, '.') == NULL && strchr(argv, ':') == NULL) { // banning with ID
+ ClientID client_id = (ClientID)atoi(argv);
- if (argc != 2) return false;
+ if (client_id == CLIENT_ID_SERVER) {
+ IConsolePrintF(CC_ERROR, "ERROR: Silly boy, you can not %s yourself!", ban ? "ban" : "kick");
+ return true;
+ }
- if (strchr(argv[1], '.') == NULL && strchr(argv[1], ':') == NULL) { // banning with ID
- client_id = (ClientID)atoi(argv[1]);
- ci = NetworkFindClientInfoFromClientID(client_id);
- } else { // banning IP
- ci = NetworkFindClientInfoFromIP(argv[1]);
+ NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(client_id);
if (ci == NULL) {
- banip = argv[1];
- client_id = (ClientID)-1;
- } else {
- client_id = ci->client_id;
+ IConsoleError("Invalid client");
+ return true;
}
+
+ if (!ban) {
+ /* Kick only this client, not all clients with that IP */
+ NetworkServerKickClient(client_id);
+ return true;
+ }
+
+ /* When banning, kick+ban all clients with that IP */
+ ip = GetClientIP(ci);
}
- if (client_id == CLIENT_ID_SERVER) {
- IConsoleError("Silly boy, you can not ban yourself!");
- return true;
+ uint n = NetworkServerKickOrBanIP(ip, ban);
+ if (n == 0) {
+ IConsolePrint(CC_DEFAULT, ban ? "Client not online, address added to banlist" : "Client not found");
+ } else {
+ IConsolePrintF(CC_DEFAULT, "%sed %u client(s)", ban ? "Bann" : "Kick", n);
}
- if (client_id == INVALID_CLIENT_ID || (ci == NULL && client_id != (ClientID)-1)) {
- IConsoleError("Invalid client");
+ return true;
+}
+
+DEF_CONSOLE_CMD(ConKick)
+{
+ if (argc == 0) {
+ IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'");
+ IConsoleHelp("For client-id's, see the command 'clients'");
return true;
}
- if (ci != NULL) {
- IConsolePrint(CC_DEFAULT, "Client banned");
- banip = GetClientIP(ci);
- } else {
- IConsolePrint(CC_DEFAULT, "Client not online, banned IP");
+ if (argc != 2) return false;
+
+ return ConKickOrBan(argv[1], false);
+}
+
+DEF_CONSOLE_CMD(ConBan)
+{
+ if (argc == 0) {
+ IConsoleHelp("Ban a client from a network game. Usage: 'ban <ip | client-id>'");
+ IConsoleHelp("For client-id's, see the command 'clients'");
+ IConsoleHelp("If the client is no longer online, you can still ban his/her IP");
+ return true;
}
- NetworkServerBanIP(banip);
+ if (argc != 2) return false;
- return true;
+ return ConKickOrBan(argv[1], true);
}
DEF_CONSOLE_CMD(ConUnBan)
@@ -612,46 +627,6 @@ DEF_CONSOLE_CMD(ConClientNickChange)
return true;
}
-DEF_CONSOLE_CMD(ConKick)
-{
- NetworkClientInfo *ci;
- ClientID client_id;
-
- if (argc == 0) {
- IConsoleHelp("Kick a client from a network game. Usage: 'kick <ip | client-id>'");
- IConsoleHelp("For client-id's, see the command 'clients'");
- return true;
- }
-
- if (argc != 2) return false;
-
- if (strchr(argv[1], '.') == NULL) {
- client_id = (ClientID)atoi(argv[1]);
- ci = NetworkFindClientInfoFromClientID(client_id);
- } else {
- ci = NetworkFindClientInfoFromIP(argv[1]);
- client_id = (ci == NULL) ? INVALID_CLIENT_ID : ci->client_id;
- }
-
- if (client_id == CLIENT_ID_SERVER) {
- IConsoleError("Silly boy, you can not kick yourself!");
- return true;
- }
-
- if (client_id == INVALID_CLIENT_ID) {
- IConsoleError("Invalid client");
- return true;
- }
-
- if (ci != NULL) {
- NetworkServerKickClient(client_id);
- } else {
- IConsoleError("Client not found");
- }
-
- return true;
-}
-
DEF_CONSOLE_CMD(ConJoinCompany)
{
if (argc < 2) {