Exemple #1
0
ChatChannel *ChatChannelList::AddClientToChannel(std::string ChannelName, Client *c) {

	if(!c) return nullptr;

	if((ChannelName.length() > 0) && (isdigit(ChannelName[0]))) {

		c->GeneralChannelMessage("The channel name can not begin with a number.");

		return nullptr;
	}

	std::string NormalisedName, Password;

	std::string::size_type Colon = ChannelName.find_first_of(":");

	if(Colon == std::string::npos)
		NormalisedName = CapitaliseName(ChannelName);
	else {
		NormalisedName = CapitaliseName(ChannelName.substr(0, Colon));

		Password = ChannelName.substr(Colon + 1);
	}

	if((NormalisedName.length() > 64) || (Password.length() > 64)) {

		c->GeneralChannelMessage("The channel name or password cannot exceed 64 characters.");

		return nullptr;
	}

	_log(UCS__TRACE, "AddClient to channel [%s] with password [%s]", NormalisedName.c_str(), Password.c_str());

	ChatChannel *RequiredChannel = FindChannel(NormalisedName);

	if(!RequiredChannel)
		RequiredChannel = CreateChannel(NormalisedName, c->GetName(), Password, false, 0);

	if(RequiredChannel->GetMinStatus() > c->GetAccountStatus()) {

		std::string Message = "You do not have the required account status to join channel " + NormalisedName;

		c->GeneralChannelMessage(Message);

		return nullptr;
	}

	if(RequiredChannel->IsClientInChannel(c))
		return nullptr;

	if(RequiredChannel->IsInvitee(c->GetName())) {

		RequiredChannel->AddClient(c);

		RequiredChannel->RemoveInvitee(c->GetName());

		return RequiredChannel;
	}

	if(RequiredChannel->CheckPassword(Password) || RequiredChannel->IsOwner(c->GetName()) || RequiredChannel->IsModerator(c->GetName()) ||
			c->IsChannelAdmin()) {

		RequiredChannel->AddClient(c);

		return RequiredChannel;
	}

	c->GeneralChannelMessage("Incorrect password for channel " + (NormalisedName));

	return nullptr;
}