diff --git a/.gitignore b/.gitignore index 0fd4ad8..63e19b3 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ building/*.svg .idea/ compile_commands.json local.sh + +iu9-ca-web-chat.db diff --git a/building/main.cpp b/building/main.cpp index 48ba626..82d3a6f 100644 --- a/building/main.cpp +++ b/building/main.cpp @@ -145,6 +145,8 @@ struct CAWebChat { "main.cpp", "initialize.cpp", "run.cpp", + "str_fields_check.cpp", + "find_db.cpp", }; for (std::string& u: T.units) u = "web_chat/" + u; diff --git a/src/web_chat/find_db.cpp b/src/web_chat/find_db.cpp new file mode 100644 index 0000000..5df436f --- /dev/null +++ b/src/web_chat/find_db.cpp @@ -0,0 +1,14 @@ +#include "find_db.h" + +int find_db_sqlite_file_path(const json::JSON& config, std::string& res_path) { + const json::JSON& type = config["database"]["type"].g(); + if (!type.isString() && type.asString() == "sqlite3") + return -1; + const json::JSON& path = config["database"]["file"].g(); + if (!path.isString()) + return -1; + if (path.asString().empty() || path.asString()[0] == ':') + return -1; + res_path = path.asString(); + return 0; +} \ No newline at end of file diff --git a/src/web_chat/find_db.h b/src/web_chat/find_db.h new file mode 100644 index 0000000..99bf2a7 --- /dev/null +++ b/src/web_chat/find_db.h @@ -0,0 +1,8 @@ +#ifndef IU9_CA_WEB_CHAT_SRC_WEB_CHAT_FIND_DB_H +#define IU9_CA_WEB_CHAT_SRC_WEB_CHAT_FIND_DB_H + +#include + +int find_db_sqlite_file_path(const json::JSON& config, std::string& res_path); + +#endif diff --git a/src/web_chat/initialize.cpp b/src/web_chat/initialize.cpp index a14406a..e814216 100644 --- a/src/web_chat/initialize.cpp +++ b/src/web_chat/initialize.cpp @@ -1,5 +1,40 @@ #include "actions.h" +#include +#include "str_fields_check.h" +#include +#include +#include + +void sqlite_single_statement(sqlite3* db_hand_ptr, const std::string& req_statement) { + sqlite3_stmt* stmt_obj_ptr = NULL; + int ret = sqlite3_prepare16_v2(db_hand_ptr, req_statement.c_str(), -1, &stmt_obj_ptr, NULL); + een9_ASSERT(ret == 0, "Can't compile request expression"); + struct Guard1{sqlite3_stmt*& r; ~Guard1(){if (sqlite3_finalize(r) != 0) {abort();}}} guard1{stmt_obj_ptr}; + while (true) { + ret = sqlite3_step(stmt_obj_ptr); + if (ret == SQLITE_DONE) + break; + if (ret != SQLITE_ROW) { + printf("sqlite_row error!!!\n"); + break; + } + + } + printf("Request steps are done\n"); +} void initialize_website(const json::JSON& config, const std::string& root_pw) { - + printf("Initialization...\n"); + een9_ASSERT(check_password(root_pw), "Bad root password"); + std::string db_path; + int ret; + ret = find_db_sqlite_file_path(config, db_path); + een9_ASSERT(ret == 0, "Invalid settings[\"database\"] field"); + een9_ASSERT(!een9::isRegularFile(db_path), "Database file exists prior to initialization. " + "Can't preceed withut harming existing data"); + sqlite3* db_hand_ptr = NULL; + ret = sqlite3_open(db_path.c_str(), &db_hand_ptr); + een9_ASSERT(ret == 0, "Can't open database"); + struct Guard1{sqlite3*& dhp; ~Guard1(){if (sqlite3_close(dhp) != 0) {abort();}}} guard1{db_hand_ptr}; + sqlite_single_statement(db_hand_ptr, "CREATE TABLE tb(a INT, b INT);"); } \ No newline at end of file diff --git a/src/web_chat/main.cpp b/src/web_chat/main.cpp index 8d90e25..1cfa7e5 100644 --- a/src/web_chat/main.cpp +++ b/src/web_chat/main.cpp @@ -3,6 +3,7 @@ #include #include #include "actions.h" +#include void usage(char** argv) { printf("Usage: %s \n", argv[0]); diff --git a/src/web_chat/str_fields_check.cpp b/src/web_chat/str_fields_check.cpp new file mode 100644 index 0000000..8da27ed --- /dev/null +++ b/src/web_chat/str_fields_check.cpp @@ -0,0 +1,37 @@ +#include "str_fields_check.h" +#include + +bool isALPHA(char ch) { + return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'); +} + +bool isNUM(char ch) { + return '0' <= ch && ch <= '9'; +} + +bool isUNCHAR(char ch) { + return isALPHA(ch) || isNUM(ch) || ch == '-' || ch == '_'; +} + +bool isSPACE(char ch) { + return ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n'; +} + + +bool check_password(const std::string &pwd) { + return isUtf8String(pwd) && pwd.size() >= 8; +} + +bool check_name(const std::string &name) { + return isUtf8String(name); +} + +bool check_nickname(const std::string &nickname) { + if (nickname.empty()) + return false; + for (char ch: nickname) { + if (!isUNCHAR(ch)) + return false; + } + return true; +} diff --git a/src/web_chat/str_fields_check.h b/src/web_chat/str_fields_check.h new file mode 100644 index 0000000..1875142 --- /dev/null +++ b/src/web_chat/str_fields_check.h @@ -0,0 +1,10 @@ +#ifndef IU9_CA_WEB_CHAT_SRC_WEB_CHAT_STR_FIELDS_CHECK_H +#define IU9_CA_WEB_CHAT_SRC_WEB_CHAT_STR_FIELDS_CHECK_H + +#include + +bool check_password(const std::string& pwd); +bool check_name(const std::string& name); +bool check_nickname(const std::string& nickname); + +#endif