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(); }