X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FTwitchBot%2FIRCMessage.php;fp=app%2FTwitchBot%2FIRCMessage.php;h=c0acfbb547cc2d205fff7ef5420fdfbe07ce9ad1;hb=898d01d4ac5ccaa23621abda0761a893ff8c1074;hp=0000000000000000000000000000000000000000;hpb=c4835dcd53401c4e618ba077726d655d476a82c8;p=alttp.git diff --git a/app/TwitchBot/IRCMessage.php b/app/TwitchBot/IRCMessage.php new file mode 100644 index 0000000..c0acfbb --- /dev/null +++ b/app/TwitchBot/IRCMessage.php @@ -0,0 +1,170 @@ +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 = ltrim($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[] = ltrim($processed, ':'); + 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 static function join($channels) { + $msg = new IRCMessage(); + $msg->command = 'JOIN'; + $msg->params[] = implode(',', $channels); + 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 isPrivMsg() { + return $this->command == 'PRIVMSG'; + } + + public function makePong() { + $msg = new IRCMessage(); + $msg->command = 'PONG'; + $msg->params = array_values($this->params); + return $msg; + } + +} + +?>