public function handleCommand(Command $command) : Promise { return resolve(function () use($command) { $commandName = $command->getCommandName(); if (!isset($this->commands[$commandName])) { return; } $eventId = $command->getEvent()->getId(); $userId = $command->getUserId(); try { $userIsBanned = (yield $this->banStorage->isBanned($command->getRoom(), $userId)); if ($userIsBanned) { $this->logger->log(Level::DEBUG, "User #{$userId} is banned, ignoring event #{$eventId} for built in commands"); return; } $this->logger->log(Level::DEBUG, "Passing event #{$eventId} to built in command handler " . get_class($this->commands[$commandName])); (yield $this->commands[$commandName]->handleCommand($command)); } catch (\Throwable $e) { $this->logger->log(Level::ERROR, "Something went wrong while handling #{$eventId} for built-in commands: {$e}"); } }); }
/** * Handle a command message * * According to http://www.strawpoll.me/11212318/r this plugin will support the following commands * !!<reminder|remind> remind this <in|at> <time> * !!at <time> remind this * !!in <time> remind this * * @param Command $command * @return Promise */ public function handleCommand(Command $command) : Promise { return resolve(function () use($command) { if ($command->hasParameters() === false) { return $this->chatClient->postMessage($command->getRoom(), self::USAGE); } /* $command->getParameter(0) can be: list | examples | flush | unset | <text> | <time> */ $textOrCommand = $command->getParameter(0); $commandName = $command->getCommandName(); // <reminder|in|at> switch ($commandName) { case 'in': return (yield $this->setReminder($command, 'in')); case 'at': return (yield $this->setReminder($command, 'at')); case 'reminder': break; } if (count(array_diff($command->getParameters(), array($textOrCommand))) < 1) { switch ($textOrCommand) { case 'list': return (yield $this->getAllReminders($command)); case 'examples': return (yield $this->getExamples($command)); case 'nuke': // nukes all reminders return (yield $this->nuke($command)); } } if ($command->getParameter(0) === 'unset' && $command->getParameter(1) !== null && count($command->getParameters()) <= 2) { return (yield $this->unset($command)); } return (yield $this->setReminder($command, 'reminder')); }); }
private function invokeHandlerForCommand(Command $command) : Promise { $roomIdent = $command->getRoom()->getIdentifier()->getIdentString(); $commandName = $command->getCommandName(); if (!isset($this->commandMap[$roomIdent][$commandName])) { return new Success(); } return resolve(function () use($command, $roomIdent, $commandName) { $userId = $command->getUserId(); $userIsBanned = (yield $this->banStorage->isBanned($command->getRoom(), $userId)); if ($userIsBanned) { $this->logger->log(Level::DEBUG, "User #{$userId} is banned, ignoring event #{$command->getEvent()->getId()} for plugin command endpoints" . " (command: {$commandName})"); return; } /** @var Plugin $plugin */ /** @var PluginCommandEndpoint $endpoint */ list($plugin, $endpoint) = $this->commandMap[$roomIdent][$commandName]; // just a sanity check, shouldn't ever be false but in case something goes horribly wrong if (!$this->isPluginEnabledForRoom($plugin, $command->getRoom())) { $this->logger->log(Level::DEBUG, "Command {$commandName} still present for {$roomIdent} but plugin {$plugin->getName()}" . " is disabled! (endpoint: {$endpoint->getName()})"); return; } (yield $this->invokeCallbackAsPromise($endpoint->getCallback(), $command)); }); }
public function handleCommand(CommandMessage $command) : Promise { switch ($command->getCommandName()) { case 'invite': return resolve($this->invite($command)); case 'approve': return resolve($this->approve($command)); case 'leave': return resolve($this->leave($command)); } return new Failure(new \LogicException("I don't handle the command '{$command->getCommandName()}'")); }