<commit_before><?php

use Dan\Irc\Location\Channel;
use Dan\Irc\Location\User;

hook('textreplace')
    ->on('irc.packets.message.public')
    ->anon(new class
    {
        protected $messages = [];

        /**
         * @param \Dan\Events\EventArgs $eventArgs
         * @return null
         */
        public function run(\Dan\Events\EventArgs $eventArgs)
        {
            $message = $eventArgs->get('message');

            /** @var Channel $channel */
            $channel = $eventArgs->get('channel');

            if (!preg_match("/^s\/([^\/]+)\/([^\/]+)?(?:\/(g)?)?/i", $message, $matches)) {
                $this->addLine($channel, $eventArgs->get('user'), $message);

                return null;
            }

            if (count($matches) < 2) {
                return false;
            }

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

            krsort($messages);

            $global = isset($matches[3]) ? $matches[3] == 'g' : false;

            foreach ($messages as $time => $data) {
                $new = preg_replace("/{$matches[1]}/", ($matches[2] ?? ''), $data['message'], ($global ? -1 : 1));

                if ($new == $data['message']) {
                    continue;
                }

                $this->messages[$channel->getLocation()][$time]['message'] = $new;

                $carbon = new \Carbon\Carbon();
                $carbon->setTimestamp($time);
                $ago = $carbon->diffForHumans();

                $channel->message("[<cyan>{$ago}</cyan>] {$data['user']}: {$new}");

                return false;
            }
        }

        /**
         * @param \Dan\Irc\Location\Channel $channel
         * @param \Dan\Irc\Location\User $user
         * @param $message
         */
        public function addLine(Channel $channel, User $user, $message)
        {
            foreach ($this->messages as $chan => $lines) {
                if (count($lines) > 30) {
                    array_shift($this->messages[$chan]);
                }
            }

            $this->messages[$channel->getLocation()][time()] = [
                'message' => $message,
                'user'    => $user->getLocation(),
            ];
        }
    });<commit_msg>46 years ago -- 2k16<commit_after><?php

use Carbon\Carbon;
use Dan\Irc\Location\Channel;
use Dan\Irc\Location\User;

hook('textreplace')
    ->on('irc.packets.message.public')
    ->anon(new class
    {
        protected $messages = [];

        /**
         * @param \Dan\Events\EventArgs $eventArgs
         * @return null
         */
        public function run(\Dan\Events\EventArgs $eventArgs)
        {
            $message = $eventArgs->get('message');

            /** @var Channel $channel */
            $channel = $eventArgs->get('channel');

            if (!preg_match("/^s\/([^\/]+)\/([^\/]+)?(?:\/(g)?)?/i", $message, $matches)) {
                $this->addLine($channel, $eventArgs->get('user'), $message);

                return null;
            }

            if (count($matches) < 2) {
                return false;
            }

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

            krsort($messages);

            $global = isset($matches[3]) ? $matches[3] == 'g' : false;

            foreach ($messages as $time => $data) {
                $new = preg_replace("/{$matches[1]}/", ($matches[2] ?? ''), $data['message'], ($global ? -1 : 1));

                if ($new == $data['message']) {
                    continue;
                }

                $this->messages[$channel->getLocation()][$time]['message'] = $new;

                /** @var Carbon $carbon */
                $carbon = $data['carbon'];
                $ago    = $carbon->diffForHumans();

                $channel->message("[<cyan>{$ago}</cyan>] {$data['user']}: {$new}");

                return false;
            }
        }

        /**
         * @param \Dan\Irc\Location\Channel $channel
         * @param \Dan\Irc\Location\User $user
         * @param $message
         */
        public function addLine(Channel $channel, User $user, $message)
        {
            foreach ($this->messages as $chan => $lines) {
                if (count($lines) > 30) {
                    array_shift($this->messages[$chan]);
                }
            }

            $this->messages[$channel->getLocation()][time()] = [
                'message'   => $message,
                'user'      => $user->getLocation(),
                'carbon'    => new Carbon(),
            ];
        }
    });
{'fraction_non_alphanumeric': 0.18620992534036013, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 1.5082599118942732, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, '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': '7363414', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1249, 'n_words': 223}