Exemplo n.º 1
0
/**
 * Join Channel
 * This function lets a client join a channel.  First, the function
 * checks that the specified channel name is valid and that the client
 * isn't already a member.  If the specified channel doesn't exist,
 * a new channel is created.  Client is added to channel by function
 * Add_Client().
 */
GLOBAL bool
Channel_Join( CLIENT *Client, const char *Name )
{
	CHANNEL *chan;

	assert(Client != NULL);
	assert(Name != NULL);

	/* Check that the channel name is valid */
	if (! Channel_IsValidName(Name)) {
		IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
				   Client_ID(Client), Name);
		return false;
	}

	chan = Channel_Search(Name);
	if(chan) {
		/* Check if the client is already in the channel */
		if (Get_Cl2Chan(chan, Client))
			return false;
	} else {
		/* If the specified channel does not exist, the channel
		 * is now created */
		chan = Channel_Create(Name);
		if (!chan)
			return false;
	}

	/* Add user to Channel */
	if (! Add_Client(chan, Client))
		return false;

	return true;
} /* Channel_Join */
Exemplo n.º 2
0
/**
 * Generate predefined persistent channels and &SERVER
 */
GLOBAL void
Channel_InitPredefined( void )
{
	CHANNEL *new_chan;
	const struct Conf_Channel *conf_chan;
	const char *c;
	size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));

	conf_chan = array_start(&Conf_Channels);

	assert(channel_count == 0 || conf_chan != NULL);

	for (i = 0; i < channel_count; i++, conf_chan++) {
		if (!conf_chan->name[0])
			continue;
		if (!Channel_IsValidName(conf_chan->name)) {
			Log(LOG_ERR,
			    "Can't create pre-defined channel: invalid name: \"%s\"",
			    conf_chan->name);
			continue;
		}

		new_chan = Channel_Search(conf_chan->name);
		if (new_chan) {
			Log(LOG_INFO,
			    "Can't create pre-defined channel \"%s\": name already in use.",
			    conf_chan->name);
			Set_KeyFile(new_chan, conf_chan->keyfile);
			continue;
		}

		new_chan = Channel_Create(conf_chan->name);
		if (!new_chan) {
			Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
							conf_chan->name);
			continue;
		}
		Log(LOG_INFO, "Created pre-defined channel \"%s\".",
						conf_chan->name);

		Channel_ModeAdd(new_chan, 'P');

		if (conf_chan->topic[0])
			Channel_SetTopic(new_chan, NULL, conf_chan->topic);

		c = conf_chan->modes;
		while (*c)
			Channel_ModeAdd(new_chan, *c++);

		Channel_SetKey(new_chan, conf_chan->key);
		Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
		Set_KeyFile(new_chan, conf_chan->keyfile);
	}
	if (channel_count)
		array_free(&Conf_Channels);

	/* Make sure the local &SERVER channel exists */
	if (!Channel_Search("&SERVER")) {
		new_chan = Channel_Create("&SERVER");
		if (new_chan) {
			Channel_SetModes(new_chan, "mnPt");
			Channel_SetTopic(new_chan, Client_ThisServer(),
					 "Server Messages");
		} else
			Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
	} else
		LogDebug("Required channel \"&SERVER\" already exists, ok.");
} /* Channel_InitPredefined */
Exemplo n.º 3
0
/**
 * Handler for the IRC+ "CHANINFO" command.
 *
 * @param Client The client from which this command has been received.
 * @param Req Request structure with prefix and all parameters.
 * @return CONNECTED or DISCONNECTED.
 */
GLOBAL bool
IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
{
	char modes_add[COMMAND_LEN], l[16];
	CLIENT *from;
	CHANNEL *chan;
	int arg_topic;

	assert( Client != NULL );
	assert( Req != NULL );

	/* Bad number of parameters? */
	if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
		return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
					  Client_ID(Client), Req->command);

	/* Compatibility kludge */
	if (Req->argc == 5)
		arg_topic = 4;
	else if(Req->argc == 3)
		arg_topic = 2;
	else
		arg_topic = -1;

	_IRC_GET_SENDER_OR_RETURN_(from, Req, Client)

	/* Search or create channel */
	chan = Channel_Search( Req->argv[0] );
	if (!chan)
		chan = Channel_Create( Req->argv[0] );
	if (!chan)
		return CONNECTED;

	if (Req->argv[1][0] == '+') {
		if (!*Channel_Modes(chan)) {
			/* OK, this channel doesn't have modes yet,
			 * set the received ones: */
			Channel_SetModes(chan, &Req->argv[1][1]);

			if(Req->argc == 5) {
				if(Channel_HasMode(chan, 'k'))
					Channel_SetKey(chan, Req->argv[2]);
				if(Channel_HasMode(chan, 'l'))
					Channel_SetMaxUsers(chan, atol(Req->argv[3]));
			} else {
				/* Delete modes which we never want to inherit */
				Channel_ModeDel(chan, 'l');
				Channel_ModeDel(chan, 'k');
			}

			strcpy(modes_add, "");
			if (Channel_HasMode(chan, 'l'))  {
				snprintf(l, sizeof(l), " %lu",
					 Channel_MaxUsers(chan));
				strlcat(modes_add, l, sizeof(modes_add));
			}
			if (Channel_HasMode(chan, 'k'))  {
				strlcat(modes_add, " ", sizeof(modes_add));
				strlcat(modes_add, Channel_Key(chan),
					sizeof(modes_add));
			}

			/* Inform members of this channel */
			IRC_WriteStrChannelPrefix(Client, chan, from, false,
						  "MODE %s +%s%s", Req->argv[0],
						  Channel_Modes(chan), modes_add);
		}
	}
	else
		Log(LOG_WARNING, "CHANINFO: invalid MODE format ignored!");

	if (arg_topic > 0) {
		/* We got a topic */
		if (!*Channel_Topic(chan) && Req->argv[arg_topic][0]) {
			/* OK, there is no topic jet */
			Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
			IRC_WriteStrChannelPrefix(Client, chan, from, false,
			     "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
		}
	}

	/* Forward CHANINFO to other servers */
	if (Req->argc == 5)
		IRC_WriteStrServersPrefixFlag(Client, from, 'C',
					      "CHANINFO %s %s %s %s :%s",
					      Req->argv[0], Req->argv[1],
					      Req->argv[2], Req->argv[3],
					      Req->argv[4]);
	else if (Req->argc == 3)
		IRC_WriteStrServersPrefixFlag(Client, from, 'C',
					      "CHANINFO %s %s :%s",
					      Req->argv[0], Req->argv[1],
					      Req->argv[2]);
	else
		IRC_WriteStrServersPrefixFlag(Client, from, 'C',
					      "CHANINFO %s %s",
					      Req->argv[0], Req->argv[1]);

	return CONNECTED;
} /* IRC_CHANINFO */