/** * Responds with a magic eight ball phrase when asked questions * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleEvent(Event $event, Queue $queue) { if ($event instanceof UserEvent) { $nick = $event->getNick(); $channel = $event->getSource(); $params = $event->getParams(); $text = $params['text']; $matched = stripos($text, '8 ball') !== false; if ($matched) { $msg = $nick . ', the magic 8 ball says "' . $this->getMagicEightBallAnswer() . '"'; $queue->ircPrivmsg($channel, $msg); } } }
/** * @param EventInterface $event * @return bool|null */ public function filter(EventInterface $event) { if (!$event instanceof UrlEvent) { return null; } $section = $event->getUrlSection($this->section); if ($section === null) { return $this->strictResponse; } $pattern = '/^' . str_replace('*', '.*', $this->value) . '$/'; if (preg_match($pattern, $section)) { return true; } return false; }
/** * Filters events that are not user-specific or are from the specified users. * * @param \Phergie\Irc\Event\EventInterface $event * @return boolean|null TRUE if the event originated from a user with a matching mask * associated with this filter, FALSE if it originated from a user without a * matching mask, or NULL if it did not originate from a user. */ public function filter(EventInterface $event) { if (!$event instanceof UserEventInterface) { return null; } $nick = $event->getNick(); if ($nick === null) { return null; } $userMask = sprintf('%s!%s@%s', $nick, $event->getUsername(), $event->getHost()); foreach ($this->masks as $mask) { $pattern = '/^' . str_replace('*', '.*', $mask) . '$/'; if (preg_match($pattern, $userMask)) { return true; } } return false; }
/** * Responds to server ping events. * * @param \Phergie\Irc\Event\EventInterface $event Ping event to respond to * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function pong(EventInterface $event, EventQueueInterface $queue) { $params = $event->getParams(); $queue->ircPong($params['server1']); }
/** * Neutralise the existing timer, if present, and create a new timer. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleReceived(Event $event, Queue $queue) { $connection = $event->getConnection(); $timers = $this->getTimers(); if ($timers->contains($connection)) { $timers->offsetGet($connection)->cancel(); } else { $this->getLogger()->debug('Attaching activity listener for connection'); } $timer = $this->getLoop()->addTimer($this->wait, array($this, 'callbackPhoneHome')); $timer->setData($connection); $timers->attach($connection, $timer); }
/** * Handles a successful request for video data. * * @param string $url URL of the request * @param \GuzzleHttp\Message\Response $data Response body * @param \Phergie\Irc\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function resolve($url, \GuzzleHttp\Message\Response $data, Event $event, Queue $queue) { $logger = $this->getLogger(); $json = json_decode($data->getBody()); $logger->info('resolve', array('url' => $url, 'json' => $json)); if (isset($json->error)) { return $logger->warning('Query response contained an error', array('url' => $url, 'error' => $json->error)); } $entries = $json->items; if (!is_array($entries) || !$entries) { return $logger->warning('Query returned no results', array('url' => $url)); } $entry = reset($entries); $replacements = $this->getReplacements($entry); $message = str_replace(array_keys($replacements), array_values($replacements), $this->responseFormat); $queue->ircPrivmsg($event->getSource(), $message); }
/** * Stores a reference to the event queue for the connection on which a USER * event occurs. * * @param \Phergie\Irc\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function setEventQueue(Event $event, Queue $queue) { $mask = $this->getConnectionMask($event->getConnection()); $this->getEventQueueDeferred($mask)->resolve($queue); }
/** * Filters events over the specified connection. * * @param \Phergie\Irc\Event\EventInterface $event * @return boolean TRUE if the event connection matches the one associated * with this filter, FALSE otherwise */ public function filter(EventInterface $event) { return in_array($event->getConnection(), $this->connections); }
/** * Stores references to each connection and their corresponding event * queues when a USER event is received. * * @param \Phergie\Irc\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function getEventQueue(Event $event, Queue $queue) { $connection = $event->getConnection(); $mask = $this->getConnectionMask($connection); $this->connections[$mask] = $connection; $this->queues[$mask] = $queue; }
/** * Nick is in use, pick another. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleEvent(Event $event, Queue $queue) { $iterator = $this->getIterator($event->getConnection()); if (!$iterator->valid()) { $queue->ircQuit('All specified alternate nicks are in use'); return; } $nick = $iterator->current(); $iterator->next(); $this->logger->debug("[AltNick] Switching nick to '{$nick}'"); $queue->ircNick($nick); $event->getConnection()->setNickname($nick); }
/** * Returns an event subtype corresponding to a given event object, used to * generate event names when emitting events. * * @param \Phergie\Irc\Event\EventInterface $event * @return string */ protected function getEventSubtype(EventInterface $event) { $subevent = ''; if ($event instanceof CtcpEvent) { $subevent = 'ctcp.' . strtolower($event->getCtcpCommand()); } elseif ($event instanceof UserEvent) { $subevent = strtolower($event->getCommand()); } elseif ($event instanceof ServerEvent) { $subevent = strtolower($event->getCode()); } return $subevent; }
/** * Nick is in use, pick another. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function handleEvent(Event $event, Queue $queue) { $iterator = $this->getIterator($event->getConnection()); if (!$iterator->valid()) { $queue->ircQuit('All specified alternate nicks are in use'); return; } if ($this->recovery && $this->primaryNick === null) { $params = $event->getParams(); $primaryNick = $params[1]; $this->logger->debug("[AltNick] Saving '{$primaryNick}' as primary nick"); $this->primaryNick = $primaryNick; } $nick = $iterator->current(); $iterator->next(); $this->logger->debug("[AltNick] Switching nick to '{$nick}'"); $queue->ircNick($nick); }
/** * Handles a successful request for video data. * * @param string $url URL of the request * @param \GuzzleHttp\Message\Response $data Response body * @param \Phergie\Irc\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function resolve($url, \GuzzleHttp\Message\Response $data, Event $event, Queue $queue) { $logger = $this->getLogger(); $json = json_decode($data->getBody()); $logger->info('resolve', ['url' => $url, 'json', $json]); if (isset($json->error)) { return $logger->warning('Twitch response error', ['url' => $url, 'error' => $json->error, 'message' => $json->message]); } if (null === $json->stream) { return $queue->ircPrivmsg($event->getSource(), "Stream is offline"); } $replacements = $this->getReplacements($json); $message = str_replace(array_keys($replacements), array_values($replacements), $this->responseFormat); $queue->ircPrivmsg($event->getSource(), $message); }
public function getCommand() { return $this->event->getCommand(); }
/** * Loads initial user mode data when the bot joins a channel. * * @param \Phergie\Irc\Event\EventInterface $event * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue */ public function loadUserModes(EventInterface $event, EventQueueInterface $queue) { $logger = $this->getLogger(); $connectionMask = $this->getConnectionMask($event->getConnection()); $params = $event->getParams(); $channel = $params[1]; $validPrefixes = implode('', array_keys($this->prefixes)); $pattern = '/^([' . preg_quote($validPrefixes) . ']+)(.+)$/'; $logger->debug('Gathering initial user mode data', array('connectionMask' => $connectionMask, 'channel' => $channel)); foreach ($params['iterable'] as $fullNick) { if (!preg_match($pattern, $fullNick, $match)) { continue; } $nickPrefixes = str_split($match[1]); $nick = $match[2]; foreach ($nickPrefixes as $prefix) { $mode = $this->prefixes[$prefix]; $logger->debug('Recording user mode', array('connectionMask' => $connectionMask, 'channel' => $channel, 'nick' => $nick, 'mode' => $mode)); $this->modes[$connectionMask][$channel][$nick][$mode] = true; } } }