<commit_before><?php

namespace Nopolabs\Yabot\Bot;

use Exception;
use Nopolabs\Yabot\Helpers\LogTrait;
use Throwable;

trait PluginTrait
{
    use LogTrait;

    private $pluginId;

    private $config = [];

    public function help(): string
    {
        return $this->config['help'] ?? 'no help available';
    }

    public function status(): string
    {
        return 'running';
    }

    public function init(string $pluginId, array $params)
    {
        $this->pluginId = $pluginId;
        $this->config = $this->canonicalConfig(array_merge($this->config, $params));
        $this->checkMatchers($this->config['matchers']);

        $this->getLog()->info("$pluginId config:", $this->config);
    }

    public function getPrefix(): string
    {
        return $this->config['prefix'];
    }

    public function dispatch(MessageInterface $message, string $text)
    {
        if ($message->isHandled()) {
            return;
        }

        if (!$message->matchesIsBot($this->getIsBot())) {
            $this->getLog()->debug('plugin isBot');
            return;
        }

        if (!$message->matchesChannel($this->getChannel())) {
            $this->getLog()->debug('plugin channel');
            return;
        }

        if (!$message->matchesUser($this->getUser())) {
            $this->getLog()->debug('plugin user');
            return;
        }

        foreach ($this->getMatchers() as $name => $params) {
            if (!$message->matchesIsBot($params['isBot'])) {
                $this->getLog()->debug('matcher isBot');
                continue;
            }

            if (!$message->matchesChannel($params['channel'])) {
                $this->getLog()->debug('matcher channel');
                continue;
            }

            if (!$message->matchesUser($params['user'])) {
                $this->getLog()->debug('matcher user');
                continue;
            }

            if (!($matches = $message->matchPatterns($params['patterns'], $text))) {
                continue;
            }

            $this->getLog()->info("matched: $name", $params);

            $this->dispatchMessage($message, [$params['method'], $matches]);

            if ($message->isHandled()) {
                return;
            }
        }
    }

    public function replaceInPatterns($search, $replace, array $matchers): array
    {
        $replaced = [];
        foreach ($matchers as $name => $params) {
            $params['patterns'] = array_map(function ($pattern) use ($search, $replace) {
                return str_replace($search, $replace, $pattern);
            }, $params['patterns']);
            $replaced[$name] = $params;
        }
        return $replaced;
    }

    protected function dispatchMessage(MessageInterface $message, array $matched)
    {
        list($method, $matches) = $matched;

        try {
            if (method_exists($this, $method)) {
                $this->$method($message, $matches);
            } else {
                $this->getLog()->warning("{$this->pluginId} no method named: $method");
            }
        } catch (Exception $e) {
            $this->getLog()->warning('Exception in ' . static::class . '::' . $method);
            $this->getLog()->warning($e->getMessage());
            $this->getLog()->warning($e->getTraceAsString());
        }
    }

    protected function setConfig(array $config)
    {
        $this->config = $config;
    }

    protected function getConfig(): array
    {
        return $this->config;
    }

    protected function getUser(): string
    {
        return $this->config['user'];
    }

    protected function getChannel(): string
    {
        return $this->config['channel'];
    }

    /**
     * @return bool|null
     */
    protected function getIsBot()
    {
        return $this->config['isBot'];
    }

    protected function getMatchers(): array
    {
        return $this->config['matchers'];
    }

    protected function canonicalConfig(array $config): array
    {
        $config['prefix'] = $config['prefix'] ?? '';
        $config['isBot'] = $config['isBot'] ?? null;
        $config['channel'] = $config['channel'] ?? '';
        $config['user'] = $config['user'] ?? '';
        $config['matchers'] = $this->canonicalMatchers($config['matchers'] ?? []);

        return $config;
    }
<commit_msg>logging<commit_after><?php

namespace Nopolabs\Yabot\Bot;

use Exception;
use Nopolabs\Yabot\Helpers\LogTrait;
use Throwable;

trait PluginTrait
{
    use LogTrait;

    private $pluginId;

    private $config = [];

