From 5f908d029156142c35eec10626af9cff76095285 Mon Sep 17 00:00:00 2001 From: celian Date: Fri, 12 Apr 2024 18:01:21 +0200 Subject: [PATCH 1/2] feat: handling of login and logout, user are created and stored in the server --- .../{heandle_create.c => handle_create.c} | 0 src/client/run_client.c | 14 ++-- src/client/run_client.h | 1 + src/client/serve_info.c | 29 +++++-- src/server/client.c | 9 +-- src/server/cmds/login.c | 81 +++++++++++++++++++ src/server/cmds/parse.c | 30 +++++++ src/server/command.h | 7 +- src/server/getters.c | 4 +- src/server/server.c | 2 +- src/server/server.h | 15 ++-- 11 files changed, 164 insertions(+), 28 deletions(-) rename src/client/event_handling/{heandle_create.c => handle_create.c} (100%) create mode 100644 src/server/cmds/login.c create mode 100644 src/server/cmds/parse.c diff --git a/src/client/event_handling/heandle_create.c b/src/client/event_handling/handle_create.c similarity index 100% rename from src/client/event_handling/heandle_create.c rename to src/client/event_handling/handle_create.c diff --git a/src/client/run_client.c b/src/client/run_client.c index 34a861d..562ac10 100644 --- a/src/client/run_client.c +++ b/src/client/run_client.c @@ -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; } @@ -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; diff --git a/src/client/run_client.h b/src/client/run_client.h index a4bce25..d92eb0a 100644 --- a/src/client/run_client.h +++ b/src/client/run_client.h @@ -12,6 +12,7 @@ typedef enum run_state { running, cli_exit, + prompt, } run_state_t; run_state_t get_serv_info(connection_t *connect); diff --git a/src/client/serve_info.c b/src/client/serve_info.c index 428e98c..a457b48 100644 --- a/src/client/serve_info.c +++ b/src/client/serve_info.c @@ -6,9 +6,11 @@ */ #include "client.h" +#include "event_handling/handle_event.h" #include "run_client.h" #include "utils.h" #include +#include #include int get_info_type(connection_t *connect, void *buff, ssize_t size) @@ -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; } diff --git a/src/server/client.c b/src/server/client.c index 4cc8c30..f3d6115 100644 --- a/src/server/client.c +++ b/src/server/client.c @@ -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) @@ -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); diff --git a/src/server/cmds/login.c b/src/server/cmds/login.c new file mode 100644 index 0000000..0e23d23 --- /dev/null +++ b/src/server/cmds/login.c @@ -0,0 +1,81 @@ +/* +** EPITECH PROJECT, 2024 +** my_teams +** File description: +** login +*/ + +#include +#include +#include +#include + +#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; +} diff --git a/src/server/cmds/parse.c b/src/server/cmds/parse.c new file mode 100644 index 0000000..29dd071 --- /dev/null +++ b/src/server/cmds/parse.c @@ -0,0 +1,30 @@ +/* +** EPITECH PROJECT, 2024 +** my_teams +** File description: +** parse +*/ + +#include "command.h" +#include + +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; +} diff --git a/src/server/command.h b/src/server/command.h index 684af20..4bce35b 100644 --- a/src/server/command.h +++ b/src/server/command.h @@ -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; @@ -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 " @@ -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); diff --git a/src/server/getters.c b/src/server/getters.c index aacb941..386ba23 100644 --- a/src/server/getters.c +++ b/src/server/getters.c @@ -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; } diff --git a/src/server/server.c b/src/server/server.c index 0aca91c..226beac 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -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]; diff --git a/src/server/server.h b/src/server/server.h index 753bede..e4dbd21 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -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 { @@ -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; @@ -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); From 2c319ea39d071e5a93cd76335c4dec8c98cc801a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <69208565+github-actions[bot]@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:05:06 +0000 Subject: [PATCH 2/2] Fix headers --- src/client/event_handling/handle_create.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/event_handling/handle_create.c b/src/client/event_handling/handle_create.c index 4acdc42..1d679df 100644 --- a/src/client/event_handling/handle_create.c +++ b/src/client/event_handling/handle_create.c @@ -2,7 +2,7 @@ ** EPITECH PROJECT, 2024 ** my_teams ** File description: -** heandle_create +** handle_create */ #include "handle_event.h"