public function onRun() { $time = (int) IRC::getInstance()->getConfig()->getData("auto_reconnect_after_timeout", 500); $max_attempts = (int) IRC::getInstance()->getConfig()->getData("max_reconnect_attempts", 5); if ($time) { if ($this->connection->getLastPing() <= $time + time() and $this->connection->isConnected()) { // Possibly time-outed. Attempt a reconnect Logger::info(BashColor::RED . "Lost connection to " . $this->connection->getAddress() . " - Reconnecting..."); $this->connection->disconnect(); if ($this->connection->connect()) { Logger::info(BashColor::GREEN . "Successfully reconnected."); // Re-schedule this task $this->reschedule($time); $this->attempts = 0; // Successfully reconnected, reset } else { Logger::info(BashColor::RED . "Error while reconnecting."); $this->attempts++; if ($this->attempts > $max_attempts) { // Remove the connection Logger::info(BashColor::RED . "Terminating connection."); IRC::getInstance()->removeConnection($this->connection); } else { Logger::info(BashColor::CYAN . "Next attempt in " . $time . "s"); $this->reschedule($time); } } } else { $this->reschedule($time); } } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { Logger::info(BashColor::HIGHLIGHT . "Nickname already in use."); $altNick = IRC::getInstance()->getConfig()->getData("alt_nickname", "FishIRC"); if (!$connection->isConnected() and $connection->getNick() !== $altNick) { $connection->changeNick($altNick); } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { //Tell the plugins that a user has quit $user = User::getUser($connection, $command->getPrefix()); $ev = new UserQuitEvent($user); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info($user->getNick() . " quit"); User::removeUser($connection, $user->getHostmask()); // Remove user } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { $user = User::getUser($connection, $command->getPrefix()); $arg = $command->getArgs(); if ($command->getArg(0) === $connection->nickname) { $channel = Channel::getChannel($connection, $user->getNick()); } else { $channel = Channel::getChannel($connection, $arg[0]); } unset($arg[0]); $args = explode(":", implode(" ", $arg), 2); if ($args[1][0] === chr(1)) { //Check whether the message is a ctcp, message or command $args[1] = explode(" ", $args[1], 2); $ctcp_command = str_replace(chr(1), "", $args[1][0]); unset($args[1][0]); $ev = new CTCPReceiveEvent($user, $ctcp_command); $connection->getEventHandler()->callEvent($ev); if (empty($args[1][1])) { if ($reply = IRC::getInstance()->getConfig()->getData("default_ctcp_replies", [])[$ctcp_command]) { if ($reply !== null) { $ev = new CTCPSendEvent($user, $ctcp_command, $reply); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { $user->sendNotice(chr(1) . $ctcp_command . " " . $ev->getMessage()); } } } } } elseif (!in_array($args[1][0], $config->getData("command_prefix", [".", "!", "\\", "@"]))) { $ev = new MessageReceiveEvent($args[1], $user, $channel); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info(BashColor::GREEN . $ev->getChannel()->getName() . " " . $ev->getUser()->getNick() . ":" . BashColor::REMOVE . " " . $ev->getMessage()); //Display the message to the console } } else { $args[1] = substr($args[1], 1); $args[1] = explode(" ", $args[1]); $cmd = strtolower($args[1][0]); //Command in lower case unset($args[1][0]); Logger::info(BashColor::CYAN . $user->getNick() . " > " . $cmd . " " . implode(" ", $args[1])); $ev = new CommandEvent($cmd, $args[1], $channel, $user); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { $connection->getCommandHandler()->handleCommand($cmd, $user, $channel, $args); } } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { //Tell the plugins that a user has parted $channel = Channel::getChannel($connection, str_replace(":", "", $command->getArg(0))); $user = User::getUser($connection, $command->getPrefix()); if ($user instanceof User) { $ev = new ChannelLeaveEvent($channel, $user); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info($user->getNick() . " left " . $channel->getName()); User::removeUser($connection, $user->getHostmask()); //Remove the user, don't care if they are in other channels the bot is in } } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { $user = User::getUser($connection, $command->getPrefix()); $new = str_replace(":", "", $command->getArg(0)); $ev = new UserChangeNickEvent($user->getNick(), $new); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info($user->getNick() . " is now known as " . $new); if ($user->getNick() === $connection->getNick()) { // Bot changed name $connection->nickname = $new; } User::removeUser($connection, $user->getHostmask()); //Remove old authentication status, mainly } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { $user = User::getUser($connection, $command->getPrefix()); $arg = $command->getArgs(); if ($command->getArg(0) === $connection->nickname) { $channel = Channel::getChannel($connection, $user->getNick()); } else { $channel = Channel::getChannel($connection, $arg[0]); } unset($arg[0]); $ev = new NoticeReceiveEvent(explode(":", implode(" ", $arg), 2)[1], $user, $channel); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info(BashColor::HIGHLIGHT . (!empty($ev->getUser()->getNick()) ? $ev->getUser()->getNick() . ": " : " ") . $ev->getNotice() . BashColor::REMOVE); //Display the notice to the console } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { //Tell the plugins that a user was kicked $channel = Channel::getChannel($connection, str_replace(":", "", $command->getArg(0))); $kicker = User::getUser($connection, $command->getPrefix()); $user = $command->getArg(1); $ev = new KickEvent($user, $channel, $kicker); $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info($user . " was kicked from " . $channel->getName()); if ($user === $connection->getNick()) { if (IRC::getInstance()->getConfig()->getData("auto_rejoin_after_kick", false)) { // Make the bot rejoin in 5 seconds $connection->getScheduler()->scheduleDelayedTask(new RejoinChannelTask($connection, $channel), 5); } } } }
public static function run(Command $command, Connection $connection, JsonConfig $config) { //Tell the plugins that a user has joined $channel = Channel::getChannel($connection, str_replace(":", "", $command->getArg(0))); $user = User::getUser($connection, $command->getPrefix()); if ($user->getNick() === $connection->getNick()) { $ev = new BotJoinChannelEvent($channel, $user); } else { $ev = new JoinChannelEvent($channel, $user); } $connection->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info($user->getNick() . " joined " . $channel->getName()); if ($ev instanceof BotJoinChannelEvent) { $connection->addChannel($channel); // Add the channel } } }
public function __construct(string $name, array $json, Connection $connection) { if (file_exists("phar://plugins" . DIRECTORY_SEPARATOR . $name . ".phar" . DIRECTORY_SEPARATOR . "plugin.json")) { Logger::info(BashColor::GREEN . "Loading plugin " . BashColor::BLUE . $name); $this->name = $json["name"]; $this->description = $json["description"]; $this->apiVersion = $json["api"]; $this->version = $json["version"]; $this->author = $json["author"]; $this->main = $json["main"]; //Instantiating plugins $info = new \SplFileInfo("phar://plugins" . DIRECTORY_SEPARATOR . $name . ".phar" . DIRECTORY_SEPARATOR . $this->main); $class = new \ReflectionClass("\\" . $name . "\\" . $info->getBasename(".php")); //Taking care of using the correct namespace $this->class = $class->newInstanceWithoutConstructor(); $this->reflectionClass = $class; $this->class->connection = $connection; $this->class->plugin = $this; //Registering commands if (isset($json["commands"])) { $this->commands = $json["commands"]; foreach ($this->commands as $command => $settings) { // Set description $description = !empty($settings["description"]) ? $settings["description"] : ""; // Set usage help $usage = !empty($settings["usage"]) ? $settings["usage"] : $command; // Set required permission $permission = !empty($settings["permission"]) ? $settings["permission"] : false; //Default permission is false $command = new Command($command, $this->class, $permission, $description, $usage); // Add aliases if (isset($settings["aliases"]) && is_array($settings["aliases"])) { foreach ($settings["aliases"] as $alias) { $command->addAlias($alias); } } $connection->getCommandMap()->registerCommand($command, $this); } } } }
/** * Send something to the server * @param String $data */ public function sendData(string $data) { $ev = new ConnectionUseEvent($this, $data); $this->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { if (is_resource($this->socket)) { fwrite($this->socket, $data . "\r\n"); if (IRC::getInstance()->verbose) { Logger::info($this->getAddress() . " > " . $data); } } } }
/** * Close a connection * @param Connection $connection * @param String $quitMessage * @return bool */ public function removeConnection(Connection $connection, string $quitMessage = null) : bool { if ($this->isConnected($connection->getAddress())) { Logger::info(BashColor::RED . "Disconnecting " . $connection->getAddress() . ":" . $connection->getPort()); if ($quitMessage === null) { $quitMessage = $this->config->getData("default_quitmsg", "Leaving"); } $connection->disconnect($quitMessage); unset($this->connections[$connection->getAddress()]); return true; } else { unset($this->connections[$connection->getAddress()]); return true; //These statements are kept for reasons of backwards-compatibility } }
/** * @param Plugin $plugin * @return bool */ public function unloadPlugin(Plugin $plugin) : bool { if ($this->hasPlugin($plugin->name)) { $ev = new PluginUnloadEvent($plugin); $this->getConnection()->getEventHandler()->callEvent($ev); if (!$ev->isCancelled()) { Logger::info(BashColor::RED . "Unloading plugin " . BashColor::BLUE . $plugin->name); $plugin->unload(); unset($this->plugins[$plugin->name]); $this->getConnection()->getCommandMap()->unregisterPlugin($plugin); $this->getConnection()->getEventHandler()->unregisterPlugin($plugin); $this->getConnection()->getScheduler()->cancelPluginTasks($plugin->name); unset($plugin); return true; } } return false; }