Skip to content

Commit

Permalink
feat: handling of login and logout, user are created and stored in th…
Browse files Browse the repository at this point in the history
…e server
  • Loading branch information
nailec1911 committed Apr 12, 2024
1 parent a81fdfd commit 5f908d0
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 28 deletions.
File renamed without changes.
14 changes: 6 additions & 8 deletions src/client/run_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ static run_state_t handle_cmd(connection_t *connect, char *cmd)
free(cmd);
if (i == LEN_OF(commands))
printf("Command not found\n");
printf("> ");
fflush(stdout);
return state;
}

Expand Down Expand Up @@ -80,16 +78,16 @@ int run_client(connection_t *connection)

if (!connection)
return MSG_ERR("Memory failed\n");
printf("> ");
fflush(stdout);
while (state != cli_exit) {
if (is_fd_ready(STDIN_FILENO))
state = handle_cmd(connection, get_command());
if (is_fd_ready(connection->servfd)) {
state = get_serv_info(connection);
if (state != prompt) {
printf("> ");
fflush(stdout);
state = prompt;
}
if (is_fd_ready(STDIN_FILENO))
state = handle_cmd(connection, get_command());
if (is_fd_ready(connection->servfd))
state = get_serv_info(connection);
}
close(connection->servfd);
return SUCCESS;
Expand Down
1 change: 1 addition & 0 deletions src/client/run_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
typedef enum run_state {
running,
cli_exit,
prompt,
} run_state_t;

run_state_t get_serv_info(connection_t *connect);
Expand Down
29 changes: 24 additions & 5 deletions src/client/serve_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/

#include "client.h"
#include "event_handling/handle_event.h"
#include "run_client.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int get_info_type(connection_t *connect, void *buff, ssize_t size)
Expand All @@ -20,17 +22,34 @@ int get_info_type(connection_t *connect, void *buff, ssize_t size)
return SUCCESS;
}

static void display_error(connection_t *connect)
{
char c = '\0';

while (c != '\n') {
if (read(connect->servfd, &c, 1) == 1)
write(1, &c, 1);
}
}

