summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-22 07:01:46 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 08:02:54 +0200
commitb14f4121170cb75aba785efc9e73a364c232301b (patch)
treeb46c1a7aa88fd305a6803cd0c14da8d5d02ae7a6 /src/network
parentdc0efd5f2efac29e40264881553f779e206d0cc4 (diff)
downloadopenttd-b14f4121170cb75aba785efc9e73a364c232301b.tar.xz
Codechange: [Network] Introduce function to validate the client name
Diffstat (limited to 'src/network')
-rw-r--r--src/network/network_client.cpp14
-rw-r--r--src/network/network_func.h1
-rw-r--r--src/network/network_gui.cpp3
-rw-r--r--src/network/network_server.cpp2
4 files changed, 17 insertions, 3 deletions
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index 5858859cb..a12215bbc 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -1251,6 +1251,20 @@ void NetworkClientsToSpectators(CompanyID cid)
}
/**
+ * Check whether the given client name is deemed valid for use in network games.
+ * An empty name (null or '') is not valid as that is essentially no name at all.
+ * A name starting with white space is not valid for tab completion purposes.
+ * @param client_name The client name to check for validity.
+ * @return True iff the name is valid.
+ */
+bool NetworkIsValidClientName(const char *client_name)
+{
+ if (StrEmpty(client_name)) return false;
+ if (*client_name == ' ') return false;
+ return true;
+}
+
+/**
* Send the server our name.
*/
void NetworkUpdateClientName()
diff --git a/src/network/network_func.h b/src/network/network_func.h
index d14dd290f..a7f2e7cb5 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -36,6 +36,7 @@ extern StringList _network_host_list;
extern StringList _network_ban_list;
byte NetworkSpectatorCount();
+bool NetworkIsValidClientName(const char *client_name);
void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company);
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index 1c80f7f3c..15cf24f49 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -808,8 +808,7 @@ public:
}
case WID_NG_CLIENT:
- /* Make sure the name does not start with a space, so TAB completion works */
- if (!StrEmpty(this->name_editbox.text.buf) && this->name_editbox.text.buf[0] != ' ') {
+ if (NetworkIsValidClientName(this->name_editbox.text.buf)) {
strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
} else {
strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 552171027..321c8aa09 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -946,7 +946,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
}
/* We need a valid name.. make it Player */
- if (StrEmpty(name)) strecpy(name, "Player", lastof(name));
+ if (!NetworkIsValidClientName(name)) strecpy(name, "Player", lastof(name));
if (!NetworkFindName(name, lastof(name))) { // Change name if duplicate
/* We could not create a name for this client */