<commit_before>/* msg.c - protocol message handlers
 * libsrsirc - a lightweight serious IRC lib - (C) 2012-14, <NAME>
 * See README for contact-, COPYING for license information. */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#define LOG_MODULE MOD_IMSG

/* C */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include <libsrsirc/defs.h>
#include "conn.h"
#include <libsrsirc/util.h>
#include <intlog.h>

#include "msg.h"

bool
msg_reghnd(irc hnd, const char *cmd, hnd_fn hndfn, const char *module)
{
	size_t i = 0;
	D("'%s' registering '%s'-handler", module, cmd);
	for (;i < hnd->msghnds_cnt; i++)
		if (!hnd->msghnds[i].cmd[0])
			break;

	if (i == hnd->msghnds_cnt) {
		size_t ncnt = hnd->msghnds_cnt * 2;
		struct msghnd *narr;
		if (!(narr = malloc(ncnt * sizeof *narr)))
			return false;

		memcpy(narr, hnd->msghnds, hnd->msghnds_cnt * sizeof *narr);
		for (size_t i = hnd->msghnds_cnt; i < ncnt; i++)
			narr[i].cmd[0] = '\0';

		free(hnd->msghnds);

		hnd->msghnds = narr;
		hnd->msghnds_cnt = ncnt;
	}

	hnd->msghnds[i].module = module;
	hnd->msghnds[i].hndfn = hndfn;
	com_strNcpy(hnd->msghnds[i].cmd, cmd, sizeof hnd->msghnds[i].cmd);
	return true;
}

bool
msg_reguhnd(irc hnd, const char *cmd, uhnd_fn hndfn, bool pre)
{
	size_t hcnt = pre ? hnd->uprehnds_cnt : hnd->uposthnds_cnt;
	struct umsghnd *harr = pre ? hnd->uprehnds : hnd->uposthnds;
	size_t i = 0;
	D("user registering %s-'%s'-handler", pre?"pre":"post", cmd);
	for (;i < hcnt; i++)
		if (!harr[i].cmd[0])
			break;

	if (i == hcnt) {
		size_t ncnt = hcnt * 2;
		struct umsghnd *narr;
		if (!(narr = malloc(ncnt * sizeof *narr)))
			return false;

		memcpy(narr, harr, hcnt * sizeof *narr);
		for (size_t i = hcnt; i < ncnt; i++)
			narr[i].cmd[0] = '\0';

		free(harr);

		harr = narr;
		hcnt = ncnt;
		if (pre) {
			hnd->uprehnds = harr;
			hnd->uprehnds_cnt = hcnt;
		} else {
			hnd->uposthnds = harr;
			hnd->uposthnds_cnt = hcnt;
		}
	}

	harr[i].hndfn = hndfn;
	com_strNcpy(harr[i].cmd, cmd, sizeof harr[i].cmd);
	return true;
}

void
msg_unregall(irc hnd, const char *module)
{
	size_t i = 0;
	for (;i < hnd->msghnds_cnt; i++)
		if (hnd->msghnds[i].cmd[0]
		    && strcmp(hnd->msghnds[i].module, module) == 0)
			hnd->msghnds[i].cmd[0] = '\0';
}