run_state_t get_serv_info(connection_t *connect)
{
char code[3] = "000";
char code[4] = "000\0";
int res = -1;

if (get_info_type(connect, code, sizeof(code)) != SUCCESS) {
if (get_info_type(connect, code, 3) != SUCCESS) {
dprintf(STDERR_FILENO, "Error: didn't manage to read from server.\n");
return running;
}
if (code[0] != 3) {
dprintf(STDOUT_FILENO, "Error: code %s should not be here.", code);
return cli_exit;
for (size_t i = 0; i < LEN_OF(func_code_tab); i++) {
if (strncmp(func_code_tab[i].code, code, 3) == 0){
res = func_code_tab[i].func(connect);
break;
}
}
if (res == -1) {
display_error(connect);
return running;
}
return running;
}
9 changes: 4 additions & 5 deletions src/server/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

void client_init(client_t *client, int fd)
{
char uuid[37];

if (client == NULL)
return;
memset(client, 0, sizeof *client);
client->fd = fd;
client->buffer[0] = '\0';
uuid_generate(client->user.uuid);
uuid_unparse(client->user.uuid, uuid);
DEBUG("Client connected: <%s>\n", uuid);
client->user = NULL;
DEBUG_MSG("Client connected\n");
}

void client_disconnect(client_t *client)
Expand All @@ -51,6 +48,8 @@ static void client_process_message(
}
*ptr = '\0';
strcat(client->buffer, buffer);
if (client->buffer[0] == '\0')
return;
exec_command(serv, client);
client->buffer[0] = '\0';
client_process_message(serv, client, ptr + 1);
Expand Down
81 changes: 81 additions & 0 deletions src/server/cmds/login.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
** EPITECH PROJECT, 2024
** my_teams
** File description:
** login
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <uuid/uuid.h>

#include "command.h"
#include "ressources_infos.h"
#include "server.h"
#include "../libs/myteams/logging_server.h"

static void send_info(server_t *server, client_t *client, char *code)
{
user_info_t info = {0};

strcpy(info.user_name, client->user->name);
uuid_copy(info.user_uuid, client->user->uuid);
for (size_t i = 0; i < server->clients.size; i++) {
dprintf(server->clients.arr[i].fd, "%s", code);
write(server->clients.arr[i].fd, &info, sizeof(info));
}
}

static void create_user(server_t *server, client_t *client, char *name)
{
user_t new = {0};
char uuid_str[37] = {0};

strcpy(new.name, name);
uuid_generate(new.uuid);
uuid_unparse(new.uuid, uuid_str);
append_to_array(&server->users, sizeof(user_t), &new);
client->user = &server->users.arr[server->users.size - 1];
server_event_user_created(uuid_str, new.name);
}

static void assign_user(client_t *client, user_t *user)
{
char uuid_str[37] = {0};

client->user = user;
uuid_unparse(user->uuid, uuid_str);
server_event_user_logged_in(uuid_str);
}

void cmd_login(server_t *server, client_t *client)
{
int ag = 0;
char *name = get_arg(client->buffer, 0, &ag);
user_t *loged = NULL;

if (!name) {
dprintf(client->fd, "503 Syntax error.\n");
return;
}
for (size_t i = 0; i < server->users.size; i ++) {
if (strcmp(name, server->users.arr[i].name) == 0)
loged = &server->users.arr[i];
}
if (loged)
assign_user(client, loged);
else
create_user(server, client, name);
send_info(server, client, "311");
}

void cmd_logout(server_t *server, client_t *client)
{
char uuid_str[37] = {0};

uuid_unparse(client->thread_uuid, uuid_str);
server_event_user_logged_out(uuid_str);
send_info(server, client, "312");
client->user = NULL;
}
30 changes: 30 additions & 0 deletions src/server/cmds/parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** EPITECH PROJECT, 2024
** my_teams
** File description:
** parse
*/

#include "command.h"
#include <stddef.h>

char *get_arg(char *buff, int start, int *end)
{
int i = start;
int j = 0;

if (!buff)
return NULL;
while (buff[i] && buff[i] != '"')
i ++;
if (buff[i] == '\0' || buff[i + 1] == '\0')
return NULL;
i ++;
while (buff[i + j] && buff[i + j] != '"')
j ++;
if (buff[i + j] == '\0')
return NULL;
buff[i + j] = '\0';
*end = i + j;
return buff + i;
}
7 changes: 5 additions & 2 deletions src/server/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "server.h"

void cmd_user(server_t *server, client_t *client);
void cmd_login(server_t *server, client_t *client);
void cmd_logout(server_t *server, client_t *client);

static const struct cmd_s {
char *name;
Expand All @@ -21,8 +23,8 @@ static const struct cmd_s {
{"INFO", NULL,
"based on the context, display details of the current resource"},
{"LIST", NULL, "based on the context, list all the sub resources"},
{"LOGIN", NULL, "set the user_name used by client"},
{"LOGOUT", NULL, "disconnect the client from the server"},
{"LOGIN", cmd_login, "set the user_name used by client"},
{"LOGOUT", cmd_logout, "disconnect the client from the server"},
{"MESSAGES", NULL, "list all messages exchanged with the specified user"},
{"SEND", NULL, "send a message to specific user"},
{"SUBSCRIBE", NULL, "subscribe to the events of a team and its sub "
Expand All @@ -36,3 +38,4 @@ static const struct cmd_s {
};

void exec_command(server_t *server, client_t *client);
char *get_arg(char *buff, int start, int *end);
4 changes: 2 additions & 2 deletions src/server/getters.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
user_t *get_user_by_uuid(server_t *server, uuid_t uuid)
{
for (size_t i = 0; i < server->clients.size; i++)
if (!memcmp(server->clients.arr[i].user.uuid, uuid, sizeof(uuid_t)))
return &server->clients.arr[i].user;
if (!memcmp(server->clients.arr[i].user, uuid, sizeof(uuid_t)))
return server->clients.arr[i].user;
return NULL;
}
2 changes: 1 addition & 1 deletion src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* T *array;
* }
*/
static void append_to_array(void *array, size_t size, void *elem)
void append_to_array(void *array, size_t size, void *elem)
{
size_t *d = array;
size_t *nmemb = &d[0];
Expand Down
15 changes: 10 additions & 5 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,17 @@ typedef struct {
typedef struct {
uuid_t uuid;
char name[MAX_NAME_LENGTH];
bool logged;
uuid_t team_uuid;
uuid_t channel_uuid;
uuid_t thread_uuid;
} user_t;

typedef struct {
int fd;
struct sockaddr_in addr;
socklen_t len;
user_t user;
user_t *user;
char buffer[BUFSIZ];
uuid_t team_uuid;
uuid_t channel_uuid;
uuid_t thread_uuid;
} client_t;

typedef struct {
Expand All @@ -101,6 +100,11 @@ typedef struct {
size_t alloc;
client_t *arr;
} clients;
struct {
size_t size;
size_t alloc;
user_t *arr;
} users;
int fd;
uint16_t port;
struct sockaddr_in addr;
Expand All @@ -109,3 +113,4 @@ typedef struct {
int myteams_server(int argc, char **argv);

user_t *get_user_by_uuid(server_t *server, uuid_t uuid);
void append_to_array(void *array, size_t size, void *elem);

0 comments on commit 5f908d0

Please sign in to comment.