diff options
Diffstat (limited to 'src/network/network_client.cpp')
-rw-r--r-- | src/network/network_client.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index a12215bbc..35c52d3bc 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -1265,6 +1265,42 @@ bool NetworkIsValidClientName(const char *client_name) } /** + * Trim the given client name in place, i.e. remove leading and trailing spaces. + * After the trim check whether the client name is valid. A client name is valid + * whenever the name is not empty and does not start with spaces. This check is + * done via \c NetworkIsValidClientName. + * When the client name is valid, this function returns true. + * When the client name is not valid a GUI error message is shown telling the + * user to set the client name and this function returns false. + * + * This function is not suitable for ensuring a valid client name at the server + * as the error message will then be shown to the host instead of the client. + * @param client_name The client name to validate. It will be trimmed of leading + * and trailing spaces. + * @return True iff the client name is valid. + */ +bool NetworkValidateClientName(char *client_name) +{ + StrTrimInPlace(client_name); + if (NetworkIsValidClientName(client_name)) return true; + + ShowErrorMessage(STR_NETWORK_ERROR_BAD_PLAYER_NAME, INVALID_STRING_ID, WL_ERROR); + return false; +} + +/** + * Convenience method for NetworkValidateClientName on _settings_client.network.client_name. + * It trims the client name and checks whether it is empty. When it is empty + * an error message is shown to the GUI user. + * See \c NetworkValidateClientName(char*) for details about the functionality. + * @return True iff the client name is valid. + */ +bool NetworkValidateClientName() +{ + return NetworkValidateClientName(_settings_client.network.client_name); +} + +/** * Send the server our name. */ void NetworkUpdateClientName() |