From 8d36d6a974caa58656b3a3119f7b95d27d67f50a Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Apr 2025 21:51:40 +0300 Subject: [PATCH] fixed --- C/irc.c | 43 +++++++++++++++++++++---------------------- C/irc.h | 2 +- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/C/irc.c b/C/irc.c index ae7e12e..6f9fb4f 100644 --- a/C/irc.c +++ b/C/irc.c @@ -18,13 +18,13 @@ int IRCC_urecv(IRCC_client *irc) { return recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0); } -int IRCC_usend(IRCC_client *irc, off_t bytes) { +int IRCC_usend(IRCC_client *irc, const char *msg, off_t bytes) { #ifdef ENABLE_SSL if (irc->irc_usingssl) - return SSL_write(irc->irc_ssl, irc->irc_raw, bytes); + return SSL_write(irc->irc_ssl, msg, bytes); #endif - return send(irc->irc_socket, irc->irc_raw, bytes, 0); + return send(irc->irc_socket, msg, bytes, 0); } int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) { @@ -32,7 +32,7 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) { if (!hp) return IRCC_ERROR; - //Only ipv4 + /* Only ipv4 */ struct sockaddr_in client_str; memset(&client_str, 0, sizeof(client_str)); client_str.sin_family = AF_INET; @@ -58,24 +58,24 @@ void IRCC_parse_msg(char *tmp, IRCC_client *irc) { irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0'; if (tmp != NULL) { - //Message + /* Message */ char *val = strchr(tmp, ':'); if (val != NULL) { val[0] = '\0'; irc->irc_msg = val + 1; - //Del space before : + /* Del space before : */ *(val - 1) = '\0'; } - //Channel + /* Channel */ val = strchr(tmp, ' '); if (val != NULL) { val[0] = '\0'; irc->irc_channel = val + 1; } - //Nickname + /* Nickname */ val = strchr(irc->irc_raw, '!'); if (val != NULL) { val[0] = '\0'; @@ -91,6 +91,13 @@ int IRCC_recv(IRCC_client *irc) { if (msg_size == 0 || msg_size == -1) return IRCC_DISCONNECTED; + char *ptr; + if ((ptr = strstr(irc->irc_raw, "PING :")) != NULL) { + *(strchr(ptr, 'I')) = 'O'; + if (IRCC_usend(irc, ptr, strlen(irc->irc_raw)) == -1) + return IRCC_ERROR; + } + return IRCC_SUCCESS; } @@ -99,15 +106,7 @@ int IRCC_parse(IRCC_client *irc) { irc->irc_msg = NULL; irc->irc_nick = NULL; - if (!strncmp(irc->irc_raw, "PING", 4)) { - *(strchr(irc->irc_raw, 'I')) = 'O'; - if (IRCC_usend(irc, strlen(irc->irc_raw)) == -1) - return IRCC_ERROR; - - return IRCC_PING; - } - - //Check end of motd + /* Check end of motd */ if (strstr(irc->irc_raw, "PRIVMSG ") == NULL && strstr(irc->irc_raw, "MOTD")) return IRCC_CONNECTED; @@ -147,14 +146,14 @@ int IRCC_parse(IRCC_client *irc) { int IRCC_register(IRCC_client *irc, const char *nickname) { off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname); - if (IRCC_usend(irc, bytes) == -1) + if (IRCC_usend(irc, irc->irc_raw, bytes) == -1) return IRCC_ERROR; bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname); - if (IRCC_usend(irc, bytes) == -1) + if (IRCC_usend(irc, irc->irc_raw, bytes) == -1) return IRCC_ERROR; - //Motd skip + /* Motd skip */ while (1) { int status = IRCC_recv(irc); if (status == IRCC_DISCONNECTED) @@ -170,7 +169,7 @@ int IRCC_register(IRCC_client *irc, const char *nickname) { int IRCC_join(IRCC_client *irc, const char *channel, const char *key) { off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : ""); - if (IRCC_usend(irc, bytes) == -1) + if (IRCC_usend(irc, irc->irc_raw, bytes) == -1) return IRCC_ERROR; return IRCC_SUCCESS; @@ -178,7 +177,7 @@ int IRCC_join(IRCC_client *irc, const char *channel, const char *key) { int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) { off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg); - if (IRCC_usend(irc, bytes) == -1) + if (IRCC_usend(irc, irc->irc_raw, bytes) == -1) return IRCC_ERROR; return IRCC_SUCCESS; diff --git a/C/irc.h b/C/irc.h index 672788c..ec55041 100644 --- a/C/irc.h +++ b/C/irc.h @@ -59,7 +59,7 @@ int IRCC_initssl(IRCC_client *irc); void IRCC_close(IRCC_client *irc); //u - mean universal. Functions uses internal fields in structure -int IRCC_usend(IRCC_client *irc, off_t bytes); +int IRCC_usend(IRCC_client *irc, const char *msg, off_t bytes); int IRCC_urecv(IRCC_client *irc); #endif