/** * Tests that event argument values can be removed using array syntax. * * @return void * @depends testSetArgumentWithValidArgument */ public function testImplementsOffsetUnset() { $this->event->setType('privmsg'); $this->event->setArgument('receiver', '#channel'); unset($this->event['receiver']); $this->assertNull($this->event->getArgument('receiver')); }
/** * 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; }
/** * log * * Wrapper method on the logging that adds in additional features, such as error handling * and retrying the long living database connection in case it dropped. * * @param Phergie_Event_Request $event The event we are operating on * @param string $nick Who did this? * @param string $message What was said? * @author Eli White <*****@*****.**> * @return void */ protected function log(Phergie_Event_Request $event, $nick, $message = NULL) { // Because of repeated logic, this is a 'two loop max loop' $attempts = 1; // Number of times we'll retry a failed connection here. $immediate = false; while ($attempts > 0) { // If we start off failed, go ahead and try to connect: if ($this->failed) { $attempts--; $retry = $this->getConfig('logging.retry', 300); if ($immediate || $retry + $this->failed < time()) { if ($this->failed = $this->_connectDB() ? false : time()) { $this->doPrivmsg($event->getSource(), "WARNING: DB connection for Logging Plugin has failed. " . "Retrying in {$retry} seconds"); } else { $this->doPrivmsg($event->getSource(), "NOTICE: DB connection for Logging Plugin reestablished."); } } } // Now if we aren't failed, attempt it, break the loop if it works, else repeat if (!$this->failed) { if ($this->_log($event, $nick, $message)) { break; } else { $this->failed = $immediate = true; } } } }
/** * Checks for an expected karma response. * * @param Phergie_Event_Request $event Event containing the karma * request * @param string $term Karma term * @param string $response Portion of the response * message following the term * from the original event * * @return void */ private function checkForKarmaResponse($event, $term, $response) { $text = $event->getNick() . ': ' . $response; $this->assertEmitsEvent('privmsg', array($event->getSource(), $text)); $this->plugin->onCommandKarma($term); }
/** * 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; }