/** * Sends reply messages. * * @param UserEventInterface $event * @param Queue $queue * @param array|string $messages */ protected function sendReply(UserEventInterface $event, Queue $queue, $messages) { $method = 'irc' . $event->getCommand(); if (is_array($messages)) { $target = $event->getSource(); foreach ($messages as $message) { $queue->{$method}($target, $message); } } else { $queue->{$method}($event->getSource(), $messages); } }
/** * Copies data from an existing event into this one. * * @param \Phergie\Irc\Event\UserEventInterface $event */ public function fromEvent(UserEventInterface $event) { // EventInterface $this->setMessage($event->getMessage()); $this->setConnection($event->getConnection()); $this->setParams($event->getParams()); $this->setCommand($event->getCommand()); // UserEventInterface $this->setPrefix($event->getPrefix()); $this->setNick($event->getNick()); $this->setUsername($event->getUsername()); $this->setHost($event->getHost()); $this->setTargets($event->getTargets()); }
/** * Parses user events for commands and emits them as events. * * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function parseCommand(UserEventInterface $event, EventQueueInterface $queue) { // Get the pattern to identify commands if ($this->nick) { $nick = $event->getConnection()->getNickname(); $identity = $this->getNickPattern($nick); } else { $identity = $this->identityPattern; } // Verify this event contains a command and remove the substring // identifying it as one $eventParams = $event->getParams(); $target = $event->getCommand() === 'PRIVMSG' ? $eventParams['receivers'] : $eventParams['nickname']; $message = $eventParams['text']; if ($identity) { if (preg_match($identity, $message, $match)) { $message = preg_replace($identity, '', $message); } elseif (preg_match($this->channelPattern, $target)) { return; } } // Parse the command and its parameters if (!preg_match($this->commandPattern, $message, $match)) { return; } $customCommand = $match['command']; if (!empty($match['params']) && preg_match_all($this->paramsPattern, $match['params'], $matches)) { $customParams = array_map(function ($param) { return trim($param, '"'); }, $matches[0]); } else { $customParams = array(); } // Populate an event object with the parsed data $commandEvent = $this->getCommandEvent(); $commandEvent->fromEvent($event); $commandEvent->setCustomCommand($customCommand); $commandEvent->setCustomParams($customParams); // Emit the event object to listeners $customEventName = 'command.' . strtolower($customCommand); $customEventParams = array($commandEvent, $queue); $this->getEventEmitter()->emit($customEventName, $customEventParams); }
/** * Extracts a list of channel names from a user event. * * @param \Phergie\Irc\Event\UserEventInterface $event * @return array */ protected function getChannels(UserEventInterface $event) { $command = $event->getCommand(); if (isset($this->parameters[$command])) { $params = $event->getParams(); $param = $this->parameters[$command]; return preg_grep('/^[#&]/', explode(',', $params[$param])); } return array(); }