<commit_before><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace IRCParser;
class IRCParser
{
	private $bot;

	// The command prefix.
	private $prefix;

	public function __construct($bot)
	{
		$this->bot = $bot;

		// Get the prefix.
		$this->prefix[] = $this->bot->getConfiguration('prefix');

		// Set up the bot name as a prefix.
		$nick = $this->bot->getConfiguration('nick');
		$this->prefix[] = $nick . ': ';
		$this->prefix[] = $nick . ', ';
		$this->prefix[] = $nick . ' ';


	}
	public function process($message)
	{
		// Commands not automagically detected.
		$commands = array('PING');

		// This array will contain everything the string includes.
		$data = array();

		// Try to filter the hostname.
		if (preg_match('/:([a-zA-Z0-9_.!~@\/-]+)/', $message, $hostname))
		{
			$data['hostname'] = $hostname[1];

			// Can we also parse a username out of it?
			if (preg_match('/:([a-zA-Z0-9_]+)!/', $message, $username))
				$data['from_user'] = $username[1];
		}

		// Parse the command. And optional parameters.
		if (preg_match('/ ?([A-Z0-9]+) ([^ ]+)?/', $message, $command))
		{
			$data['command'] = $command[1];

			if (!empty($command[2]))
				$data['argument'] = $command[2];
		}
		else
		{
			$data['command'] = '';
			foreach ($commands as $command)
			{
				if (substr($message, strlen($command)) == $command)
					$data['command'] = $command;
			}
		}

		// Strip both.
		$data['string'] = trim(preg_replace('/(:[a-zA-Z0-9.!~@\/]+) ([A-Z0-9]+) ([^ ]+)?/', '', $message));

		// Strip off the ':' we sometimes get.
		if (substr($data['string'], 0, 1) == ':')
			$data['string'] = substr($data['string'], 1, strlen($data['string']));

		// Try to filter the command out, if it's in there.
		foreach ($this->prefix as $prefix)
		{
			if (substr($data['string'], 0, strlen($prefix)) == $prefix)
			{
				// Get the command.
				if (preg_match('/' . preg_quote($prefix) . '([a-zA-Z0-9]+)/', strtolower($data['string']), $botcmd))
				{
					$this->bot->log('Command detected: ' . $botcmd[1], 'COMMAND');
					$data['bot_command'] = $botcmd[1];
					$data['string'] = trim(preg_replace('/' . preg_quote($prefix) . '([a-zA-Z0-9]+)/', '', $data['string']));
					break;
				}
			}
		}

		return $data;
	}
}<commit_msg>Clean up the IRCParser some.<commit_after><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace IRCParser;
class IRCParser
{
	private $bot;

	// The command prefix.
	private $prefix;

	public function __construct($bot)
	{
		$this->bot = $bot;

		// Get the prefix.
		$this->prefix[] = $this->bot->getConfig('prefix');

		// Set up the bot name as a prefix.
		$nick = $this->bot->getConfig('nick');
		$this->prefix[] = $nick . ': ';
		$this->prefix[] = $nick . ', ';
		$this->prefix[] = $nick . ' ';


	}
	public function process($message)
	{
		// This array will contain everything the string includes.
		$data = array();

		// No match? Bummer.
		if (!preg_match('/^(?::([^ ]+) )?([A-Z]+|\d{3}) ((?:(?! :)).*?)(?: :(.*))?$/', $message, $matches))
			return false;

		// We should have gathered everything we need.
		$data = array(
			'full' => $message,
			'hostname' => $matches[1],
			'command' => $matches[2],
			'arguments' => explode(' ', $matches[3]),
			'string' => $matches[4],
		);

		// Does the hostname contain a nickname?
		if (preg_match('/([a-zA-Z0-9_]+)!/', $data['hostname'], $username))
			$data['nickname'] = $username[1];

		// Time for command parsing.
		if ($data['command'] == 'PRIVMSG')
		{
			// Get any possible commands.
			$result = $this->parseCommand($data['string']);

			// Got one?
			if (!empty($result['bot_command']))
				$data = array_merge($data, $result);
		}

		return $data;
	}

	public function parseCommand($string)
	{
		$command = false;
		$string = false;

		// We have to do this for every prefix.
		foreach ($this->prefix as $prefix)
		{
			if (substr($string, 0, strlen($prefix)) != $prefix)
				continue;

			// Get the command.
			if (!preg_match('/' . preg_quote($prefix) . '([a-z0-9]+)/', strtolower($string), $botcmd))
				continue;

			// We detected a command! Good!
			$this->bot->log('Command detected: ' . $botcmd[1], 'COMMAND');
			$command = $botcmd[1];

			// Strip out the command to get what's left of it. That should be just the arguments.
			$string = trim(preg_replace('/' . preg_quote($prefix) . '([a-zA-Z0-9]+)/', '', $string));
			break;
		}

		// Return both.
		return array('bot_command' => $command, 'command_arguments' => $string);
	}
}
{'fraction_non_alphanumeric': 0.18236507390855963, 'fraction_numerical': 0.008594018563080097, 'mean_word_length': 3.3490284005979074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4982088', 'n_tokens_mistral': 2177, 'n_tokens_neox': 1915, 'n_words': 596}