Finish off logging

This commit is contained in:
Alex D. 2020-12-21 21:40:43 +00:00
parent 677403b2f3
commit 25345574e2
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
5 changed files with 77 additions and 32 deletions

View File

@ -35,4 +35,4 @@ endif()
target_link_libraries(uircd ${UIRC_PATH})
set_property(TARGET uircd PROPERTY C_STANDARD 99)
install(TARGETS RUNTIME)
install(TARGETS uircd RUNTIME)

View File

@ -90,6 +90,17 @@ get_msgchannel(IRC_Message* mesg)
case PRIVMSG:
case NOTICE: {
if (CHANNELMASK(mesg->args[0])) return 0;
break;
}
case RPL_TOPIC:
case RPL_NOTOPIC:
case RPL_ENDOFNAMES: {
if (CHANNELMASK(mesg->args[1])) return 1;
break;
}
case RPL_NAMREPLY: {
if (CHANNELMASK(mesg->args[2])) return 2;
break;
}
}
return -1;
@ -102,7 +113,9 @@ get_categ(IRC_Message* mesg)
case JOIN:
case PART:
case KICK: return "events";
case TOPIC: return "topic";
case TOPIC:
case RPL_TOPIC:
case RPL_NOTOPIC: return "topic";
case PRIVMSG:
case NOTICE: return "msgs";
case RPL_MOTD:
@ -111,6 +124,8 @@ get_categ(IRC_Message* mesg)
case ERR_NOMOTD: return "motd";
case PING:
case PONG: return "pings";
case RPL_NAMREPLY:
case RPL_ENDOFNAMES: return "names";
default: return NULL;
}
}

View File

@ -64,7 +64,8 @@ write_log(const char* path, const char* message)
{
FILE* logfile;
if ((logfile = fopen(path, "a")) != NULL) {
fprintf(logfile, "%s\r\n", message);
fprintf(logfile, "%s", message);
LOG(LOG_DEBUG, "Wrote to log %s.", path);
fclose(logfile);
return 1;
} else
@ -102,7 +103,7 @@ add_socket_flags(int fd, int flags)
}
ssize_t
get_log_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global, bool input)
get_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global, bool input, bool output)
{
if (buf == NULL || conn == NULL) return -1;
ssize_t res;
@ -111,6 +112,7 @@ get_log_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool glo
|| (res = snprintf(buf, lim, "%s.%s", conn->data.address, conn->data.service)) == -1)
return -1;
clean_and_push(&lim, (size_t) res, &buf);
int ci = -1;
if (global) {
if ((res = snprintf(buf, lim, "global")) == -1) return -1;
@ -122,18 +124,19 @@ get_log_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool glo
if ((res = snprintf(buf, lim, "user")) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf);
if ((res = snprintf(buf, lim, "%s", msg->name.nick)) == -1) return -1;
} else {
} else
return -1;
}
clean_and_push(&lim, (size_t) res, &buf);
const char* category = NULL;
if (input) {
if ((res = snprintf(buf, lim, "in")) == -1) return -1;
} else if (output) {
if ((res = snprintf(buf, lim, "out")) == -1) return -1;
} else if ((category = get_categ(msg)) != NULL) {
if ((res = snprintf(buf, lim, "%s", category)) == -1) return -1;
} else {
if ((res = snprintf(buf, lim, "out")) == -1) return -1;
}
} else
return -1;
return (buf - save);
}

View File

@ -38,7 +38,7 @@ int makeinput(const char* path);
bool write_log(const char* path, const char* message);
bool cleanup_path_names(char* name);
bool add_socket_flags(int fd, int flags);
ssize_t get_log_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global, bool input);
ssize_t get_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global, bool input, bool output);
void clean_and_push(size_t* lim, size_t res, char** buf);
#endif /* UIRCD_GUARD_FILESYSTEM */

View File

