3 namespace App\TwitchBot;
7 public $command = null;
12 public $servername = null;
15 public static function fromString($message) {
18 $raw = rtrim($message);
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];
31 if ($processed[0] == ':') {
34 $prefix = explode(' ', $processed, 2);
35 $processed = $prefix[1];
36 $prefix = ltrim($prefix[0], ':');
37 if (strpos($prefix, '!') !== false) {
40 if (strpos($prefix, '@') !== false) {
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];
58 $msg->servername = $prefix;
62 $command = explode(' ', $processed, 2);
63 $msg->command = $command[0];
64 $processed = $command[1] ?? '';
66 while (strlen($processed)) {
67 if ($processed[0] == ':') {
68 $msg->params[] = ltrim($processed, ':');
71 $e = explode(' ', $processed, 2);
72 $msg->params[] = $e[0];
83 public function encode() {
85 if (!empty($this->tags)) {
88 foreach ($this->tags as $name => $value) {
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;
109 if (!empty($this->host)) {
110 $str .= '@'.$this->host;
115 $str .= $this->command;
117 if (!empty($this->params)) {
118 $n = count($this->params) - 1;
119 for ($i = 0; $i < $n; ++$i) {
120 $str .= ' '.$this->params[$i];
122 $str .= ' :'.$this->params[$n];
128 public static function join($channels) {
129 $msg = new IRCMessage();
130 $msg->command = 'JOIN';
131 $msg->params[] = implode(',', $channels);
135 public static function part($channels) {
136 $msg = new IRCMessage();
137 $msg->command = 'PART';
138 $msg->params[] = implode(',', $channels);
142 public static function privmsg($target, $message) {
143 $msg = new IRCMessage();
144 $msg->command = 'PRIVMSG';
145 $msg->params[] = $target;
146 $msg->params[] = $message;
150 public function getPrivMsgTarget() {
151 if (!empty($this->params)) {
152 return $this->params[0];
157 public function getText() {
158 if (!empty($this->params)) {
159 return $this->params[count($this->params) - 1];
164 public function isNotice() {
165 return $this->command == 'NOTICE';
168 public function isPing() {
169 return $this->command == 'PING';
172 public function isPrivMsg() {
173 return $this->command == 'PRIVMSG';
176 public function makePong() {
177 $msg = new IRCMessage();
178 $msg->command = 'PONG';
179 $msg->params = array_values($this->params);