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 = $prefix[0][0] === ':' ? substr($prefix[0], 1) : $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[] = substr($processed, 1);
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 function tokenize() {
140 return TokenizedMessage::fromIRC($this);
143 public static function join($channels) {
144 $msg = new IRCMessage();
145 $msg->command = 'JOIN';
146 $msg->params[] = implode(',', $channels);
150 public static function part($channels) {
151 $msg = new IRCMessage();
152 $msg->command = 'PART';
153 $msg->params[] = implode(',', $channels);
157 public static function ping($token = 'localhorsttv') {
158 $msg = new IRCMessage();
159 $msg->command = 'PING';
160 $msg->params[] = $token;
164 public static function privmsg($target, $message) {
165 $msg = new IRCMessage();
166 $msg->command = 'PRIVMSG';
167 $msg->params[] = $target;
168 $msg->params[] = $message;
172 public function getPrivMsgTarget() {
173 if (!empty($this->params)) {
174 return $this->params[0];
179 public function getText() {
180 if (!empty($this->params)) {
181 return $this->params[count($this->params) - 1];
186 public function isNotice() {
187 return $this->command == 'NOTICE';
190 public function isPing() {
191 return $this->command == 'PING';
194 public function isPong() {
195 return $this->command == 'PONG';
198 public function isPrivMsg() {
199 return $this->command == 'PRIVMSG';
202 public function isOwner() {
203 return substr($this->getPrivMsgTarget(), 1) == $this->nick;
206 public function isMod() {
207 return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1');
210 public function makePong() {
211 $msg = new IRCMessage();
212 $msg->command = 'PONG';
213 $msg->params = array_values($this->params);