]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/IRCMessage.php
remove some useless svg groups
[alttp.git] / app / TwitchBot / IRCMessage.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\ChatLog;
6
7 class IRCMessage {
8
9         public $command = null;
10         public $params = [];
11         public $nick = null;
12         public $user = null;
13         public $host = null;
14         public $servername = null;
15         public $tags = [];
16
17         public static function fromString($message) {
18                 $msg = new static();
19
20                 $raw = rtrim($message);
21                 $processed = $raw;
22
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];
30                         }
31                 }
32
33                 if ($processed[0] == ':') {
34                         $has_user = false;
35                         $has_host = false;
36                         $prefix = explode(' ', $processed, 2);
37                         $processed = $prefix[1];
38                         $prefix = ltrim($prefix[0], ':');
39                         if (strpos($prefix, '!') !== false) {
40                                 $has_user = true;
41                         }
42                         if (strpos($prefix, '@') !== false) {
43                                 $has_host = true;
44                         }
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];
59                         } else {
60                                 $msg->servername = $prefix;
61                         }
62                 }
63
64                 $command = explode(' ', $processed, 2);
65                 $msg->command = $command[0];
66                 $processed = $command[1] ?? '';
67
68                 while (strlen($processed)) {
69                         if ($processed[0] == ':') {
70                                 $msg->params[] = ltrim($processed, ':');
71                                 break;
72                         }
73                         $e = explode(' ', $processed, 2);
74                         $msg->params[] = $e[0];
75                         if (isset($e[1])) {
76                                 $processed = $e[1];
77                         } else {
78                                 break;
79                         }
80                 }
81
82                 return $msg;
83         }
84
85         public function encode() {
86                 $str = '';
87                 if (!empty($this->tags)) {
88                         $str .= '@';
89                         $first = true;
90                         foreach ($this->tags as $name => $value) {
91                                 if ($first) {
92                                         $first = false;
93                                 } else {
94                                         $str .= ';';
95                                 }
96                                 $str .= $name.'=';
97                                 if (!empty($value)) {
98                                         $str .= $value;
99                                 }
100                         }
101                         $str .= ' ';
102                 }
103
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;
110                         }
111                         if (!empty($this->host)) {
112                                 $str .= '@'.$this->host;
113                         }
114                         $str .= ' ';
115                 }
116
117                 $str .= $this->command;
118
119                 if (!empty($this->params)) {
120                         $n = count($this->params) - 1;
121                         for ($i = 0; $i < $n; ++$i) {
122                                 $str .= ' '.$this->params[$i];
123                         }
124                         $str .= ' :'.$this->params[$n];
125                 }
126
127                 return $str;
128         }
129
130         public function log() {
131                 ChatLog::create([
132                         'command' => $this->command,
133                         'nick' => $this->nick,
134                         'params' => $this->params,
135                         'tags' => $this->tags,
136                 ]);
137         }
138
139         public static function join($channels) {
140                 $msg = new IRCMessage();
141                 $msg->command = 'JOIN';
142                 $msg->params[] = implode(',', $channels);
143                 return $msg;
144         }
145
146         public static function part($channels) {
147                 $msg = new IRCMessage();
148                 $msg->command = 'PART';
149                 $msg->params[] = implode(',', $channels);
150                 return $msg;
151         }
152
153         public static function ping($token = 'localhorsttv') {
154                 $msg = new IRCMessage();
155                 $msg->command = 'PING';
156                 $msg->params[] = $token;
157                 return $msg;
158         }
159
160         public static function privmsg($target, $message) {
161                 $msg = new IRCMessage();
162                 $msg->command = 'PRIVMSG';
163                 $msg->params[] = $target;
164                 $msg->params[] = $message;
165                 return $msg;
166         }
167
168         public function getPrivMsgTarget() {
169                 if (!empty($this->params)) {
170                         return $this->params[0];
171                 }
172                 return '';
173         }
174
175         public function getText() {
176                 if (!empty($this->params)) {
177                         return $this->params[count($this->params) - 1];
178                 }
179                 return '';
180         }
181
182         public function isNotice() {
183                 return $this->command == 'NOTICE';
184         }
185
186         public function isPing() {
187                 return $this->command == 'PING';
188         }
189
190         public function isPong() {
191                 return $this->command == 'PONG';
192         }
193
194         public function isPrivMsg() {
195                 return $this->command == 'PRIVMSG';
196         }
197
198         public function isOwner() {
199                 return substr($this->getPrivMsgTarget(), 1) == $this->nick;
200         }
201
202         public function isMod() {
203                 return $this->isOwner() || (isset($this->tags['mod']) && $this->tags['mod'] == '1');
204         }
205
206         public function makePong() {
207                 $msg = new IRCMessage();
208                 $msg->command = 'PONG';
209                 $msg->params = array_values($this->params);
210                 return $msg;
211         }
212
213 }
214
215 ?>