File diff r26193:4bc7915a2156 → r26194:f7347205838e
src/network/network_server.cpp
Show inline comments
 
@@ -31,12 +31,13 @@
 
#include "../core/random_func.hpp"
 
#include "../company_cmd.h"
 
#include "../rev.h"
 
#include <mutex>
 
#include <condition_variable>
 

	
 
#include "../fileio_func.h"
 
#include "../safeguards.h"
 

	
 

	
 
/* This file handles all the server-commands */
 

	
 
DECLARE_POSTFIX_INCREMENT(ClientID)
 
@@ -1563,12 +1564,15 @@ static void NetworkAutoCleanCompanies()
 
			if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !_network_company_states[c->index].password.empty()) {
 
				/* Unprotect the company */
 
				_network_company_states[c->index].password.clear();
 
				IConsolePrint(CC_INFO, "Auto-removed protection from company #{}.", c->index + 1);
 
				_network_company_states[c->index].months_empty = 0;
 
				NetworkServerUpdateCompanyPassworded(c->index, false);
 
				if (_settings_client.network.save_password) {
 
					NetworkSavePassword();
 
				}
 
			}
 
			/* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
 
			if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) {
 
				/* Shut the company down */
 
				Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
 
				IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1);
 
@@ -1656,12 +1660,16 @@ void NetworkServerSetCompanyPassword(Com
 
		_network_company_states[company_id].password = password;
 
	} else {
 
		_network_company_states[company_id].password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _settings_game.game_creation.generation_seed);
 
	}
 

	
 
	NetworkServerUpdateCompanyPassworded(company_id, !_network_company_states[company_id].password.empty());
 

	
 
	if (_settings_client.network.save_password) {
 
		NetworkSavePassword();
 
	}
 
}
 

	
 
/**
 
 * Handle the command-queue of a socket.
 
 * @param cs The socket to handle the queue for.
 
 */
 
@@ -2073,12 +2081,15 @@ void NetworkServerNewCompany(const Compa
 

	
 
	if (!_network_server) return;
 

	
 
	_network_company_states[c->index].months_empty = 0;
 
	_network_company_states[c->index].password.clear();
 
	NetworkServerUpdateCompanyPassworded(c->index, false);
 
	if (_settings_client.network.save_password) {
 
		NetworkSavePassword();
 
	}
 

	
 
	if (ci != nullptr) {
 
		/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
 
		ci->client_playas = c->index;
 
		NetworkUpdateClientInfo(ci->client_id);
 
		Command<CMD_RENAME_PRESIDENT>::SendNet(STR_NULL, c->index, ci->client_name);
 
@@ -2091,6 +2102,44 @@ void NetworkServerNewCompany(const Compa
 
		/* ci is nullptr when replaying, or for AIs. In neither case there is a client.
 
		   We need to send Admin port update here so that they first know about the new company
 
		   and then learn about a possibly joining client (see FS#6025) */
 
		NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, c->index + 1);
 
	}
 
}
 

	
 
void NetworkSavePassword()
 
{
 
	std::string password_file_name = std::to_string(_settings_game.game_creation.generation_seed) + ".pwd";
 
	Debug(net, 0, "Saving company passwords to %s", password_file_name);
 
	FILE* file_pointer = FioFOpenFile(password_file_name, "wb", SAVE_DIR);
 
	if (file_pointer != nullptr) {
 
		for (CompanyID l_company = (CompanyID)0; l_company < MAX_COMPANIES; l_company++) {
 
			if (NetworkCompanyIsPassworded(l_company)) {
 
				const char* password = _network_company_states[l_company].password.c_str();
 
				fwrite(password, strlen(password), 1, file_pointer);
 
			}
 
			fwrite("\n", 1, 1, file_pointer);
 
		}
 
		fclose(file_pointer);
 
	}
 
}
 

	
 
void NetworkLoadPassword()
 
{
 
	std::string password_file_name = std::to_string(_settings_game.game_creation.generation_seed) + ".pwd";
 
	FILE* file_pointer = FioFOpenFile(password_file_name, "rb", SAVE_DIR);
 
	if (file_pointer != nullptr) {
 
		Debug(net, 0, "Loading company passwords from %s", password_file_name);
 
		for (CompanyID l_company = (CompanyID)0; l_company < MAX_COMPANIES; l_company++) {
 
			char password[NETWORK_PASSWORD_LENGTH] = { '\0' };
 
			fgets(password, NETWORK_PASSWORD_LENGTH, file_pointer);
 
			if (strlen(password) > 1) {
 
				_network_company_states[l_company].password = password;
 
				NetworkServerUpdateCompanyPassworded(l_company, !_network_company_states[l_company].password.empty());
 
				fseek(file_pointer, 1L, SEEK_CUR);
 
			}
 
		}
 
	}
 
	else {
 
		Debug(net, 0, "Password file %s not found", password_file_name);
 
	}
 
}