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 capReq($cap) {
144 $msg = new IRCMessage();
145 $msg->command = 'CAP REQ';
146 $msg->params[] = $cap;
150 public static function join($channels) {
151 $msg = new IRCMessage();
152 $msg->command = 'JOIN';
153 $msg->params[] = implode(',', $channels);
157 public static function part($channels) {
158 $msg = new IRCMessage();
159 $msg->command = 'PART';
160 $msg->params[] = implode(',', $channels);
164 public static function ping($token = 'localhorsttv') {
165 $msg = new IRCMessage();
166 $msg->command = 'PING';
167 $msg->params[] = $token;
171 public static function privmsg($target, $message) {
172 $msg = new IRCMessage();
173 $msg->command = 'PRIVMSG';
174 $msg->params[] = $target;
175 $msg->params[] = $message;
179 public function getPrivMsgTarget() {
180 if (!empty($this->params)) {
181 return $this->params[0];
186 public function getText() {
187 if (!empty($this->params)) {
188 return $this->params[count($this->params) - 1];
193 public function isNotice() {
194 return $this->command == 'NOTICE';
197 public function isPing() {
198 return $this->command == 'PING';
201 public function isPong() {
202 return $this->command == 'PONG';
205 public function isPrivMsg() {
206 return $this->command == 'PRIVMSG';
209 public function isWhisper() {
210 return $this->command == 'WHISPER';
213 public function isOwner() {
214 return substr($this->getPrivMsgTarget(), 1) == $this->nick;
217 public function isMod() {
218 return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1');
221 public function makePong() {
222 $msg = new IRCMessage();
223 $msg->command = 'PONG';
224 $msg->params = array_values($this->params);