diff --git a/main.c b/main.c index b12ec30..d3936dd 100644 --- a/main.c +++ b/main.c @@ -44,11 +44,14 @@ void WriteToFile(void) { fclose(fp); } -void recvinfo(void) { +void recvinfo(int ignore) { while (1) { unsigned int irc_status = IRCC_recv(&client); - if (irc_status == IRCC_DISCONNECTED) - die("Disconnected"); + if (irc_status == IRCC_DISCONNECTED) { + if (!ignore) + die("Disconnected"); + return; + } else if (client.nick != NULL && client.channel != NULL && client.msg != NULL && irc_status == IRCC_PRIVMSG) WriteToFile(); @@ -68,14 +71,22 @@ int main(void) { if (status == IRCC_ERROR) die("Conn refused"); - //Register and join in channel - recvinfo(); + //Socket timeout + struct timeval tv = {5, 5}; + 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(); + recvinfo(0); sleep(5); for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) { + if (channels[i] == NULL) + break; + size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1; char *tmp = malloc(join_size); if (tmp == NULL) @@ -86,5 +97,6 @@ int main(void) { free(tmp); } - recvinfo(); + recvinfo(0); } +