/**
  * Tests fromEvent().
  */
 public function testFromEvent()
 {
     $event = Phake::mock('\\Phergie\\Irc\\Event\\UserEventInterface');
     // EventInterface
     $message = 'message';
     $connection = Phake::mock('\\Phergie\\Irc\\ConnectionInterface');
     $params = array('param1', 'param2');
     $command = 'command';
     Phake::when($event)->getMessage()->thenReturn($message);
     Phake::when($event)->getConnection()->thenReturn($connection);
     Phake::when($event)->getParams()->thenReturn($params);
     Phake::when($event)->getCommand()->thenReturn($command);
     // UserEventInterface
     $prefix = 'prefix';
     $nick = 'nick';
     $username = '******';
     $host = 'host';
     $targets = array('target1', 'target2');
     Phake::when($event)->getPrefix()->thenReturn($prefix);
     Phake::when($event)->getNick()->thenReturn($nick);
     Phake::when($event)->getUsername()->thenReturn($username);
     Phake::when($event)->getHost()->thenReturn($host);
     Phake::when($event)->getTargets()->thenReturn($targets);
     $this->event->fromEvent($event);
     // EventInterface
     $this->assertSame($message, $this->event->getMessage());
     $this->assertSame($connection, $this->event->getConnection());
     $this->assertSame($params, $this->event->getParams());
     $this->assertSame($command, $this->event->getCommand());
     // UserEventInterface
     $this->assertSame($prefix, $this->event->getPrefix());
     $this->assertSame($nick, $this->event->getNick());
     $this->assertSame($username, $this->event->getUsername());
     $this->assertSame($host, $this->event->getHost());
     $this->assertSame($targets, $this->event->getTargets());
 }
 /**
  * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event
  * @param integer $total
  * @param array $results
  * @return string
  */
 public function generateResponse(Event $event, $total, array $results)
 {
     return sprintf("%s: You rolled %d %s", $event->getNick(), $total, $this->showSums && count($results) > 1 ? sprintf('(%s)', implode('+', $results)) : '');
 }
 /**
  * Handles a failed request for weather information.
  *
  * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  */
 public function reject(Event $event, Queue $queue)
 {
     $target = $event->getSource();
     $nick = $event->getNick();
     $response = 'I CAN\'T GET THE F*****G WEATHER.';
     if ($target != $nick) {
         $response = $nick . ': ' . $response;
     }
     $queue->ircPrivmsg($target, $response);
 }
 /**
  * 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}");
 }
示例#5
0
 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];
 }
 /**
  * Terminates the connection to a server from which a quit command is
  * received.
  *
  * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  */
 public function handleQuitCommand(CommandEvent $event, EventQueueInterface $queue)
 {
     $message = sprintf($this->message, $event->getNick());
     $queue->ircQuit($message);
 }
 /**
  * Responds to a parameter-less help command with a list of available
  * commands.
  *
  * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  */
 protected function listCommands(CommandEvent $event, EventQueueInterface $queue)
 {
     $targets = $event->getTargets();
     $target = reset($targets);
     $nick = $event->getNick();
     if ($target === $event->getConnection()->getNickname()) {
         $target = $nick;
         $address = '';
     } else {
         $address = $nick . ': ';
     }
     $method = 'irc' . $event->getCommand();
     $message = $address . $this->listText . implode(' ', $this->commands);
     $queue->{$method}($target, $message);
 }