tags[$parts[0]] = $parts[1] == '' ? null : $parts[1]; } } if ($processed[0] == ':') { $has_user = false; $has_host = false; $prefix = explode(' ', $processed, 2); $processed = $prefix[1]; $prefix = $prefix[0][0] === ':' ? substr($prefix[0], 1) : $prefix[0]; if (strpos($prefix, '!') !== false) { $has_user = true; } if (strpos($prefix, '@') !== false) { $has_host = true; } if ($has_user && $has_host) { $prefix_part = explode('!', $prefix, 2); $msg->nick = $prefix_part[0]; $prefix_part = explode('@', $prefix_part[1], 2); $msg->user = $prefix_part[0]; $msg->host = $prefix_part[1]; } else if ($has_user) { $prefix_part = explode('!', $prefix, 2); $msg->nick = $prefix_part[0]; $msg->user = $prefix_part[1]; } else if ($has_host) { $prefix_part = explode('@', $prefix, 2); $msg->nick = $prefix_part[0]; $msg->host = $prefix_part[1]; } else { $msg->servername = $prefix; } } $command = explode(' ', $processed, 2); $msg->command = $command[0]; $processed = $command[1] ?? ''; while (strlen($processed)) { if ($processed[0] == ':') { $msg->params[] = substr($processed, 1); break; } $e = explode(' ', $processed, 2); $msg->params[] = $e[0]; if (isset($e[1])) { $processed = $e[1]; } else { break; } } return $msg; } public function encode() { $str = ''; if (!empty($this->tags)) { $str .= '@'; $first = true; foreach ($this->tags as $name => $value) { if ($first) { $first = false; } else { $str .= ';'; } $str .= $name.'='; if (!empty($value)) { $str .= $value; } } $str .= ' '; } if (!empty($this->servername)) { $str .= ':'.$this->servername.' '; } else if (!empty($this->nick)) { $str .= ':'.$this->nick; if (!empty($this->user)) { $str .= '!'.$this->user; } if (!empty($this->host)) { $str .= '@'.$this->host; } $str .= ' '; } $str .= $this->command; if (!empty($this->params)) { $n = count($this->params) - 1; for ($i = 0; $i < $n; ++$i) { $str .= ' '.$this->params[$i]; } $str .= ' :'.$this->params[$n]; } return $str; } public function log() { ChatLog::create([ 'command' => $this->command, 'nick' => $this->nick, 'params' => $this->params, 'tags' => $this->tags, ]); } public function tokenize() { return TokenizedMessage::fromIRC($this); } public static function join($channels) { $msg = new IRCMessage(); $msg->command = 'JOIN'; $msg->params[] = implode(',', $channels); return $msg; } public static function part($channels) { $msg = new IRCMessage(); $msg->command = 'PART'; $msg->params[] = implode(',', $channels); return $msg; } public static function ping($token = 'localhorsttv') { $msg = new IRCMessage(); $msg->command = 'PING'; $msg->params[] = $token; return $msg; } public static function privmsg($target, $message) { $msg = new IRCMessage(); $msg->command = 'PRIVMSG'; $msg->params[] = $target; $msg->params[] = $message; return $msg; } public function getPrivMsgTarget() { if (!empty($this->params)) { return $this->params[0]; } return ''; } public function getText() { if (!empty($this->params)) { return $this->params[count($this->params) - 1]; } return ''; } public function isNotice() { return $this->command == 'NOTICE'; } public function isPing() { return $this->command == 'PING'; } public function isPong() { return $this->command == 'PONG'; } public function isPrivMsg() { return $this->command == 'PRIVMSG'; } public function isOwner() { return substr($this->getPrivMsgTarget(), 1) == $this->nick; } public function isMod() { return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1'); } public function makePong() { $msg = new IRCMessage(); $msg->command = 'PONG'; $msg->params = array_values($this->params); return $msg; } } ?>