From 1965b068686df1a2dc2061b9613992b1ec0da30b Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 12 Apr 2023 00:26:47 +0200 Subject: [PATCH] Configuration file readout specification of the format in the spec --- spec/spec.md | 19 ++++++ src/database.cpp | 6 ++ src/default-cavecomm.conf | 27 ++++++++ src/main.cpp | 24 ++++++- src/utilities.cpp | 139 +++++++++++++++++++++++++++++++------- 5 files changed, 186 insertions(+), 29 deletions(-) create mode 100644 src/default-cavecomm.conf diff --git a/spec/spec.md b/spec/spec.md index 73a5263..5dd5e23 100644 --- a/spec/spec.md +++ b/spec/spec.md @@ -72,6 +72,7 @@ $completed - marker to set if the request has been completed ## email delivery email is sent via email server given by the admin, while the software functions as a client to it. +https://hackernoon.com/how-to-implement-smtp-client-on-c # Interface @@ -206,3 +207,21 @@ Monetary payouts will be handled through the payment gateways provided. For now, This means that, in the case of BTCPayServer, the [payout system](https://docs.btcpayserver.org/Payouts/) should be used. In the case of Stripe, the transaction should use the [multiparty payout system](https://stripe.com/docs/connect/add-and-pay-out-guide). Both will require a central, authoritative set of accounts respectively to shuffle money between client and freelancer. This authoritative account, by default, should give 97% of a commission's value to the freelancer, with the 3% being reserved for itself as a service fee. This ratio should be able to be edited by the system's administrator. + ++ configuration file can be given as a parameter at startup else it is "cavecomm.conf". + +must start with ++configstart+ +and end with ++configend+ + +comment + + +mandatory: + emailAddress= + emailPassword= + emailServerAddress= + +optional:{default} + emailServerPort={587} + emailAddressDisplay={Cavecomm Automated Management System} diff --git a/src/database.cpp b/src/database.cpp index 6662f76..90d9d5d 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -100,6 +100,9 @@ namespace Database { bool onDeliverPaid = false; bool completed = false; + /* + * outputs request item in a JSON String + */ std::string toJSONString(){ std::string outputString = "{\"requestItem:\":{"; outputString += "\"id\": " + std::to_string(id) + ","; @@ -122,6 +125,9 @@ namespace Database { return outputString; } + /* + * prints request item into standard out + */ void outputItem(){ std::cout << "id = " << id << std::endl; std::cout << "customerName = " << customerName << std::endl; diff --git a/src/default-cavecomm.conf b/src/default-cavecomm.conf new file mode 100644 index 0000000..129ca5b --- /dev/null +++ b/src/default-cavecomm.conf @@ -0,0 +1,27 @@ +#configstart# + +# Mandatory: +# emailAddress= +# emailPassword= +# emailServerAddress= + +# Optional: {default} +# emailServerPort={587} +# emailAddressDisplay={Cavecomm Automated Management System} + + + + + + +emailAddress=testuser0ed6e@waifu.club + +emailPassword=Ve4m6GSjJ + +emailServerAddress=mail.cock.li + +emailServerPort=587 + +emailAddressDisplay=Cavecomm Automated Management System + +#configend# diff --git a/src/main.cpp b/src/main.cpp index fa1dd0d..2c8e3ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,9 +8,16 @@ using namespace std; -int main() { +int main(int argc, char *argv[]) { Utilities::config configuration; - configuration.readConfigFile(); + if (argc > 0) + configuration.configPath = argv[1]; + + if (configuration.readConfigFile()) { + std::cout << "ERROR: Unable to read configuration file: " << configuration.configPath << endl; + std::cout << "Aborting Startup!" << endl; + return 1; + } crow::SimpleApp app; @@ -126,6 +133,17 @@ int main() { auto page = crow::mustache::load(templateHTML); crow::mustache::context ctx(resultJsonTemplate); + + bool commissionState = false; + if (resultTemplate.size() > 0) { + //use freelancerID based on SQL_STATEMENT_SELECT_TEMPLATE + pqxx::result resultCommissionState = Database::executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(databaseConnection, stoi(resultTemplate.at(0).at(2).c_str())); + //the commissionstate + if (resultCommissionState.size() == 0 || stoi(resultCommissionState.at(0).at(0).c_str()) == 1) + commissionState = true; + } + ctx["ERROR_COMMISSIONS_CLOSED"] = commissionState; + return page.render(ctx); }); @@ -202,7 +220,7 @@ int main() { pqxx::result resultCommissionState = Database::executePreparedStatement_SELECT_FREELANCER_COMMISSION_STATE(databaseConnection, newRequest.freelancerID); //the commissionstate - if (resultCommissionState.size() == 0 || stoi(resultTemplate.at(0).at(0).c_str()) == 1) { + if (resultCommissionState.size() == 0 || stoi(resultCommissionState.at(0).at(0).c_str()) == 1) { ctx["ERROR_COMMISSIONS_CLOSED"] = true; } else { diff --git a/src/utilities.cpp b/src/utilities.cpp index 32c1d2b..ab5fef9 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -19,32 +22,6 @@ using namespace jed_utils::cpp; namespace Utilities { const static std::map HTML_URL_CODES = {{"%20", " "}, {"%21", "!"}, {"%22", "\""}, {"%23", "#"}, {"%24", "$"}, {"%25", "%"}, {"%26", "&"}, {"%27", "'"}, {"%28", "("}, {"%29", ")"}, {"%2A", "*"}, {"%2B", "+"}, {"%2C", ","}, {"%2D", "-"}, {"%2E", "."}, {"%2F", "/"}, {"%30", "0"}, {"%31", "1"}, {"%32", "2"}, {"%33", "3"}, {"%34", "4"}, {"%35", "5"}, {"%36", "6"}, {"%37", "7"}, {"%38", "8"}, {"%39", "9"}, {"%3A", ":"}, {"%3B", ";"}, {"%3C", "<"}, {"%3D", "="}, {"%3E", ">"}, {"%3F", "?"}, {"%40", "@"}, {"%5B", "["}, {"%5C", "\\"}, {"%5D", "]"}, {"%5E", "^"}, {"%5F", "_"}, {"%60", "`"}, {"%7B", "{"}, {"%7C", "|"}, {"%7D", "}"}, {"%7E", "~"}, {"%7F", " "}, {"%80", "€"}, {"%82", "‚"}, {"%83", "ƒ"}, {"%84", "„"}, {"%85", "…"}, {"%86", "†"}, {"%87", "‡"}, {"%88", "ˆ"}, {"%89", "‰"}, {"%8A", "Š"}, {"%8B", "‹"}, {"%8C", "Œ"}, {"%8E", "Ž"}, {"%91", "‘"}, {"%92", "’"}, {"%93", "“"}, {"%94", "”"}, {"%95", "•"}, {"%96", "–"}, {"%97", "—"}, {"%98", "˜"}, {"%99", "™"}, {"%9A", "š"}, {"%9B", "›"}, {"%9C", "œ"}, {"%9E", "ž"}, {"%9F", "Ÿ"}, {"%A1", "¡"}, {"%A2", "¢"}, {"%A3", "£"}, {"%A4", "¤"}, {"%A5", "¥"}, {"%A6", "¦"}, {"%A7", "§"}, {"%A8", "¨"}, {"%A9", "©"}, {"%AA", "ª"}, {"%AB", "«"}, {"%AC", "¬"}, {"%AE", "®"}, {"%AF", "¯"}, {"%B0", "°"}, {"%B1", "±"}, {"%B2", "²"}, {"%B3", "³"}, {"%B4", "´"}, {"%B5", "µ"}, {"%B6", "¶"}, {"%B7", "·"}, {"%B8", "¸"}, {"%B9", "¹"}, {"%BA", "º"}, {"%BB", "»"}, {"%BC", "¼"}, {"%BD", "½"}, {"%BE", "¾"}, {"%BF", "¿"}, {"%C0", "À"}, {"%C1", "Á"}, {"%C2", "Â"}, {"%C3", "Ã"}, {"%C4", "Ä"}, {"%C5", "Å"}, {"%C6", "Æ"}, {"%C7", "Ç"}, {"%C8", "È"}, {"%C9", "É"}, {"%CA", "Ê"}, {"%CB", "Ë"}, {"%CC", "Ì"}, {"%CD", "Í"}, {"%CE", "Î"}, {"%CF", "Ï"}, {"%D0", "Ð"}, {"%D1", "Ñ"}, {"%D2", "Ò"}, {"%D3", "Ó"}, {"%D4", "Ô"}, {"%D5", "Õ"}, {"%D6", "Ö"}, {"%D7", "×"}, {"%D8", "Ø"}, {"%D9", "Ù"}, {"%DA", "Ú"}, {"%DB", "Û"}, {"%DC", "Ü"}, {"%DD", "Ý"}, {"%DE", "Þ"}, {"%DF", "ß"}, {"%E0", "à"}, {"%E1", "á"}, {"%E2", "â"}, {"%E3", "ã"}, {"%E4", "ä"}, {"%E5", "å"}, {"%E6", "æ"}, {"%E7", "ç"}, {"%E8", "è"}, {"%E9", "é"}, {"%EA", "ê"}, {"%EB", "ë"}, {"%EC", "ì"}, {"%ED", "í"}, {"%EE", "î"}, {"%EF", "ï"}, {"%F0", "ð"}, {"%F1", "ñ"}, {"%F2", "ò"}, {"%F3", "ó"}, {"%F4", "ô"}, {"%F5", "õ"}, {"%F6", "ö"}, {"%F7", "÷"}, {"%F8", "ø"}, {"%F9", "ù"}, {"%FA", "ú"}, {"%FB", "û"}, {"%FC", "ü"}, {"%FD", "ý"}, {"%FE", "þ"}, {"%FF", "ÿ"}}; - /* - * Struct representing the configuration file - */ - struct config { - std::string configPath = "cavecomm.conf"; - std::string emailAddress = "testuser0ed6e@waifu.club"; - std::string emailPassword = "Ve4m6GSjJ"; - std::string emailServerAddress = "mail.cock.li"; - int emailServerPort = 587; - std::string emailAddressDisplay = "Cavecomm Automated Management System"; - - /* - * Parses the config file and fills the struct. - */ - void readConfigFile(){ - - } - }; - - /* - * Takes String and cuts from the start-up to the length of valueName - */ - std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName) { - return responseBody.substr(valueName.length() + 1, responseBody.length()); - } - /* * Takes string to split it into a vector based on a given delimiter */ @@ -59,6 +36,116 @@ namespace Utilities { return splitVector; } + /* + * Struct representing the configuration file + */ + struct config { + std::string configPath = "cavecomm.conf"; + std::string emailAddress; + std::string emailPassword; + std::string emailServerAddress; + int emailServerPort = 587; + std::string emailAddressDisplay = "Cavecomm Automated Management System"; + + /* + * validates existence of mandatory variables in config + * returns 0 if successful else 1 + */ + int checkMandatoryVariables() { + if (emailAddress.compare("") == 0 + || emailPassword.compare("") == 0 + || emailServerAddress.compare("") == 0) + return 1; + + return 0; + } + + /* + * Parses the config file and fills the struct. + * returns 0 if successful else 1 + */ + int readConfigFile(){ + bool errorLevel = 0; + std::ifstream configFile(configPath); + std::cout << "Loading Configuration: " << configPath << std::endl; + + if (configFile.good()) { + bool sanityCheckStart = true; + std::string line; + while (std::getline(configFile, line)) + { + if (sanityCheckStart) { + if (line.find("#configstart#") != std::string::npos) { + sanityCheckStart = false; + continue; + } + else { + errorLevel = 1; + std::cout << "ERROR: Config file is invalid" << std::endl; + break; + } + } + else { + if (line.size() <= 1) + continue; + } + + if (line.find("#configend#") != std::string::npos) + break; + + if (line.at(0) == '#') { + std::cout << "COMMENT: "; + std::cout << line << std::endl; + } + else { + std::cout << "CONFIG: "; + + std::vector lineVector = Utilities::splitStringIntoVector(line, '='); + + if (lineVector.size() == 2) { + std::cout << lineVector.at(0) << " - " << lineVector.at(1) << std::endl; + if (lineVector.at(0).compare("emailAddress") == 0) { + emailAddress = lineVector.at(1); + continue; + } + if (lineVector.at(0).compare("emailPassword") == 0) { + emailPassword = lineVector.at(1); + continue; + } + if (lineVector.at(0).compare("emailServerAddress") == 0) { + emailServerAddress = lineVector.at(1); + continue; + } + if (lineVector.at(0).compare("emailServerPort") == 0) { + emailServerPort = std::stoi(lineVector.at(1)); + continue; + } + if (lineVector.at(0).compare("emailAddressDisplay") == 0) { + emailAddressDisplay = lineVector.at(1); + continue; + } + } + } + } + } + else { + errorLevel = 1; + } + + if (errorLevel == 0) + errorLevel = checkMandatoryVariables(); + + return errorLevel; + } + }; + + /* + * Takes String and cuts from the start-up to the length of valueName + */ + std::string extractSingleValueFromRequestBody(std::string responseBody, std::string valueName) { + return responseBody.substr(valueName.length() + 1, responseBody.length()); + } + /* * replaces a string with another string within a string */