<commit_before>#include <json/json.h>
#include <cpr/cpr.h>
#include <ExpressionParser.h>
#include "stdafx.h"
#include "OptionParser.h"
#include "SkillMap.h"

CommandHandler::CommandHandler(const std::string &name, const std::string &channel, Moderator *mod, URLParser *urlp, EventManager *evtp)
	:m_name(name), m_channel(channel), m_modp(mod), m_parsep(urlp), m_customCmds(&m_defaultCmds, &m_cooldowns, m_wheel.cmd()),
	m_counting(false), m_evtp(evtp), m_gen(m_rd())
{
	// initializing pointers to all default commands
	m_defaultCmds["ehp"] = &CommandHandler::ehpFunc;
	m_defaultCmds["level"] = &CommandHandler::levelFunc;
	m_defaultCmds["lvl"] = &CommandHandler::levelFunc;
	m_defaultCmds["ge"] = &CommandHandler::geFunc;
	m_defaultCmds["calc"] = &CommandHandler::calcFunc;
	m_defaultCmds["cml"] = &CommandHandler::cmlFunc;
	m_defaultCmds["8ball"] = &CommandHandler::eightballFunc;
	m_defaultCmds["sp"] = &CommandHandler::strawpollFunc;
	m_defaultCmds["active"] = &CommandHandler::activeFunc;
	m_defaultCmds["count"] = &CommandHandler::countFunc;
	m_defaultCmds["uptime"] = &CommandHandler::uptimeFunc;
	m_defaultCmds["whitelist"] = &CommandHandler::whitelistFunc;
	m_defaultCmds["permit"] = &CommandHandler::permitFunc;
	m_defaultCmds["commands"] = &CommandHandler::commandsFunc;
	m_defaultCmds["about"] = &CommandHandler::aboutFunc;
	m_defaultCmds["addcom"] = &CommandHandler::makecomFunc;
	m_defaultCmds["editcom"] = &CommandHandler::makecomFunc;
	m_defaultCmds["delcom"] = &CommandHandler::delcomFunc;
	m_defaultCmds["addrec"] = &CommandHandler::addrecFunc;
	m_defaultCmds["delrec"] = &CommandHandler::delrecFunc;
	m_defaultCmds["listrec"] = &CommandHandler::listrecFunc;

	if (!m_customCmds.isActive()) {
		std::cerr << " Custom commands will be disabled for this session." << std::endl;
		std::cin.get();
	}

	// read all responses from file
	m_responding = utils::readJSON("responses.json", m_responses);
	if (m_responding) {
		// add response cooldowns to TimerManager
		for (auto &val : m_responses["responses"]) {
			m_cooldowns.add(val["name"].asString(), val["cooldown"].asInt());
		}
	}
	else {
		std::cerr << "Failed to read responses.json. Responses disabled for this session.";
		std::cin.get();
	}

	// set all command cooldowns
	for (auto &p : m_defaultCmds) {
		m_cooldowns.add(p.first);
	}
	m_cooldowns.add(m_wheel.name(), 10);

	// read extra 8ball responses
	std::ifstream reader(utils::getApplicationDirectory() + "/extra8ballresponses.txt");
	if (!reader.is_open()) {
		std::cerr << "Could not read extra8ballresponses.txt" << std::endl;
		return;
	}
	std::string line;
	while (std::getline(reader, line)) {
		m_eightballResponses.push_back(line);
	}
	reader.close();
}

CommandHandler::~CommandHandler() {}

std::string CommandHandler::processCommand(const std::string &nick, const std::string &fullCmd, bool privileges)
{
	std::string output = "";

	// the command is the first part of the string up to the first space
	std::string cmd = fullCmd.substr(0, fullCmd.find(' '));

	if (m_defaultCmds.find(cmd) != m_defaultCmds.end() && (privileges || m_cooldowns.ready(cmd))) {
		output += (this->*m_defaultCmds[cmd])(nick, fullCmd, privileges);
		m_cooldowns.setUsed(cmd);
	}
	else if (m_wheel.isActive() && cmd == m_wheel.cmd() && (privileges || m_cooldowns.ready(m_wheel.name()))) {
		output += wheelFunc(nick, fullCmd, privileges);
		m_cooldowns.setUsed(m_wheel.name());
	}
	else if (m_customCmds.isActive()) {
		/* check for custom command */
		Json::Value *customCmd = m_customCmds.getCom(cmd);
		if (!customCmd->empty() && (privileges || m_cooldowns.ready((*customCmd)["cmd"].asString()))) {
			output += (*customCmd)["response"].asString();
			m_cooldowns.setUsed((*customCmd)["cmd"].asString());
		}
		else {
			std::cerr << "Invalid command or is on cooldown: " << cmd << std::endl << std::endl;
		}
	}

	return output;
}

