From 4cfaf0ae6fd7a647dc3575649b6a84c9bbfa7f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Fri, 8 Mar 2024 11:17:57 +0100 Subject: [PATCH] Fix HELP command --- src/messages/message.c | 25 +++++++++++++++---------- src/messages/messages.h | 1 + src/messages/misc.c | 26 +++++++++++++++++++------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/messages/message.c b/src/messages/message.c index 36e3885..692c10c 100644 --- a/src/messages/message.c +++ b/src/messages/message.c @@ -34,13 +34,6 @@ static char *clear_msg(const struct msg_s *msg, char *buffer) return buffer; } -static int compare(const char *buffer, const struct msg_s *msg) -{ - return (buffer == NULL || msg == NULL || msg->cmd == NULL) - ? 1 - : strncmp(buffer, msg->cmd, strlen(msg->cmd)); -} - static void data_fork_child( client_t *client, const char *buffer, void (*func)(int, client_t *, const char *)) @@ -79,6 +72,20 @@ void fork_data_sock( } } +static int compare(const char *buffer, const struct msg_s *msg) +{ + return (buffer == NULL || msg == NULL || msg->cmd == NULL) + ? 1 + : strncmp(buffer, msg->cmd, strlen(msg->cmd)); +} + +struct msg_s *get_message(const char *cmd) +{ + return bsearch( + cmd, INCOMMING_MSG, LEN_OF(INCOMMING_MSG), sizeof(*INCOMMING_MSG), + (int (*)(const void *, const void *))compare); +} + void client_process_message(client_t *client, char *buffer) { const struct msg_s *msg = NULL; @@ -86,9 +93,7 @@ void client_process_message(client_t *client, char *buffer) if (client == NULL || buffer == NULL) return; DEBUG("client said: %s", buffer); - msg = bsearch( - buffer, INCOMMING_MSG, LEN_OF(INCOMMING_MSG), sizeof(*INCOMMING_MSG), - (int (*)(const void *, const void *))compare); + msg = get_message(buffer); if (msg == NULL) { client_write(client, MSG_500); return; diff --git a/src/messages/messages.h b/src/messages/messages.h index 5cc8e20..5ad2184 100644 --- a/src/messages/messages.h +++ b/src/messages/messages.h @@ -11,6 +11,7 @@ #include "myftp.h" +struct msg_s *get_message(const char *cmd); void client_process_message(client_t *client, char *buffer); bool client_logged(client_t *client); void fork_data_sock( diff --git a/src/messages/misc.c b/src/messages/misc.c index d7ab360..b400146 100644 --- a/src/messages/misc.c +++ b/src/messages/misc.c @@ -33,19 +33,31 @@ void msg_noop(client_t *client, UNUSED const char *buffer) client_write(client, MSG_200); } -void msg_help(client_t *client, UNUSED const char *buffer) +static const char *desc(const struct msg_s *msg) +{ + static char buf[BUFSIZ]; + + if (msg == NULL || msg->help == NULL) + return "No description available"; + strcpy(buf, msg->cmd); + strcat(buf, " - "); + strcat(buf, msg->help); + return buf; +} + +void msg_help(client_t *client, const char *buffer) { const struct msg_s *msg = NULL; if (client == NULL) return; + if (buffer[0] != '\0') { + client_write(client, MSG_214, desc(get_message(buffer))); + return; + } for (size_t i = 0; i < LEN_OF(INCOMMING_MSG); i++) { msg = &INCOMMING_MSG[i]; - if (strcmp(buffer, msg->cmd) != 0) - continue; - client_write( - client, MSG_214, (msg->help != NULL) ? msg->help : "(null)"); - return; + if (msg->func != NULL) + client_write(client, MSG_214, desc(msg)); } - client_write(client, MSG_500); }