diff --git a/src/main.c b/src/main.c index fb62bca..eca051e 100644 --- a/src/main.c +++ b/src/main.c @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) break; } } else if (cpid == 0) { - return run_main(&cons[actcon], delay, 10, quitmsg, timeout); + return run_main(&cons[actcon], delay, quitmsg, timeout); } else { LOG(LOG_VERBOSE, "Successfully forked for connection " ADDRFMT ".", cons[actcon].data.addr, cons[actcon].data.port); actcon++; @@ -106,15 +106,15 @@ int main(int argc, char* argv[]) return EXIT_SUCCESS; } -int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg, int timeout) +int run_main(Connection* conn, unsigned int recon_inter, char* quitmsg, int timeout) { IRC_Message buffer; - char sendbuf[MAXLINE + 1], recvbuf[MAXLINE + 1], fifobuf[MAXLINE + 1], /* Buffers */ - connstr[100], *path[4] = {connstr, NULL, 0}; /* Path buffers */ - size_t sendbufpos = 0, recvbufpos = 0, fifobufpos = 0; /* Positions */ - signed int connstate = CONN_PENDING, fds[2] = {-1, -1}; /* Connection state and file descriptors */ - unsigned int lastping = 0, lastconnect = 0, ctime = 0, lastpong = 0, /* Timestamps */ - pinginter = (ping_inter) ? ping_inter : 10, reconinter = (recon_inter) ? recon_inter : 10; /* Intervals */ + char sendbuf[MAXLINE + 1], recvbuf[MAXLINE + 1], fifobuf[MAXLINE + 1], /* Buffers */ + connstr[100], *path[4] = {connstr, NULL, 0}; /* Path buffers */ + size_t sendbufpos = 0, recvbufpos = 0, fifobufpos = 0; /* Positions */ + signed int connstate = CONN_PENDING, fds[2] = {-1, -1}; /* Connection state and file descriptors */ + unsigned int lastping = 0, lastconnect = 0, ctime = 0, lastpong = 0, lastmesg = 0, /* Timestamps */ + reconinter = (recon_inter) ? recon_inter : 10; /* Intervals */ get_connstr(connstr, sizeof(connstr), conn); srand(time(NULL)); @@ -210,12 +210,12 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter continue; } } else if (connstate == CONN_ACTIVE) { - if (lastpong < lastping - timeout) { + if (lastmesg < ctime - timeout && lastpong < lastping - timeout) { LOG(LOG_WARN, "Server " ADDRFMT " didn't respond to a PING in time.", conn->data.addr, conn->data.port); connstate = CONN_PENDING; continue; } - if (ctime - lastping >= ping_inter) { + if (ctime - lastmesg >= timeout / 4 && ctime - lastping >= timeout / 4) { char mesg[] = "uIRC PING -- XXXXXX"; snprintf(mesg + sizeof(mesg) - 7, 7, "%.7i", rand()); if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PING(mesg, NULL), sizeof(sendbuf))) > 0) { @@ -249,6 +249,7 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter if (ppoi > recvbuf && *(ppoi - 1) == '\r') *(ppoi - 1) = '\0'; LOG(LOG_DEBUG, "Recieved line: %s", recvbuf); + lastmesg = ctime; memset((void*)&buffer, '\0', sizeof(IRC_Message)); if (Tok_mesg(recvbuf, &buffer) == 1) { char bufname[MAXPATH + 1], logpath[PATH_MAX], *bprint = "out"; @@ -324,13 +325,16 @@ int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter case (RPL_WELCOME): { LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port); connstate = CONN_ACTIVE; - LOG(LOG_VERBOSE, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port); - if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_JOIN(conn->data.chans, NULL), 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); - connstate = CONN_PENDING; - continue; + if (conn->data.chans != NULL) { + LOG(LOG_VERBOSE, "Auto-joining channels \"%s\" on " ADDRFMT ".", conn->data.chans, conn->data.addr, conn->data.port); + if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_JOIN(conn->data.chans, NULL), 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); + connstate = CONN_PENDING; + continue; + } } + } break; } case (ERROR): { diff --git a/src/main.h b/src/main.h index 37f8a6c..06d6b91 100644 --- a/src/main.h +++ b/src/main.h @@ -30,5 +30,5 @@ #include #define VERSION "indev - testing" -int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg, int timeout); +int run_main(Connection* conn, unsigned int recon_inter, char* quitmsg, int timeout); void print_help(void);