3 namespace App\TwitchBot;
5 use App\Models\ChatLog;
9 public $command = null;
14 public $servername = null;
17 public static function fromString($message) {
20 $raw = rtrim($message);
23 if ($processed[0] == '@') {
24 $tags = explode(' ', $processed, 2);
25 $processed = $tags[1];
26 $tags = explode(';', ltrim(trim($tags[0]), '@'));
27 foreach ($tags as $tag) {
28 $parts = explode('=', $tag, 2);
29 $msg->tags[$parts[0]] = $parts[1] == '' ? null : $parts[1];
33 if ($processed[0] == ':') {
36 $prefix = explode(' ', $processed, 2);
37 $processed = $prefix[1];
38 $prefix = ltrim($prefix[0], ':');
39 if (strpos($prefix, '!') !== false) {
42 if (strpos($prefix, '@') !== false) {
45 if ($has_user && $has_host) {
46 $prefix_part = explode('!', $prefix, 2);
47 $msg->nick = $prefix_part[0];
48 $prefix_part = explode('@', $prefix_part[1], 2);
49 $msg->user = $prefix_part[0];
50 $msg->host = $prefix_part[1];
51 } else if ($has_user) {
52 $prefix_part = explode('!', $prefix, 2);
53 $msg->nick = $prefix_part[0];
54 $msg->user = $prefix_part[1];
55 } else if ($has_host) {
56 $prefix_part = explode('@', $prefix, 2);
57 $msg->nick = $prefix_part[0];
58 $msg->host = $prefix_part[1];
60 $msg->servername = $prefix;
64 $command = explode(' ', $processed, 2);
65 $msg->command = $command[0];
66 $processed = $command[1] ?? '';
68 while (strlen($processed)) {
69 if ($processed[0] == ':') {
70 $msg->params[] = ltrim($processed, ':');
73 $e = explode(' ', $processed, 2);
74 $msg->params[] = $e[0];
85 public function encode() {
87 if (!empty($this->tags)) {
90 foreach ($this->tags as $name => $value) {
104 if (!empty($this->servername)) {
105 $str .= ':'.$this->servername.' ';
106 } else if (!empty($this->nick)) {
107 $str .= ':'.$this->nick;
108 if (!empty($this->user)) {
109 $str .= '!'.$this->user;
111 if (!empty($this->host)) {
112 $str .= '@'.$this->host;
117 $str .= $this->command;
119 if (!empty($this->params)) {
120 $n = count($this->params) - 1;
121 for ($i = 0; $i < $n; ++$i) {
122 $str .= ' '.$this->params[$i];
124 $str .= ' :'.$this->params[$n];
130 public function log() {
132 'command' => $this->command,
133 'nick' => $this->nick,
134 'params' => $this->params,
135 'tags' => $this->tags,
139 public static function join($channels) {
140 $msg = new IRCMessage();
141 $msg->command = 'JOIN';
142 $msg->params[] = implode(',', $channels);
146 public static function part($channels) {
147 $msg = new IRCMessage();
148 $msg->command = 'PART';
149 $msg->params[] = implode(',', $channels);
153 public static function ping($token = 'localhorsttv') {
154 $msg = new IRCMessage();
155 $msg->command = 'PING';
156 $msg->params[] = $token;
160 public static function privmsg($target, $message) {
161 $msg = new IRCMessage();
162 $msg->command = 'PRIVMSG';
163 $msg->params[] = $target;
164 $msg->params[] = $message;
168 public function getPrivMsgTarget() {
169 if (!empty($this->params)) {
170 return $this->params[0];
175 public function getText() {
176 if (!empty($this->params)) {
177 return $this->params[count($this->params) - 1];
182 public function isNotice() {
183 return $this->command == 'NOTICE';
186 public function isPing() {
187 return $this->command == 'PING';
190 public function isPong() {
191 return $this->command == 'PONG';
194 public function isPrivMsg() {
195 return $this->command == 'PRIVMSG';
198 public function isOwner() {
199 return substr($this->getPrivMsgTarget(), 1) == $this->nick;
202 public function isMod() {
203 return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1');
206 public function makePong() {
207 $msg = new IRCMessage();
208 $msg->command = 'PONG';
209 $msg->params = array_values($this->params);