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}"); } }); }
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)); }); }