std::string CommandHandler::processResponse(const std::string &message)
{
	if (!m_responding) {
		return "";
	}

	/* test the message against all response regexes */
	for (auto &val : m_responses["responses"]) {
		std::string name = val["name"].asString();
		std::string regex = val["regex"].asString();

		std::regex responseRegex(regex, std::regex_constants::ECMAScript | std::regex_constants::icase);
		std::smatch match;
		if (std::regex_search(message.begin(), message.end(), match, responseRegex) && m_cooldowns.ready(name)) {
			m_cooldowns.setUsed(name);
			return val["response"].asString();
		}
<commit_msg>Changed response names for cooldown tracking to prevent collisions<commit_after>#include <json/json.h>
#include <cpr/cpr.h>
#include <ExpressionParser.h>
#include "stdafx.h"
#include "OptionParser.h"
#include "SkillMap.h"

CommandHandler::CommandHandler(const std::string &name, const std::string &channel, Moderator *mod, URLParser *urlp, EventManager *evtp)
	:m_name(name), m_channel(channel), m_modp(mod), m_parsep(urlp), m_customCmds(&m_defaultCmds, &m_cooldowns, m_wheel.cmd()),
	m_counting(false), m_evtp(evtp), m_gen(m_rd())
{
	// initializing pointers to all default commands
	m_defaultCmds["ehp"] = &CommandHandler::ehpFunc;
	m_defaultCmds["level"] = &CommandHandler::levelFunc;
	m_defaultCmds["lvl"] = &CommandHandler::levelFunc;
	m_defaultCmds["ge"] = &CommandHandler::geFunc;
	m_defaultCmds["calc"] = &CommandHandler::calcFunc;
	m_defaultCmds["cml"] = &CommandHandler::cmlFunc;
	m_defaultCmds["8ball"] = &CommandHandler::eightballFunc;
	m_defaultCmds["sp"] = &CommandHandler::strawpollFunc;
	m_defaultCmds["active"] = &CommandHandler::activeFunc;
	m_defaultCmds["count"] = &CommandHandler::countFunc;
	m_defaultCmds["uptime"] = &CommandHandler::uptimeFunc;
	m_defaultCmds["whitelist"] = &CommandHandler::whitelistFunc;
	m_defaultCmds["permit"] = &CommandHandler::permitFunc;
	m_defaultCmds["commands"] = &CommandHandler::commandsFunc;
	m_defaultCmds["about"] = &CommandHandler::aboutFunc;
	m_defaultCmds["addcom"] = &CommandHandler::makecomFunc;
	m_defaultCmds["editcom"] = &CommandHandler::makecomFunc;
	m_defaultCmds["delcom"] = &CommandHandler::delcomFunc;
	m_defaultCmds["addrec"] = &CommandHandler::addrecFunc;
	m_defaultCmds["delrec"] = &CommandHandler::delrecFunc;
	m_defaultCmds["listrec"] = &CommandHandler::listrecFunc;

	if (!m_customCmds.isActive()) {
		std::cerr << " Custom commands will be disabled for this session." << std::endl;
		std::cin.get();
	}

	// read all responses from file
	m_responding = utils::readJSON("responses.json", m_responses);
	if (m_responding) {
		// add response cooldowns to TimerManager
		for (auto &val : m_responses["responses"]) {
			m_cooldowns.add('_' + val["name"].asString(), val["cooldown"].asInt());
		}
	}
	else {
		std::cerr << "Failed to read responses.json. Responses disabled for this session.";
		std::cin.get();
	}

	// set all command cooldowns
	for (auto &p : m_defaultCmds) {
		m_cooldowns.add(p.first);
	}
	m_cooldowns.add(m_wheel.name(), 10);

	// read extra 8ball responses
	std::ifstream reader(utils::getApplicationDirectory() + "/extra8ballresponses.txt");
	if (!reader.is_open()) {
		std::cerr << "Could not read extra8ballresponses.txt" << std::endl;
		return;
	}
	std::string line;
	while (std::getline(reader, line)) {
		m_eightballResponses.push_back(line);
	}
	reader.close();
}

CommandHandler::~CommandHandler() {}

std::string CommandHandler::processCommand(const std::string &nick, const std::string &fullCmd, bool privileges)
{
	std::string output = "";

	// the command is the first part of the string up to the first space
	std::string cmd = fullCmd.substr(0, fullCmd.find(' '));

	if (m_defaultCmds.find(cmd) != m_defaultCmds.end() && (privileges || m_cooldowns.ready(cmd))) {
		output += (this->*m_defaultCmds[cmd])(nick, fullCmd, privileges);
		m_cooldowns.setUsed(cmd);
	}
	else if (m_wheel.isActive() && cmd == m_wheel.cmd() && (privileges || m_cooldowns.ready(m_wheel.name()))) {
		output += wheelFunc(nick, fullCmd, privileges);
		m_cooldowns.setUsed(m_wheel.name());
	}
	else if (m_customCmds.isActive()) {
		/* check for custom command */
		Json::Value *customCmd = m_customCmds.getCom(cmd);
		if (!customCmd->empty() && (privileges || m_cooldowns.ready((*customCmd)["cmd"].asString()))) {
			output += (*customCmd)["response"].asString();
			m_cooldowns.setUsed((*customCmd)["cmd"].asString());
		}
		else {
			std::cerr << "Invalid command or is on cooldown: " << cmd << std::endl << std::endl;
		}
	}

	return output;
}

std::string CommandHandler::processResponse(const std::string &message)
{
	if (!m_responding) {
		return "";
	}

	/* test the message against all response regexes */
	for (auto &val : m_responses["responses"]) {
		std::string name = '_' + val["name"].asString();
		std::string regex = val["regex"].asString();

		std::regex responseRegex(regex, std::regex_constants::ECMAScript | std::regex_constants::icase);
		std::smatch match;
		if (std::regex_search(message.begin(), message.end(), match, responseRegex) && m_cooldowns.ready(name)) {
			m_cooldowns.setUsed(name);
			return val["response"].asString();
		}

{'fraction_non_alphanumeric': 0.17023637776051492, 'fraction_numerical': 0.001553656641882144, 'mean_word_length': 6.356734693877551, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 13, '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': '623983', 'n_tokens_mistral': 3300, 'n_tokens_neox': 3246, 'n_words': 575}