]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/IRCMessage.php
fix some twitch bot stuff
[alttp.git] / app / TwitchBot / IRCMessage.php
diff --git a/app/TwitchBot/IRCMessage.php b/app/TwitchBot/IRCMessage.php
new file mode 100644 (file)
index 0000000..c0acfbb
--- /dev/null
@@ -0,0 +1,170 @@
+<?php
+
+namespace App\TwitchBot;
+
+class IRCMessage {
+
+       public $command = null;
+       public $params = [];
+       public $nick = null;
+       public $user = null;
+       public $host = null;
+       public $servername = null;
+       public $tags = [];
+
+       public static function fromString($message) {
+               $msg = new static();
+
+               $raw = rtrim($message);
+               $processed = $raw;
+
+               if ($processed[0] == '@') {
+                       $tags = explode(' ', $processed, 2);
+                       $processed = $tags[1];
+                       $tags = explode(';', ltrim(trim($tags[0]), '@'));
+                       foreach ($tags as $tag) {
+                               $parts = explode('=', $tag, 2);
+                               $msg->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;
+       }
+
+}
+
+?>