/** * 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()); }
/** * Joins a channel if nickname and channel matches the own nickname * and a channel in 'channels' configuration respectively. * * @param string $nickname * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function joinOnMatch($nickname, UserEventInterface $event, EventQueueInterface $queue) { if ($nickname == $event->getConnection()->getNickname() && ($index = array_search($event->getSource(), $this->channels)) !== false) { $queue->ircJoin($this->channels[$index], $this->keys ? $this->keys[$index] : null); } }
/** * Monitor channel notices. * * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function processNotice(UserEvent $event, Queue $queue) { $logger = $this->getLogger(); $source = $event->getSource(); $nick = $event->getNick(); if ($source === null || $nick === null || $source == $nick) { $logger->debug('Incoming NOTICE not in channel, ignoring'); return; } $server = strtolower($event->getConnection()->getServerHostname()); $channel = $source; $params = $event->getParams(); $message = $params['text']; $logger->debug('Processing incoming NOTICE', array('server' => $server, 'channel' => $channel, 'nick' => $nick, 'message' => $message)); try { $this->db->fetchAssoc(self::SQL_UPDATE, array(':time' => time(), ':server' => $server, ':channel' => $channel, ':nick' => $nick, ':type' => self::TYPE_NOTICE, ':text' => $message)); } catch (\Exception $e) { $logger->error($e->getMessage()); } }
/** * Handles a failed fetch of tweet data. * * @param \Exception $error * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleError(\Exception $error, Event $event, Queue $queue) { $this->logger->debug('Received error', array('error' => $error)); $message = 'Error fetching tweet: ' . get_class($error) . ': ' . $error->getMessage(); $queue->ircPrivmsg($event->getSource(), $message); }
/** * Enqueues a new event. * * @param \Phergie\Irc\Event\UserEventInterface * @param string $command * @param array $params */ protected function queueRequest(UserEventInterface $event, $command, array $params) { $event->setPrefix($this->prefix); $event->setCommand($command); $event->setParams(array_filter($params)); $this->queue->insert($event, $this->getPriority($command, $params)); }
/** * 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(); }
/** * Listen for an user activity and send all stored messages. * * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function deliverMessage(UserEventInterface $event, EventQueueInterface $queue) { if ($event->getNick() != $event->getConnection()->getNickname()) { $messages = $this->database->retrieveMessages($event->getNick()); foreach ($messages as $row) { $message = sprintf('(%s) %s: %s', (new \DateTime($row['timestamp']))->format('m/d h:ia'), $row['sender'], $row['message']); $queue->ircNotice($event->getNick(), $message); } } }
/** * Changes the nick associated with the bot in local memory when a change * to it is successfully registered with the server. * * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleNick(UserEvent $event, Queue $queue) { $connection = $event->getConnection(); if (strcasecmp($event->getNick(), $connection->getNickname()) === 0) { $params = $event->getParams(); $connection->setNickname($params['nickname']); } }
/** * Handle primary nick recovery. * * @param \Phergie\Irc\Event\UserEventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleQuit(UserEvent $event, Queue $queue) { $nick = $event->getNick(); if ($this->primaryNick !== null && $nick == $this->primaryNick) { $this->logger->debug("[AltNick] '{$nick}' disconnected, switching to primary nick"); $queue->ircNick($this->primaryNick); $this->primaryNick = null; } }
/** * Accounts for user nick changes in stored data. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function changeUserNick(UserEventInterface $event, EventQueueInterface $queue) { $logger = $this->getLogger(); $connectionMask = $this->getConnectionMask($event->getConnection()); $old = $event->getNick(); $params = $event->getParams(); $new = $params['nickname']; $logger->debug('Changing user nick', array('connectionMask' => $connectionMask, 'oldNick' => $old, 'newNick' => $new)); foreach (array_keys($this->modes[$connectionMask]) as $channel) { if (!isset($this->modes[$connectionMask][$channel][$old])) { continue; } $logger->debug('Moving user mode data', array('connectionMask' => $connectionMask, 'channel' => $channel, 'oldNick' => $old, 'newNick' => $new)); $this->modes[$connectionMask][$channel][$new] = $this->modes[$connectionMask][$channel][$old]; unset($this->modes[$connectionMask][$channel][$old]); } }
/** * Accounts for user nick changes in stored data. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function changeUserNick(UserEventInterface $event, EventQueueInterface $queue) { $connection = $event->getConnection(); if (!$this->channelLists->contains($connection)) { return; } $logger = $this->getLogger(); $modesArray = $this->channelLists[$connection]; $old = $event->getNick(); $params = $event->getParams(); $new = $params['nickname']; foreach (array_keys($modesArray->getArrayCopy()) as $channel) { if (!isset($modesArray[$channel][$old])) { continue; } $logger->debug('Moving user mode data', array('channel' => $channel, 'oldNick' => $old, 'newNick' => $new)); $modesArray[$channel][$new] = $modesArray[$channel][$old]; unset($modesArray[$channel][$old]); } }