From 73b55ebd79bf1e2b58162c67d96f33d636d5dc34 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 28 Jun 2025 10:58:12 +0300 Subject: [PATCH] fixed --- C/irc.c | 38 ++++++++----------------- C/irc.h | 85 ++++++++++++++++++++++++++++++++++++++------------------ C/main.c | 44 +++++++++++++++++++++-------- 3 files changed, 103 insertions(+), 64 deletions(-) diff --git a/C/irc.c b/C/irc.c index 271184d..7ab8c52 100644 --- a/C/irc.c +++ b/C/irc.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -57,11 +58,12 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const int port) { return IRCC_ERROR; } - irc->irc_alloc = 1; return IRCC_SUCCESS; } void IRCC_parse_msg(char *tmp, IRCC_client *irc) { + irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0'; + if (tmp != NULL) { /* Message */ char *val = strchr(tmp, ':'); @@ -79,7 +81,7 @@ void IRCC_parse_msg(char *tmp, IRCC_client *irc) { val[0] = '\0'; irc->irc_channel = val + 1; } - + /* Nickname */ val = strchr(irc->irc_raw, '!'); if (val != NULL) { @@ -94,6 +96,7 @@ int IRCC_recv(IRCC_client *irc) { free(irc->irc_raw); irc->irc_raw = NULL; + irc->irc_alloc = 0; size_t size = 0; char *buf = malloc(1); @@ -127,6 +130,7 @@ int IRCC_recv(IRCC_client *irc) { } irc->irc_raw = buf; + irc->irc_alloc = 1; return IRCC_SUCCESS; } @@ -182,32 +186,14 @@ int IRCC_parse(IRCC_client *irc) { return IRCC_SUCCESS; } -int IRCC_register(IRCC_client *irc, const char *nickname) { +int IRCC_send(IRCC_client *irc, const char *fmt, ...) { + va_list args; + va_start(args, fmt); + char buf[512]; + ssize_t bytes = vsnprintf(buf, sizeof(buf) - 1, fmt, args); + va_end(args); - int bytes = snprintf(buf, sizeof(buf) - 1, "NICK %s\r\n", nickname); - if (IRCC_usend(irc, buf, bytes) < 0) - return IRCC_ERROR; - - bytes = snprintf(buf, sizeof(buf) - 1, "USER %s 0 localhost :%s\r\n", nickname, nickname); - if (IRCC_usend(irc, buf, bytes) < 0) - return IRCC_ERROR; - - return IRCC_SUCCESS; -} - -int IRCC_join(IRCC_client *irc, const char *channel, const char *key) { - char buf[512]; - ssize_t bytes = snprintf(buf, sizeof(buf) - 1, "JOIN %s %s\r\n", channel, (key) ? key : ""); - if (IRCC_usend(irc, buf, bytes) < 0) - return IRCC_ERROR; - - return IRCC_SUCCESS; -} - -int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) { - char buf[512]; - ssize_t bytes = snprintf(buf, sizeof(buf) - 1, "PRIVMSG %s :%s\r\n", channel, msg); if (IRCC_usend(irc, buf, bytes) < 0) return IRCC_ERROR; diff --git a/C/irc.h b/C/irc.h index 04a6096..4b07548 100644 --- a/C/irc.h +++ b/C/irc.h @@ -4,15 +4,15 @@ Dont support: ddc ipv6 - motd */ #ifndef _IRCC_H #define _IRCC_H #define IRCC_PING_TIMEOUT 600 -#define IRCC_VERSION "2.1.0b" +#define IRCC_VERSION "2.1.0" +#include #if defined(ENABLE_SSL) || defined(ENABLE_TLS) #include #endif @@ -34,43 +34,74 @@ enum { IRCC_MODE }; -static const char *IRCC_ERROR_MSGS[] = { - "IRCC_ALLOC", - "IRCC_DISCONNECTED", - "IRCC_ERROR", -}; -#define IRCC_GET_ERROR(n) (IRCC_ERROR_MSGS[n + 3]) +#ifndef IRCC_FMT +#define IRCC_FMT + enum { + IRCC_FMT_SEND, + IRCC_FMT_TOPIC, + IRCC_FMT_USER, + IRCC_FMT_NICK, + IRCC_FMT_PASS, + IRCC_FMT_JOIN, + IRCC_FMT_JOIN_PASS, + _IRCC_FMT_MAX + }; -typedef struct { - int irc_connected; - int irc_socket; + static const char *IRCC_FMTS[] = { + "PRIVMSG %s :%s\r\n", + "TOPIC %s :%s\r\n", + "USER %s %s %s %s\r\n", + "NICK %s\r\n", + "PASS %s\r\n", + "JOIN %s\r\n", + "JOIN %s :%s\r\n", + }; - int irc_alloc; - char *irc_raw; + #define IRCC_GET_CMD(n) ((n >= _IRCC_FMT_MAX) ? "error" : IRCC_FMTS[n]) +#endif - /* RONLY */ - char *irc_msg; - char *irc_channel; - char *irc_nick; +#ifndef IRCC_ERR_STR +#define IRCC_ERR_STR + static const char *IRCC_ERROR_MSGS[] = { + "IRCC_ALLOC", + "IRCC_DISCONNECTED", + "IRCC_ERROR", + }; + + #define IRCC_GET_ERROR(n) ((n >= sizeof(IRCC_ERROR_MSGS) / sizeof(char *)) ? "error" : IRCC_ERROR_MSGS[n + 3]) +#endif + +#ifndef IRCC_CLIENT +#define IRCC_CLIENT + typedef struct { + int irc_connected; + int irc_socket; + + int irc_alloc; + char *irc_raw; - #if defined(ENABLE_SSL) || defined(ENABLE_TLS) /* RONLY */ - int irc_usingssl; + char *irc_msg; + char *irc_channel; + char *irc_nick; - SSL_METHOD *irc_sslmethod; - SSL_CTX *irc_sslctx; - SSL *irc_ssl; - #endif + #if defined(ENABLE_SSL) || defined(ENABLE_TLS) + /* RONLY */ + int irc_usingssl; -} IRCC_client; + SSL_METHOD *irc_sslmethod; + SSL_CTX *irc_sslctx; + SSL *irc_ssl; + #endif + + } IRCC_client; +#endif int IRCC_connect(IRCC_client *irc, const char *ip, const int port); int IRCC_recv(IRCC_client *irc); int IRCC_parse(IRCC_client *irc); -int IRCC_register(IRCC_client *irc, const char *nickname); -int IRCC_join(IRCC_client *irc, const char *channel, const char *key); -int IRCC_send(IRCC_client *irc, const char *channel, const char *msg); +int IRCC_send(IRCC_client *irc, const char *fmt, ...); int IRCC_initssl(IRCC_client *irc); void IRCC_close(IRCC_client *irc); diff --git a/C/main.c b/C/main.c index aed6668..1e6661a 100644 --- a/C/main.c +++ b/C/main.c @@ -6,10 +6,13 @@ #include "irc.h" /* Config */ -#define HOST "irc.rizon.net" +#define HOST "192.168.0.136" #define PORT 6667 -#define NICK "tester134" -char *channels[] = {"#testo"}; +#define NICK "tester" +#define PASS NULL +char *channels[] = { + "#test" +}; #define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL) IRCC_client client; @@ -34,6 +37,8 @@ void recvinfo(void) { die("Disconnected"); irc_status = IRCC_parse(&client); + if (irc_status < 0) + die("Parser"); /* Print */ if (CHECK_NULL() && irc_status == IRCC_PRIVMSG) @@ -46,10 +51,10 @@ void recvinfo(void) { printf("[T] %s\n", client.irc_msg); else if (client.irc_nick != NULL && irc_status == IRCC_JOIN) - printf("[>] %s\n", client.irc_nick); + printf("[>] %s (%s)\n", client.irc_nick, client.irc_msg); else if (client.irc_nick != NULL && irc_status == IRCC_QUIT) - printf("[<] %s\n", client.irc_nick); + printf("[<] %s (%s)\n", client.irc_nick, client.irc_msg); else printf("> %s\n", client.irc_raw); @@ -60,23 +65,40 @@ int main(void) { signal(SIGINT, handler); int status = IRCC_connect(&client, HOST, PORT); - if (status == IRCC_ERROR) + if (status < 0) die("Conn refused"); - if (IRCC_initssl(&client) == IRCC_ERROR) { + if (IRCC_initssl(&client) < 0) { IRCC_close(&client); die("ssl error"); } - /* Register and skip motd */ - IRCC_register(&client, NICK); + if (PASS) { + IRCC_send(&client, + IRCC_GET_CMD(IRCC_FMT_PASS), + PASS); + } + + /* Registration */ + sleep(1); + IRCC_send(&client, + IRCC_GET_CMD(IRCC_FMT_USER), + NICK, + NICK, + NICK, + NICK); + + IRCC_send(&client, + IRCC_GET_CMD(IRCC_FMT_NICK), + NICK); - sleep(5); for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) { if (channels[i] == NULL) break; - IRCC_join(&client, channels[i], NULL); + IRCC_send(&client, + IRCC_GET_CMD(IRCC_FMT_JOIN), + channels[i]); } recvinfo();