void Chatserver::removeAllFriends()
{
	while (! friends.empty())
	{
		if (!rsPeers->removeFriend(friends.front()))
		{
			std::cout << "ERROR: Could not remove friend with id " << friends.front() << std::endl;
			break;
		}
		friends.pop_front();
	}
	saveChatServerStore();
}
void Chatserver::removeOldestFriend()
{
	std::cout << "ChatServer::removeOldestFriend()" << std::endl;

	while (numberOfFriends() > maxFriends)
	{
		if (!rsPeers->removeFriend(friends.front()))
		{
			std::cout << "FATAL: Could not remove friend with id " << friends.front() << std::endl;
		}
		friends.pop_front();
	};
	saveChatServerStore();
}
void Chatserver::checkForNewCertificates()
{
	std::vector<std::string> newCerts;
	if (getdir(certificatePath, newCerts) != 0)
	{
		std::cout << "ChatServer: reading directory failed" << std::endl;
		return;
	}
	if (newCerts.size() == 0)
	{
		std::cout << "Chatserver: no new certificates found." << std::endl;
		return;
	}

	for (std::vector<std::string>::const_iterator it = newCerts.begin(); it != newCerts.end(); ++it)
	{
		std::string fileName = certificatePath + (*it);
		std::cout << "adding certificate " << fileName << std::endl;
		// read file
		std::ifstream in(fileName);
		std::stringstream buffer;
		buffer << in.rdbuf();
		std::string cert(buffer.str());

		// we need to add "cert" now
		std::string errorString;
		RsPgpId gpgId;
		RsPeerId sslId;
		try
		{
			bool success = rsPeers->loadCertificateFromString(cert, sslId, gpgId, errorString);
			if (!success)
			{
				std::cout << "ERROR: load certificate " << fileName << ", error " << errorString << " discarding it" << std::endl;
				removeFile(fileName);
				continue;
			}
		}
		catch (uint32_t /*error_code*/) // thrown in RsCertificate::RsCertificate(..)
		{
			std::cout << "ERROR: caught exception while loading certificate " << fileName << ", discarding it" << std::endl;
			removeFile(fileName);
			continue;
		}

		if (rsPeers->isGPGAccepted(gpgId))
		{
			std::cout << "ERROR: peer with gpg id " << gpgId << " is already accepted. Discarding it." << std::endl;
			removeFile(fileName);
		}

		// TODO: enable discovery or not?
		if (!rsPeers->addFriend(sslId, gpgId, RS_NODE_PERM_NONE))
		{
			std::cout << "ERROR: could not add friend." << std::endl;
			continue;
		}
		std::cout << "SUCCESS: added friend with gpg_id " << gpgId << std::endl;
		// remove all existing gpgids from the friend fifo list before pushing a gpgid to the list
		// this is to prevent users from adding them too often to the chatserver and reducing the capacity
		// http://www.cplusplus.com/reference/list/list/remove/
		friends.remove(gpgId);
		// add the unique ID to the fifo list
		friends.push_back(gpgId);
		std::cout << "Chatserver: " << (removeFile(fileName) == false ? "couldn't " : "") << "remove " << fileName << std::endl;
		saveChatServerStore();
	}
}
void Chatserver::checkForNewCertificates()
{
	std::vector<std::string> newCerts;
	if (getdir(certificatePath, newCerts) != 0)
	{
		std::cout << "ChatServer: reading directory failed" << std::endl;
		return;
	}
	if (newCerts.size() == 0)
	{
		std::cout << "Chatserver: no new certificates found." << std::endl;
		return;
	}

	for (std::vector<std::string>::const_iterator it = newCerts.begin(); it != newCerts.end(); ++it)
	{
		std::string fileName = certificatePath + (*it);
		std::cout << "adding certificate " << fileName << std::endl;
		// read file
		std::ifstream in(fileName);
		std::stringstream buffer;
		buffer << in.rdbuf();
		std::string cert(buffer.str());

		// we need to add "cert" now
		std::string sslId, gpgId, errorString;
		try
		{
			bool success = rsPeers->loadCertificateFromString(cert, sslId, gpgId, errorString);
			if (!success)
			{
				std::cout << "ERROR: load certificate " << fileName << ", error " << errorString << " discarding it" << std::endl;
				removeFile(fileName);
				continue;
			}
		}
		catch (uint32_t error_code) // thrown in RsCertificate::RsCertificate(..)
		{
			std::cout << "ERROR: caught exception while loading certificate " << fileName << ", discarding it" << std::endl;
			removeFile(fileName);
			continue;
		}

		if (rsPeers->isGPGAccepted(gpgId))
		{
			std::cout << "ERROR: peer with gpg id " << gpgId << " is already accepted. Discarding it." << std::endl;
			removeFile(fileName);
		}

		// TODO: enable discovery or not?
		if (!rsPeers->addFriend(sslId, gpgId, RS_SERVICE_PERM_DISCOVERY))
		{
			std::cout << "ERROR: could not add friend." << std::endl;
			continue;
		}
		std::cout << "SUCCESS: added friend with gpg_id " << gpgId << std::endl;
		friends.push_back(gpgId);
		std::cout << "Chatserver: " << (removeFile(fileName) == false ? "couldn't " : "") << "remove " << fileName << std::endl;
		saveChatServerStore();
	}
}