diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/ChatRoom.cpp | 144 | ||||
-rw-r--r-- | src/server/ChatRoom.hpp | 1 | ||||
-rw-r--r-- | src/server/ChatRoom_cmd.cpp | 54 | ||||
-rw-r--r-- | src/server/ChatServer.cpp | 4 |
4 files changed, 93 insertions, 110 deletions
diff --git a/src/server/ChatRoom.cpp b/src/server/ChatRoom.cpp index 50c364c..9d99251 100644 --- a/src/server/ChatRoom.cpp +++ b/src/server/ChatRoom.cpp @@ -25,58 +25,8 @@ ChatRoom::~ChatRoom() void ChatRoom::HandleMessage(UserInfo *u, const char *str) { - int status = u->GetStatus(); - 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"); - } - else { - u->SetStatus(wait_reg); - u->Send("Please, create account using /reg"); - } - } - else { - // handling error - CloseSession(u); - return; - } - // query to server - - delete[] msg; - delete ans; - return; - } + if(u->GetStatus() != no_wait) + return Identification(u, str); if(str[0] == '/') { // if user sent a command int argc = 0; @@ -95,11 +45,7 @@ void ChatRoom::HandleMessage(UserInfo *u, const char *str) delete[] msg; } - 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"); - } + else u->Send("In the lobby you can write only commands"); } const char *ChatRoom::GetSecretPass() @@ -157,6 +103,90 @@ void ChatRoom::CloseSession(UserInfo *u) serv->CloseConnection(u); } +void ChatRoom::Identification(UserInfo *u, const char *str) +{ + int status = u->GetStatus(); + 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(); + + if(row) { + u->SetPassword((*row)[0]); + u->SetStatus(wait_login); + + sprintf(msg, "Hello, %s! Please, type your password:", u->GetName()); + u->Send(msg); + } + else { + u->SetStatus(wait_reg); + sprintf(msg, "First time here, %s? Please, come up and write password:", u->GetName()); + u->Send(msg); + } + } + else { + // handling error + CloseSession(u); + return; + } + // query to server + + delete[] msg; + delete ans; + } + else if(status == wait_reg) { + if(!CheckEnterPassword(u, str)) + return; + + // 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(), str); + if(the_server->QueryInsert(msg) != 0) { + perror("mariadb insert"); + } + // query to server + + sprintf(msg, "Congratulations, %s! You have registered successfully.", u->GetName()); + u->Send(msg); + delete[] msg; + + u->SetStatus(no_wait); + } + else if(status == wait_login) { + const char *right_pass = u->GetPassword(); + + if(strcmp(str, right_pass) != 0) + return u->Send("Password are not right, try again."); + + u->Send("Yeh, right password! Glad you came back to WhatChat."); + u->SetStatus(no_wait); + } + + if(u->GetStatus() == no_wait) { + 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(" "); + } +} + bool ChatRoom::CheckEnterNickname(UserInfo *u, const char *name) { int len = strlen(name); diff --git a/src/server/ChatRoom.hpp b/src/server/ChatRoom.hpp index 183dcc1..fa56fb2 100644 --- a/src/server/ChatRoom.hpp +++ b/src/server/ChatRoom.hpp @@ -39,6 +39,7 @@ private: static char **ParseToArg(const char *input, int &arrc); // in _cmd static bool CheckForbiddenSymbols(const char *str); // in _cmd + void Identification(UserInfo *u, const char *str); bool CheckEnterNickname(UserInfo *u, const char *name); bool CheckEnterPassword(UserInfo *u, const char *pass); diff --git a/src/server/ChatRoom_cmd.cpp b/src/server/ChatRoom_cmd.cpp index d37ad5e..8b1f6e9 100644 --- a/src/server/ChatRoom_cmd.cpp +++ b/src/server/ChatRoom_cmd.cpp @@ -23,18 +23,14 @@ 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 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 - {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*"} + {cmd_id_rooms, "/rooms", ChatRoom::Hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"} // print all public rooms // IDEA: /clear - clear screen }; @@ -56,12 +52,6 @@ void ChatRoom::HandleCommand(UserInfo *u, int count, 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!"); @@ -127,46 +117,6 @@ void ChatRoom::HandleCommand(UserInfo *u, int count, // 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; } } diff --git a/src/server/ChatServer.cpp b/src/server/ChatServer.cpp index 2d700e9..7b6a9ea 100644 --- a/src/server/ChatServer.cpp +++ b/src/server/ChatServer.cpp @@ -12,7 +12,7 @@ const int qlen_for_listen = 6; ChatServer::ChatServer(EventSelector *sel, DatabaseManager *db, int fd) - : FdHandler(fd), the_selector(sel), dbase(db) + : FdHandler(fd), the_selector(sel), dbase(db), room(0) { the_selector->Add(this); lobby = new ChatRoom(this, std_id_lobby, 0); @@ -163,6 +163,8 @@ void ChatServer::Handle(bool r, bool w) talkers->AddUser(u); lobby->AddSession(u); the_selector->Add(u); + + u->Send("Welcome to WantChat! What is your name?"); } /////////////////////////////////////////////////////////////// |