Split forked main loop into another function
This commit is contained in:
parent
90a7bc1169
commit
cf97130aef
2
lib/uIRC
2
lib/uIRC
|
@ -1 +1 @@
|
|||
Subproject commit 1b46e3895227aed6294f01288ef28f414c0c1a94
|
||||
Subproject commit 5f19f7a4c18ac6deb068aa124e62c318d4a35d3c
|
439
src/main.c
439
src/main.c
|
@ -93,222 +93,7 @@ int main(int argc, char* argv[])
|
|||
break;
|
||||
}
|
||||
} else if (cpid == 0) {
|
||||
IRC_Message buffer;
|
||||
char sendbuf[MAXLINE + 1], recvbuf[MAXLINE + 1], fifobuf[MAXLINE + 1];
|
||||
size_t sendbufpos = 0, recvbufpos = 0, fifobufpos = 0;
|
||||
signed int connstate = CONN_PENDING, socket[2] = {-1, -1};
|
||||
unsigned int lastping = 0, lastconnect = 0, ctime = 0;
|
||||
char connstr[100], *path[4] = {connstr, NULL, 0};
|
||||
get_connstr(connstr, sizeof(connstr), &cons[actcon]);
|
||||
for (;;) {
|
||||
/* Connection manager */
|
||||
ctime = time(NULL);
|
||||
if (!run || connstate == CONN_CLOSING) {
|
||||
if (socket[0] != -1) {
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_QUIT(quitmsg), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a QUIT message to " ADDRFMT " containing \"%s\".", cons[actcon].data.addr, cons[actcon].data.port, quitmsg);
|
||||
if (flush_buffer(sendbuf, sendbufpos, socket[0]) == -1)
|
||||
LOG(LOG_WARN, "Couldn't flush send buffer to " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
}
|
||||
}
|
||||
close(socket[0]);
|
||||
close(socket[1]);
|
||||
connstate = CONN_CLOSED;
|
||||
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", cons[actcon].data.addr, cons[actcon].data.port);
|
||||
break;
|
||||
} else if (connstate == CONN_CLOSED)
|
||||
break;
|
||||
else if (connstate == CONN_PENDING) {
|
||||
if (ctime - lastconnect < delay)
|
||||
continue;
|
||||
connstate = CONN_CLOSED;
|
||||
int tmp;
|
||||
if ((tmp = init_conn(&cons[actcon])) > 0) {
|
||||
socket[0] = tmp;
|
||||
memset(recvbuf, '\0', sizeof(recvbuf));
|
||||
memset(sendbuf, '\0', sizeof(sendbuf));
|
||||
|
||||
int flags;
|
||||
if ((flags = fcntl(socket[0], F_GETFL)) != -1) {
|
||||
if (fcntl(socket[0], F_SETFL, flags | O_NONBLOCK) == -1)
|
||||
LOG(LOG_WARN, "Couldn't set socket to be non-blocking. Messages may be delayed as a result. " ERRNOFMT, strerror(errno), errno);
|
||||
} else
|
||||
LOG(LOG_WARN, "Failed to get socket flags. Messages may be delayed as sockets will block. " ERRNOFMT, strerror(errno), errno);
|
||||
|
||||
/* Create server.port/global for FIFO and open it */
|
||||
path[1] = category[CAT_GLOB];
|
||||
path[2] = NULL;
|
||||
if (mkdir_bottomup(path)) {
|
||||
path[2] = "in";
|
||||
path[3] = NULL;
|
||||
char pbuf[PATH_MAX];
|
||||
int tmp;
|
||||
if (assemble_path(path, pbuf, sizeof(pbuf))) {
|
||||
if ((mkfifo(pbuf, S_IRUSR | S_IWUSR | S_IWGRP)) != 0 && errno != EEXIST) {
|
||||
LOG(LOG_WARN, "Couldn't create FIFO at \"%s\" for input. " ERRNOFMT, pbuf, strerror(errno), errno);
|
||||
} else {
|
||||
LOG(LOG_VERBOSE, "Created a FIFO pipe for input at %s.", pbuf);
|
||||
ssize_t fd;
|
||||
if ((fd = open(pbuf, O_RDONLY | O_NONBLOCK)) != -1)
|
||||
socket[1] = fd;
|
||||
else
|
||||
LOG(LOG_WARN, "Couldn't open FIFO pipe \"%s\" for reading. " ERRNOFMT, pbuf, strerror(errno), errno);
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Couldn't assemble path for FIFO.");
|
||||
}
|
||||
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_NICK(cons[actcon].names.nick), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a NICK registration to " ADDRFMT " containing \"%s\".", cons[actcon].data.addr, cons[actcon].data.port, cons[actcon].names.nick);
|
||||
if (flush_buffer(sendbuf, sendbufpos, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register nickname on " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_USER(cons[actcon].names.user, cons[actcon].names.real, 0), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a USER registration to " ADDRFMT " containing \"%s\".", cons[actcon].data.addr, cons[actcon].data.port, cons[actcon].names.real);
|
||||
if (flush_buffer(sendbuf, sendbufpos, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register user and real name on " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", cons[actcon].data.addr, cons[actcon].data.port);
|
||||
connstate = CONN_ACTIVE;
|
||||
lastconnect = ctime;
|
||||
} else
|
||||
break;
|
||||
} else if (connstate == CONN_ACTIVE) {
|
||||
if (ctime - lastping >= 10) {
|
||||
char* mesg = "UIRC PONG";
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PING(mesg, NULL), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_DEBUG, "Sending ping to " ADDRFMT " with message \"%s\"", cons[actcon].data.addr, cons[actcon].data.port, mesg);
|
||||
if (flush_buffer(sendbuf, sendbufpos, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't ping " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
lastping = ctime;
|
||||
}
|
||||
}
|
||||
struct timespec sleep = {0, 10000000L};
|
||||
nanosleep(&sleep, NULL);
|
||||
}
|
||||
|
||||
/* Buffer reader */
|
||||
ssize_t brd;
|
||||
if ((brd = read(socket[0], recvbuf + recvbufpos, sizeof(recvbuf) - recvbufpos - 1)) > 0) {
|
||||
LOG(LOG_DEBUG, "Read %li bytes.", brd);
|
||||
*(recvbuf + (recvbufpos += brd)) = '\0';
|
||||
} else if (brd == 0) {
|
||||
connstate = CONN_PENDING;
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read inbound traffic. " ERRNOFMT, strerror(errno), errno);
|
||||
connstate = CONN_PENDING;
|
||||
continue;
|
||||
}
|
||||
|
||||
char* ppoi;
|
||||
if ((ppoi = strchr(recvbuf, '\n')) != NULL) {
|
||||
*ppoi = '\0';
|
||||
if (ppoi > recvbuf && *(ppoi - 1) == '\r')
|
||||
*(ppoi - 1) = '\0';
|
||||
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.", NULL);
|
||||
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)
|
||||
bprint = buffer.args[0];
|
||||
else if (categ == CAT_USER)
|
||||
bprint = buffer.name.nick;
|
||||
snprintf(bufname, sizeof(bufname), "%s.log", bprint);
|
||||
cleanup_path_names(bufname);
|
||||
path[2] = bufname;
|
||||
path[3] = NULL;
|
||||
if (assemble_path(path, logpath, sizeof(logpath))) {
|
||||
char linebuf[513];
|
||||
if (Assm_mesg(sendbuf, &buffer, sizeof(sendbuf)) > 0) {
|
||||
LOG(LOG_DEBUG, "Assembled line: \"%s\" to write to %s.", sendbuf, bufname);
|
||||
if ((logfile = fopen(logpath, "a")) != NULL) {
|
||||
fprintf(logfile, "%s", sendbuf);
|
||||
fclose(logfile);
|
||||
} else
|
||||
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, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't pong " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* Autojoin channels from current connection on first response from the server */
|
||||
case (RPL_WELCOME): {
|
||||
LOG(LOG_INFO, "Auto-joining channels \"%s\" on " ADDRFMT ".", cons[actcon].data.chans, cons[actcon].data.addr, cons[actcon].data.port);
|
||||
IRC_Message* mesg = Assm_cmd_JOIN(cons[actcon].data.chans, NULL);
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, mesg, sizeof(sendbuf))) > 0)
|
||||
if (flush_buffer(sendbuf, sendbufpos, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, cons[actcon].data.chans, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case (RPL_BOUNCE): {
|
||||
break; // TODO: Make bounces work
|
||||
}
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Log directory couldn't be created. " ERRNOFMT, strerror(errno), errno);
|
||||
} else
|
||||
LOG(LOG_WARN, "Received invalid IRC message (see RFC2812).");
|
||||
for (char* x = (ppoi + 1); *x; x++)
|
||||
*(recvbuf + (x - (ppoi + 1))) = *x;
|
||||
recvbufpos -= (ppoi + 1) - recvbuf;
|
||||
*(recvbuf + recvbufpos) = '\0';
|
||||
}
|
||||
|
||||
/* Buffer writer */
|
||||
if ((brd = read(socket[1], fifobuf + fifobufpos, sizeof(fifobuf) - fifobufpos - 1)) > 0) {
|
||||
LOG(LOG_DEBUG, "Read %li bytes from FIFO.", brd);
|
||||
*(fifobuf + (fifobufpos += brd)) = '\0';
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read FIFO input for connection " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((ppoi = strchr(fifobuf, '\n')) != NULL) {
|
||||
*ppoi = '\0';
|
||||
LOG(LOG_DEBUG, "Current FIFO for " ADDRFMT "is: %s", cons[actcon].data.addr, cons[actcon].data.port, fifobuf);
|
||||
if (ppoi > fifobuf && *(ppoi - 1) == '\r')
|
||||
*(ppoi - 1) = '\0';
|
||||
LOG(LOG_DEBUG, "Recieved 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, socket[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't send FIFO input to " ADDRFMT ". " ERRNOFMT, cons[actcon].data.addr, cons[actcon].data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Received invalid IRC message (see RFC2812).");
|
||||
for (char* x = (ppoi + 1); *x; x++)
|
||||
*(fifobuf + (x - (ppoi + 1))) = *x;
|
||||
fifobufpos -= (ppoi + 1) - fifobuf;
|
||||
*(fifobuf + fifobufpos) = '\0';
|
||||
}
|
||||
}
|
||||
LOG(LOG_VERBOSE, "Exiting thread with connection " ADDRFMT " gracefully.", cons[actcon].data.addr, cons[actcon].data.port);
|
||||
return EXIT_SUCCESS;
|
||||
return run_main(&cons[actcon], delay, 10, quitmsg);
|
||||
} else {
|
||||
LOG(LOG_VERBOSE, "Successfully forked for connection " ADDRFMT ".", cons[actcon].data.addr, cons[actcon].data.port);
|
||||
actcon++;
|
||||
|
@ -321,3 +106,225 @@ int main(int argc, char* argv[])
|
|||
LOG(LOG_VERBOSE, "Exiting gracefully.");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg)
|
||||
{
|
||||
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, /* Timestamps */
|
||||
pinginter = (ping_inter) ? ping_inter : 10, reconinter = (recon_inter) ? recon_inter : 10; /* Intervals */
|
||||
|
||||
get_connstr(connstr, sizeof(connstr), conn);
|
||||
for (;;) {
|
||||
/* Connection manager */
|
||||
ctime = time(NULL);
|
||||
if (!run || connstate == CONN_CLOSING) {
|
||||
if (fds[0] != -1) {
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_QUIT(quitmsg), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a QUIT message to " ADDRFMT " containing \"%s\".", conn->data.addr, conn->data.port, quitmsg);
|
||||
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1)
|
||||
LOG(LOG_WARN, "Couldn't flush send buffer to " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno);
|
||||
}
|
||||
}
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
connstate = CONN_CLOSED;
|
||||
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", conn->data.addr, conn->data.port);
|
||||
break;
|
||||
} else if (connstate == CONN_CLOSED)
|
||||
break;
|
||||
else if (connstate == CONN_PENDING) {
|
||||
if (ctime - lastconnect < recon_inter)
|
||||
continue;
|
||||
connstate = CONN_CLOSED;
|
||||
int tmp;
|
||||
if ((tmp = init_conn(conn)) > 0) {
|
||||
fds[0] = tmp;
|
||||
memset(recvbuf, '\0', sizeof(recvbuf));
|
||||
memset(sendbuf, '\0', sizeof(sendbuf));
|
||||
|
||||
int flags;
|
||||
if ((flags = fcntl(fds[0], F_GETFL)) != -1) {
|
||||
if (fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == -1)
|
||||
LOG(LOG_WARN, "Couldn't set socket to be non-blocking. Messages may be delayed as a result. " ERRNOFMT, strerror(errno), errno);
|
||||
} else
|
||||
LOG(LOG_WARN, "Failed to get socket flags. Messages may be delayed as sockets will block. " ERRNOFMT, strerror(errno), errno);
|
||||
|
||||
/* Create server.port/global for FIFO and open it */
|
||||
path[1] = category[CAT_GLOB];
|
||||
path[2] = NULL;
|
||||
if (mkdir_bottomup(path)) {
|
||||
path[2] = "in";
|
||||
path[3] = NULL;
|
||||
char pbuf[PATH_MAX];
|
||||
int tmp;
|
||||
if (assemble_path(path, pbuf, sizeof(pbuf))) {
|
||||
if ((mkfifo(pbuf, S_IRUSR | S_IWUSR | S_IWGRP)) != 0 && errno != EEXIST) {
|
||||
LOG(LOG_WARN, "Couldn't create FIFO at \"%s\" for input. " ERRNOFMT, pbuf, strerror(errno), errno);
|
||||
} else {
|
||||
LOG(LOG_VERBOSE, "Created a FIFO pipe for input at %s.", pbuf);
|
||||
ssize_t fd;
|
||||
if ((fd = open(pbuf, O_RDONLY | O_NONBLOCK)) != -1)
|
||||
fds[1] = fd;
|
||||
else
|
||||
LOG(LOG_WARN, "Couldn't open FIFO pipe \"%s\" for reading. " ERRNOFMT, pbuf, strerror(errno), errno);
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Couldn't assemble path for FIFO.");
|
||||
}
|
||||
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_NICK(conn->names.nick), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a NICK registration to " ADDRFMT " containing \"%s\".", conn->data.addr, conn->data.port, conn->names.nick);
|
||||
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register nickname on " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_USER(conn->names.user, conn->names.real, 0), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_VERBOSE, "Sending a USER registration to " ADDRFMT " containing \"%s\".", conn->data.addr, conn->data.port, conn->names.real);
|
||||
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register user and real name on " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.addr, conn->data.port);
|
||||
connstate = CONN_ACTIVE;
|
||||
lastconnect = ctime;
|
||||
} else
|
||||
break;
|
||||
} else if (connstate == CONN_ACTIVE) {
|
||||
if (ctime - lastping >= ping_inter) {
|
||||
char* mesg = "UIRC PONG";
|
||||
if ((sendbufpos = Assm_mesg(sendbuf, Assm_cmd_PING(mesg, NULL), sizeof(sendbuf))) > 0) {
|
||||
LOG(LOG_DEBUG, "Sending ping to " ADDRFMT " with message \"%s\"", conn->data.addr, conn->data.port, mesg);
|
||||
if (flush_buffer(sendbuf, sendbufpos, fds[0]) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't ping " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
lastping = ctime;
|
||||
}
|
||||
}
|
||||
struct timespec sleep = {0, 10000000L};
|
||||
nanosleep(&sleep, NULL);
|
||||
}
|
||||
|
||||
/* Buffer reader */
|
||||
ssize_t brd;
|
||||
if ((brd = read(fds[0], recvbuf + recvbufpos, sizeof(recvbuf) - recvbufpos - 1)) > 0) {
|
||||
LOG(LOG_DEBUG, "Read %li bytes.", brd);
|
||||
*(recvbuf + (recvbufpos += brd)) = '\0';
|
||||
} else if (brd == 0) {
|
||||
connstate = CONN_PENDING;
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read inbound traffic. " ERRNOFMT, strerror(errno), errno);
|
||||
connstate = CONN_PENDING;
|
||||
continue;
|
||||
}
|
||||
|
||||
char* ppoi;
|
||||
if ((ppoi = strchr(recvbuf, '\n')) != NULL) {
|
||||
*ppoi = '\0';
|
||||
if (ppoi > recvbuf && *(ppoi - 1) == '\r')
|
||||
*(ppoi - 1) = '\0';
|
||||
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.", NULL);
|
||||
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)
|
||||
bprint = buffer.args[0];
|
||||
else if (categ == CAT_USER)
|
||||
bprint = buffer.name.nick;
|
||||
snprintf(bufname, sizeof(bufname), "%s.log", bprint);
|
||||
cleanup_path_names(bufname);
|
||||
path[2] = bufname;
|
||||
path[3] = NULL;
|
||||
if (assemble_path(path, logpath, sizeof(logpath))) {
|
||||
char linebuf[513];
|
||||
if (Assm_mesg(sendbuf, &buffer, sizeof(sendbuf)) > 0) {
|
||||
LOG(LOG_DEBUG, "Assembled line: \"%s\" to write to %s.", sendbuf, bufname);
|
||||
if ((logfile = fopen(logpath, "a")) != NULL) {
|
||||
fprintf(logfile, "%s", sendbuf);
|
||||
fclose(logfile);
|
||||
} else
|
||||
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, "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
|
||||
}
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Log directory couldn't be created. " ERRNOFMT, strerror(errno), errno);
|
||||
} else
|
||||
LOG(LOG_WARN, "Received invalid IRC message (see RFC2812).");
|
||||
for (char* x = (ppoi + 1); *x; x++)
|
||||
*(recvbuf + (x - (ppoi + 1))) = *x;
|
||||
recvbufpos -= (ppoi + 1) - recvbuf;
|
||||
*(recvbuf + recvbufpos) = '\0';
|
||||
}
|
||||
|
||||
/* Buffer writer */
|
||||
if ((brd = read(fds[1], fifobuf + fifobufpos, sizeof(fifobuf) - fifobufpos - 1)) > 0) {
|
||||
LOG(LOG_DEBUG, "Read %li bytes from FIFO.", brd);
|
||||
*(fifobuf + (fifobufpos += brd)) = '\0';
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read FIFO input for connection " ADDRFMT ". " ERRNOFMT, conn->data.addr, conn->data.port, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
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) {
|
||||
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).");
|
||||
for (char* x = (ppoi + 1); *x; x++)
|
||||
*(fifobuf + (x - (ppoi + 1))) = *x;
|
||||
fifobufpos -= (ppoi + 1) - fifobuf;
|
||||
*(fifobuf + fifobufpos) = '\0';
|
||||
}
|
||||
}
|
||||
LOG(LOG_VERBOSE, "Exiting thread with connection " ADDRFMT " gracefully.", conn->data.addr, conn->data.port);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
@ -30,3 +30,4 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#define VERSION "indev - testing"
|
||||
int run_main(Connection* conn, unsigned int recon_inter, unsigned int ping_inter, char* quitmsg);
|
||||
|
|
Reference in New Issue