Beispiel #1
0
Datei: client.c Projekt: Plop/IRC
int     irc_client_send_msg(t_client *client, char *msg_type, char **arg)
{
    char    *msg;
    char    *tmp;
    int     i;

    msg = strdup(msg_type);
    i = 0;
    if (arg && arg[0])
        while (arg[++i])
        {
            msg = str_concat_var(3, (tmp = msg), " ", arg[i]);
            free(tmp);
        }
    irc_client_send(client, msg);
    free(msg);
    return (1);
}
Beispiel #2
0
bool chatcmd_help(IRCHANDLE handle, const irc_command* cmd, unsigned int argc, const char** args, char* buffer, unsigned int buffer_size, long cmdArg)
{
	char* b = (char*)alloca(sizeof(char) * BUFF_SIZE_LARGE);
	unsigned int b_size = BUFF_SIZE_LARGE;
	unsigned int i;
	unsigned int j;
	unsigned int len;
	bool isDirect = cmd->receiver[0] != '#';
	char* receiver;
	if (!isDirect)
	{
		#ifdef DEBUG
		printf("[DEBU]\tHelp Command - Is channel message\n");
		#endif
		receiver = cmd->receiver;
	}
	else
	{
		#ifdef DEBUG
		printf("[DEBU]\tHelp Command - Is direct message\n");
		#endif
		i = (strchr(cmd->sender, '!') - cmd->sender) + 1;
		receiver = alloca(sizeof(char) * i);
		strncpy(receiver, cmd->sender, i);
		receiver[i - 1] = '\0';
	}

	#ifdef DEBUG
	printf("[DEBU]\tHelp Command - Start to build Non-Auth command list\n");
	#endif
	len = snprintf(b, b_size, "PRIVMSG %s :*Non-Auth*     ", receiver);
	b += len;
	b_size -= len;

	for (i = 0; i < containers_index; i++)
	{
		if (containers[i].requires_auth || (!isDirect && containers[i].direct_only))
			continue;
		len = snprintf(b, b_size, containers[i].args_size ? " - '%s' args: " : " - '%s'", containers[i].name);
		b += len;
		b_size -= len;
		for (j = 0; j < containers[i].args_size; j++)
		{
			if (containers[i].args_defaults[j] != NULL)
			{
				len = snprintf(b, b_size, j ? ", '%s' (%s)" : "'%s' (%s)", containers[i].args[j], containers[i].args_defaults[j]);
				b += len;
				b_size -= len;
			}
			else
			{
				len = snprintf(b, b_size, j ? ", '%s'" : "'%s'", containers[i].args[j]);
				b += len;
				b_size -= len;
			}
		}
	}
	if (is_auth_user(cmd->sender))
	{
		#ifdef DEBUG
		printf("[DEBU]\tHelp Command - Start to build Auth-Required command list\n");
		#endif
		len = snprintf(b, b_size, "\r\nPRIVMSG %s :*Auth-Required*", receiver);
		b += len;
		b_size -= len;

		for (i = 0; i < containers_index; i++)
		{
			if (!containers[i].requires_auth || (!isDirect && containers[i].direct_only))
				continue;
			len = snprintf(b, b_size, containers[i].args_size ? " - '%s' args: " : " - '%s'", containers[i].name);
			b += len;
			b_size -= len;
			for (j = 0; j < containers[i].args_size; j++)
			{
				if (containers[i].args_defaults[j] != NULL)
				{
					len = snprintf(b, b_size, j ? ", '%s' (%s)" : "'%s' (%s)", containers[i].args[j], containers[i].args_defaults[j]);
					b += len;
					b_size -= len;
				}
				else
				{
					len = snprintf(b, b_size, j ? ", '%s'" : "'%s'", containers[i].args[j]);
					b += len;
					b_size -= len;
				}
			}
		}
	}
	snprintf(b, b_size, "\r\n");
	b -= BUFF_SIZE_LARGE - b_size;
	irc_client_send(handle, b, strlen(b));
	#ifdef DEBUG
	printf("[DEBU]\tHelp Command - Execution finished\n");
	#endif
	return false;
}
Beispiel #3
0
bool irc_chat_handle_chatcommands(IRCHANDLE handle, const irc_command* cmd)
{
	unsigned int i, j = 0;
	const char* res, *res2;
	char** args;
	char* buffer, *buffer2;
	const char* content = cmd->content + 1;
	int flag;
	const char* spotFound = NULL;
	char* responsee;
	bool isDirectMessage;
	if (cmd->type == IRC_PRIVMSG)
	{
		if (str_swi(content, botname))
			return false;
		if (cmd->receiver[0] == '#')
		{
			responsee = cmd->receiver;
			isDirectMessage = false;
		}
		else
		{
			i = (strchr(cmd->sender, '!') - cmd->sender) + 1;
			responsee = alloca(sizeof(char) * i);
			strncpy(responsee, cmd->sender, i);
			responsee[i - 1] = '\0';
			isDirectMessage = true;
		}
		if (flag = irc_user_check_antiflood(responsee, cmd->sender))
		{
			res = random_response("antiflood");
			if (!isDirectMessage)
			{
				i = (strchr(cmd->sender, '!') - cmd->sender) + 1;
				responsee = alloca(sizeof(char) * i);
				strncpy(responsee, cmd->sender, i);
				responsee[i - 1] = '\0';
			}
			i = sizeof("PRIVMSG  :\r") + strlen(responsee) + strlen(res) + 1;
			buffer = alloca(sizeof(char) * i);
			irc_client_send(handle, buffer, snprintf(buffer, i, "PRIVMSG %s :%s\r\n", responsee, res));
			return true;
		}
		content += botname_length;
		for (i = 0; i < containers_index; i++)
		{
			if ((res = str_strwrdi(content, containers[i].name, NULL)))
			{
				if (res < spotFound || spotFound == NULL)
				{
					j = i;
					spotFound = res;
				}
			}
		}
		i = j;
		if (spotFound != NULL)
		{
			res = spotFound;
			flag = 0;
			if (containers[i].requires_auth && !is_auth_user(cmd->sender))
			{
				res = random_notallowed_message();
				flag = sizeof("PRIVMSG  :\r") + strlen(res) + strlen(responsee);
				buffer2 = alloca(sizeof(char) * flag);
				snprintf(buffer2, flag, "PRIVMSG %s :%s\r\n", responsee, res);
				irc_client_send(handle, buffer2, strlen(buffer2));
				return true;
			}
			for (j = 0; j < containers[i].args_size; j++)
			{
				res2 = str_strwrdi(res, containers[i].args[j], NULL);
				if (!res2 && containers[i].args_defaults[j] == NULL)
				{
					flag = 1;
					break;
				}
			}
			if (flag)
			{
				res = random_error_message();
				flag = sizeof("PRIVMSG  :\r") + strlen(responsee) + strlen(res);
				buffer2 = alloca(sizeof(char) * flag);
				snprintf(buffer2, flag, "PRIVMSG %s :%s\r\n", responsee, res);
				irc_client_send(handle, buffer2, flag);
				return true;
			}
			buffer = alloca(sizeof(char) * BUFF_SIZE_SMALL);
			args = alloca(sizeof(char*) * containers[i].args_size);
			for (j = 0; j < containers[i].args_size; j++)
			{
				res2 = str_strwrdi(res, containers[i].args[j], NULL);
				if (!res2)
				{
					args[j] = containers[i].args_defaults[j];
				}
				else
				{
					res = strchr(res2 + 1, ' ');
					if (res[1] == '"')
					{
						res++;
						res2 = strchr(res + 1, '"');
					}
					else
					{
						res2 = strchr(res + 1, ' ');
					}
					if (res2 == NULL)
						res2 = strlen(res) + res;
					flag = res2 - res;
					args[j] = alloca(sizeof(char) * flag);
					strncpy(args[j], res + 1, res2 - res);
					args[j][flag - 1] = '\0';
				}
			}
			if (containers[i].cmd(handle, cmd, containers[i].args_size, (const char**)args, buffer, BUFF_SIZE_SMALL, containers[i].cmdArg))
			{
				flag = BUFF_SIZE_SMALL + sizeof("PRIVMSG  :\r") + strlen(responsee);
				buffer2 = alloca(sizeof(char) * flag);
				snprintf(buffer2, flag, "PRIVMSG %s :%s\r\n", responsee, buffer);
				irc_client_send(handle, buffer2, strlen(buffer2));
			}
			return true;
		}
		res = random_unknowncommand_message();
		flag = sizeof("PRIVMSG  :\r") + strlen(res) + strlen(responsee);
		buffer2 = alloca(sizeof(char) * flag);
		snprintf(buffer2, flag, "PRIVMSG %s :%s\r\n", responsee, res);
		irc_client_send(handle, buffer2, strlen(buffer2));

	}
	return false;
}