/**
  * 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());
 }
 /**
  * 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());
     }
 }
 /**
  * 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();
 }
 /**
  * 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']);
     }
 }
 /**
  * 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]);
     }
 }