summaryrefslogtreecommitdiffstats
path: root/src/server/ChatRoom.cpp
blob: 50c364c10c3e86e60e5f743baace42416be5b60e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <string.h>

#include "ChatRoom.hpp"
#include "ChatServer.hpp"
#include "UserInfo.hpp"

ChatRoom::ChatRoom(ChatServer *i_server, int id, char *pass)
        : the_server(i_server), code(id)
{
	users = new StorageOfUsers();
	if(pass)
		strcpy(secret_pass, pass);
	else secret_pass[0] = 0;
}

ChatRoom::~ChatRoom()
{
	UserInfo *u = 0;
	while((u = users->Disconnect())) {
		the_server->CloseConnection(u);
	}
    delete users;
}

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(str[0] == '/') { // if user sent a command
		int argc = 0;
		char **argv = ParseToArg(str, argc);
		this->HandleCommand(u, argc, argv);

		for(int i = 0; i < argc; i++)
			delete[] argv[i];
		delete[] argv;
	}
	else if(code != std_id_lobby) {
	    char *msg = new char[max_msg_len];
	    sprintf(msg, "%s: %s", u->GetName(), str);

	    users->SendAllUsers(msg, 0, usual_msg);
	    
	    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");
	}
}

const char *ChatRoom::GetSecretPass()
{
	if(secret_pass[0] != 0)
		return secret_pass;
	return 0;
}

void ChatRoom::AddSession(UserInfo *u)
{
	users->AddUser(u);
    if(code == std_id_lobby)
    	return;

    const char welcome_msg[] = "Welcome to the room #";
	const char entered_msg[] = " has entered the room";

	const char *name = u->GetName();

    char *wmsg = new char[sizeof(welcome_msg) + 2];
    sprintf(wmsg, "%s%d", welcome_msg, code);
    u->Send(wmsg);
    delete[] wmsg;

    char *emsg = new char[strlen(name) + sizeof(entered_msg)];
    sprintf(emsg, "%s%s", name, entered_msg);
    users->SendAllUsers(emsg, u);
   	delete[] emsg;

}

void ChatRoom::RemoveSession(UserInfo *u)
{
	if(code != std_id_lobby) {
		const char left_msg[] = " has left the room";
		const char *name = u->GetName();

	    int len = strlen(name);
	    char *lmsg = new char[len + sizeof(left_msg) + 2];
	    sprintf(lmsg, "%s%s", name, left_msg);
	    users->SendAllUsers(lmsg, u);
	    delete[] lmsg;
	}

	users->RemoveUser(u);
	if(code != std_id_lobby && users->GetOnline() < 1)
		the_server->DeleteRoom(code);
}

void ChatRoom::CloseSession(UserInfo *u)
{
	ChatServer *serv = the_server;
	this->RemoveSession(u);
	serv->CloseConnection(u);
}

bool ChatRoom::CheckEnterNickname(UserInfo *u, const char *name)
{
	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;
	}
	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::CheckEnterPassword(UserInfo *u, const char *pass)
{
	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;
	}
	if(CheckForbiddenSymbols(pass)) {
		u->Send("Incorrect password. You use forbidden symbols");
		return 0;
	}
	return 1;
}