Parse a message into parts

@param string $message Message to parse

@return array|bool
191592
private function parseMessage($message)
    {
        if (!$message) {
            return false;
        }

        $startLine = null;
        $headers = [];
        $body = '';

        // Iterate over each line in the message, accounting for line endings
        $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
        for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {

            $line = $lines[$i];

            // If two line breaks were encountered, then this is the end of body
            if (empty($line)) {
                if ($i < $totalLines - 1) {
                    $body = implode('', array_slice($lines, $i + 2));
                }
                break;
            }

            // Parse message headers
            if (!$startLine) {
                $startLine = explode(' ', $line, 3);
            } elseif (strpos($line, ':')) {
                $parts = explode(':', $line, 2);
                $key = trim($parts[0]);
                $value = isset($parts[1]) ? trim($parts[1]) : '';
                if (!isset($headers[$key])) {
                    $headers[$key] = $value;
                } elseif (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key], $value];
                } else {
                    $headers[$key][] = $value;
                }
            }
        }

        return [
            'start_line' => $startLine,
            'headers'    => $headers,
            'body'       => $body
        ];
    }
1262182
public static function formatMessage($message): string
    {
        if ($message instanceof \Throwable) {
            while ($message) {
                $tmp[] = ($message instanceof \ErrorException
                        ? Helpers::errorTypeToString($message->getSeverity()) . ': ' . $message->getMessage()
                        : Helpers::getClass($message) . ': ' . $message->getMessage() . ($message->getCode() ? ' #' . $message->getCode() : '')
                    ) . ' in ' . $message->getFile() . ':' . $message->getLine();
                $message = $message->getPrevious();
            }
            $message = \implode("\ncaused by ", $tmp);

        } elseif (!\is_string($message)) {
            $message = self::toText($message);
        }

        return \trim($message);
    }
59856
protected function getPlaceHolders(/*# string */ $message)/*# : array */
    {
        // not found
        if (false === strpos($message, '{')) {
            return [];
        }

        $matches = [];
        $pattern = '~\{([^\}]+)\}~';
        if (preg_match_all($pattern, $message, $matches)) {
            return array_combine($matches[1], $matches[0]);
        }
        return [];
    }
1359012
public function resolveCustomMessage($message)
    {
        preg_match_all('/\{[\w.| ]+\}/', $message, $segments);
        foreach (current($segments) as $segment) {
            $s    = str_replace(['{', '}'], '', $segment);
            $keys = explode('|', $s);

            if (empty($keys[1]) && isset($keys[2])) {
                $keys[1] = $this->callback($keys[2]);
            }

            $valueSegmented = $this->getValueSegmented($this, $keys[0], isset($keys[1]) ? $keys[1] : false);
            if (!$valueSegmented) {
                return false;
            }
            $message = str_replace($segment, $valueSegmented, $message);
        }

        return $message;
    }
845531
private function uriSafeDecode($message)
    {
        $message = str_replace(['-', '_'], ['+', '/'], $message);

        $decoded = base64_decode($message, true);
        if (!is_string($decoded)) {
            return null;
        }

        return $decoded;
    }
926498
public function reply ($message) {
		// Make sure we actually have a text to reply to
		if (!$this->inbound_message) {
			return false;
		}

		return $this->sendText($this->from, $this->to, $message);
	}
1311969
protected function getPostedAction($message = true)
    {
        $action = $this->getPosted('action', array(), true, 'array');

        if (!empty($action)) {

            if (empty($action['name'])) {
                $error = $this->text('An error occurred');
            } else if (empty($action['items'])) {
                $error = $this->text('Please select at least one item');
            } else {
                $parts = explode('|', $action['name'], 2);
                return array($action['items'], $parts[0], isset($parts[1]) ? $parts[1] : null);
            }

            if (isset($error) && $message) {
                $this->setMessage($error, 'warning');
            }
        }

        return array(array(), null, null);
    }
28598
public function prepareTranslatedMessage(array $message = [])
    {
        $translatedMessage = \App\Registry::$translate->_($message['message']);
        array_unshift($message['vars'], $translatedMessage);
        return call_user_func_array('sprintf', $message['vars']);
    }
