diff --git a/src/server.c b/src/server.c index 491b582..545b96c 100644 --- a/src/server.c +++ b/src/server.c @@ -240,27 +240,10 @@ int cmd_list_connected(int sender_id) { } /* Function for CMD_SEND_MESSAGE */ -int cmd_send_message(char *message, int sender_id) { - int receiver_id; - char *token; - char *endptr = NULL; - char *message_header; +int cmd_send_message(int sender_id, int receiver_id, char* message) { char *formated_message; size_t size_for_malloc; - /* Message should be: " " */ - - /* Extract receiver_id */ - token = strtok_r(message, " ", &message); - receiver_id = strtol(token, &endptr, 10); - if ((receiver_id < 0) || ((receiver_id == 0) && (endptr == token))) { - fprintf(stderr, "Invalid receiver ID \"%s\"\n", token); - return -1; - } - if (clients[receiver_id].id < 0) { - fprintf(stderr, "Receiver \"%d\" does not exist !\n", receiver_id); - } - printf("Message Sender : %d\n", sender_id); printf("Message Receiver : %d\n", receiver_id); printf("Message to send : %s\n", message); @@ -285,7 +268,7 @@ void *Dispatcher(void *arg) { char *token; char *endptr = NULL; char *rest = message; - int n = 0, k = 0, sender_id; + int n = 0, k = 0, sender_id, receiver_id; /* Message should be: " []" */ @@ -324,7 +307,19 @@ void *Dispatcher(void *arg) { break; case 's': printf("cmd_send_message (from %d)\n", sender_id); - cmd_send_message(rest, sender_id); + + /* Extract receiver_id */ + token = strtok_r(rest, " ", &rest); + receiver_id = strtol(token, &endptr, 10); + if ((receiver_id < 0) || ((receiver_id == 0) && (endptr == token))) { + fprintf(stderr, "Invalid receiver ID \"%s\"\n", token); + break; + } + if (clients[receiver_id].id < 0) { + fprintf(stderr, "Receiver \"%d\" does not exist !\n", receiver_id); + break; + } + cmd_send_message(sender_id, receiver_id, rest); break; default: fprintf(stderr, "WARNING: %s is not a command\n", token); diff --git a/src/server.h b/src/server.h index e58d3e3..a36eb6c 100644 --- a/src/server.h +++ b/src/server.h @@ -26,7 +26,7 @@ int disconnect_client(int); int cmd_list_connected(int); /* Function for CMD_SEND_MESSAGE */ -int cmd_send_message(char*, int); +int cmd_send_message(int, int, char*); /* Thread, pop message from the message_buffer pipe, parse it and send it to the good ClientSender */ void *Dispatcher(void*);