static bool
dispatch_uhnd(irc hnd, tokarr *msg, size_t ac, bool pre)
{
	size_t hcnt = pre ? hnd->uprehnds_cnt : hnd->uposthnds_cnt;
	struct umsghnd *harr = pre ? hnd->uprehnds : hnd->uposthnds;

	for (size_t i = 0; i < hcnt; i++) {
		if (!harr[i].cmd[0])
			continue;

		if (strcmp((*msg)[1], harr[i].cmd) != 0)
			continue;

		D("dispatch a pre-'%s'", (*msg)[1]);
		if (!harr[i].hndfn(hnd, msg, ac, pre))
			return false;
	}
<commit_msg>fix debug msg<commit_after>/* msg.c - protocol message handlers
 * libsrsirc - a lightweight serious IRC lib - (C) 2012-14, <NAME>
 * See README for contact-, COPYING for license information. */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#define LOG_MODULE MOD_IMSG

/* C */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include <libsrsirc/defs.h>
#include "conn.h"
#include <libsrsirc/util.h>
#include <intlog.h>

#include "msg.h"

bool
msg_reghnd(irc hnd, const char *cmd, hnd_fn hndfn, const char *module)
{
	size_t i = 0;
	D("'%s' registering '%s'-handler", module, cmd);
	for (;i < hnd->msghnds_cnt; i++)
		if (!hnd->msghnds[i].cmd[0])
			break;

	if (i == hnd->msghnds_cnt) {
		size_t ncnt = hnd->msghnds_cnt * 2;
		struct msghnd *narr;
		if (!(narr = malloc(ncnt * sizeof *narr)))
			return false;

		memcpy(narr, hnd->msghnds, hnd->msghnds_cnt * sizeof *narr);
		for (size_t i = hnd->msghnds_cnt; i < ncnt; i++)
			narr[i].cmd[0] = '\0';

		free(hnd->msghnds);

		hnd->msghnds = narr;
		hnd->msghnds_cnt = ncnt;
	}

	hnd->msghnds[i].module = module;
	hnd->msghnds[i].hndfn = hndfn;
	com_strNcpy(hnd->msghnds[i].cmd, cmd, sizeof hnd->msghnds[i].cmd);
	return true;
}

bool
msg_reguhnd(irc hnd, const char *cmd, uhnd_fn hndfn, bool pre)
{
	size_t hcnt = pre ? hnd->uprehnds_cnt : hnd->uposthnds_cnt;
	struct umsghnd *harr = pre ? hnd->uprehnds : hnd->uposthnds;
	size_t i = 0;
	D("user registering %s-'%s'-handler", pre?"pre":"post", cmd);
	for (;i < hcnt; i++)
		if (!harr[i].cmd[0])
			break;

	if (i == hcnt) {
		size_t ncnt = hcnt * 2;
		struct umsghnd *narr;
		if (!(narr = malloc(ncnt * sizeof *narr)))
			return false;

		memcpy(narr, harr, hcnt * sizeof *narr);
		for (size_t i = hcnt; i < ncnt; i++)
			narr[i].cmd[0] = '\0';

		free(harr);

		harr = narr;
		hcnt = ncnt;
		if (pre) {
			hnd->uprehnds = harr;
			hnd->uprehnds_cnt = hcnt;
		} else {
			hnd->uposthnds = harr;
			hnd->uposthnds_cnt = hcnt;
		}
	}

	harr[i].hndfn = hndfn;
	com_strNcpy(harr[i].cmd, cmd, sizeof harr[i].cmd);
	return true;
}

void
msg_unregall(irc hnd, const char *module)
{
	size_t i = 0;
	for (;i < hnd->msghnds_cnt; i++)
		if (hnd->msghnds[i].cmd[0]
		    && strcmp(hnd->msghnds[i].module, module) == 0)
			hnd->msghnds[i].cmd[0] = '\0';
}

static bool
dispatch_uhnd(irc hnd, tokarr *msg, size_t ac, bool pre)
{
	size_t hcnt = pre ? hnd->uprehnds_cnt : hnd->uposthnds_cnt;
	struct umsghnd *harr = pre ? hnd->uprehnds : hnd->uposthnds;

	for (size_t i = 0; i < hcnt; i++) {
		if (!harr[i].cmd[0])
			continue;

		if (strcmp((*msg)[1], harr[i].cmd) != 0)
			continue;

		D("dispatch a %s-'%s'", pre?"pre":"post", (*msg)[1]);
		if (!harr[i].hndfn(hnd, msg, ac, pre))
			return false;
	}

{'fraction_non_alphanumeric': 0.18824829618714312, 'fraction_numerical': 0.00957819119543194, 'mean_word_length': 3.6331058020477816, 'pattern_counts': {'":': 3, '<': 31, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6489601', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2463, 'n_words': 561}