5470
public static function parse($message, $options = [])
    {
        $parts = explode("\r\n\r\n", $message, 2);
        if (count($parts) < 2) {
            throw new NetException("The CRLFCRLF separator between headers and body is missing.");
        }
        $response = new static($options + ['format' => null]);

        list($header, $body) = $parts;

        $data = str_replace("\r", '', explode("\n", $header));
        $data = array_filter($data);

        preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)(?:\s+(.*))?/i', array_shift($data), $matches);

        $headers = $response->headers();
        $headers->push($data);

        if ($matches) {
            $response->version($matches[1]);
            $response->status([$matches[2], isset($matches[3]) ? $matches[3] : '']);
        }

        if ($headers['Transfer-Encoding']->value() === 'chunked') {
            $decoded = '';
            while (!empty($body)) {
                $pos = strpos($body, "\r\n");
                $len = hexdec(substr($body, 0, $pos));
                $decoded .= substr($body, $pos + 2, $len);
                $body = substr($body, $pos + 2 + $len);
            }
            $body = $decoded;
        }

        $response->body($body);
        return $response;
    }
847992
public function parseLine($msg)
    {
        if (!strncmp($msg, ':', 1)) {
            $pos    = strcspn($msg, ' ');
            $origin = (string) substr($msg, 1, $pos - 1);
            $msg    = new \Erebot\IrcTextWrapper((string) substr($msg, $pos + 1));
        } else {
            /// @FIXME the RFCs say we should assume origin = server instead.
            $origin = '';
            $msg    = new \Erebot\IrcTextWrapper($msg);
        }

        $type   = $msg[0];
        $type   = strtoupper($type);
        unset($msg[0]);

        $method = 'handle'.$type;
        $exists = method_exists($this, $method);
        // We need a backup for numeric events
        // as the method may alter the message.
        $backup = clone $msg;

        if ($exists) {
            $res = $this->$method($origin, $msg);
        }

        if (ctype_digit($type)) {
            // For numeric events, the first token is always the target.
            $target = $backup[0];
            unset($backup[0]);
            return $this->connection->dispatch(
                $this->makeEvent(
                    '!Numeric',
                    intval($type, 10),
                    $origin,
                    $target,
                    $backup
                )
            );
        }

        if ($exists) {
            return $res;
        }

        /// @TODO: logging
        return false;
    }
1023936
protected function formatMessage($message)
    {
        $traces = array();
        foreach ($this->getBacktrace() as $trace) {
            $traces[] = "in {$trace['file']}:{$trace['line']}";
        }

        $str = "[" . date('Y-m-d H:i:s', time()) . "]#" .  $this->getUserIp() . "# - " . $message;
        $str .= (empty($traces) ? '' : "\n    " . implode("\n    ", $traces));
        return $str;
    }
850596
protected function parseMessage(\CharlotteDunois\Yasmin\Models\Message $message) {
        // Find the command to run by patterns
        foreach($this->client->registry->commands as $command) {
            if($command->patterns === null) {
                continue;
            }
            
            foreach($command->patterns as $ptrn) {
                \preg_match($ptrn, $message->content, $matches);
                if(!empty($matches)) {
                    return (new \CharlotteDunois\Livia\Commands\Context($this->client, $message, $command, null, $matches));
                }
            }
        }
        
        $prefix = $this->client->getGuildPrefix($message->guild);
        if(empty($this->commandPatterns[$prefix])) {
            $this->buildCommandPattern($prefix);
        }
        
        $cmdMessage = $this->matchDefault($message, $this->commandPatterns[$prefix], 2);
        if(!$cmdMessage && $message->guild === null) {
            $cmdMessage = $this->matchDefault($message, '/^([^\s]+)/i');
        }
        
        return $cmdMessage;
    }
252609
public function parseException($e, $message = '')
    {
        $response = [
            'code' => $e->getCode(),
            'response' => $message . ' ' . $e->getMessage(),
            'error' => (bool)true
        ];
        return $response;
    }
467017
public function getExceptionByMessage($message)
    {
        $exceptions = array();
        foreach ($this->_exceptions as $e) {
            if ($message == $e->getMessage()) {
                $exceptions[] = $e;
            }
        }

        if (empty($exceptions)) {
            $exceptions = false;
        }

        return $exceptions;
    }
1268892
private function splitNewLines($messages)
    {
        $exploded = array_map(function ($line) {
            return explode("\n", $line);
        }, (array) $messages);
        $out = [];
        array_walk_recursive($exploded, function ($a) use (&$out) {
            $out[] = $a;
        });
        return $out;
    }
1022068
private function getMessage(string $message): string
    {
        /** @var FormatterFactoryInterface $factory */
        $factory   = $this->getContainer()->get(FormatterFactoryInterface::class);
        $formatter = $factory->createFormatter(Messages::NAMESPACE_NAME);
        $result    = $formatter->formatMessage($message);

        return $result;
    }
1079215
public function unpack(string $message): array
    {
        $messageAry = json_decode($message, true);
        $type       = $messageAry['pipeType'];
        $data       = $messageAry['message'];

        return [$type, $data];
    }