    public function help(): string
    {
        return $this->config['help'] ?? 'no help available';
    }

    public function status(): string
    {
        return 'running';
    }

    public function init(string $pluginId, array $params)
    {
        $this->pluginId = $pluginId;
        $this->config = $this->canonicalConfig(array_merge($this->config, $params));
        $this->checkMatchers($this->config['matchers']);

        $this->getLog()->info("$pluginId config:", $this->config);
    }

    public function getPrefix(): string
    {
        return $this->config['prefix'];
    }

    public function dispatch(MessageInterface $message, string $text)
    {
        if ($message->isHandled()) {
            return;
        }

        if (!$message->matchesIsBot($this->getIsBot())) {
            $data = [
                'message' => $message->isBot(),
                'plugin' => $this->getIsBot(),
            ];
            $this->getLog()->debug('isBot match failed: '.json_encode($data));
            return;
        }

        if (!$message->matchesChannel($this->getChannel())) {
            $data = [
                'message' => $message->getChannel(),
                'plugin' => $this->getChannel(),
            ];
            $this->getLog()->debug('channel match failed: '.json_encode($data));
            return;
        }

        if (!$message->matchesUser($this->getUser())) {
            $data = [
                'message' => $message->getUser(),
                'plugin' => $this->getUser(),
            ];
            $this->getLog()->debug('user match failed: '.json_encode($data));
            return;
        }

        foreach ($this->getMatchers() as $name => $params) {
            if (!$message->matchesIsBot($params['isBot'])) {
                $this->getLog()->debug('!matchesIsBot');
                continue;
            }

            if (!$message->matchesChannel($params['channel'])) {
                $this->getLog()->debug('!matchesChannel');
                continue;
            }

            if (!$message->matchesUser($params['user'])) {
                $this->getLog()->debug('!matchesUser');
                continue;
            }

            if (!($matches = $message->matchPatterns($params['patterns'], $text))) {
                continue;
            }

            $this->getLog()->info("matched: $name", $params);

            $this->dispatchMessage($message, [$params['method'], $matches]);

            if ($message->isHandled()) {
                return;
            }
        }
    }

    public function replaceInPatterns($search, $replace, array $matchers): array
    {
        $replaced = [];
        foreach ($matchers as $name => $params) {
            $params['patterns'] = array_map(function ($pattern) use ($search, $replace) {
                return str_replace($search, $replace, $pattern);
            }, $params['patterns']);
            $replaced[$name] = $params;
        }
        return $replaced;
    }

    protected function dispatchMessage(MessageInterface $message, array $matched)
    {
        list($method, $matches) = $matched;

        try {
            if (method_exists($this, $method)) {
                $this->$method($message, $matches);
            } else {
                $this->getLog()->warning("{$this->pluginId} no method named: $method");
            }
        } catch (Exception $e) {
            $this->getLog()->warning('Exception in ' . static::class . '::' . $method);
            $this->getLog()->warning($e->getMessage());
            $this->getLog()->warning($e->getTraceAsString());
        }
    }

    protected function setConfig(array $config)
    {
        $this->config = $config;
    }

    protected function getConfig(): array
    {
        return $this->config;
    }

    protected function getUser()
    {
        return $this->config['user'];
    }

    protected function getChannel()
    {
        return $this->config['channel'];
    }

    /**
     * @return bool|null
     */
    protected function getIsBot()
    {
        return $this->config['isBot'];
    }

    protected function getMatchers(): array
    {
        return $this->config['matchers'];
    }

    protected function canonicalConfig(array $config): array
    {
        $config['prefix'] = $config['prefix'] ?? '';
        $config['isBot'] = $config['isBot'] ?? null;
        $config['channel'] = $config['channel'] ?? '';
        $config['user'] = $config['user'] ?? '';
        $config['matchers'] = $this->canonicalMatchers($config['matchers'] ?? []);

        return $config;
    }

{'fraction_non_alphanumeric': 0.17065704602601947, 'fraction_numerical': 0.0, 'mean_word_length': 1.9944353518821605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 135, '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': '7185677', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2462, 'n_words': 464}