<commit_before><?php namespace Plugins\TextReplace; 


use Dan\Contracts\PluginContract;
use Dan\Events\EventArgs;
use Dan\Irc\Location\Channel;
use Dan\Irc\Location\User;
use Dan\Plugins\Plugin;

class TextReplace extends Plugin implements PluginContract {

    /** @var array */
    protected $messages = [];


    public function register()
    {
        $this->listenForEvent('irc.packets.message.public', [$this, 'doReplace'], 7);
    }


    public function doReplace(EventArgs $eventArgs)
    {
        /** @var Channel $channel */
        $channel    = $eventArgs->get('channel');
        /** @var User $user */
        $user       = $eventArgs->get('user');
        $message    = $eventArgs->get('message');

        if(strpos($message, 's/') === 0)
        {
            $replace = explode('/', $message);

            if(count($replace) !== 3)
                return false;

            if(!array_key_exists($channel->getName(), $this->messages))
                return false;

            $arr = $this->messages[$channel->getName()];

            krsort($arr);

            foreach($arr as $time => $data)
            {
                if(strpos($data['message'], $replace[1]) !== false)
                {
                    $safe = preg_quote($replace[1]);

                    $this->messages[$channel->getName()][$time]['message'] = preg_replace("/{$safe}/", $replace[2], $data['message'], 1);

                    $newMessage = preg_replace("/{$safe}/", "{bold}{$replace[2]}{normal}", $data['message'], 1);
                    $channel->sendMessage("{reset}[ {cyan}{bold}{$data['user']->getNick()}{normal} ] {$newMessage}");

                    return false;
                }
            }

            $channel->sendMessage("Nothing found to replace!");
            return false;
        }

        foreach($this->messages as $chan => $lines)
        {
            if (count($lines) > 30)
            {
                array_shift($this->messages[$chan]);
            }
        }

        $this->messages[$channel->getName()][time()] = [
            'message'   => $message,
            'user'      => $user
        ];
    }
}<commit_msg>Update text replace plugin to allow global replace using /g<commit_after><?php namespace Plugins\TextReplace; 


use Dan\Contracts\PluginContract;
use Dan\Events\EventArgs;
use Dan\Irc\Location\Channel;
use Dan\Irc\Location\User;
use Dan\Plugins\Plugin;

class TextReplace extends Plugin implements PluginContract {

    /** @var array */
    protected $messages = [];


    public function register()
    {
        $this->listenForEvent('irc.packets.message.public', [$this, 'doReplace'], 7);
    }


    public function doReplace(EventArgs $eventArgs)
    {
        /** @var Channel $channel */
        $channel    = $eventArgs->get('channel');
        /** @var User $user */
        $user       = $eventArgs->get('user');
        $message    = $eventArgs->get('message');

        if(strpos($message, 's/') === 0)
        {
            $replace = explode('/', $message);

            if(count($replace) < 3)
                return false;

            if(!array_key_exists($channel->getName(), $this->messages))
                return false;

            $arr = $this->messages[$channel->getName()];

            krsort($arr);

            foreach($arr as $time => $data)
            {
                if(strpos($data['message'], $replace[1]) !== false)
                {
                    $safe = preg_quote($replace[1]);

                    $replaceClean   = null;
                    $newMessage     = null;

                    if(count($replace) == 4 && $replace[3] == 'g')
                    {
                        $replaceClean = preg_replace("/{$safe}/", $replace[2], $data['message']);
                        $newMessage = preg_replace("/{$safe}/", "{bold}{$replace[2]}{normal}", $data['message']);
                    }
                    else
                    {
                        $replaceClean = preg_replace("/{$safe}/", $replace[2], $data['message'], 1);
                        $newMessage = preg_replace("/{$safe}/", "{bold}{$replace[2]}{normal}", $data['message'], 1);
                    }

                    $this->messages[$channel->getName()][$time]['message'] = $replaceClean;
                    $channel->sendMessage("{reset}[ {cyan}{bold}{$data['user']->getNick()}{normal} ] {$newMessage}");

                    return false;
                }
            }

            $channel->sendMessage("Nothing found to replace!");
            return false;
        }

        foreach($this->messages as $chan => $lines)
            if (count($lines) > 30)
                array_shift($this->messages[$chan]);

        $this->messages[$channel->getName()][time()] = [
            'message'   => $message,
            'user'      => $user
        ];
    }
}
{'fraction_non_alphanumeric': 0.17209398186314923, 'fraction_numerical': 0.0053586150041220115, 'mean_word_length': 1.7683970336565886, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 47, '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': '4873898', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1272, 'n_words': 240}