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