| 116 | | $this->read(375, 376, FALSE); |
| 117 | | $this->log(2, 'Received MOTD (suppressed)'); |
| | 121 | Event::add('kirc.375', array($this, 'read_motd')); |
| | 122 | Event::add('kirc.372', array($this, 'read_motd')); |
| | 123 | Event::add('kirc.376', array($this, 'read_motd')); |
| | 124 | } |
| | 125 | |
| | 126 | public function read_motd() |
| | 127 | { |
| | 128 | switch (Event::$data['command']) |
| | 129 | { |
| | 130 | case '375': |
| | 131 | // Prepare to read the MOTD |
| | 132 | $this->motd = array(); |
| | 133 | break; |
| | 134 | case '372': |
| | 135 | // Read the MOTD |
| | 136 | $this->motd[] = substr(Event::$data['message'], 2); |
| | 137 | break; |
| | 138 | case '376': |
| | 139 | // Log the number of lines in the MOTD |
| | 140 | $this->log(1, 'Read '.count($this->motd).' MOTD lines'); |
| | 141 | |
| | 142 | // Make the MOTD into a string |
| | 143 | $this->motd = implode("\n", $this->motd); |
| | 144 | break; |
| | 145 | } |
| 128 | | $this->channels[$channel] = explode(':', $this->read(353)); |
| 129 | | $this->channels[$channel] = explode(' ', $this->channels[$channel][1]); |
| 130 | | |
| 131 | | // The end of the USERS command |
| 132 | | $this->read(366, FALSE); |
| 133 | | $this->log(2, 'Received USERS (suppressed)'); |
| | 159 | Event::add('kirc.353', array($this, 'read_userlist')); |
| | 160 | } |
| | 161 | } |
| | 162 | |
| | 163 | public function read_userlist() |
| | 164 | { |
| | 165 | if (strpos(Event::$data['target'], ' @ ') !== FALSE) |
| | 166 | { |
| | 167 | // Get the channel name from the target |
| | 168 | list ($bot, $channel) = explode(' @ ', Event::$data['target'], 2); |
| | 169 | |
| | 170 | // Set the current users |
| | 171 | $this->channels[$channel] = explode(' ', Event::$data['message']); |
| | 172 | |
| | 173 | // Log the user count |
| | 174 | $this->log(1, 'Read '.count($this->channels[$channel]).' users'); |
| 197 | | while ($raw = fgets($this->socket, 128)) |
| 198 | | { |
| 199 | | if ( ! empty($start)) |
| 200 | | { |
| 201 | | // Get the host and command from the stream |
| 202 | | list ($host, $cmd, $msg) = explode(' ', $raw, 3); |
| 203 | | |
| 204 | | if ($cmd === $start) |
| 205 | | { |
| 206 | | // Trim the message |
| 207 | | $msg = trim($msg); |
| 208 | | |
| 209 | | if (empty($end)) |
| 210 | | { |
| 211 | | // Return the message |
| 212 | | return ($return === TRUE) ? $msg : NULL; |
| 213 | | } |
| 214 | | |
| 215 | | $buffer .= $msg; |
| 216 | | } |
| 217 | | |
| 218 | | if ( ! empty($buffer)) |
| 219 | | { |
| 220 | | if ($return === TRUE) |
| 221 | | { |
| 222 | | // Only add to the buffer for returns |
| 223 | | $buffer .= $msg; |
| 224 | | } |
| 225 | | |
| 226 | | if ($cmd === $end) |
| 227 | | { |
| 228 | | // Return the complete buffer |
| 229 | | return ($return === TRUE) ? $buffer : NULL; |
| 230 | | } |
| 231 | | } |
| 232 | | } |
| 233 | | else |
| 234 | | { |
| 235 | | $this->log(2, '<<< '.trim($raw)); |
| 236 | | } |
| | 232 | while ($raw = fgets($this->socket, 512)) |
| | 233 | { |
| | 234 | $this->log(2, '<<< '.trim($raw)); |
| | 235 | |
| | 236 | // Parse the command |
| | 237 | $data = array_combine(array('sender', 'sendhost', 'command', 'target', 'message'), $this->parse($raw)); |
| | 238 | |
| | 239 | // Run the event |
| | 240 | Event::run('kirc.'.strtolower($data['command']), $data); |
| | 245 | } |
| | 246 | |
| | 247 | // Return: array(sender, sendhost, command, target, message) |
| | 248 | protected function parse($raw) |
| | 249 | { |
| | 250 | // These will always be returned |
| | 251 | $sender = NULL; |
| | 252 | $sendhost = NULL; |
| | 253 | $command = NULL; |
| | 254 | $target = NULL; |
| | 255 | $message = NULL; |
| | 256 | |
| | 257 | // Split the message |
| | 258 | $message = explode(' ', trim($raw), 2); |
| | 259 | |
| | 260 | if ( ! empty($message[0]) AND $message[0]{0} === ':') |
| | 261 | { |
| | 262 | // Is a receivable command |
| | 263 | $prefix = substr($message[0], 1); |
| | 264 | |
| | 265 | if (strpos($prefix, '!') !== FALSE) |
| | 266 | { |
| | 267 | // sender!sendhost |
| | 268 | list ($sender, $sendhost) = explode('!', $prefix, 2); |
| | 269 | } |
| | 270 | else |
| | 271 | { |
| | 272 | // sender |
| | 273 | $sender = $prefix; |
| | 274 | } |
| | 275 | |
| | 276 | // Separate the command and message |
| | 277 | list ($command, $params) = explode(' ', $message[1], 2); |
| | 278 | |
| | 279 | if (strpos($params, ' :') !== FALSE) |
| | 280 | { |
| | 281 | // target :message |
| | 282 | list ($target, $message) = explode(' :', $params, 2); |
| | 283 | } |
| | 284 | elseif ($params{0} === ':') |
| | 285 | { |
| | 286 | // :target |
| | 287 | $target = substr($params, 1); |
| | 288 | $message = NULL; |
| | 289 | } |
| | 290 | else |
| | 291 | { |
| | 292 | // target |
| | 293 | $target = $params; |
| | 294 | $message = NULL; |
| | 295 | } |
| | 296 | } |
| | 297 | else |
| | 298 | { |
| | 299 | // Is a raw command, like PING |
| | 300 | $command = $message[0]; |
| | 301 | $message = empty($message[1]) ? NULL : trim($message[1]); |
| | 302 | } |
| | 303 | |
| | 304 | return array($sender, $sendhost, $command, $target, $message); |