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.
long long duration; //!< The duration of the ban, in seconds.
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)
{
QString args_str = argv[1];
if (argc > 2) {
for (int i = 2; i < argc; i++)
QString args_str = argv[2];
if (argc > 3) {
for (int i = 3; i < argc; i++)
args_str += " " + argv[i];
}
DBManager::BanInfo ban;
QRegularExpression quoteMatcher("['\"](.+?)[\"']");
QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str);
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>\"");
if (argc < 3) {
sendServerMessage("Invalid syntax. Usage:\n/ban <ipid> <duration> <reason>");
return;
}
ban.reason = unquoted_args.at(0);
if (unquoted_args.length() > 1)
duration = unquoted_args.at(1);
long long duration_seconds = 0;
if (duration == "perma")
if (argv[1] == "perma")
duration_seconds = -2;
else
duration_seconds = parseTime(duration);
duration_seconds = parseTime(argv[1]);
if (duration_seconds == -1) {
sendServerMessage("Invalid time format. Format example: 1h30m");
@ -61,16 +47,17 @@ void AOClient::cmdBan(int argc, QStringList argv)
}
ban.duration = duration_seconds;
ban.ipid = argv[0];
ban.reason = args_str;
ban.time = QDateTime::currentDateTime().toSecsSinceEpoch();
bool ban_logged = false;
int kick_counter = 0;
if (argc > 2) {
for (int i = 2; i < argv.length(); i++) {
ban.reason += " " + argv[i];
}
if (server->auth_type == "advanced") {
ban.moderator = moderator_name;
}
else {
ban.moderator = "moderator";
}
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);
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();
kick_counter++;
}
@ -185,6 +179,7 @@ void AOClient::cmdBans(int argc, QStringList argv)
recent_bans << "Reason for ban: " + ban.reason;
recent_bans << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("MM/dd/yyyy, hh:mm");
recent_bans << "Ban lasts until: " + banned_until;
recent_bans << "Moderator: " + ban.moderator;
recent_bans << "-----";
}
sendServerMessage(recent_bans.join("\n"));
@ -369,12 +364,14 @@ void AOClient::cmdBanInfo(int argc, QStringList argv)
if (ban.duration == -2)
banned_until = "The heat death of the universe";
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 HDID: " + ban.hdid;
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 << "Moderator: " + ban.moderator;
ban_info << "-----";
}
sendServerMessage(ban_info.join("\n"));

View File

@ -24,9 +24,7 @@ DBManager::DBManager() :
db.setDatabaseName("config/akashi.db");
if (!db.open())
qCritical() << "Database Error:" << db.lastError();
qDebug() << QString::number(DB_VERSION);
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_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)
@ -173,6 +171,7 @@ QList<DBManager::BanInfo> DBManager::getRecentBans()
ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
ban.reason = query.value(5).toString();
ban.duration = query.value(6).toLongLong();
ban.moderator = query.value(7).toString();
return_list.append(ban);
}
std::reverse(return_list.begin(), return_list.end());
@ -182,13 +181,14 @@ QList<DBManager::BanInfo> DBManager::getRecentBans()
void DBManager::addBan(BanInfo ban)
{
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.hdid);
query.addBindValue(ban.ip.toString());
query.addBindValue(QString::number(ban.time));
query.addBindValue(ban.reason);
query.addBindValue(ban.duration);
query.addBindValue(ban.moderator);
if (!query.exec())
qDebug() << "SQL Error:" << query.lastError().text();
}
@ -352,15 +352,16 @@ QList<DBManager::BanInfo> DBManager::getBanInfo(QString lookup_type, QString id)
query.addBindValue(id);
query.setForwardOnly(true);
query.exec();
while (query.next()) {
BanInfo ban;
ban.ipid = query.value(0).toString();
ban.hdid = query.value(1).toString();
ban.ip = QHostAddress(query.value(2).toString());
ban.time = static_cast<unsigned long>(query.value(3).toULongLong());
ban.reason = query.value(4).toString();
ban.duration = query.value(5).toLongLong();
ban.id = query.value(0).toInt();
ban.ipid = query.value(1).toString();
ban.hdid = query.value(2).toString();
ban.ip = QHostAddress(query.value(3).toString());
ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
ban.reason = query.value(5).toString();
ban.duration = query.value(6).toLongLong();
ban.moderator = query.value(7).toString();
return_list.append(ban);
}
std::reverse(return_list.begin(), return_list.end());