| 120 | | // Read the MOTD before continuing |
| 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']) |
| | 136 | // Set the MOTD response |
| | 137 | $this->set_response('375', array($this, 'read_motd')); |
| | 138 | $this->set_response('372', array($this, 'read_motd')); |
| | 139 | $this->set_response('376', array($this, 'read_motd')); |
| | 140 | } |
| | 141 | |
| | 142 | public function read_motd($data) |
| | 143 | { |
| | 144 | switch ($data['command']) |
| 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'); |
| | 175 | $this->set_response('353', array($this, 'read_userlist')); |
| 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); |
| | 245 | // Remove the whitespace garbage |
| | 246 | $raw = trim($raw); |
| | 247 | |
| | 248 | // These values are always returned |
| | 249 | $data = array |
| | 250 | ( |
| | 251 | 'sender' => NULL, |
| | 252 | 'sendhost' => NULL, |
| | 253 | 'command' => NULL, |
| | 254 | 'target' => NULL, |
| | 255 | 'message' => NULL, |
| | 256 | ); |
| | 257 | |
| | 258 | // Extract the prefix from the string |
| | 259 | list ($prefix, $str) = explode(' ', $raw, 2); |
| | 260 | |
| | 261 | if ( ! empty($prefix) AND $prefix{0} === ':') |
| | 262 | { |
| | 263 | // A user-level command, like PRIVMSG or NOTICE |
| | 264 | $prefix = substr($prefix, 1); |
| 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; |
| | 273 | // sender, Typically a server |
| | 274 | $data['sender'] = $prefix; |
| | 275 | } |
| | 276 | |
| | 277 | // CMD str, Extract the command from the remaining string |
| | 278 | list ($data['command'], $str) = explode(' ', $str, 2); |
| | 279 | |
| | 280 | if (strpos($str, ' :') !== FALSE) |
| | 281 | { |
| | 282 | // target :message, some kind of communication |
| | 283 | list ($data['target'], $data['message']) = explode(' :', $params, 2); |
| | 284 | } |
| | 285 | elseif ($str{0} === ':') |
| | 286 | { |
| | 287 | // :target, without a message |
| | 288 | $data['target'] = substr($str, 1); |
| 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); |
| | 297 | // A server-level command, like PING |
| | 298 | $data['command'] = $prefix; |
| | 299 | $data['message'] = empty($str) ? NULL : $str; |
| | 300 | } |
| | 301 | |
| | 302 | return $data; |
| 406 | | protected function url_status($url) |
| 407 | | { |
| 408 | | if (($status = $this->db_url_status($url)) === NULL) |
| 409 | | { |
| 410 | | // Extract the URL params |
| 411 | | extract(parse_url($url), EXTR_PREFIX_ALL, 'url'); |
| 412 | | |
| 413 | | // Invalid URL by default |
| 414 | | $status = FALSE; |
| 415 | | if ($socket = fsockopen($url_host, 80, $errno, $errstr, 6)) |
| 416 | | { |
| 417 | | // Fetch the HTTP HEAD |
| 418 | | fwrite($socket, "HEAD $url_path HTTP/1.0\r\nHost: $url_host\r\n\r\n"); |
| 419 | | |
| 420 | | // Read the response |
| 421 | | $status = fgets($socket, 22); |
| 422 | | |
| 423 | | // Set the response |
| 424 | | $status = (strpos($status, '200 OK') !== FALSE); |
| 425 | | |
| 426 | | // Close the connection |
| 427 | | fclose($socket); |
| 428 | | } |
| 429 | | |
| 430 | | // Save the URL to the database |
| 431 | | $this->db->insert('urls', array('url' => $url, 'status' => (int) $status)); |
| 432 | | } |
| 433 | | |
| 434 | | return $status; |
| 435 | | } |
| 436 | | |
| 437 | | protected function db_url_status($url) |
| 438 | | { |
| 439 | | // Fetch the status of the URL |
| 440 | | $status = $this->db->select('status')->where('url', $url)->limit(1)->get('urls'); |
| 441 | | |
| 442 | | return $status->count() ? (bool) $status->current()->status : NULL; |
| 443 | | } |
| 444 | | |
| 445 | | } |
| | 404 | /** |
| | 405 | * Kobot default responses. You can overload these in your own extension class, |
| | 406 | * or attach your own event handlers |
| | 407 | */ |
| | 408 | |
| | 409 | public function response_drop($data) |
| | 410 | { |
| | 411 | // Log dropped responses |
| | 412 | $this->log(2, $data); |
| | 413 | } |
| | 414 | |
| | 415 | public function response_ping($data) |
| | 416 | { |
| | 417 | // Reply with a PONG |
| | 418 | $this->send('PONG '.substr($data['message'], 1)); |
| | 419 | } |
| | 420 | |
| | 421 | // 353 |
| | 422 | public function response_userlist($data) |
| | 423 | { |
| | 424 | if (strpos($data['target'], ' @ ') !== FALSE) |
| | 425 | { |
| | 426 | // Get the channel name from the target |
| | 427 | list ($bot, $channel) = explode(' @ ', $data['target'], 2); |
| | 428 | |
| | 429 | // Set the current users |
| | 430 | $this->channels[$channel] = explode(' ', $data['message']); |
| | 431 | |
| | 432 | // Log the user count |
| | 433 | $this->log(1, 'Read '.count($this->channels[$channel]).' users'); |
| | 434 | } |
| | 435 | } |
| | 436 | |
| | 437 | } // End Kobot |