/** * Tests that the event raw data can be changed. * * @return void */ public function testSetRawData() { $description = ':irc.freeode.net 401 No such nick'; $this->event->setRawData($description); $this->assertSame($description, $this->event->getRawData()); }
/** * Listens for an event on the current connection. * * @return Phergie_Event_Interface|null Event instance if an event was * received, NULL otherwise */ public function getEvent() { // Check the socket is still active if (feof($this->socket)) { throw new Phergie_Driver_Exception('EOF detected on socket', Phergie_Driver_Exception::ERR_CONNECTION_READ_FAILED); } // Check for a new event on the current connection $buffer = fgets($this->socket, 512); // If no new event was found, return NULL if (empty($buffer)) { return null; } // Strip the trailing newline from the buffer $buffer = rtrim($buffer); // If the event is from the server... if (substr($buffer, 0, 1) != ':') { // Parse the command and arguments list($cmd, $args) = array_pad(explode(' ', $buffer, 2), 2, null); $hostmask = new Phergie_Hostmask(null, null, $this->connection->getHost()); } else { // If the event could be from the server or a user... // Parse the server hostname or user hostmask, command, and arguments list($prefix, $cmd, $args) = array_pad(explode(' ', ltrim($buffer, ':'), 3), 3, null); if (strpos($prefix, '@') !== false) { $hostmask = Phergie_Hostmask::fromString($prefix); } else { $hostmask = new Phergie_Hostmask(null, null, $prefix); } } // Parse the event arguments depending on the event type $cmd = strtolower($cmd); switch ($cmd) { case 'names': case 'nick': case 'quit': case 'ping': case 'join': case 'error': $args = array(ltrim($args, ':')); break; case 'privmsg': case 'notice': $args = $this->parseArguments($args, 2); list($source, $ctcp) = $args; if (substr($ctcp, 0, 1) === "" && substr($ctcp, -1) === "") { $ctcp = substr($ctcp, 1, -1); $reply = $cmd == 'notice'; list($cmd, $args) = array_pad(explode(' ', $ctcp, 2), 2, null); $cmd = strtolower($cmd); switch ($cmd) { case 'version': case 'time': case 'finger': if ($reply) { $args = $ctcp; } break; case 'ping': if ($reply) { $cmd .= 'Response'; } else { $cmd = 'ctcpPing'; } break; case 'action': $args = array($source, $args); break; default: $cmd = 'ctcp'; if ($reply) { $cmd .= 'Response'; } $args = array($source, $args); break; } } break; case 'oper': case 'topic': case 'mode': $args = $this->parseArguments($args); break; case 'part': case 'kill': case 'invite': $args = $this->parseArguments($args, 2); break; case 'kick': $args = $this->parseArguments($args, 3); break; // Remove the target from responses // Remove the target from responses default: $args = substr($args, strpos($args, ' ') + 1); break; } // Create, populate, and return an event object if (ctype_digit($cmd)) { $event = new Phergie_Event_Response(); $event->setCode($cmd)->setDescription($args); } else { $event = new Phergie_Event_Request(); $event->setType($cmd)->setArguments($args); if (isset($hostmask)) { $event->setHostmask($hostmask); } } $event->setRawData($buffer); return $event; }
/** * Listens for an event on the current connection. * * @return Phergie_Event_Interface|null Event instance if an event was * received, NULL otherwise */ public function getEvent() { // Check for a new event on the current connection $buffer = ''; do { $buffer .= fgets($this->socket, 512); } while (!empty($buffer) && !preg_match('/\\v+$/', $buffer)); $buffer = trim($buffer); // If no new event was found, return NULL if (empty($buffer)) { return null; } // If the event has a prefix, extract it $prefix = ''; if (substr($buffer, 0, 1) == ':') { $parts = explode(' ', $buffer, 3); $prefix = substr(array_shift($parts), 1); $buffer = implode(' ', $parts); } // Parse the command and arguments list($cmd, $args) = array_pad(explode(' ', $buffer, 2), 2, null); // Parse the server name or hostmask if (strpos($prefix, '@') === false) { $hostmask = new Phergie_Hostmask(null, null, $prefix); } else { $hostmask = Phergie_Hostmask::fromString($prefix); } // Parse the event arguments depending on the event type $cmd = strtolower($cmd); switch ($cmd) { case 'names': case 'nick': case 'quit': case 'ping': case 'pong': case 'error': case 'part': $args = array_filter(array(ltrim($args, ':'))); break; case 'privmsg': case 'notice': $args = $this->parseArguments($args, 2); list($source, $ctcp) = $args; if (substr($ctcp, 0, 1) === "" && substr($ctcp, -1) === "") { $ctcp = substr($ctcp, 1, -1); $reply = $cmd == 'notice'; list($cmd, $args) = array_pad(explode(' ', $ctcp, 2), 2, array()); $cmd = strtolower($cmd); switch ($cmd) { case 'version': case 'time': case 'finger': case 'ping': if ($reply) { $args = array($args); } break; case 'action': $args = array($source, $args); break; } } // This fixes the issue that seems to occur, but why does it? if (!is_array($args)) { $args = array($args); } break; case 'topic': case 'invite': case 'join': $args = $this->parseArguments($args, 2); break; case 'kick': case 'mode': $args = $this->parseArguments($args, 3); break; // Remove target and colon preceding description from responses // Remove target and colon preceding description from responses default: $args = substr($args, strpos($args, ' ') + 2); break; } // Create, populate, and return an event object if (ctype_digit($cmd)) { $event = new Phergie_Event_Response(); $event->setCode($cmd)->setDescription($args); } else { $event = new Phergie_Event_Request(); $event->setType($cmd)->setArguments($args); $event->setHostmask($hostmask); } $event->setRawData($buffer); return $event; }