diff --git a/include/advertiser.h b/include/advertiser.h index bdd834c..62ab1f6 100644 --- a/include/advertiser.h +++ b/include/advertiser.h @@ -89,6 +89,14 @@ class Advertiser : public QObject { */ void socketDisconnected(); + /** + * @brief Handles updating the advertiser and recontacting the master server. + * + * @param p_name The new server name. + * @param p_desc The new server description. + */ + void reloadRequested(QString p_name, QString p_desc); + private: /** * @copydoc ConfigManager::server_settings::ms_ip diff --git a/include/aoclient.h b/include/aoclient.h index e513b1b..41f8562 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -1444,6 +1444,15 @@ class AOClient : public QObject { */ void cmd8Ball(int argc, QStringList argv); + /** + * @brief Reloads all server configuration files. + * + * @details No arguments. + * + * @iscommand + */ + void cmdReload(int argc, QStringList argv); + /** * @brief Sends an out-of-character message with the judgelog of an area. * @@ -1695,6 +1704,7 @@ class AOClient : public QObject { {"pause", {ACLFlags.value("CM"), 0, &AOClient::cmdPauseTestimony}}, {"delete", {ACLFlags.value("CM"), 0, &AOClient::cmdDeleteStatement}}, {"update", {ACLFlags.value("CM"), 0, &AOClient::cmdUpdateStatement}}, + {"reload", {ACLFlags.value("SUPER"), 0, &AOClient::cmdReload}}, }; /** diff --git a/include/area_data.h b/include/area_data.h index 4dceb90..5cab8ef 100644 --- a/include/area_data.h +++ b/include/area_data.h @@ -339,6 +339,11 @@ class AreaData : public QObject { * @brief The last IC packet sent in an area. */ QStringList last_ic_message; + + /** + * @brief The value of logger in config.ini. + */ + QString log_type; }; #endif // AREA_DATA_H diff --git a/include/config_manager.h b/include/config_manager.h index 8c95af3..c5405c2 100644 --- a/include/config_manager.h +++ b/include/config_manager.h @@ -93,6 +93,13 @@ class ConfigManager { * @return See brief description. */ bool fileExists(QFileInfo *file); + + /** + * @brief Verifies the existence of the command configuration files found in config/text/. + * + * @return True if the config files exist, and are files. False otherwise. + */ + bool verifyCommandConfig(); }; #endif // CONFIG_MANAGER_H diff --git a/include/server.h b/include/server.h index 225c5fc..97f49db 100644 --- a/include/server.h +++ b/include/server.h @@ -125,24 +125,6 @@ class Server : public QObject { */ void broadcast(AOPacket packet); - /** - * @brief Returns the server's name according to the configuration file. - * - * @return See brief description. - */ - QString getServerName(); - - /** - * @brief Returns some value regarding the @ref AOClient::diceThrower "dice thrower commands". - * - * @param value_type `max_value` for the maximum amount of faces a die may have, - * `max_dice` for the maximum amount of dice that may be thrown at once. - * - * @return The associated value if it is found in the configuration file under the "Dice" section, - * or `100` if not. - */ - int getDiceValue(QString value_type); - /** * @brief Returns the character's character ID (= their index in the character list). * @@ -199,13 +181,20 @@ class Server : public QObject { */ DBManager* db_manager; + /** + * @brief The max amount of players on the server. + */ + QString max_players; /** * @brief The user-facing server name. - * - * @note Unused. getServerName() serves its purpose instead. */ QString server_name; + /** + * @brief The server description. + */ + QString server_desc; + /** * @brief The Message Of The Day of the server, shown upon entry to the server and on request. */ @@ -216,11 +205,70 @@ class Server : public QObject { */ int maximum_statements; + /** + * @brief The authorization type of the server. + * + * @details In simple mode, the modpass stored in config.ini is used for moderator logins. In advanced mode, logins found in the database are used. + */ + QString auth_type; + + /** + * @brief The modpass for moderator login with simple auth_type. + */ + QString modpass; + + /** + * @brief The amount of subscripts zalgo is stripped by. + */ + int zalgo_tolerance; + + /** + * @brief The highest value dice can have. + */ + uint dice_value; + + /** + * @brief The max amount of dice that can be rolled at once. + */ + int max_dice; + /** * @brief The server-wide global timer. */ QTimer* timer; + /** + * @brief Loads values from config.ini. + */ + void loadServerConfig(); + + /** + * @brief Loads the configuration files for commands into stringlists. + */ + void loadCommandConfig(); + + /** + * @brief Returns a stringlist with the contents of a .txt file from config/text/. + * + * @param Name of the file to load. + */ + QStringList loadConfigFile(QString filename); + + /** + * @brief List holding the contents of 8ball.txt, used by /8ball. + */ + QStringList magic_8ball_answers; + + /** + * @brief List holding the contents of praise.txt, used by AOClient::getReprimand. + */ + QStringList praise_list; + + /** + * @brief List holding the contents of reprimands.txt, used by AOClient::getReprimand. + */ + QStringList reprimands_list; + public slots: /** * @brief Handles a new connection. @@ -230,6 +278,16 @@ class Server : public QObject { */ void clientConnected(); + signals: + + /** + * @brief Sends the server name and description, emitted by /reload. + * + * @param p_name The server name. + * @param p_desc The server description. + */ + void reloadRequest(QString p_name, QString p_desc); + private: /** * @brief The proxy used for WebSocket connections. diff --git a/src/advertiser.cpp b/src/advertiser.cpp index 5921b53..5bc8aec 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -64,6 +64,13 @@ void Advertiser::socketDisconnected() qDebug("Connection to master server lost"); } +void Advertiser::reloadRequested(QString p_name, QString p_desc) +{ + name = p_name; + description = p_desc; + socketConnected(); +} + Advertiser::~Advertiser() { socket->deleteLater(); diff --git a/src/aoclient.cpp b/src/aoclient.cpp index d9599b5..762d99f 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -276,17 +276,17 @@ void AOClient::calculateIpid() void AOClient::sendServerMessage(QString message) { - sendPacket("CT", {server->getServerName(), message, "1"}); + sendPacket("CT", {server->server_name, message, "1"}); } void AOClient::sendServerMessageArea(QString message) { - server->broadcast(AOPacket("CT", {server->getServerName(), message, "1"}), current_area); + server->broadcast(AOPacket("CT", {server->server_name, message, "1"}), current_area); } void AOClient::sendServerBroadcast(QString message) { - server->broadcast(AOPacket("CT", {server->getServerName(), message, "1"})); + server->broadcast(AOPacket("CT", {server->server_name, message, "1"})); } bool AOClient::checkAuth(unsigned long long acl_mask) @@ -300,14 +300,11 @@ bool AOClient::checkAuth(unsigned long long acl_mask) else if (!authenticated) { return false; } - QSettings settings("config/config.ini", QSettings::IniFormat); - settings.beginGroup("Options"); - QString auth_type = settings.value("auth", "simple").toString(); - if (auth_type == "advanced") { + if (server->auth_type == "advanced") { unsigned long long user_acl = server->db_manager->getACL(moderator_name); return (user_acl & acl_mask) != 0; } - else if (auth_type == "simple") { + else if (server->auth_type == "simple") { return authenticated; } } diff --git a/src/area_data.cpp b/src/area_data.cpp index 6c2fad3..c40271c 100644 --- a/src/area_data.cpp +++ b/src/area_data.cpp @@ -43,6 +43,7 @@ AreaData::AreaData(QString p_name, int p_index) : QSettings config_ini("config/config.ini", QSettings::IniFormat); config_ini.beginGroup("Options"); int log_size = config_ini.value("logbuffer", 50).toInt(); + log_type = config_ini.value("logger","modcall").toString(); config_ini.endGroup(); if (log_size == 0) log_size = 500; diff --git a/src/commands.cpp b/src/commands.cpp index 69ce4d4..19c3dca 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -27,21 +27,16 @@ void AOClient::cmdDefault(int argc, QStringList argv) void AOClient::cmdLogin(int argc, QStringList argv) { - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - QString modpass = config.value("modpass", "default").toString(); - QString auth_type = config.value("auth", "simple").toString(); - if (authenticated) { sendServerMessage("You are already logged in!"); return; } - if (auth_type == "simple") { - if (modpass == "") { + if (server->auth_type == "simple") { + if (server->modpass == "") { sendServerMessage("No modpass is set! Please set a modpass before authenticating."); } - else if(argv[0] == modpass) { + else if(argv[0] == server->modpass) { sendPacket("AUTH", {"1"}); // Client: "You were granted the Disable Modcalls button." sendServerMessage("Logged in as a moderator."); // pre-2.9.1 clients are hardcoded to display the mod UI when this string is sent in OOC authenticated = true; @@ -52,7 +47,7 @@ void AOClient::cmdLogin(int argc, QStringList argv) } server->areas.value(current_area)->logger->logLogin(this, authenticated, "moderator"); } - else if (auth_type == "advanced") { + else if (server->auth_type == "advanced") { if (argc < 2) { sendServerMessage("You must specify a username and a password"); return; @@ -194,11 +189,7 @@ void AOClient::cmdKick(int argc, QStringList argv) void AOClient::cmdChangeAuth(int argc, QStringList argv) { - QSettings settings("config/config.ini", QSettings::IniFormat); - settings.beginGroup("Options"); - QString auth_type = settings.value("auth", "simple").toString(); - - if (auth_type == "simple") { + if (server->auth_type == "simple") { change_auth_started = true; sendServerMessage("WARNING!\nThis command will change how logging in as a moderator works.\nOnly proceed if you know what you are doing\nUse the command /rootpass to set the password for your root account."); } @@ -214,6 +205,7 @@ void AOClient::cmdSetRootPass(int argc, QStringList argv) QSettings settings("config/config.ini", QSettings::IniFormat); settings.beginGroup("Options"); settings.setValue("auth", "advanced"); + server->auth_type = "advanced"; #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) qsrand(QDateTime::currentMSecsSinceEpoch()); @@ -835,14 +827,11 @@ void AOClient::cmdToggleGlobal(int argc, QStringList argv) void AOClient::cmdMods(int argc, QStringList argv) { QStringList entries; - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - QString auth_type = config.value("auth", "simple").toString(); int online_count = 0; for (AOClient* client : server->clients) { if (client->authenticated) { entries << "---"; - if (auth_type != "simple") + if (server->auth_type != "simple") entries << "Moderator: " + client->moderator_name; entries << "OOC name: " + client->ooc_name; entries << "ID: " + QString::number(client->id); @@ -1256,35 +1245,17 @@ void AOClient::cmdNoteCardReveal(int argc, QStringList argv) void AOClient::cmd8Ball(int argc, QStringList argv) { - QFileInfo magic8ball_info("config/text/8ball.txt"); - if (!(magic8ball_info.exists() && magic8ball_info.isFile())) { - qWarning() << "8ball.txt doesn't exist!"; - sendServerMessage("8ball.txt doesn't exist."); - } + if (server->magic_8ball_answers.isEmpty()) { + qWarning() << "8ball.txt is empty!"; + sendServerMessage("8ball.txt is empty."); + } else { - QStringList answers; - QFile file("config/text/8ball.txt"); - file.open(QIODevice::ReadOnly | QIODevice::Text); - while (!file.atEnd()) { - answers.append(file.readLine().trimmed()); + QString response = server->magic_8ball_answers[(genRand(1, server->magic_8ball_answers.size() - 1))]; + QString sender_name = ooc_name; + QString sender_message = argv.join(" "); + + sendServerMessageArea(sender_name + " asked the magic 8-ball, \"" + sender_message + "\" and the answer is: " + response); } - file.close(); - - if (answers.isEmpty()) { - qWarning() << "8ball.txt is empty!"; - sendServerMessage("8ball.txt is empty."); - } - else { - int answerindex = answers.size(); - QString response = answers[(genRand(1, answerindex))]; - QString sender_name = ooc_name; - QString sender_message = argv.join(" "); - - sendServerMessageArea(sender_name + " asked the magic 8-ball, \"" + sender_message + "\" and the answer is: " + response); - } - - } - } void AOClient::cmdJudgeLog(int argc, QStringList argv) @@ -1398,7 +1369,6 @@ void AOClient::cmdDeleteStatement(int argc, QStringList argv) area->testimony.remove(c_statement); sendServerMessage("The statement with id " + QString::number(c_statement) + " has been deleted from the testimony."); } - } void AOClient::cmdUpdateStatement(int argc, QStringList argv) @@ -1424,6 +1394,14 @@ void AOClient::cmdAddStatement(int argc, QStringList argv) sendServerMessage("Unable to add anymore statements. Please remove any unused ones."); } +void AOClient::cmdReload(int argc, QStringList argv) +{ + server->loadServerConfig(); + server->loadCommandConfig(); + emit server->reloadRequest(server->server_name, server->server_desc); + sendServerMessage("Reloaded configurations"); +} + QStringList AOClient::buildAreaList(int area_idx) { QStringList entries; @@ -1473,8 +1451,8 @@ int AOClient::genRand(int min, int max) void AOClient::diceThrower(int argc, QStringList argv, RollType type) { QString sender_name = ooc_name; - int max_value = server->getDiceValue("max_value"); - int max_dice = server->getDiceValue("max_dice"); + int max_value = server->dice_value; + int max_dice = server->max_dice; int bounded_value; int bounded_amount; QString dice_results; @@ -1596,18 +1574,10 @@ long long AOClient::parseTime(QString input) QString AOClient::getReprimand(bool positive) { - QString filename = positive ? "praise" : "reprimands"; - QFileInfo reprimands_info("config/text/" + filename + ".txt"); - if (!(reprimands_info.exists() && reprimands_info.isFile())) { - qWarning() << filename + ".txt doesn't exist!"; - return ""; - } - QStringList reprimands; - QFile file("config/text/" + filename + ".txt"); - file.open(QIODevice::ReadOnly | QIODevice::Text); - while (!file.atEnd()) { - reprimands.append(file.readLine().trimmed()); - } - file.close(); - return reprimands[genRand(0, reprimands.size() - 1)]; + if (positive) { + return server->praise_list[genRand(0, server->praise_list.size() - 1)]; + } + else { + return server->reprimands_list[genRand(0, server->reprimands_list.size() - 1)]; + } } diff --git a/src/config_manager.cpp b/src/config_manager.cpp index d6893a8..016991e 100644 --- a/src/config_manager.cpp +++ b/src/config_manager.cpp @@ -91,6 +91,9 @@ bool ConfigManager::initConfig() qCritical() << "config.ini is invalid!"; return false; } + if (!(verifyCommandConfig())) { + return false; + } else { // Config is valid and up to date, so let's go ahead @@ -176,3 +179,16 @@ bool ConfigManager::fileExists(QFileInfo* file) { return (file->exists() && file->isFile()); } + +bool ConfigManager::verifyCommandConfig() +{ + QStringList filelist = {"8ball", "praise", "reprimands"}; + foreach (QString filename, filelist) { + QFileInfo file("config/text/" + filename + ".txt"); + if (!(fileExists(&file))) { + qCritical() << (filename + ".txt doesn't exist!"); + return false; + } + } + return true; +} diff --git a/src/logger.cpp b/src/logger.cpp index 4dca853..96ea2c6 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -77,13 +77,10 @@ QString Logger::buildEntry(AOClient *client, QString type, QString message) void Logger::addEntry(QString entry) { - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - QString log_type = config.value("logging", "modcall").toString(); if (buffer.length() < max_length) { buffer.enqueue(entry); - if (log_type == "full") { - flush(); + if (area->log_type == "full") { + flush(); } } else { @@ -99,20 +96,20 @@ void Logger::flush() dir.mkpath("."); } - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - QString log_type = config.value("logging", "modcall").toString(); QFile logfile; - if (log_type == "modcall") { + if (area->log_type == "modcall") { logfile.setFileName(QString("logs/report_%1_%2.log").arg((area->name), (QDateTime::currentDateTime().toString("yyyy-MM-dd_hhmmss")))); } - else if (log_type == "full") { + else if (area->log_type == "full") { logfile.setFileName(QString("logs/%1.log").arg(QDate::currentDate().toString("yyyy-MM-dd"))); } - if (logfile.open(QIODevice::WriteOnly | QIODevice::Append)) { - QTextStream file_stream(&logfile); - while (!buffer.isEmpty()) - file_stream << buffer.dequeue(); + else { + qCritical("Invalid logger set!"); + } + if (logfile.open(QIODevice::WriteOnly | QIODevice::Append)) { + QTextStream file_stream(&logfile); + while (!buffer.isEmpty()) + file_stream << buffer.dequeue(); } logfile.close(); } diff --git a/src/main.cpp b/src/main.cpp index defcf23..8579592 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,7 @@ int main(int argc, char* argv[]) } server = new Server(settings.port, settings.ws_port); + QObject::connect(server, &Server::reloadRequest, advertiser, &Advertiser::reloadRequested); server->start(); } } else { diff --git a/src/packets.cpp b/src/packets.cpp index 8bafbb8..c811568 100644 --- a/src/packets.cpp +++ b/src/packets.cpp @@ -37,10 +37,7 @@ void AOClient::pktHardwareId(AreaData* area, int argc, QStringList argv, AOPacke void AOClient::pktSoftwareId(AreaData* area, int argc, QStringList argv, AOPacket packet) { - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - QString max_players = config.value("max_players").toString(); - config.endGroup(); + // Full feature list as of AO 2.8.5 // The only ones that are critical to ensuring the server works are @@ -64,7 +61,7 @@ void AOClient::pktSoftwareId(AreaData* area, int argc, QStringList argv, AOPacke version.minor = match.captured(3).toInt(); } - sendPacket("PN", {QString::number(server->player_count), max_players}); + sendPacket("PN", {QString::number(server->player_count), server->max_players}); sendPacket("FL", feature_list); } @@ -181,7 +178,7 @@ void AOClient::pktOocChat(AreaData* area, int argc, QStringList argv, AOPacket p } ooc_name = dezalgo(argv[0]).replace(QRegExp("\\[|\\]|\\{|\\}|\\#|\\$|\\%|\\&"), ""); // no fucky wucky shit here - if (ooc_name.isEmpty() || ooc_name == server->getServerName()) // impersonation & empty name protection + if (ooc_name.isEmpty() || ooc_name == server->server_name) // impersonation & empty name protection return; QString message = dezalgo(argv[1]); @@ -668,14 +665,7 @@ AOPacket AOClient::validateIcPacket(AOPacket packet) QString AOClient::dezalgo(QString p_text) { - QSettings config("config/config.ini", QSettings::IniFormat); - config.beginGroup("Options"); - bool zalgo_tolerance_conversion_success; - int zalgo_tolerance = config.value("zalgo_tolerance", "3").toInt(&zalgo_tolerance_conversion_success); - if (!zalgo_tolerance_conversion_success) - zalgo_tolerance = 3; - - QRegExp rxp("([\u0300-\u036f\u1ab0-\u1aff\u1dc0-\u1dff\u20d0-\u20ff\ufe20-\ufe2f\u115f\u1160\u3164]{" + QRegExp::escape(QString::number(zalgo_tolerance)) + ",})"); + QRegExp rxp("([\u0300-\u036f\u1ab0-\u1aff\u1dc0-\u1dff\u20d0-\u20ff\ufe20-\ufe2f\u115f\u1160\u3164]{" + QRegExp::escape(QString::number(server->zalgo_tolerance)) + ",})"); QString filtered = p_text.replace(rxp, ""); return filtered; } diff --git a/src/server.cpp b/src/server.cpp index 7d7b1b2..7f57164 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -51,7 +51,8 @@ void Server::start() qDebug() << "Server listening on" << port; } - MOTD = config.value("motd","MOTD is not set.").toString(); + loadServerConfig(); + loadCommandConfig(); maximum_statements = config.value("maximum_statements", 50).toInt(); @@ -170,24 +171,6 @@ void Server::broadcast(AOPacket packet) } } -QString Server::getServerName() -{ - QSettings settings("config/config.ini", QSettings::IniFormat); - settings.beginGroup("Options"); - QString server_name = settings.value("server_name", "Akashi").toString(); - return server_name; -} - -int Server::getDiceValue(QString value_type) -{ - QSettings settings("config/config.ini", QSettings::IniFormat); - - settings.beginGroup("Dice"); - int value = settings.value(value_type, "100").toUInt(); - settings.endGroup(); - return value; -} - QList Server::getClientsByIpid(QString ipid) { QList return_clients; @@ -217,6 +200,49 @@ int Server::getCharID(QString char_name) return -1; // character does not exist } +void Server::loadCommandConfig() +{ + magic_8ball_answers = (loadConfigFile("8ball")); + praise_list = (loadConfigFile("praise")); + reprimands_list = (loadConfigFile("reprimands")); +} + +QStringList Server::loadConfigFile(QString filename) +{ + QStringList stringlist; + QFile file("config/text/" + filename + ".txt"); + file.open(QIODevice::ReadOnly | QIODevice::Text); + while (!(file.atEnd())) { + stringlist.append(file.readLine().trimmed()); + } + file.close(); + return stringlist; +} + +void Server::loadServerConfig() +{ + QSettings config("config/config.ini", QSettings::IniFormat); + config.beginGroup("Options"); + //Load config.ini values + max_players = config.value("max_players","100").toString(); + server_name = config.value("server_name","An Unnamed Server").toString(); + server_desc = config.value("server_description","This is a placeholder server description. Tell the world of AO who you are here!").toString(); + MOTD = config.value("motd","MOTD is not set.").toString(); + auth_type = config.value("auth","simple").toString(); + modpass = config.value("modpass","").toString(); + bool zalgo_tolerance_conversion_success; + zalgo_tolerance = config.value("zalgo_tolerance", "3").toInt(&zalgo_tolerance_conversion_success); + if (!zalgo_tolerance_conversion_success) + zalgo_tolerance = 3; + config.endGroup(); + + //Load dice values + config.beginGroup("Dice"); + dice_value = config.value("value_type", "100").toInt(); + max_dice = config.value("max_dice","100").toInt(); + config.endGroup(); +} + Server::~Server() { for (AOClient* client : clients) {