@ -106,7 +106,7 @@ main(int argc, char* argv[])
time_t ctime;
struct timespec sleep = { 0, 50000000L };
struct {
Buffer_Info recv, send, input;
Buffer_Info recv, send, fifo, log;
} buf;
init_chanarray(&connection.info.channels);
@ -119,6 +119,15 @@ main(int argc, char* argv[])
case 0: return EXIT_SUCCESS;
case -1: return EXIT_FAILURE;
}
if (connection.data.path != NULL) {
int tmpres;
// TODO: Add chroot()/jail support by default where supported
if ((tmpres = chdir(connection.data.path)) != 0) {
LOG(LOG_ERROR, "Couldn't change log directory to %s. " ERRNOFMT, connection.data.path, strerror(errno), errno);
return EXIT_FAILURE;
}
LOG(LOG_VERBOSE, "Changed directory to %s.", connection.data.path);
}
setup_signals();
for (;;) {
ctime = time(NULL);
@ -142,6 +151,7 @@ main(int argc, char* argv[])
errno);
}
close(buf.send.fd);
close(buf.fifo.fd);
connection.info.state = CONN_CLOSED;
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", connection.data.address, connection.data.service);
break;
@ -149,6 +159,7 @@ main(int argc, char* argv[])
break;
else if (connection.info.state == CONN_RECONNECTING) {
close(buf.send.fd);
close(buf.fifo.fd);
connection.info.state = CONN_PENDING;
if (connection.info.reconinter <= 300) connection.info.reconinter += 5;
continue;
@ -160,12 +171,11 @@ main(int argc, char* argv[])
memset(&buf, '\0', sizeof(buf));
buf.recv.fd = -1;
buf.send.fd = -1;
buf.input.fd = STDIN_FILENO;
buf.fifo.fd = -1;
connection.info.l_ping = 0;
connection.info.l_pong = 0;
connection.info.l_message = 0;
add_socket_flags(buf.input.fd, O_NONBLOCK);
if ((buf.send.fd = init_connection(&connection)) > 0) {
buf.recv.fd = buf.send.fd;
add_socket_flags(buf.send.fd, O_NONBLOCK);
@ -281,31 +291,31 @@ main(int argc, char* argv[])
}
}
if (buf.input.fd != -1) {
if (buf.fifo.fd != -1) {
ssize_t brd, len;
/* Buffer writer */
if ((brd = read(
buf.input.fd, buf.input.buffer + buf.input.append_pos, sizeof(buf.input.buffer) - buf.input.append_pos - 1))
if ((brd =
read(buf.fifo.fd, buf.fifo.buffer + buf.fifo.append_pos, sizeof(buf.fifo.buffer) - buf.fifo.append_pos - 1))
> 0) {
*(buf.input.buffer + (buf.input.append_pos += (size_t) brd)) = '\0';
LOG(LOG_DEBUG, "Read %li bytes from stdin. Now appending at %li", brd, buf.input.append_pos);
*(buf.fifo.buffer + (buf.fifo.append_pos += (size_t) brd)) = '\0';
LOG(LOG_DEBUG, "Read %li bytes from FIFO. Now appending at %li", brd, buf.fifo.append_pos);
connection.info.active = true;
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
LOG(LOG_ERROR, "Failed to read stdin input. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_ERROR, "Failed to read FIFO fifo. " ERRNOFMT, strerror(errno), errno);
connection.info.state = CONN_RECONNECTING;
continue;
}
memset((void*) &buffer, '\0', sizeof(IRC_Message));
if ((len = get_buffer_line(buf.input.buffer)) > 0) {
LOG(LOG_DEBUG, "Got stdin message: %s", buf.input.buffer);
if (Tok_mesg(buf.input.buffer, &buffer) == 1) {
LOG(LOG_DEBUG, "%s", "Tokenized stdin message successfully.");
if ((len = get_buffer_line(buf.fifo.buffer)) > 0) {
LOG(LOG_DEBUG, "Got FIFO message: %s", buf.fifo.buffer);
if (Tok_mesg(buf.fifo.buffer, &buffer) == 1) {
LOG(LOG_DEBUG, "%s", "Tokenized FIFO message successfully.");
signed long temp;
if ((temp = Assm_mesg(buf.input.buffer, &buffer, sizeof(buf.input.buffer))) > 0) {
if (flush_buffer(buf.input.buffer, (size_t) temp, buf.send.fd) == -1) {
if ((temp = Assm_mesg(buf.fifo.buffer, &buffer, sizeof(buf.fifo.buffer))) > 0) {
if (flush_buffer(buf.fifo.buffer, (size_t) temp, buf.send.fd) == -1) {
LOG(LOG_WARN,
"Couldn't send stdin input to " ADDRFMT ". " ERRNOFMT,
"Couldn't send FIFO fifo to " ADDRFMT ". " ERRNOFMT,
connection.data.address,
connection.data.service,
strerror(errno),
@ -314,15 +324,20 @@ main(int argc, char* argv[])
continue;
}
}
for (long unsigned int x = 0; x < sizeof(buf.input.buffer) && *(buf.input.buffer + len + x); x++)
*(buf.input.buffer + x) = *(buf.input.buffer + x + len);
buf.input.append_pos -= (unsigned long) len;
*(buf.input.buffer + buf.input.append_pos) = '\0';
connection.info.active = true;
for (long unsigned int x = 0; x < sizeof(buf.fifo.buffer) && *(buf.fifo.buffer + len + x); x++)
*(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len);
buf.fifo.append_pos -= (unsigned long) len;
*(buf.fifo.buffer + buf.fifo.append_pos) = '\0';
connection.info.active = true;
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
} else if (len == -1)
connection.info.state = CONN_RECONNECTING;
} else {
char pathbuf[UIRCD_LIMITS_PATH];
if (get_path(pathbuf, sizeof(pathbuf), &connection, NULL, true, true, false) >= 0) {
if (mkdir_bottomup(pathbuf)) buf.fifo.fd = makeinput(pathbuf);
}
}
}
@ -341,7 +356,6 @@ main(int argc, char* argv[])
memset((void*) &buffer, '\0', sizeof(IRC_Message));
if ((len = get_buffer_line(buf.recv.buffer)) > 0) {
LOG(LOG_DEBUG, "Got IRC message: %s", buf.recv.buffer);
printf("%s\r\n", buf.recv.buffer);
if (Tok_mesg(buf.recv.buffer, &buffer) == 1) {
connection.info.l_message = ctime;
char datebuf[25];
@ -349,6 +363,19 @@ main(int argc, char* argv[])
Assm_tag_timestamp(datebuf, sizeof(datebuf), ctime);
buffer.tags.time.value = datebuf;
}
char logpath[UIRCD_LIMITS_PATH];
if (Assm_mesg(buf.log.buffer, &buffer, sizeof(buf.log.buffer)) > 0) {
printf("%s\r\n", buf.log.buffer);
if (get_path(logpath, sizeof(logpath), &connection, &buffer, false, false, false) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
if (get_path(logpath, sizeof(logpath), &connection, &buffer, true, false, false) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
if (get_path(logpath, sizeof(logpath), &connection, &buffer, true, false, true) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
}
if (!auto_msg_actions(&buffer, &connection, &buf.send)) continue;
for (long unsigned int x = 0; x < sizeof(buf.recv.buffer) && *(buf.recv.buffer + len + x); x++)
*(buf.recv.buffer + x) = *(buf.recv.buffer + x + len);