diff --git a/src/main.c b/src/main.c index 3dd4065..c02487c 100644 --- a/src/main.c +++ b/src/main.c @@ -147,11 +147,13 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter else if (connstate == CONN_PENDING) { if (ctime - lastconnect < recon_inter) continue; + lastconnect = ctime; int tmp; if ((tmp = init_conn(conn)) > 0) { fds[0] = tmp; memset(recvbuf, '\0', sizeof(recvbuf)); memset(sendbuf, '\0', sizeof(sendbuf)); + memset(fifobuf, '\0', sizeof(fifobuf)); int flags; if ((flags = fcntl(fds[0], F_GETFL)) != -1) { @@ -198,11 +200,13 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter } } connstate = CONN_REGISTERED; - lastconnect = ctime; - } else if (tmp == INIT_SOFTFAIL) + } else if (tmp == INIT_SOFTFAIL) { connstate = CONN_PENDING; - else if (tmp == INIT_HARDFAIL) + continue; + } else if (tmp == INIT_HARDFAIL) { connstate = CONN_CLOSED; + continue; + } } else if (connstate == CONN_ACTIVE) { if (ctime - lastping >= ping_inter) { char* mesg = "UIRC PONG"; @@ -240,17 +244,34 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter LOG(LOG_DEBUG, "Recieved line: %s", recvbuf); memset((void*)&buffer, '\0', sizeof(IRC_Message)); if (Tok_mesg(recvbuf, &buffer) == 1) { - LOG(LOG_DEBUG, "Tokenized message successfully."); - int categ = get_category(&buffer); - path[1] = category[categ]; - path[2] = NULL; char bufname[MAXPATH + 1], logpath[PATH_MAX], *bprint = "out"; FILE* logfile; - if (mkdir_bottomup(path)) { - if (categ == CAT_CHAN) + LOG(LOG_DEBUG, "Tokenized message successfully."); + + /* Path Assembly and Filtering */ + if (buffer.cmd == PRIVMSG || buffer.cmd == NOTICE) { + if (strchr("&!#+", *buffer.args[0])) { + path[1] = category[CAT_CHAN]; bprint = buffer.args[0]; - else if (categ == CAT_USER) + } else if (buffer.name.nick != NULL) { + path[1] = category[CAT_USER]; bprint = buffer.name.nick; + } + } else if (buffer.cmd == RPL_TOPIC || buffer.cmd == RPL_NOTOPIC || buffer.cmd == MODE || buffer.cmd == TOPIC || buffer.cmd == RPL_BANLIST || buffer.cmd == RPL_ENDOFBANLIST) { + path[1] = category[CAT_CHAN]; + bprint = buffer.args[0]; + } else if (buffer.cmd == KILL || buffer.cmd == RPL_AWAY) { + path[1] = category[CAT_USER]; + bprint = buffer.name.nick; + } else if (buffer.cmd == JOIN || buffer.cmd == PART) { + path[1] = category[CAT_CHAN]; + bprint = buffer.args[0]; + } else { + path[1] = category[CAT_GLOB]; + } + path[2] = NULL; + + if (mkdir_bottomup(path)) { snprintf(bufname, sizeof(bufname), "%s.log", bprint); cleanup_path_names(bufname); path[2] = bufname; @@ -266,38 +287,41 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter LOG(LOG_WARN, "Couldn't open file \"%s\" for appending.", logpath); } } - switch (buffer.cmd) { - case (PING): { - LOG(LOG_VERBOSE, "Auto-replying to ping \"%s\".", buffer.args[0]); - if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PONG(buffer.args[0], NULL), sizeof(sendbuf))) > 0) - if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) { - LOG(LOG_WARN, "Couldn't pong " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno); - continue; - } - break; - } - /* Autojoin channels from current connection on first response from the server */ - case (RPL_WELCOME): { - LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port); - connstate = CONN_ACTIVE; - LOG(LOG_INFO, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port); - IRC_Message* mesg = Assm_cmd_JOIN(conn->data.chans, NULL); - if ((sendbufpos = Assm_mesg(sendbuf, mesg, sizeof(sendbuf))) > 0) - if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) { - LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, conn->data.chans, conn->data.addr, conn->data.port, strerror(errno), errno); - continue; - } - break; - } - case (RPL_BOUNCE): { - break; // TODO: Make bounces work - } - case (ERROR): { - LOG(LOG_ERROR, "Received error on connection " ADDRFMT " with the message \"%s\".", conn->data.addr, conn->data.port, buffer.args[0]); - } - } } else LOG(LOG_WARN, "Log directory couldn't be created. " ERRNOFMT, strerror(errno), errno); + + /* Automatic message handling */ + switch (buffer.cmd) { + case (PING): { + LOG(LOG_VERBOSE, "Auto-replying to ping \"%s\".", buffer.args[0]); + if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PONG(buffer.args[0], NULL), sizeof(sendbuf))) > 0) + if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) { + LOG(LOG_WARN, "Couldn't pong " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno); + continue; + } + break; + } + /* Autojoin channels from current connection on first response from the server */ + case (RPL_WELCOME): { + LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port); + connstate = CONN_ACTIVE; + LOG(LOG_INFO, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port); + IRC_Message* mesg = Assm_cmd_JOIN(conn->data.chans, NULL); + if ((sendbufpos = Assm_mesg(sendbuf, mesg, sizeof(sendbuf))) > 0) + if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) { + LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, conn->data.chans, conn->data.addr, conn->data.port, strerror(errno), errno); + continue; + } + break; + } + case (RPL_BOUNCE): { + break; // TODO: Make bounces work + } + case (ERROR): { + LOG(LOG_ERROR, "Received error on connection " ADDRFMT " with the message \"%s\".", conn->data.addr, conn->data.port, buffer.args[0]); + } + } + } else LOG(LOG_WARN, "Received invalid IRC message (see RFC2812)."); for (char* x = (ppoi + 1); *x; x++) @@ -317,24 +341,23 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter if ((ppoi = strchr(fifobuf, '\n')) != NULL) { *ppoi = '\0'; - LOG(LOG_DEBUG, "Current FIFO for " ADDRFMT "is: %s", conn->data.addr, conn->data.port, fifobuf); if (ppoi > fifobuf && *(ppoi - 1) == '\r') *(ppoi - 1) = '\0'; - LOG(LOG_DEBUG, "Recieved line: %s", fifobuf); + LOG(LOG_DEBUG, "Recieved FIFO line: %s", fifobuf); memset((void*)&buffer, '\0', sizeof(IRC_Message)); if (Tok_mesg(fifobuf, &buffer) == 1) { LOG(LOG_DEBUG, "Tokenized FIFO message successfully.", NULL); - if ((fifobufpos = Assm_mesg(fifobuf, &buffer, sizeof(fifobuf))) > 0) { - if (flush_buffer(fifobuf, fifobufpos, fds[0]) == -1) { + if ((sendbufpos = Assm_mesg(sendbuf, &buffer, sizeof(sendbuf))) > 0) { + if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) { LOG(LOG_WARN, "Couldn't send FIFO input to " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno); continue; } } } else - LOG(LOG_WARN, "Received invalid IRC message (see RFC2812)."); + LOG(LOG_WARN, "Received invalid IRC message on FIFO (see RFC2812)."); for (char* x = (ppoi + 1); *x; x++) *(fifobuf + (x - (ppoi + 1))) = *x; - fifobufpos -= (ppoi + 1) - fifobuf; + fifobufpos -= ppoi + 1 - fifobuf; *(fifobuf + fifobufpos) = '\0'; } } diff --git a/src/misc.c b/src/misc.c index f15f396..5cc3a18 100644 --- a/src/misc.c +++ b/src/misc.c @@ -46,19 +46,3 @@ int get_connstr(char* buf, size_t maxlen, Connection* conn) return 0; return len; } -int get_category(IRC_Message* mesg) -{ - if (mesg->cmd == PRIVMSG || mesg->cmd == NOTICE) { - if (strchr("&!#+", *mesg->args[0])) - return CAT_CHAN; - else if (mesg->name.nick != NULL) - return CAT_USER; - } else if (mesg->cmd == RPL_TOPIC || mesg->cmd == RPL_NOTOPIC || mesg->cmd == MODE || mesg->cmd == TOPIC || mesg->cmd == RPL_BANLIST || mesg->cmd == RPL_ENDOFBANLIST) { - return CAT_CHAN; - } else if (mesg->cmd == KILL || mesg->cmd == RPL_AWAY) { - return CAT_USER; - } else if (mesg->cmd == JOIN || mesg->cmd == PART) { - return CAT_CHAN; - } - return CAT_GLOB; -} diff --git a/src/net.c b/src/net.c index 0781e3d..cc2e900 100644 --- a/src/net.c +++ b/src/net.c @@ -25,25 +25,27 @@ signed int init_conn(Connection* info) struct addrinfo* conn; if ((getaddrres = getaddrinfo(info->data.addr, info->data.port, NULL, &conn)) == -1) { LOG(LOG_ERROR, "Failed to get address info for %s:%s. %s (%i)", info->data.addr, info->data.port, strerror(errno), errno); + freeaddrinfo(conn); if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) { - freeaddrinfo(conn); return INIT_HARDFAIL; - } else + } else return INIT_SOFTFAIL; } if ((sockfd = socket(conn->ai_family, conn->ai_socktype, conn->ai_protocol)) < 0) { LOG(LOG_ERROR, "Failed to open a socket for " ADDRFMT ". " ERRNOFMT, info->data.addr, info->data.port, strerror(errno), errno); + freeaddrinfo(conn); return INIT_HARDFAIL; } if ((connectres = connect(sockfd, conn->ai_addr, conn->ai_addrlen)) == -1) { LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, info->data.addr, info->data.port, strerror(errno), errno); + close(sockfd); + freeaddrinfo(conn); if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) { - close(sockfd); return INIT_HARDFAIL; - } else { + } else return INIT_SOFTFAIL; - } } + freeaddrinfo(conn); return sockfd; }