From f45099e47b9630b1d762d30521a6b234ff801fa4 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Sat, 13 Mar 2021 05:10:36 -0600 Subject: [PATCH] implement tsu-like time parsing --- include/aoclient.h | 7 +++- src/commands.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index 4ff94c7..0cbf476 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -240,11 +240,15 @@ class AOClient : public QObject { void cmdToggleGlobal(int argc, QStringList argv); void cmdPM(int argc, QStringList argv); + // DELETE THIS LATER MARISA + void cmdTestDateTime(int argc, QStringList argv); + // Command helper functions QString getAreaTimer(int area_idx, QTimer* timer); QStringList buildAreaList(int area_idx); int genRand(int min, int max); void diceThrower(int argc, QStringList argv, RollType Type); + long long parseTime(QString input); // Command function global variables bool change_auth_started = false; @@ -309,7 +313,8 @@ class AOClient : public QObject { {"motd", {ACLFlags.value("NONE"), 0, &AOClient::cmdMOTD}}, {"announce", {ACLFlags.value("ANNOUNCE"), 1, &AOClient::cmdAnnounce}}, {"m", {ACLFlags.value("MODCHAT"), 1, &AOClient::cmdM}}, - {"gm", {ACLFlags.value("MODCHAT"), 1, &AOClient::cmdGM}} + {"gm", {ACLFlags.value("MODCHAT"), 1, &AOClient::cmdGM}}, + {"test", {ACLFlags.value("NONE"), 1, &AOClient::cmdTestDateTime}} }; QString partial_packet; diff --git a/src/commands.cpp b/src/commands.cpp index 04243a9..c0f2d18 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -89,19 +89,44 @@ void AOClient::cmdGetArea(int argc, QStringList argv) void AOClient::cmdBan(int argc, QStringList argv) { + QString args_str = argv[1]; + if (argc > 2) { + for (int i = 2; i < argc; i++) + args_str += " " + argv[i]; + } + + QRegularExpression quoteMatcher("['\"](.+?)[\"']"); + QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str); + QList unquoted_args; + while (matches.hasNext()) { + QRegularExpressionMatch match = matches.next(); + unquoted_args.append(match.captured(1)); + } + + QString reason; + QString duration = "perma"; + + if (unquoted_args.length() < 1) { + sendServerMessage("Invalid syntax. Usage:\n/ban \"\" \"\""); + return; + } + + reason = unquoted_args.at(0); + if (unquoted_args.length() > 1) + duration = unquoted_args.at(1); + + + qDebug() << "Reason: " << reason; + qDebug() << "Duration: " << duration; + return; + QString target_ipid = argv[0]; QHostAddress ip; QString hdid; unsigned long time = QDateTime::currentDateTime().toTime_t(); - QString reason = argv[1]; + //QString reason = argv[1]; bool ban_logged = false; - if (argc > 2) { - for (int i = 2; i < argv.length(); i++) { - reason += " " + argv[i]; - } - } - for (AOClient* client : server->clients) { if (client->getIpid() == target_ipid) { if (!ban_logged) { @@ -1019,3 +1044,53 @@ QString AOClient::getAreaTimer(int area_idx, QTimer* timer) } } +void AOClient::cmdTestDateTime(int argc, QStringList argv) +{ + qDebug() << parseTime(argv[0]); +} + +long long AOClient::parseTime(QString input) +{ + QRegularExpression regex("(?:(?:(?.*?)y)*(?:(?.*?)w)*(?:(?.*?)d)*(?:(?
.*?)h)*(?:(?.*?)m)*(?:(?.*?)s)*)"); + QRegularExpressionMatch match = regex.match(input); + QString str_year, str_week, str_hour, str_day, str_minute, str_second; + int year, week, day, hour, minute, second; + + str_year = match.captured("year"); + str_week = match.captured("week"); + str_day = match.captured("day"); + str_hour = match.captured("hr"); + str_minute = match.captured("min"); + str_second = match.captured("sec"); + + qDebug() << str_year << "years," << str_week << "weeks," << str_day << "days," << str_hour << "hours," << str_minute << "minutes," << str_second << "seconds"; + + bool is_well_formed = false; + QString concat_str(str_year + str_week + str_day + str_hour + str_minute + str_second); + concat_str.toInt(&is_well_formed); + + if (!is_well_formed) { + qDebug() << "retard alert!"; + return -1; + } + + year = str_year.toInt(); + week = str_week.toInt(); + day = str_day.toInt(); + hour = str_hour.toInt(); + minute = str_minute.toInt(); + second = str_second.toInt(); + + long long total = 0; + total += 31622400 * year; + total += 604800 * week; + total += 86400 * day; + total += 3600 * hour; + total += 60 * minute; + total += second; + + if (total < 0) + return -1; + + return total; +}