Parses a message

@param string $msg
@return Message
@psalm-suppress UnusedMethod
516823
public static function parse(string $msg): Message
    {
        $obj = new self;
        $parts = explode("\r\n", $msg);
        $obj->body = MessageBody::parse(array_pop($parts));
        foreach ($parts as $line) {
            if ($line) {
                $pair = explode(': ', $line);
                $obj->headers[$pair[0]] = $pair[1];
            }
        }
        return $obj;
    }
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;
    }
1171308
public static function error($msg)
    {
        $msg = 'Error: ' . $msg;
        $space = strlen($msg) + 4;
        $out  = static::colorize(str_pad(' ', $space), Color::FG_WHITE, Color::AT_BOLD, Color::BG_RED) . PHP_EOL;
        $out .= static::colorize('  ' . $msg . '  ', Color::FG_WHITE, Color::AT_BOLD, Color::BG_RED) . PHP_EOL;
        $out .= static::colorize(str_pad(' ', $space), Color::FG_WHITE, Color::AT_BOLD, Color::BG_RED) . PHP_EOL;

        return $out;
    }
1109403
public function parseMessage($data)
    {
        $message = new Message();
        if ($this->parse($data, $message) !== $message) {
            throw new InvalidArgumentException('Unable to parse binary message');
        }

        return $message;
    }
26815
private function message($msg, $def = ""){
    if ($def && !isset($msg[$def])) {
      $msg[$def] = array();
    }
    if ($def && empty($msg[$def])) {
      $msg[$def] = (object)$msg[$def];
    }

    if (!isset($msg["context"])) {
      $msg["context"] = array();
    }
    $msg["context"] = array_merge($this->getDefaultContext(), $msg["context"]);

    if (!isset($msg["timestamp"])) {
      $msg["timestamp"] = null;
    }
    $msg["timestamp"] = $this->formatTime($msg["timestamp"]);
    $msg["messageId"] = self::messageId();

    return $msg;
  }
672261
public function send($msg)
    {
        // Check queue and send messages
        if ($this->queue && is_resource($this->queue) && msg_stat_queue($this->queue)) {
            return msg_send($this->queue, 1, array(
                'from' => $this->isMaster() ? $this->ppid : $this->pid,
                'to'   => $this->isMaster() ? $this->pid : $this->ppid,
                'body' => $msg
            ), true, false, $error);
        }
        return false;
    }
465278
private function msg($msg): string
    {
        // 将数组转为字符串
        if (is_array($msg) or is_object($msg) or is_bool($msg)) {
            return json($msg);
        }

        // 返回可显示的字符串
        return (string)$msg;
    }
359013
public static function fromMessage(Message $msg)
    {
        return new self($msg->shift(), $msg->shift(), $msg->toArray());
    }
1298526
public function handlesMessage(Message $msg)
    {
        $handledMsgCodes = [
            Message::MSG_CALL,
            Message::MSG_CANCEL,
            Message::MSG_REGISTER,
            Message::MSG_UNREGISTER,
            Message::MSG_YIELD,
            Message::MSG_INTERRUPT
        ];

        if (in_array($msg->getMsgCode(), $handledMsgCodes)) {
            return true;
        } elseif ($msg instanceof ErrorMessage && $msg->getErrorMsgCode() === Message::MSG_INVOCATION) {
            return true;
        } elseif ($msg instanceof ErrorMessage && $msg->getErrorMsgCode() === Message::MSG_INTERRUPT) {
            return true;
        } else {
            return false;
        }
    }
512807
protected function send($msg)
    {
        $msg .= PHP_EOL;

        return socket_write($this->socket, $msg, strlen($msg));
    }
1071777
public function integer($msg)
    {
        if(filter_var($this->_value, FILTER_VALIDATE_INT) === FALSE) {
            $this->_errors[] = $msg;
        }
        return $this;
    }
1157305
public static function getMessage(array $config = []): Message
    {
        $message = Message::class;
        $msgConf = $config['message'] ?? [];
        if (!empty($config['message']['instance'])) {
            $message = $config['message']['instance'];
            unset($config['message']['instance']);
        }
        $message = new $message($msgConf);

        return $message;
    }
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
        ];
    }
104982
def parse(cls, msg):
        """"""
        lines = msg.splitlines()
        method, uri, version = lines[0].split()
        headers = cls.parse_headers('\r\n'.join(lines[1:]))
        return cls(version=version, uri=uri, method=method, headers=headers)
1263388
public function regexp($pattern, $msg)
    {
        if(!preg_match($pattern, (string)$this->_value)) {
            $this->_errors[] = $msg;
        }
        return $this;
    }
20850
public function toJsonString(WxSendMsg $msg)
    {
        if ($msg instanceof WxSendTextMsg) {
            return $this->sendTextMsgToJsonString($msg);
        } elseif ($msg instanceof WxSendImageMsg) {
            return $this->sendImageMsgToJsonString($msg);
        } elseif ($msg instanceof WxSendVoiceMsg) {
            return $this->sendVoiceMsgToJsonString($msg);
        } elseif ($msg instanceof WxSendVideoMsg) {
            return $this->sendVideoMsgToJsonString($msg);
        } elseif ($msg instanceof WxSendMusicMsg) {
            return $this->sendMusicMsgToJsonString($msg);
        } elseif ($msg instanceof WxSendNewsMsg) {
            return $this->sendNewsMsgToJsonString($msg);
        }

        throw new \InvalidArgumentException("unknown wx send msg");
    }
1144639
public function message($msg)
    {
        foreach ($this as $client) {
            try {
                $this->execute(
                    "sendtextmessage",
                    array("msg" => $msg, "target" => $client, "targetmode" => Teamspeak::TEXTMSG_CLIENT)
                );
            } catch (TeamSpeak3_Adapter_ServerQuery_Exception $e) {
                /* ERROR_client_invalid_id */
                if ($e->getCode() != 0x0200) {
                    throw $e;
                }
            }
        }
    }