/** * Get the url for the API request * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @return string */ public function getApiRequestUrl(Event $event) { $params = $event->getCustomParams(); $query = trim(implode(" ", $params)); $querystringParams = ['v' => '1.0', 'q' => $query]; return sprintf("%s?%s", $this->apiUrl, http_build_query($querystringParams)); }
/** * Return the url for the API request * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * * @return string */ public function getApiRequestUrl(Event $event) { $params = $event->getCustomParams(); $query = trim(implode(" ", $params)); $querystringParams = array('q' => $query, 'appid' => $this->appId); return sprintf("%s?%s", $this->apiUrl, http_build_query($querystringParams)); }
/** * Handler for the weather command * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleCommand(Event $event, Queue $queue) { if ($this->provider->validateParams($event->getCustomParams())) { $request = $this->getApiRequest($event, $queue); $this->getEventEmitter()->emit('http.request', array($request)); } else { $this->handleCommandhelp($event, $queue); } }
/** * Notice Command * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleNoticeCommand(Event $event, Queue $queue) { $params = $event->getCustomParams(); if (count($params) < 2) { $this->handleNoticeHelp($event, $queue); } else { $channels = array_shift($params); $queue->ircNotice($channels, implode(' ', $params)); } }
/** * Handles a command to part a specified channel. * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handlePartCommand(CommandEvent $event, EventQueueInterface $queue) { $params = $event->getCustomParams(); if (!$params) { $this->handlePartHelp($event, $queue); } else { $channels = $params[0]; $queue->ircPart($channels); } }
/** * Return the url for the API request * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @return string */ public function getApiRequestUrl(Event $event) { $params = $event->getCustomParams(); // Final parameter should be the country $country = $params[count($params) - 1]; // Remove the final paramater unset($params[count($params) - 1]); // Merge the remainder of the supplied params and remove disallowed punctuation $place = trim(implode("_", preg_replace('/[^\\da-z\\ ]/i', '', $params))); return sprintf("%s/%s/conditions/q/%s/%s.json", $this->apiUrl, $this->appId, $country, $place); }
/** * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue * * @return string */ public function handleTableflipCommand(Event $event, Queue $queue) { $channel = $event->getSource(); $params = $event->getCustomParams(); if (count($params) < 1) { $this->handleTableflipHelp($event, $queue); $msgString = $this->getFlippedTable(); } else { $msgString = $this->getFlippedWords($event->getCustomParams()); } $queue->ircPrivmsg($channel, $msgString); }
/** * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue * */ public function handleCfpCommand(Event $event, Queue $queue) { $channel = $event->getSource(); $params = $event->getCustomParams(); if ($params[0] = "help") { $this->handleCfpHelp($event, $queue); $msgString = ""; } else { $msgString = $this->getAllCfp($event->getCustomParams()); } $queue->ircPrivmsg($channel, $msgString); }
/** * Returns weather information for a given location. * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleCommand(Event $event, Queue $queue) { $params = $event->getCustomParams(); if (!$params) { return $this->handleHelp($event, $queue); } $where = reset($params); $url = 'http://www.thefuckingweather.com/?where=' . urlencode($where); $self = $this; $request = new HttpRequest(array('url' => $url, 'resolveCallback' => function ($data) use($self, $event, $queue) { $self->resolve($data, $event, $queue); }, 'rejectCallback' => function () use($self, $event, $queue) { $self->reject($event, $queue); })); $this->getEventEmitter()->emit('http.request', array($request)); }
/** * * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function queryExpress(Event $event, Queue $queue) { $number = $event->getCustomParams()[0]; if ($number && is_numeric($number)) { $query = new Kuaidi($number); $result = json_decode($query->quick_query(), true); $queue->ircPrivmsg($event->getSource(), 'Query Result: '); for ($i = 0; $i < $this->lines; $i++) { if (@isset($result['data'][$i])) { $msg = $result['data'][$i]['time'] . ' -- ' . $result['data'][$i]['context']; $queue->ircPrivmsg($event->getSource(), $msg); } } } else { $queue->ircPrivmsg($event->getSource(), "Param missing or incorrect! Express ID should be numeric."); } }
/** * Returns an array of lines to send back to IRC when there are no results * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @return array */ public function getNoResultsLines(Event $event) { return array(sprintf('%s does not exist on libre.fm', $event->getCustomParams()[0])); }
/** * Handles the seen command. * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleCommand(CommandEvent $event, Queue $queue) { $logger = $this->getLogger(); $server = strtolower($event->getConnection()->getServerHostname()); $source = $event->getSource(); $nick = $event->getNick(); if ($source === null || $nick === null || $source == $nick) { $logger->debug('Command request not in channel, ignoring'); return; } $params = $event->getCustomParams(); if (empty($params)) { $this->handleCommandHelp($event, $queue); return; } $target = $params[0]; try { $data = $this->db->fetchAssoc('SELECT "time", "nick", "type", "text" FROM "seen" WHERE "server" = :server AND "channel" = :channel AND ( "nick" = :nick OR ( "type" = ' . self::TYPE_NICK . ' AND "text" = :nick ) ) ORDER BY "time" DESC LIMIT 1', array(':server' => $server, ':channel' => $source, ':nick' => $target)); } catch (\Exception $e) { $logger->error($e->getMessage()); $queue->ircPrivmsg($source, "Error: Could not retrieve data."); return; } if ($data === false) { $queue->ircPrivmsg($source, "I haven't seen {$target} in {$source}!"); return; } switch ($data['type']) { case self::TYPE_JOIN: $message = 'joining the channel.'; break; case self::TYPE_PART: if ($data['text']) { $message = "leaving the channel ({$data['text']})"; } else { $message = 'leaving the channel.'; } break; case self::TYPE_KICK: if ($data['text']) { $message = "being kicked from the channel ({$data['text']})"; } else { $message = 'being kicked from the channel.'; } break; case self::TYPE_QUIT: if ($data['text']) { $message = "disconnecting from IRC ({$data['text']})"; } else { $message = 'disconnecting from IRC.'; } break; case self::TYPE_PRIVMSG: $message = "saying: {$data['text']}"; break; case self::TYPE_NOTICE: $message = "sending a notice: {$data['text']}"; break; case self::TYPE_ACTION: $message = "saying: * {$data['nick']} {$data['text']}"; break; case self::TYPE_NICK: if (!strcasecmp($target, $data['nick'])) { $target = $data['nick']; $message = "changing nick to {$data['text']}."; } else { $target = $data['text']; $message = "changing nick from {$data['nick']}."; } break; default: $logger->error('Unknown parameter type retrieved from database', $data); $queue->ircPrivmsg($source, "Error: A database error occurred."); return; } // Canonicalise capitalisation if ($data['type'] != self::TYPE_NICK) { $target = $data['nick']; } if ($this->isInChannel($server, $source, $target)) { $prefix = "{$target} is currently in the channel! Last seen"; } else { $prefix = "{$target} was last seen"; } $queue->ircPrivmsg($source, "{$prefix} " . $this->ago($data['time']) . " {$message}"); }
protected function parseReminder(Event $event) { $params = $event->getCustomParams(); $nick = $event->getNick(); if (isset($params[1])) { $name = $params[1]; } else { $name = null; } $source = $event->getSource(); if ($this->forcePMs) { $source = $nick; } $command = 'irc' . $event->getCommand(); unset($params[0]); unset($params[1]); $time = join(" ", $params); return ['nick' => $nick, 'name' => $name, 'source' => $source, 'command' => $command, 'time' => $time]; }
public function handleNfoCommand(CommandEvent $event, Queue $queue, $showError = true) { if (isset($event->getCustomParams()[0])) { $errorCallback = function ($error) use($event, $queue, $showError) { if ($showError) { $this->sendReply($event, $queue, $error); } }; $this->emitter->emit('http.request', [new Request(['url' => 'http://api.xrel.to/api/release/info.json?dirname=' . rawurlencode($event->getCustomParams()[0]), 'resolveCallback' => function ($data) use($event, $queue, $errorCallback) { // remove /*-secure- ... */ encapsulation $data = trim(substr($data, 10, count($data) - 3)); if (!empty($data) && ($data = json_decode($data, true)) !== null) { if (isset($data['payload'])) { $string = $this->stringifyReleaseData($data['payload'], true, true); if ($string !== false) { $this->sendReply($event, $queue, $string); } } } else { $errorCallback('Nothing found!'); } }, 'rejectCallback' => $errorCallback])]); } elseif ($showError) { $this->handleNfoHelp($event, $queue); } }
/** * Main plugin handler for help requests * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleCommandHelp(Event $event, Queue $queue) { $params = $event->getCustomParams(); $provider = $this->getProvider($event->getCustomCommand() === "help" ? $params[0] : $event->getCustomCommand()); if ($provider) { $this->sendIrcResponse($event, $queue, $provider->getHelpLines()); } }
/** * Tests setCustomParams(). */ public function testSetCustomParams() { $params = array('foo', 'bar'); $this->event->setCustomParams($params); $this->assertSame($params, $this->event->getCustomParams()); }
/** * Responds to the help command. * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleHelpCommand(CommandEvent $event, EventQueueInterface $queue) { $params = $event->getCustomParams(); if ($params) { $command = strtolower(reset($params)); $eventName = 'command.' . $command . '.help'; $this->getEventEmitter()->emit($eventName, array($event, $queue)); } else { $this->listCommands($event, $queue); } }
/** * Verify parameter 2 is valid * * @param Event $event * @return bool */ private function secondParamValidation(Event $event) { $params = $event->getCustomParams(); return !isset($params[1]) || is_numeric($params[1]) && $params[1] >= 1 && $params[1] <= $this->maxDieSides; }
/** * Forwards events for command aliases to handlers for their corresponding * commands. * * @param string $alias * @param string $command * @param string $eventName * @param array $customParameters * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function forwardEvent($alias, $command, $eventName, $customParameters, Event $event, Queue $queue) { if (is_array($customParameters)) { $eventCustomParams = $event->getCustomParams(); if (is_array($eventCustomParams)) { $event->setCustomParams(array_merge($customParameters, $eventCustomParams)); } else { $event->setCustomParams($customParameters); } } $logger = $this->getLogger(); $emitter = $this->getEventEmitter(); $listeners = $emitter->listeners($eventName); if (!$listeners) { $logger->warning('Alias references unknown command', array('alias' => $alias, 'command' => $command)); return; } $logger->debug('Forwarding event', array('event_name' => $eventName, 'alias' => $alias, 'command' => $command)); // Set the event customCommand to match the command being aliased $event->setCustomCommand($command); $emitter->emit($eventName, array($event, $queue)); }
/** * Reincarnate Command - Resets the karma for a term to 0. * * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleReincarnateCommand(CommandEvent $event, Queue $queue) { $params = $event->getCustomParams(); if (count($params) < 1) { $this->handleReincarnateHelp($event, $queue); } else { $term = $params[0]; $nick = $event->getNick(); $this->messenger->rpc(MessageFactory::rpc('modifyKarma', ['term' => $this->getCanonicalTerm($term, $nick), 'karma' => 0]))->then(function ($payload) use($event, $queue, $term, $nick) { $this->logDebug('payload: ' . var_export($payload, true)); $this->logDebug("'{$term}' was reincarnated by {$nick}."); }); } }