/** * @param RequestHandlerInterface $handler * @param RequestDto $dto * @return bool */ public function shouldExecute(RequestHandlerInterface $handler, RequestDto $dto) { $section = $this->config->getSection('custom'); if (empty($section)) { return true; } $dtoChannel = $dto->getChannel(); if (null === $dtoChannel) { return true; } $handlerId = $handler->getId(); $handlerConfig = $this->config->getEntry('custom.' . $handlerId); if (empty($handlerConfig)) { return false; } $dtoChannelType = $dtoChannel[0]; if ('D' === $dtoChannelType) { return Ar::get($handlerConfig, 'dm') ?: true; } if ('C' === $dtoChannelType) { $dtoChannelInfo = $this->slackApi->channelsInfo($dtoChannel); $dtoChannelName = '#' . Ar::get($dtoChannelInfo, 'channel.name'); } else { $dtoChannelInfo = $this->slackApi->groupsInfo($dtoChannel); $dtoChannelName = Ar::get($dtoChannelInfo, 'group.name'); } $defaultBehavior = $this->config->getEntry('custom.' . $handlerId . '.default'); $handlerChannels = $this->config->getEntry('custom.' . $handlerId . '.channels'); if (empty($handlerChannels)) { return 'allow' === $defaultBehavior ? true : false; } $handlerChannelConfig = $this->config->getSectionFromArray('custom.' . $handlerId . '.channels', 'name=' . $dtoChannelName); if (empty($handlerChannelConfig)) { return 'allow' === $defaultBehavior ? true : false; } $this->params = Ar::get($handlerChannelConfig, 'params') ?: []; return true; }
/** * Processes !-command * @param RequestDto $dto */ public function processCommand(RequestDto $dto) { $words = preg_split('/\\s+/is', $dto->getText()); $command = Ar::get($words, 0); if (null === $command || '!' !== substr($command, 0, 1)) { return; } $command = substr($command, 1); Logger::get()->info("id: %s, %s is executing command \"%s\" at %s", $dto->getId(), $dto->getUser(), $dto->getText(), $dto->getChannel()); /** @var BaseCommandHandler $commandHandler */ foreach ($this->commandHandlers as $commandHandler) { $commandName = $commandHandler->getName(); if (is_array($commandName)) { if (!in_array($command, $commandName)) { continue; } } else { if ($commandName !== $command) { continue; } } $allowed = $this->checkAccess($dto, $commandHandler); if (!$allowed) { $this->slackFacade->multiSendMessage([$dto->getChannel()], sprintf('SYSTEM: access to command !%s denied', $commandHandler->getName())); Logger::get()->warning("id: %s, access denied for %s trying to run \"%s\" at %s", $dto->getId(), $dto->getUser(), $dto->getText(), $dto->getChannel()); return; } unset($words[0]); if ($commandHandler->canProcessCommand($words, $dto->getChannel())) { /** @var SlackFacade $slackFacade */ $slackFacade = Registry::get('container')['slack_facade']; $commandHandler->setCallerId($dto->getUser()); $commandHandler->setCallerName($slackFacade->getUserNameById($dto->getUser())); Logger::get()->debug("id: %s, starting %s command handler", $dto->getId(), $commandHandler->getName()); $commandHandler->processCommand($words, $dto->getChannel()); Logger::get()->debug("id: %s, finished %s command handler", $dto->getId(), $commandHandler->getName()); } } }