Fix /bans and /baninfo

* Adds moderator to /bans and /baninfo
* Fixes /bans and /baninfo returning incorrect information
* Changes syntax of /ban to: `/ban <ipid> <duration> <reason>`
This commit is contained in:
MangosArentLiterature 2021-05-07 20:43:31 -05:00
parent c54f70dfdf
commit 72d073e6df
3 changed files with 38 additions and 39 deletions

View File

@ -129,6 +129,7 @@ public:
QString reason; //!< The reason given for the ban by the moderator who registered it. QString reason; //!< The reason given for the ban by the moderator who registered it.
long long duration; //!< The duration of the ban, in seconds. long long duration; //!< The duration of the ban, in seconds.
int id; //!< The unique ID of the ban. int id; //!< The unique ID of the ban.
QString moderator; //!< The moderator who issued the ban.
}; };
/** /**

View File

@ -22,38 +22,24 @@
void AOClient::cmdBan(int argc, QStringList argv) void AOClient::cmdBan(int argc, QStringList argv)
{ {
QString args_str = argv[1]; QString args_str = argv[2];
if (argc > 2) { if (argc > 3) {
for (int i = 2; i < argc; i++) for (int i = 3; i < argc; i++)
args_str += " " + argv[i]; args_str += " " + argv[i];
} }
DBManager::BanInfo ban; DBManager::BanInfo ban;
QRegularExpression quoteMatcher("['\"](.+?)[\"']"); if (argc < 3) {
QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str); sendServerMessage("Invalid syntax. Usage:\n/ban <ipid> <duration> <reason>");
QList<QString> unquoted_args;
while (matches.hasNext()) {
QRegularExpressionMatch match = matches.next();
unquoted_args.append(match.captured(1));
}
QString duration = "perma";
if (unquoted_args.length() < 1) {
sendServerMessage("Invalid syntax. Usage:\n/ban <ipid> \"<reason>\" \"<duration>\"");
return; return;
} }
ban.reason = unquoted_args.at(0);
if (unquoted_args.length() > 1)
duration = unquoted_args.at(1);
long long duration_seconds = 0; long long duration_seconds = 0;
if (duration == "perma") if (argv[1] == "perma")
duration_seconds = -2; duration_seconds = -2;
else else
duration_seconds = parseTime(duration); duration_seconds = parseTime(argv[1]);
if (duration_seconds == -1) { if (duration_seconds == -1) {
sendServerMessage("Invalid time format. Format example: 1h30m"); sendServerMessage("Invalid time format. Format example: 1h30m");
@ -61,16 +47,17 @@ void AOClient::cmdBan(int argc, QStringList argv)
} }
ban.duration = duration_seconds; ban.duration = duration_seconds;
ban.ipid = argv[0]; ban.ipid = argv[0];
ban.reason = args_str;
ban.time = QDateTime::currentDateTime().toSecsSinceEpoch(); ban.time = QDateTime::currentDateTime().toSecsSinceEpoch();
bool ban_logged = false; bool ban_logged = false;
int kick_counter = 0; int kick_counter = 0;
if (argc > 2) { if (server->auth_type == "advanced") {
for (int i = 2; i < argv.length(); i++) { ban.moderator = moderator_name;
ban.reason += " " + argv[i]; }
} else {
ban.moderator = "moderator";
} }
for (AOClient* client : server->getClientsByIpid(ban.ipid)) { for (AOClient* client : server->getClientsByIpid(ban.ipid)) {
@ -81,7 +68,14 @@ void AOClient::cmdBan(int argc, QStringList argv)
sendServerMessage("Banned user with ipid " + ban.ipid + " for reason: " + ban.reason); sendServerMessage("Banned user with ipid " + ban.ipid + " for reason: " + ban.reason);
ban_logged = true; ban_logged = true;
} }
client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(server->db_manager->getBanID(ban.ip)) + "\nUntil: " + QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("dd.MM.yyyy, hh:mm")}); QString ban_duration;
if (!(ban.duration == -2)) {
ban_duration = QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("MM/dd/yyyy, hh:mm");
}
else {
ban_duration = "The heat death of the universe.";
}
client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(server->db_manager->getBanID(ban.ip)) + "\nUntil: " + ban_duration});
client->socket->close(); client->socket->close();
kick_counter++; kick_counter++;
} }
@ -185,6 +179,7 @@ void AOClient::cmdBans(int argc, QStringList argv)
recent_bans << "Reason for ban: " + ban.reason; recent_bans << "Reason for ban: " + ban.reason;
recent_bans << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("MM/dd/yyyy, hh:mm"); recent_bans << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("MM/dd/yyyy, hh:mm");
recent_bans << "Ban lasts until: " + banned_until; recent_bans << "Ban lasts until: " + banned_until;
recent_bans << "Moderator: " + ban.moderator;
recent_bans << "-----"; recent_bans << "-----";
} }
sendServerMessage(recent_bans.join("\n")); sendServerMessage(recent_bans.join("\n"));
@ -369,12 +364,14 @@ void AOClient::cmdBanInfo(int argc, QStringList argv)
if (ban.duration == -2) if (ban.duration == -2)
banned_until = "The heat death of the universe"; banned_until = "The heat death of the universe";
else else
banned_until = QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("dd.MM.yyyy, hh:mm"); banned_until = QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("MM/dd/yyyy, hh:mm");
ban_info << "Ban ID: " + QString::number(ban.id);
ban_info << "Affected IPID: " + ban.ipid; ban_info << "Affected IPID: " + ban.ipid;
ban_info << "Affected HDID: " + ban.hdid; ban_info << "Affected HDID: " + ban.hdid;
ban_info << "Reason for ban: " + ban.reason; ban_info << "Reason for ban: " + ban.reason;
ban_info << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("dd.MM.yyyy, hh:mm"); ban_info << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("MM/dd/yyyy, hh:mm");
ban_info << "Ban lasts until: " + banned_until; ban_info << "Ban lasts until: " + banned_until;
ban_info << "Moderator: " + ban.moderator;
ban_info << "-----"; ban_info << "-----";
} }
sendServerMessage(ban_info.join("\n")); sendServerMessage(ban_info.join("\n"));

View File

@ -24,9 +24,7 @@ DBManager::DBManager() :
db.setDatabaseName("config/akashi.db"); db.setDatabaseName("config/akashi.db");
if (!db.open()) if (!db.open())
qCritical() << "Database Error:" << db.lastError(); qCritical() << "Database Error:" << db.lastError();
qDebug() << QString::number(DB_VERSION);
db_version = checkVersion(); db_version = checkVersion();
qDebug() << QString::number(db_version);
QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, 'DURATION' INTEGER, 'MODERATOR' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))"); QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, 'DURATION' INTEGER, 'MODERATOR' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))");
QSqlQuery create_user_table("CREATE TABLE IF NOT EXISTS users ('ID' INTEGER, 'USERNAME' TEXT, 'SALT' TEXT, 'PASSWORD' TEXT, 'ACL' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))"); QSqlQuery create_user_table("CREATE TABLE IF NOT EXISTS users ('ID' INTEGER, 'USERNAME' TEXT, 'SALT' TEXT, 'PASSWORD' TEXT, 'ACL' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))");
if (db_version != DB_VERSION) if (db_version != DB_VERSION)
@ -173,6 +171,7 @@ QList<DBManager::BanInfo> DBManager::getRecentBans()
ban.time = static_cast<unsigned long>(query.value(4).toULongLong()); ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
ban.reason = query.value(5).toString(); ban.reason = query.value(5).toString();
ban.duration = query.value(6).toLongLong(); ban.duration = query.value(6).toLongLong();
ban.moderator = query.value(7).toString();
return_list.append(ban); return_list.append(ban);
} }
std::reverse(return_list.begin(), return_list.end()); std::reverse(return_list.begin(), return_list.end());
@ -182,13 +181,14 @@ QList<DBManager::BanInfo> DBManager::getRecentBans()
void DBManager::addBan(BanInfo ban) void DBManager::addBan(BanInfo ban)
{ {
QSqlQuery query; QSqlQuery query;
query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION) VALUES(?, ?, ?, ?, ?, ?)"); query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION, MODERATOR) VALUES(?, ?, ?, ?, ?, ?, ?)");
query.addBindValue(ban.ipid); query.addBindValue(ban.ipid);
query.addBindValue(ban.hdid); query.addBindValue(ban.hdid);
query.addBindValue(ban.ip.toString()); query.addBindValue(ban.ip.toString());
query.addBindValue(QString::number(ban.time)); query.addBindValue(QString::number(ban.time));
query.addBindValue(ban.reason); query.addBindValue(ban.reason);
query.addBindValue(ban.duration); query.addBindValue(ban.duration);
query.addBindValue(ban.moderator);
if (!query.exec()) if (!query.exec())
qDebug() << "SQL Error:" << query.lastError().text(); qDebug() << "SQL Error:" << query.lastError().text();
} }
@ -352,15 +352,16 @@ QList<DBManager::BanInfo> DBManager::getBanInfo(QString lookup_type, QString id)
query.addBindValue(id); query.addBindValue(id);
query.setForwardOnly(true); query.setForwardOnly(true);
query.exec(); query.exec();
while (query.next()) { while (query.next()) {
BanInfo ban; BanInfo ban;
ban.ipid = query.value(0).toString(); ban.id = query.value(0).toInt();
ban.hdid = query.value(1).toString(); ban.ipid = query.value(1).toString();
ban.ip = QHostAddress(query.value(2).toString()); ban.hdid = query.value(2).toString();
ban.time = static_cast<unsigned long>(query.value(3).toULongLong()); ban.ip = QHostAddress(query.value(3).toString());
ban.reason = query.value(4).toString(); ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
ban.duration = query.value(5).toLongLong(); ban.reason = query.value(5).toString();
ban.duration = query.value(6).toLongLong();
ban.moderator = query.value(7).toString();
return_list.append(ban); return_list.append(ban);
} }
std::reverse(return_list.begin(), return_list.end()); std::reverse(return_list.begin(), return_list.end());