private function getBranchAndSearchTerm(Command $command) : array { $parameters = $command->getParameters(); if (count($parameters) < 3 || strtolower($command->getParameter(0)) !== '-b' || !preg_match('#^(?:[0-9]+\\.[0-9]+|PECL|MASTER)$#i', $command->getParameter(1))) { return [self::DEFAULT_BRANCH, implode(' ', $parameters)]; } return [strtoupper($command->getParameter(1)), implode(' ', array_slice($parameters, 2))]; }
public function changelog(Command $command) : \Generator { $repository = $command->getParameter(0) ?? 'Room-11/Jeeves'; if (preg_match(self::EXPRESSION, $repository)) { return yield from $this->getLastCommit($command, $repository); } return $this->chatClient->postMessage($command->getRoom(), "Usage: !!changelog [ <profile>/<repo> ]"); }
public function github(Command $command) : \Generator { $obj = $command->getParameter(0) ?? 'status'; if ($obj === 'status') { return yield from $this->status($command->getRoom()); } elseif (strpos($obj, '/') === false) { return yield from $this->profile($command->getRoom(), $obj); } elseif (strpos($obj, '/') === strrpos($obj, '/')) { return yield from $this->repo($command->getRoom(), $obj); } return $this->chatClient->postMessage($command->getRoom(), "Usage: !!github [status | <project> | <profile> | <profile>/<repo> ]"); }
private function execute(CommandMessage $command) { if (!(yield $command->getRoom()->isApproved())) { return; } if (!in_array($command->getParameter(0), self::ACTIONS, true)) { return; } if ($command->getParameter(0) === "list") { return yield from $this->list($command); } if (!(yield $this->storage->isAdmin($command->getRoom(), $command->getUserId()))) { return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that"); } switch ($command->getParameter(0)) { case 'add': return yield from $this->add($command, (int) $command->getParameter(1)); case 'remove': return yield from $this->remove($command, (int) $command->getParameter(1)); } throw new \LogicException('Operation ' . $command->getParameter(0) . ' was considered valid but not handled??'); }
public function getRFC(Command $command) { $rfc = $command->getParameter(0); if ($rfc === null) { // e.g.: !!rfc pipe-operator return $this->chatClient->postMessage($command->getRoom(), "RFC id required"); } $uri = self::BASE_URI . '/' . urlencode($rfc); /** @var HttpResponse $response */ $response = (yield $this->httpClient->request($uri)); if ($response->getStatus() !== 200) { return $this->chatClient->postMessage($command->getRoom(), "Nope, we can't have nice things."); } $votes = self::parseVotes($response->getBody()); if (empty($votes)) { return $this->chatClient->postMessage($command->getRoom(), "No votes found"); } $messages = []; foreach ($votes as $id => $vote) { $breakdown = []; $total = array_sum($vote['votes']); if ($total > 0) { foreach ($vote['votes'] as $option => $value) { $breakdown[] = sprintf("%s (%d: %d%%)", $option, $value, 100 * $value / $total); } } $messages[] = ['name' => $vote['name'], 'href' => $uri . '#' . $id, 'breakdown' => implode(', ', $breakdown)]; } if (count($messages) === 1) { return $this->chatClient->postMessage($command->getRoom(), sprintf("[tag:rfc-vote] [%s](%s) %s", $messages[0]['name'], $messages[0]['href'], $messages[0]['breakdown'])); } $message = implode("\n", array_map(function ($message) { return sprintf('%s %s - %s (%s)', self::BULLET, $message['name'], $message['breakdown'], $message['href']); }, $messages)); return $this->chatClient->postMessage($command->getRoom(), $message); }
/** * Handle a command message * * @param Command $command * @return Promise */ public function handleCommand(Command $command) : Promise { if ($command->getParameter(0) === "list") { return $this->getSupportedCanonicals($command); } if (!in_array($command->getParameter(0), self::ACTIONS)) { return $this->getMessage($command, implode(" ", $command->getParameters())); } return resolve(function () use($command) { if (!(yield $this->admin->isAdmin($command->getRoom(), $command->getUserId()))) { return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that."); } switch ($command->getParameter(0)) { case 'add': return (yield $this->add($command, (string) $command->getParameter(1), (string) $command->getParameter(2))); case 'remove': return (yield $this->remove($command, (string) $command->getParameter(1))); case 'fire': return (yield $this->fire($command)); } }); }
public function tweet(Command $command) { if (!$this->isMessageValid($command->getParameter(0))) { return new Success(); } if (!(yield $this->admin->isAdmin($command->getRoom(), $command->getUserId()))) { return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that"); } $tweetText = (yield from $this->getMessage($command, $command->getParameters()[0])); if (mb_strlen($tweetText, "UTF-8") > 140) { return $this->chatClient->postReply($command, "Boo! The message exceeds the 140 character limit. :-("); } $keys = ['oauth.consumer_key', 'oauth.access_token', 'oauth.consumer_secret', 'oauth.access_token_secret']; $config = []; foreach ($keys as $key) { if (!(yield $this->storage->exists($key, $command->getRoom()))) { return $this->chatClient->postReply($command, "I'm not currently configured for tweeting :-("); } $config[$key] = (yield $this->storage->get($key, $command->getRoom())); } $oauthParameters = ["oauth_consumer_key" => $config['oauth.consumer_key'], "oauth_token" => $config['oauth.access_token'], "oauth_nonce" => $this->getNonce(), "oauth_timestamp" => (new \DateTimeImmutable())->format("U"), "oauth_signature_method" => "HMAC-SHA1", "oauth_version" => "1.0", "status" => $tweetText]; $oauthParameters = array_map("rawurlencode", $oauthParameters); asort($oauthParameters); ksort($oauthParameters); $queryString = urldecode(http_build_query($oauthParameters, '', '&')); $baseString = "POST&" . rawurlencode(self::BASE_URI . "/statuses/update.json") . "&" . rawurlencode($queryString); $key = $config['oauth.consumer_secret'] . "&" . $config['oauth.access_token_secret']; $signature = rawurlencode(base64_encode(hash_hmac('sha1', $baseString, $key, true))); $oauthParameters["oauth_signature"] = $signature; $oauthParameters = array_map(function ($value) { return '"' . $value . '"'; }, $oauthParameters); unset($oauthParameters["status"]); asort($oauthParameters); ksort($oauthParameters); $authorizationHeader = $auth = "OAuth " . urldecode(http_build_query($oauthParameters, '', ', ')); $request = (new HttpRequest())->setUri(self::BASE_URI . "/statuses/update.json")->setMethod('POST')->setProtocol('1.1')->setBody('status=' . urlencode($tweetText))->setAllHeaders(['Authorization' => $authorizationHeader, 'Content-Type' => 'application/x-www-form-urlencoded']); /** @var HttpResponse $result */ $result = (yield $this->httpClient->request($request)); $tweetInfo = json_decode($result->getBody(), true); $tweetUri = 'https://twitter.com/' . $tweetInfo['user']['screen_name'] . '/status/' . $tweetInfo['id_str']; return $this->chatClient->postMessage($command->getRoom(), $tweetUri); }
/** * 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 execute(CommandMessage $command) { if (!(yield $command->getRoom()->isApproved())) { return null; } switch ($command->getParameter(0)) { case 'list': return $this->list($command); case 'enable': return $this->enable($command); case 'disable': return $this->disable($command); case 'status': return $this->status($command); } return $this->chatClient->postReply($command, "Syntax: plugin [list|disable|enable] [plugin-name]"); }
public function dadGreet(Command $command) { $room = $command->getRoom(); switch ($command->getParameter(0)) { case 'on': if (!(yield $this->admin->isAdmin($room, $command->getUserId()))) { return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that"); } if (preg_match('#[0-9]+#', $command->getParameter(1))) { if (1 > ($frequency = (int) $command->getParameter(1))) { return $this->chatClient->postReply($command, 'Frequency cannot be less than 1'); } yield from $this->setDadGreetFrequency($room, $frequency); } yield from $this->setDadGreetEnabled($room, true); return $this->chatClient->postMessage($room, 'Dad greeting is now enabled with a frequency of ' . (yield from $this->getDadGreetFrequency($room))); case 'off': if (!(yield $this->admin->isAdmin($room, $command->getUserId()))) { return $this->chatClient->postReply($command, "I'm sorry Dave, I'm afraid I can't do that"); } yield from $this->setDadGreetEnabled($room, false); return $this->chatClient->postMessage($room, 'Dad greeting is now disabled'); case 'status': $state = (yield from $this->isDadGreetEnabled($room)) ? 'enabled with a frequency of ' . (yield from $this->getDadGreetFrequency($room)) : 'disabled'; return $this->chatClient->postMessage($room, 'Dad greeting is currently ' . $state); } return $this->chatClient->postReply($command, 'Syntax: ' . $command->getCommandName() . ' on|off|status [frequency]'); }
private function execute(CommandMessage $command) : \Generator { if (!(yield $command->getRoom()->isApproved())) { return; } try { switch ($command->getParameter(0)) { case 'alias': return yield from $this->alias($command); case 'list': return $this->list($command); case 'map': return yield from $this->map($command); case 'remap': return yield from $this->remap($command); case 'unmap': return yield from $this->unmap($command); } } catch (\Throwable $e) { return $this->chatClient->postReply($command, self::message('unexpected_error', $e->getMessage())); } return $this->chatClient->postMessage($command->getRoom(), self::message('syntax')); }