From a50ebb3d2639fb6636fd55ecb144684ae57d8ffd Mon Sep 17 00:00:00 2001 From: 8nlight <8nlight@disroot.org> Date: Thu, 17 Aug 2023 10:01:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?C=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C/irc.c | 1 + C/irc.h | 1 + C/main.c | 34 ++++++++++++++-------------------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/C/irc.c b/C/irc.c index c5f0893..1c553b0 100644 --- a/C/irc.c +++ b/C/irc.c @@ -31,6 +31,7 @@ unsigned int IRCC_register(IRCC_client *irc, const char *nickname){ exit(1); } + sleep(2); snprintf(tmp, size, "NICK %s\r\nUSER %s 0 localhost :%s\r\n", nickname, nickname, nickname); send(irc->socket, tmp, strlen(tmp), 0); diff --git a/C/irc.h b/C/irc.h index 5968aab..a451bb2 100644 --- a/C/irc.h +++ b/C/irc.h @@ -20,6 +20,7 @@ #include #include #define IRCC_MSG_MAX 512 +#define IRCC_PING_TIMEOUT 600 enum { IRCC_NICK = 1, diff --git a/C/main.c b/C/main.c index ac4cda1..c083efb 100644 --- a/C/main.c +++ b/C/main.c @@ -1,10 +1,11 @@ #include "irc.h" //Config -#define HOST "irc.server.net" +#define HOST "localhost" #define PORT 6667 -#define NICK "tester" -char *channels[] = {"#test"}; +#define NICK "tester134" +char *channels[] = {"#channel"}; + #define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL) IRCC_client client; @@ -15,29 +16,23 @@ void die(char *msg) { exit(1); } -void recvinfo(int ignore) { +void recvinfo(void) { while (1) { unsigned int irc_status = IRCC_recv(&client); - if (irc_status == IRCC_DISCONNECTED) { - if (!ignore) - die("Disconnected"); - return; - } + if (irc_status == IRCC_DISCONNECTED) + die("Disconnected"); - else if (strstr(client.raw, "No Ident") || irc_status == IRCC_CONNECTED) + else if (irc_status == IRCC_CONNECTED) return; //Print - if (!CHECK_NULL()) - return; - - if (irc_status == IRCC_PRIVMSG) + if (CHECK_NULL() && irc_status == IRCC_PRIVMSG) printf("[%s %s] %s\n", client.channel, client.nick, client.msg); - else if (irc_status == IRCC_NICK) + else if (CHECK_NULL() && irc_status == IRCC_NICK) printf("[N] %s is know as %s\n", client.nick, client.msg); - else if (irc_status == IRCC_TOPIC) + else if (CHECK_NULL() && irc_status == IRCC_TOPIC) printf("[T] %s\n", client.msg); else if (irc_status == IRCC_JOIN) @@ -57,16 +52,15 @@ int main(void) { die("Conn refused"); //Socket timeout - struct timeval tv = {5, 5}; + struct timeval tv = {IRCC_PING_TIMEOUT, IRCC_PING_TIMEOUT}; if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) die("setsockopt"); //Register - recvinfo(1); IRCC_register(&client, NICK); //Recv motd and join in channel - recvinfo(0); + recvinfo(); sleep(5); for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) { if (channels[i] == NULL) @@ -82,6 +76,6 @@ int main(void) { free(tmp); } - recvinfo(0); + recvinfo(); }