diff options
author | Joursoir <chat@joursoir.net> | 2020-12-05 18:12:04 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2020-12-05 18:12:04 +0000 |
commit | 91d777da4f5baeac47b3097913b2407b45bc031e (patch) | |
tree | 26c1edbbdb0b6998bb533a5862151f56e1fda025 | |
parent | 6f43ecd3758f445249db2bd4c4b32b912243cbdd (diff) | |
download | want-chat-91d777da4f5baeac47b3097913b2407b45bc031e.tar.gz want-chat-91d777da4f5baeac47b3097913b2407b45bc031e.tar.bz2 want-chat-91d777da4f5baeac47b3097913b2407b45bc031e.zip |
add registration and authorization
-rw-r--r-- | src/const_vars.hpp | 3 | ||||
-rw-r--r-- | src/server/ChatRoom.cpp | 296 | ||||
-rw-r--r-- | src/server/ChatRoom.hpp | 18 | ||||
-rw-r--r-- | src/server/ChatRoom_cmd.cpp | 229 | ||||
-rw-r--r-- | src/server/ChatServer.cpp | 18 | ||||
-rw-r--r-- | src/server/ChatServer.hpp | 6 | ||||
-rw-r--r-- | src/server/Makefile | 2 | ||||
-rw-r--r-- | src/server/UserInfo.cpp | 18 | ||||
-rw-r--r-- | src/server/UserInfo.hpp | 12 | ||||
-rw-r--r-- | src/server/database.cpp | 8 | ||||
-rw-r--r-- | src/server/database.hpp | 16 |
11 files changed, 382 insertions, 244 deletions
diff --git a/src/const_vars.hpp b/src/const_vars.hpp index 54ef70f..66d31d9 100644 --- a/src/const_vars.hpp +++ b/src/const_vars.hpp @@ -9,6 +9,9 @@ const int max_name_len = 18; const int min_name_len = 3; +const int max_player_lenpass = 24; +const int min_player_lenpass = 4; + const int oneline_len = 57; const int max_usermsg_len = oneline_len * 3 - max_name_len - 2; // ": " = 2 // 57 * 3 - 18 - 2 = 151 diff --git a/src/server/ChatRoom.cpp b/src/server/ChatRoom.cpp index 2d368e3..50c364c 100644 --- a/src/server/ChatRoom.cpp +++ b/src/server/ChatRoom.cpp @@ -1,42 +1,10 @@ #include <stdio.h> #include <string.h> -#include <stdlib.h> #include "ChatRoom.hpp" #include "ChatServer.hpp" #include "UserInfo.hpp" -#define DB_BUFFER_SIZE 128 -#define USE_IN_ROOM 0 -#define USE_IN_LOBBY 1 -#define USE_ANYWHERE 2 - -struct cmd_info { - int id; // never -1 !!! - char name[15]; - const unsigned long name_hash; - int lobby_cmd; // USE_IN_ROOM, USE_IN_LOBBY, USE_ANYWHERE - int min_argc; - char usage[64]; -}; - -const int cmd_id_help = 0; -const int cmd_id_create = 1; -const int cmd_id_join = 2; -const int cmd_id_exit = 3; -const int cmd_id_rooms = 4; - -const int cmd_count = 5; -const struct cmd_info cmd[cmd_count] = { - {cmd_id_help, "/help", ChatRoom::hash("/help"), USE_ANYWHERE, 0, "Usage: /help"}, - {cmd_id_create, "/create", ChatRoom::hash("/create"), USE_IN_LOBBY, 0, "Usage: /create [pass-key]"}, - {cmd_id_join, "/join", ChatRoom::hash("/join"), USE_IN_LOBBY, 1, "Usage: /join *id* [pass-key]"}, - {cmd_id_exit, "/exit", ChatRoom::hash("/exit"), USE_IN_ROOM, 0, "Usage: /exit"}, - {cmd_id_rooms, "/rooms", ChatRoom::hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"} // print all public rooms - - // IDEA: /clear - clear screen -}; - ChatRoom::ChatRoom(ChatServer *i_server, int id, char *pass) : the_server(i_server), code(id) { @@ -58,59 +26,55 @@ ChatRoom::~ChatRoom() void ChatRoom::HandleMessage(UserInfo *u, const char *str) { int status = u->GetStatus(); - if(status != no_wait) { - if(status == wait_name) { - // handle name, if OK - will send hello msg - int len = strlen(str); - if(len > max_name_len || len < min_name_len) { - u->Send("Incorrect name. Name length from 3 to 18 chars"); - CloseSession(u); - } - if(checkForbiddenSymbols(str)) { - u->Send("Incorrect name. You use forbidden symbols."); - CloseSession(u); + if(status == wait_name) { + if(!CheckEnterNickname(u, str)) { + CloseSession(u); + return; + } + u->SetName(str); + + // query to server + char *msg = new char[DB_BUFFER_SIZE]; + sprintf(msg, "SELECT password FROM users WHERE name = '%s' LIMIT 1", str); + AnswerDB *ans = the_server->QuerySelect(msg); + if(ans) { + DB_ROW *row = ans->GetNextRow(); + + sprintf(msg, "Welcome to WantChat, %s!", u->GetName()); + u->Send(msg); + u->Send(" "); + u->Send("Welcome to anonymous chat in retro-style 80s."); + u->Send("Use our chat-client for more immersed."); + u->Send(" "); + u->Send("This project is open source :)"); + u->Send("Github: github.com/Joursoir/want-chat"); + u->Send(" "); + u->Send("To join to room using /join room_id"); + u->Send("You can find rooms using /rooms"); + u->Send(" "); + u->Send("For more detailed info: /help. Good chatting!"); + u->Send(" "); + + if(row) { + u->SetPassword((*row)[0]); + u->SetStatus(wait_login); + + u->Send("Please, log in using /login"); } - - //if(userInServer(char *nickname)) - // проверить если кто-то с данным именем.. - - u->SetName(str); - - char *msg = new char[DB_BUFFER_SIZE]; - sprintf(msg, "SELECT password FROM users WHERE name = '%s' LIMIT 1", str); - CONSOLE_LOG("%s\n", msg); - AnswerDB *ans = the_server->QuerySelect(msg); - if(ans) { - DB_ROW *row = ans->GetNextRow(); - - char *pass = 0; - if(row) { - pass = new char[max_player_lenpass]; - strcpy(pass, (*row)[0]); - - CONSOLE_LOG("account exist! \n"); - } + else { + u->SetStatus(wait_reg); + u->Send("Please, create account using /reg"); } - else CONSOLE_LOG("account noexist :( \n"); - delete[] msg; - - u->Send("Welcome to WantChat!"); - u->Send(" "); - u->Send("It is anonymous chat in retro-style 80s."); - u->Send("Use our chat-client for more immersed."); - u->Send(" "); - u->Send("This project is open source :)"); - u->Send("Github: github.com/Joursoir/want-chat"); - u->Send(" "); - u->Send("To join to room using /join room_id"); - u->Send("You can find rooms using /rooms"); - u->Send(" "); - u->Send("For more detailed info: /help. Good chatting!"); - u->Send(" "); - - u->SetStatus(no_wait); } + else { + // handling error + CloseSession(u); + return; + } + // query to server + delete[] msg; + delete ans; return; } @@ -131,89 +95,10 @@ void ChatRoom::HandleMessage(UserInfo *u, const char *str) delete[] msg; } - else u->Send("In the lobby you can only write commands"); -} - -void ChatRoom::HandleCommand(UserInfo *u, int count, - char **argvar) -{ - unsigned long hash_cmd = -1; - hash_cmd = hash(argvar[0]); - - int what_command = -1; - for(int i = 0; i < cmd_count; i++) { - if(cmd[i].name_hash == hash_cmd) { - what_command = i; - break; - } - } - - if(what_command == -1) - return u->Send("Unknown command. Use: /help"); - - const char onlyroom_msg[] = "You can use this command only in rooms!"; - const char onlylobby_msg[] = "You can use this command only in lobby!"; - - // scope of command: - if(cmd[what_command].lobby_cmd == USE_IN_ROOM && code == std_id_lobby) - return u->Send(onlyroom_msg); - else if(cmd[what_command].lobby_cmd == USE_IN_LOBBY && code != std_id_lobby) - return u->Send(onlylobby_msg); - - // right usage: - if(cmd[what_command].min_argc > count-1) - return u->Send(cmd[what_command].usage); - - switch(cmd[what_command].id) { - case cmd_id_help: { - u->Send("Help info..."); - break; - } - case cmd_id_create: { - char *pass = 0; - if(count > 1) { - if(strlen(argvar[1]) > max_room_lenpass) - return u->Send("Maximum length of pass equals 24"); - pass = argvar[1]; - } - int id = the_server->AddRoom(pass); - - const char fcmsg[] = "You create a room #"; - const char scmsg[] = " with password: "; - char *cmsg = new char[strlen(fcmsg)+3+strlen(scmsg)+max_room_lenpass]; - if(pass) sprintf(cmsg, "%s%d%s%s", fcmsg, id, scmsg, pass); - else sprintf(cmsg, "%s%d", fcmsg, id); - u->Send(cmsg); - delete[] cmsg; - - the_server->ChangeSessionRoom(this, u, id, pass); - break; - } - case cmd_id_join: { - int id = atoi(argvar[1]); - char *pass = argvar[2]; // if count == 2, then argvar[2] = 0 - - int h_status = the_server->ChangeSessionRoom(this, u, id, pass); - if(h_status == enter_noexist) - return u->Send("Room with that ID didn't exist"); - else if(h_status == enter_private) - return u->Send("It is private room, join using password"); - else if(h_status == enter_uncorrect_pass) - return u->Send("Oops, this password is not valid"); - - u->Send("You has entered the room"); - break; - } - case cmd_id_exit: { - u->Send("You has left the room"); - the_server->GotoLobby(this, u); - break; - } - case cmd_id_rooms: { - // in development - break; - } - default: break; + else { + if(status == wait_reg) u->Send(first_reg); + else if(status == wait_login) u->Send(first_login); + else u->Send("In the lobby you can write only commands"); } } @@ -272,61 +157,42 @@ void ChatRoom::CloseSession(UserInfo *u) serv->CloseConnection(u); } -////////////////////////////////////////////////////////////// - -char **ChatRoom::ParseToArg(const char *input, int &arrc) +bool ChatRoom::CheckEnterNickname(UserInfo *u, const char *name) { - int max_argv = 5; - arrc = 0; - char **arr = new char*[max_argv+1]; - - int start = 0; - for(int i = 0; i < (int) strlen(input)+1; i++) { - if(input[i] == ' ' || input[i] == '\0' || input[i] == '\n') { // end - if(start == std_id_lobby) - continue; - - int size = i - start; - arr[arrc] = new char[size+1]; - memcpy(arr[arrc], input + start, sizeof(char) * size); - arr[arrc][size] = '\0'; - - start = -1; - arrc++; - if(arrc == max_argv) - break; - } - else if(start == -1) - start = i; + int len = strlen(name); + if(len > max_name_len || len < min_name_len) { + int length = strlen("Incorrect name. Name length from to chars"); + char *msg = new char[length + 2 + 2]; + sprintf(msg, "Incorrect name. Name length from %d to %d chars", min_name_len, max_name_len); + u->Send(msg); + delete[] msg; + return 0; } - - arr[arrc] = 0; - return arr; -} - -unsigned long ChatRoom::hash(const char *str) -{ - unsigned long hash = 5381; - char c; - - while( (c = *str++) ) - hash = ((hash << 5) + hash) + c; - - return hash; + if(CheckForbiddenSymbols(name)) { + u->Send("Incorrect name. You use forbidden symbols"); + return 0; + } + if(the_server->IsUserOnline(name) != 0) { + u->Send("Someone with this name is in the chat already"); + return 0; + } + return 1; } -bool ChatRoom::checkForbiddenSymbols(const char *str) +bool ChatRoom::CheckEnterPassword(UserInfo *u, const char *pass) { - char banned_symbols[] = {'!', '@', '#', '\'', - '\"', '\\', '/', '^', '&', '*', ';', ','}; - - int num = sizeof(banned_symbols)/sizeof(char); - for(int i = 0; i < (int)strlen(str); i++) { - for(int j = 0; j < num; j++) { - if(str[i] == banned_symbols[j]) - return 1; - } + int len = strlen(pass); + if(len > max_player_lenpass || len < min_player_lenpass) { + int length = strlen("Incorrect pass. Length from to chars"); + char *msg = new char[length + 2 + 2]; + sprintf(msg, "Incorrect pass. Length from %d to %d chars", min_player_lenpass, max_player_lenpass); + u->Send(msg); + delete[] msg; + return 0; } - - return 0; + if(CheckForbiddenSymbols(pass)) { + u->Send("Incorrect password. You use forbidden symbols"); + return 0; + } + return 1; }
\ No newline at end of file diff --git a/src/server/ChatRoom.hpp b/src/server/ChatRoom.hpp index eac97c5..183dcc1 100644 --- a/src/server/ChatRoom.hpp +++ b/src/server/ChatRoom.hpp @@ -3,9 +3,12 @@ #include "../const_vars.hpp" +#define DB_BUFFER_SIZE 128 + +const char first_reg[] = "First you must register using /reg"; +const char first_login[] = "First you must log in using /login"; const int std_id_lobby = -1; const int max_room_lenpass = 24; -const int max_player_lenpass = 24; class ChatServer; class UserInfo; @@ -23,7 +26,7 @@ public: void HandleMessage(UserInfo *u, const char *str); void HandleCommand(UserInfo *u, int cmd_counter, - char **commands); + char **commands); // in _cmd const char *GetSecretPass(); @@ -31,10 +34,15 @@ public: void RemoveSession(UserInfo *u); void CloseSession(UserInfo *u); - static unsigned long hash(const char *str); + static unsigned long Hash(const char *str); // in _cmd private: - static char **ParseToArg(const char *input, int &arrc); - static bool checkForbiddenSymbols(const char *str); + static char **ParseToArg(const char *input, int &arrc); // in _cmd + static bool CheckForbiddenSymbols(const char *str); // in _cmd + + bool CheckEnterNickname(UserInfo *u, const char *name); + bool CheckEnterPassword(UserInfo *u, const char *pass); + + // IsPasswordRight }; #endif
\ No newline at end of file diff --git a/src/server/ChatRoom_cmd.cpp b/src/server/ChatRoom_cmd.cpp new file mode 100644 index 0000000..d37ad5e --- /dev/null +++ b/src/server/ChatRoom_cmd.cpp @@ -0,0 +1,229 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "ChatRoom.hpp" +#include "ChatServer.hpp" +#include "UserInfo.hpp" + +#define USE_IN_ROOM 0 +#define USE_IN_LOBBY 1 +#define USE_ANYWHERE 2 + +struct cmd_info { + int id; // never -1 !!! + char name[15]; + const unsigned long name_hash; + int lobby_cmd; // USE_IN_ROOM, USE_IN_LOBBY, USE_ANYWHERE + int min_argc; + char usage[64]; +}; + +const int cmd_id_help = 0; +const int cmd_id_create = 1; +const int cmd_id_join = 2; +const int cmd_id_exit = 3; +const int cmd_id_rooms = 4; +const int cmd_id_reg = 5; +const int cmd_id_login = 6; + +const int cmd_count = 7; +const struct cmd_info cmd[cmd_count] = { + {cmd_id_help, "/help", ChatRoom::Hash("/help"), USE_ANYWHERE, 0, "Usage: /help"}, + {cmd_id_create, "/create", ChatRoom::Hash("/create"), USE_IN_LOBBY, 0, "Usage: /create [pass-key]"}, + {cmd_id_join, "/join", ChatRoom::Hash("/join"), USE_IN_LOBBY, 1, "Usage: /join *id* [pass-key]"}, + {cmd_id_exit, "/exit", ChatRoom::Hash("/exit"), USE_IN_ROOM, 0, "Usage: /exit"}, + {cmd_id_rooms, "/rooms", ChatRoom::Hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"}, // print all public rooms + {cmd_id_reg, "/reg", ChatRoom::Hash("/reg"), USE_IN_LOBBY, 2, "Usage: /reg *pass* *pass*"}, + {cmd_id_login, "/login", ChatRoom::Hash("/login"), USE_IN_LOBBY, 1, "Usage: /login *pass*"} + + // IDEA: /clear - clear screen +}; + +void ChatRoom::HandleCommand(UserInfo *u, int count, + char **argvar) +{ + unsigned long hash_cmd = -1; + hash_cmd = Hash(argvar[0]); + + int what_command = -1; + for(int i = 0; i < cmd_count; i++) { + if(cmd[i].name_hash == hash_cmd) { + what_command = i; + break; + } + } + + if(what_command == -1) + return u->Send("Unknown command. Use: /help"); + + enum_status status = u->GetStatus(); + if(status == wait_reg && what_command != cmd_id_reg) + return u->Send(first_reg); + if(status == wait_login && what_command != cmd_id_login) + return u->Send(first_login); + + // scope of command: + if(cmd[what_command].lobby_cmd == USE_IN_ROOM && code == std_id_lobby) + return u->Send("You can use this command only in rooms!"); + if(cmd[what_command].lobby_cmd == USE_IN_LOBBY && code != std_id_lobby) + return u->Send("You can use this command only in lobby!"); + + // right usage: + if(cmd[what_command].min_argc > count-1) + return u->Send(cmd[what_command].usage); + + switch(cmd[what_command].id) { + case cmd_id_help: { + u->Send(""); + u->Send("/help: print this message"); + u->Send("/create [pass-key]: create public/private room"); + u->Send("/join *id* [pass-key]: join to public/private room"); + u->Send("/exit: leave from room"); + u->Send("/rooms: show public rooms"); + u->Send(""); + break; + } + case cmd_id_create: { + char *pass = 0; + if(count > 1) { + if(strlen(argvar[1]) > max_room_lenpass) + return u->Send("Maximum length of pass equals 24"); + pass = argvar[1]; + } + int id = the_server->AddRoom(pass); + + const char fcmsg[] = "You create a room #"; + const char scmsg[] = " with password: "; + char *cmsg = new char[strlen(fcmsg)+3+strlen(scmsg)+max_room_lenpass]; + if(pass) sprintf(cmsg, "%s%d%s%s", fcmsg, id, scmsg, pass); + else sprintf(cmsg, "%s%d", fcmsg, id); + u->Send(cmsg); + delete[] cmsg; + + the_server->ChangeSessionRoom(this, u, id, pass); + break; + } + case cmd_id_join: { + int id = atoi(argvar[1]); + char *pass = argvar[2]; // if count == 2, then argvar[2] = 0 + + int h_status = the_server->ChangeSessionRoom(this, u, id, pass); + if(h_status == enter_noexist) + return u->Send("Room with that ID didn't exist"); + else if(h_status == enter_private) + return u->Send("It is private room, join using password"); + else if(h_status == enter_uncorrect_pass) + return u->Send("Oops, this password is not valid"); + + u->Send("You has entered the room"); + break; + } + case cmd_id_exit: { + u->Send("You has left the room"); + the_server->GotoLobby(this, u); + break; + } + case cmd_id_rooms: { + // in development + break; + } + + case cmd_id_reg: { + char *pass_one = argvar[1]; + char *pass_two = argvar[2]; + + if(!CheckEnterPassword(u, pass_one)) + return; + if(strcmp(pass_one, pass_two) != 0) + return u->Send("Passwords are not match, try again"); + + // query to server (add to database) + char *msg = new char[DB_BUFFER_SIZE]; + sprintf(msg, "INSERT INTO users (name, password) VALUES ('%s', '%s')", u->GetName(), pass_one); + if(the_server->QueryInsert(msg) != 0) { + perror("mariadb insert"); + } + // query to server + + sprintf(msg, "Congratulations, %s. You registered in WhatChat.", u->GetName()); + u->Send(msg); + delete[] msg; + u->SetStatus(no_wait); + break; + } + case cmd_id_login: { + CONSOLE_LOG("[!] pass\n"); + char *enter_pass = argvar[1]; + const char *right_pass = u->GetPassword(); + + if(strcmp(enter_pass, right_pass) != 0) + return u->Send("Password are not right, try again"); + + int msg_len = strlen("Hello, ! Glad you came back to WhatChat."); + char *msg = new char[msg_len + max_name_len]; + sprintf(msg, "Hello, %s! Glad you came back to WhatChat.", u->GetName()); + u->Send(msg); + delete[] msg; + u->SetStatus(no_wait); + break; + } + default: break; + } +} + +unsigned long ChatRoom::Hash(const char *str) +{ + unsigned long hash = 5381; + char c; + + while( (c = *str++) ) + hash = ((hash << 5) + hash) + c; + + return hash; +} + +char **ChatRoom::ParseToArg(const char *input, int &arrc) +{ + int max_argv = 5; + arrc = 0; + char **arr = new char*[max_argv+1]; + + int start = 0; + for(int i = 0; i < (int) strlen(input)+1; i++) { + if(input[i] == ' ' || input[i] == '\0' || input[i] == '\n') { // end + if(start == std_id_lobby) + continue; + + int size = i - start; + arr[arrc] = new char[size+1]; + memcpy(arr[arrc], input + start, sizeof(char) * size); + arr[arrc][size] = '\0'; + + start = -1; + arrc++; + if(arrc == max_argv) + break; + } + else if(start == -1) + start = i; + } + + arr[arrc] = 0; + return arr; +} + +bool ChatRoom::CheckForbiddenSymbols(const char *str) +{ + char banned_symbols[] = {'!', '@', '#', '\'', + '\"', '\\', '/', '^', '&', '*', ';', ','}; + + int num = sizeof(banned_symbols)/sizeof(char); + for(int i = 0; i < (int)strlen(str); i++) { + for(int j = 0; j < num; j++) { + if(str[i] == banned_symbols[j]) + return 1; + } + } + + return 0; +}
\ No newline at end of file diff --git a/src/server/ChatServer.cpp b/src/server/ChatServer.cpp index 0800ee6..2d700e9 100644 --- a/src/server/ChatServer.cpp +++ b/src/server/ChatServer.cpp @@ -186,6 +186,21 @@ void StorageOfUsers::SendAllUsers(const char *msg, UserInfo *except, p->u->Send(msg, spec_msg); } +UserInfo *StorageOfUsers::SearchUserByName(const char *name) +{ + item *tmp = first; + while(tmp) { + const char *search = name; + const char *checking = tmp->u->GetName(); + if(strcmp(search, checking) == 0) + return tmp->u; + + tmp = tmp->next; + } + + return 0; +} + void StorageOfUsers::AddUser(UserInfo *u) { item *p = new item; @@ -205,6 +220,7 @@ void StorageOfUsers::RemoveUser(UserInfo *u) // not delete UserInfo! online--; delete tmp; + tmp = 0; // null for first return; } } @@ -220,5 +236,7 @@ UserInfo *StorageOfUsers::Disconnect() delete tmp; return rn; } + + first = 0; return 0; }
\ No newline at end of file diff --git a/src/server/ChatServer.hpp b/src/server/ChatServer.hpp index 4fa90e5..f966e84 100644 --- a/src/server/ChatServer.hpp +++ b/src/server/ChatServer.hpp @@ -30,6 +30,7 @@ public: void SendAllUsers(const char *msg, UserInfo *except = 0, const int spec_msg = system_msg); + UserInfo *SearchUserByName(const char *name); void AddUser(UserInfo *u); void RemoveUser(UserInfo *u); @@ -68,7 +69,12 @@ public: // work with database: AnswerDB *QuerySelect(const char *sql) { return dbase->QuerySelect(sql); } + int QueryInsert(const char *sql) + { return dbase->QueryInsert(sql); } + // players actions: + UserInfo *IsUserOnline(const char *name) + { return talkers->SearchUserByName(name); } private: virtual void Handle(bool r, bool w); }; diff --git a/src/server/Makefile b/src/server/Makefile index 03836e8..42d8361 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -1,6 +1,6 @@ CPP = g++ CPPFLAGS = -Wall -g $(shell mariadb_config --cflags --libs) -SOURCES = main.cpp sockets.cpp ChatServer.cpp UserInfo.cpp UserInfo_io.cpp ChatRoom.cpp database.cpp +SOURCES = main.cpp sockets.cpp ChatServer.cpp UserInfo.cpp UserInfo_io.cpp ChatRoom.cpp ChatRoom_cmd.cpp database.cpp OBJECTS = $(SOURCES:.cpp=.o) EXECUTABLE = server diff --git a/src/server/UserInfo.cpp b/src/server/UserInfo.cpp index e7c0334..e0239eb 100644 --- a/src/server/UserInfo.cpp +++ b/src/server/UserInfo.cpp @@ -7,9 +7,7 @@ void UserInfo::SetRoom(ChatRoom *new_master) const char *UserInfo::GetName() const { - if(name[0]) - return name; - return 0; + return name; } void UserInfo::SetName(const char *n_name) @@ -17,7 +15,17 @@ void UserInfo::SetName(const char *n_name) strcpy(name, n_name); } -void UserInfo::SetStatus(const enum_status e_s) +const char *UserInfo::GetPassword() const { - state = e_s; + return password; +} + +void UserInfo::SetPassword(const char *pass) +{ + strcpy(password, pass); +} + +void UserInfo::SetStatus(const enum_status n_status) +{ + state = n_status; } diff --git a/src/server/UserInfo.hpp b/src/server/UserInfo.hpp index e033b0c..efe6d5e 100644 --- a/src/server/UserInfo.hpp +++ b/src/server/UserInfo.hpp @@ -12,7 +12,7 @@ class ChatServer; enum enum_status { wait_name, // expecting a username from player wait_reg, // expecting registration - wait_log, // expecting login + wait_login, // expecting login no_wait }; @@ -22,6 +22,7 @@ class UserInfo : FdHandler { friend class ChatServer; char name[max_name_len]; + char password[max_player_lenpass]; char buffer[max_msg_len]; int buf_used; bool ignoring; @@ -29,8 +30,8 @@ class UserInfo : FdHandler { enum_status state; ChatRoom *the_master; - UserInfo(ChatRoom *i_master, int i_fd) : FdHandler(i_fd), - buf_used(0), ignoring(false), state(wait_name), + UserInfo(ChatRoom *i_master, int i_fd) : FdHandler(i_fd), name("0"), + password("0"), buf_used(0), ignoring(false), state(wait_name), the_master(i_master) {} ~UserInfo() {} @@ -47,9 +48,12 @@ public: const char *GetName() const; void SetName(const char *n_name); + + const char *GetPassword() const; + void SetPassword(const char *pass); enum_status GetStatus() const { return state; } - void SetStatus(const enum_status e_s); + void SetStatus(const enum_status n_status); }; #endif
\ No newline at end of file diff --git a/src/server/database.cpp b/src/server/database.cpp index 4438cc2..dcec5a6 100644 --- a/src/server/database.cpp +++ b/src/server/database.cpp @@ -16,6 +16,9 @@ char *DB_ROW::operator[](const int index) AnswerDB::~AnswerDB() { + if(current_row) + delete current_row; + mysql_free_result(result); } @@ -68,4 +71,9 @@ AnswerDB *DatabaseManager::QuerySelect(const char *sql) unsigned int fields = mysql_num_fields(result); unsigned long rows = mysql_num_rows(result); return new AnswerDB(result, fields, rows); +} + +int DatabaseManager::QueryInsert(const char *sql) +{ + return mysql_query(connection, sql); }
\ No newline at end of file diff --git a/src/server/database.hpp b/src/server/database.hpp index 8be6f06..b4b731d 100644 --- a/src/server/database.hpp +++ b/src/server/database.hpp @@ -47,19 +47,7 @@ public: const char *unix_socket = 0, unsigned long flags = 0); AnswerDB *QuerySelect(const char *sql); + int QueryInsert(const char *sql); }; -#endif - -/* - -надо как-то вызывать ~AnswerDB!! - -Answer DB *ans = server->Query("SELECT password FROM users WHERE name = '%s' ONLY ONE", strlen(..)) -DB_ROW *row = ans->GetNextRow(); -if(row) - name = row[0]; - password = row[1]; - - -*/
\ No newline at end of file +#endif
\ No newline at end of file |