From 7385c0352c7fe7e9ceca343400a5caa5ddd999d6 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Mon, 30 Nov 2020 02:29:18 +0300 Subject: refactoring and some feature client: type nick in argument server: basis for registration; change style add commands; edit send msg to user; feature: private room --- src/server/rooms.cpp | 188 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 126 insertions(+), 62 deletions(-) (limited to 'src/server/rooms.cpp') diff --git a/src/server/rooms.cpp b/src/server/rooms.cpp index 1ffb89a..ba1ee1b 100644 --- a/src/server/rooms.cpp +++ b/src/server/rooms.cpp @@ -5,40 +5,46 @@ #include "rooms.hpp" #include "chat.hpp" -// hash of commands: -#define CMD_NAME 0 -#define CMD_CREATE 1 -#define CMD_JOIN 2 -#define CMD_EXIT 3 -#define CMD_ROOMS 4 - #define USE_IN_ROOM 0 #define USE_IN_LOBBY 1 #define USE_ANYWHERE 2 -char **ParseToArg(const char *input, int &arrc); -unsigned long hash(const char *str); +static char **ParseToArg(const char *input, int &arrc); +static unsigned long hash(const char *str); +static bool checkForbiddenSymbols(const char *str); struct cmd_info { + int id; // never -1 !!! char name[15]; - unsigned long name_hash; + 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_count = 5; +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 = 4; const struct cmd_info cmd[cmd_count] = { - {"/name", hash("/name"), USE_IN_LOBBY, 1, "#Usage: /name *your_good_name*\n"}, - {"/create", hash("/create"), USE_IN_LOBBY, 0, "#Usage: /create [pass-key]\n"}, - {"/join", hash("/join"), USE_IN_LOBBY, 1, "#Usage: /join *id* [pass-key]\n"}, - {"/exit", hash("/exit"), USE_IN_ROOM, 0, "#Usage: /exit\n"}, - {"/rooms", hash("/rooms"), USE_IN_LOBBY, 0, "#Usage: /rooms\n"} // print all public rooms - // if u add cmd, then add define too (see above) + {cmd_id_create, "/create", hash("/create"), USE_IN_LOBBY, 0, "Usage: /create [pass-key]"}, + {cmd_id_join, "/join", hash("/join"), USE_IN_LOBBY, 1, "Usage: /join *id* [pass-key]"}, + {cmd_id_exit, "/exit", hash("/exit"), USE_IN_ROOM, 0, "Usage: /exit"}, + {cmd_id_rooms, "/rooms", hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"} // print all public rooms // IDEA: /clear - clear screen }; +ChatRoom::ChatRoom(Server *i_server, int id, char *pass) + : the_server(i_server), code(id), first(0) +{ + if(pass) + strcpy(secret_pass, pass); + else secret_pass[0] = 0; +} + ChatRoom::~ChatRoom() { while(first) { @@ -49,17 +55,53 @@ ChatRoom::~ChatRoom() } } -void ChatRoom::SendAll(const char *msg, ChatSession *except) +void ChatRoom::SendAll(const char *msg, ChatSession *except, + const int spec_msg) { CONSOLE_LOG("Send message all: %s\n", msg); item *p; for(p = first; p; p = p->next) if(p->s != except) - p->s->Send(msg); + p->s->Send(msg, spec_msg); } void ChatRoom::HandleMessage(ChatSession *ses, const char *str) { + int status = ses->state; + 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) { + ses->Send("Incorrect name. Name length from 3 to 18 chars"); + CloseSession(ses); + } + if(checkForbiddenSymbols(str)) { + ses->Send("Incorrect name. You use forbidden symbols."); + CloseSession(ses); + } + ses->SetName(str); + + ses->Send("Welcome to WantChat!"); + ses->Send(" "); + ses->Send("It is anonymous chat in retro-style 80s."); + ses->Send("Use our chat-client for more immersed."); + ses->Send(" "); + ses->Send("This project is open source :)"); + ses->Send("Github: github.com/Joursoir/want-chat"); + ses->Send(" "); + ses->Send("To join to room using /join room_id"); + ses->Send("You can find rooms using /rooms"); + ses->Send(" "); + ses->Send("For more detailed info: /help. Good chatting!"); + ses->Send(" "); + + ses->state = no_wait; + } + + return; + } + if(str[0] == '/') { // if user sent a command int argc = 0; char **argv = ParseToArg(str, argc); @@ -71,12 +113,13 @@ void ChatRoom::HandleMessage(ChatSession *ses, const char *str) } else if(code != std_id_lobby) { char *msg = new char[max_msg_len]; - sprintf(msg, "%s: %s\n", ses->GetName(), str); + sprintf(msg, "%s: %s", ses->GetName(), str); - this->SendAll(msg); + this->SendAll(msg, 0, usual_msg); delete[] msg; } + else ses->Send("In the lobby you can only write commands"); } void ChatRoom::HandleCommand(ChatSession *ses, int count, @@ -94,11 +137,10 @@ void ChatRoom::HandleCommand(ChatSession *ses, int count, } if(what_command == -1) - return ses->Send("#Unknown command. Use: /help\n"); + return ses->Send("Unknown command. Use: /help"); - const char onlyroom_msg[] = "#You can use this command only in rooms!\n"; - const char onlylobby_msg[] = "#You can use this command only in lobby!\n"; - const char initname_msg[] = "#First come up with name!\n"; + 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) @@ -110,55 +152,60 @@ void ChatRoom::HandleCommand(ChatSession *ses, int count, if(cmd[what_command].min_argc > count-1) return ses->Send(cmd[what_command].usage); - // name check: - if((!ses->GetName()) && what_command != CMD_NAME) - return ses->Send(initname_msg); - - switch(what_command) { - case CMD_NAME: { - int len = strlen(argvar[1]); - if(len > max_name_len || len < 3) - return ses->Send("#Incorrect name. Name length from 3 to 18 chars\n"); - - ses->SetName(argvar[1]); - break; - } - case CMD_CREATE: { - int id = the_server->AddRoom(); - - const char fcmsg[] = "#You create a room #"; - const char scmsg[] = " with password "; - char *cmsg = new char[strlen(fcmsg)+strlen(scmsg)+4]; - sprintf(cmsg, "%s%d\n", fcmsg, id); + switch(cmd[what_command].id) { + case cmd_id_create: { + char *pass = 0; + if(count > 1) { + if(strlen(argvar[1]) > max_room_lenpass) + return ses->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); ses->Send(cmsg); delete[] cmsg; - the_server->ChangeSessionRoom(this, ses, id); + the_server->ChangeSessionRoom(this, ses, id, pass); break; } - case CMD_JOIN: { + case cmd_id_join: { int id = atoi(argvar[1]); - bool success = false; - if(id >= 0) - success = the_server->ChangeSessionRoom(this, ses, id); + char *pass = argvar[2]; // if count == 2, then argvar[2] = 0 - if(!success) - ses->Send("#Room with that id didn't exist\n"); + int h_status = the_server->ChangeSessionRoom(this, ses, id, pass); + if(h_status == enter_noexist) + return ses->Send("Room with that ID didn't exist"); + else if(h_status == enter_private) + return ses->Send("It is private room, join using password"); + else if(h_status == enter_uncorrect_pass) + return ses->Send("Oops, this password is not valid"); break; } - case CMD_EXIT: { + case cmd_id_exit: { the_server->GotoLobby(this, ses); break; } - case CMD_ROOMS: { - + case cmd_id_rooms: { + // in development break; } default: break; } } +const char *ChatRoom::GetSecretPass() +{ + if(secret_pass[0] != 0) + return secret_pass; + return 0; +} + void ChatRoom::AddSession(ChatSession *ses) { item *p = new item; @@ -175,12 +222,12 @@ void ChatRoom::AddSession(ChatSession *ses) const char *name = ses->GetName(); char *wmsg = new char[sizeof(welcome_msg) + 2]; - sprintf(wmsg, "#%s%d\n", welcome_msg, code); + sprintf(wmsg, "%s%d", welcome_msg, code); ses->Send(wmsg); delete[] wmsg; - char *emsg = new char[strlen(name) + sizeof(entered_msg) + 2]; - sprintf(emsg, "#%s%s\n", name, entered_msg); + char *emsg = new char[strlen(name) + sizeof(entered_msg)]; + sprintf(emsg, "%s%s", name, entered_msg); this->SendAll(emsg, ses); delete[] emsg; @@ -194,7 +241,7 @@ void ChatRoom::RemoveSession(ChatSession *ses) int len = strlen(name); char *lmsg = new char[len + sizeof(left_msg) + 2]; - sprintf(lmsg, "#%s%s\n", name, left_msg); + sprintf(lmsg, "%s%s", name, left_msg); this->SendAll(lmsg, ses); delete[] lmsg; } @@ -224,11 +271,11 @@ void ChatRoom::CloseSession(ChatSession *ses) ////////////////////////////////////////////////////////////// -char **ParseToArg(const char *input, int &arrc) +static char **ParseToArg(const char *input, int &arrc) { int max_argv = 5; arrc = 0; - char **arr = new char*[max_argv]; + char **arr = new char*[max_argv+1]; int start = 0; for(int i = 0; i < (int) strlen(input)+1; i++) { @@ -250,10 +297,11 @@ char **ParseToArg(const char *input, int &arrc) start = i; } + arr[arrc] = 0; return arr; } -unsigned long hash(const char *str) +static unsigned long hash(const char *str) { unsigned long hash = 5381; char c; @@ -262,4 +310,20 @@ unsigned long hash(const char *str) hash = ((hash << 5) + hash) + c; return hash; +} + +static bool 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 -- cgit v1.2.3-18-g5258