WIP: separate logging into other binaries

This commit is contained in:
Alex D. 2020-12-21 15:34:38 +00:00
parent 8b035c5871
commit 677403b2f3
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
6 changed files with 49 additions and 60 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
project(microircd LANGUAGES C)
add_compile_definitions(
UIRCD_VERSION="2020.12.16-0-beta"
UIRCD_VERSION="2020.12.21-0-beta"
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

View File

@ -1,11 +0,0 @@
address = "41.157.98.109";
service = "9006";
user = {
nickname = "caskd-test";
username = "caskd-test";
}
channels = (
{ name = "#general" },
{ name = "#crxn" },
{ name = "#programming" }
)

View File

@ -54,7 +54,7 @@ resize_chanarray(Channel** chans)
int
set_channel(Channel* chan, const char* name, const char* key, bool joined)
{
LOG(LOG_DEBUG, "Setting channel %s", name);
LOG(LOG_DEBUG, "Setting channel %s", (name == NULL) ? chan->name : name);
if (name != NULL) {
if (!allocate_copy(&chan->name, name)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel name %s. " ERRNOFMT, name, strerror(errno), errno);

View File

@ -88,7 +88,9 @@ get_msgchannel(IRC_Message* mesg)
case INVITE:
case KICK:
case PRIVMSG:
case NOTICE: return 0;
case NOTICE: {
if (CHANNELMASK(mesg->args[0])) return 0;
}
}
return -1;
}
@ -103,6 +105,12 @@ get_categ(IRC_Message* mesg)
case TOPIC: return "topic";
case PRIVMSG:
case NOTICE: return "msgs";
case RPL_MOTD:
case RPL_MOTDSTART:
case RPL_ENDOFMOTD:
case ERR_NOMOTD: return "motd";
case PING:
case PONG: return "pings";
default: return NULL;
}
}

View File

@ -104,7 +104,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)
{
if (buf == NULL || conn == NULL || msg == NULL) return -1;
if (buf == NULL || conn == NULL) return -1;
ssize_t res;
const char* save = buf;
if (conn->data.address == NULL || conn->data.service == NULL
@ -114,11 +114,11 @@ get_log_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool glo
int ci = -1;
if (global) {
if ((res = snprintf(buf, lim, "global")) == -1) return -1;
} else if ((ci = get_msgchannel(msg)) != -1 && msg->args[ci] != NULL) {
} else if (msg != NULL && (ci = get_msgchannel(msg)) != -1 && msg->args[ci] != NULL) {
if ((res = snprintf(buf, lim, "channel")) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf);
if ((res = snprintf(buf, lim, "%s", msg->args[ci])) == -1) return -1;
} else if (msg->name.nick != NULL) {
} else if (msg != NULL && msg->name.nick != NULL) {
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;

View File

@ -21,10 +21,11 @@
int
parse_args(int argc, char** argv, Connection* conn)
{
int c;
int c;
unsigned long chanindex = 0;
while ((c = getopt(argc,
argv,
"V:d:a:p:t:N:U:R:P:q:c:k:"
"V:a:p:t:N:U:R:P:q:c:k:"
#ifdef UIRCD_FEATURE_LIBCONFIG
"C:"
#endif /* UIRCD_FEATURE_LIBCONFIG */
@ -32,7 +33,6 @@ parse_args(int argc, char** argv, Connection* conn)
!= -1) {
switch (c) {
case 'V': loglevel = atoi(optarg); break;
case 'd': allocate_copy(&conn->data.path, optarg); break;
case 'a': allocate_copy(&conn->data.address, optarg); break;
case 'p': allocate_copy(&conn->data.service, optarg); break;
case 't': conn->data.timeout = (unsigned int) atoi(optarg); break;
@ -41,9 +41,16 @@ parse_args(int argc, char** argv, Connection* conn)
case 'R': allocate_copy(&conn->user.realname, optarg); break;
case 'P': allocate_copy(&conn->user.password, optarg); break;
case 'q': allocate_copy(&conn->data.quitmsg, optarg); break;
case 'c':
case 'c': {
resize_chanarray(&conn->info.channels);
chanindex = get_channelindex(optarg, conn->info.channels);
if (!set_channel(&conn->info.channels[chanindex], optarg, NULL, true))
LOG(LOG_WARN, "Couldn't set channel %s as chan #%lu.", optarg, chanindex);
break;
}
case 'k': {
// TODO: Parse channel/key arguments
if (!set_channel(&conn->info.channels[chanindex], NULL, optarg, true))
LOG(LOG_WARN, "Couldn't set key for channel #%lu.", chanindex);
break;
}
case 'v': printf("uIRCd version " UIRCD_VERSION); return 0;
@ -99,7 +106,7 @@ main(int argc, char* argv[])
time_t ctime;
struct timespec sleep = { 0, 50000000L };
struct {
Buffer_Info recv, send, fifo;
Buffer_Info recv, send, input;
} buf;
init_chanarray(&connection.info.channels);
@ -112,15 +119,7 @@ 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);
if (!connection.info.active) nanosleep(&sleep, NULL);
@ -143,7 +142,6 @@ 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;
@ -151,7 +149,6 @@ 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;
@ -163,11 +160,12 @@ main(int argc, char* argv[])
memset(&buf, '\0', sizeof(buf));
buf.recv.fd = -1;
buf.send.fd = -1;
buf.fifo.fd = -1;
buf.input.fd = STDIN_FILENO;
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);
@ -283,31 +281,31 @@ main(int argc, char* argv[])
}
}
if (buf.fifo.fd != -1) {
if (buf.input.fd != -1) {
ssize_t brd, len;
/* Buffer writer */
if ((brd =
read(buf.fifo.fd, buf.fifo.buffer + buf.fifo.append_pos, sizeof(buf.fifo.buffer) - buf.fifo.append_pos - 1))
if ((brd = read(
buf.input.fd, buf.input.buffer + buf.input.append_pos, sizeof(buf.input.buffer) - buf.input.append_pos - 1))
> 0) {
*(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);
*(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);
connection.info.active = true;
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
LOG(LOG_ERROR, "Failed to read FIFO input. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_ERROR, "Failed to read stdin input. " ERRNOFMT, strerror(errno), errno);
connection.info.state = CONN_RECONNECTING;
continue;
}
memset((void*) &buffer, '\0', sizeof(IRC_Message));
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.");
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.");
signed long temp;
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) {
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) {
LOG(LOG_WARN,
"Couldn't send FIFO input to " ADDRFMT ". " ERRNOFMT,
"Couldn't send stdin input to " ADDRFMT ". " ERRNOFMT,
connection.data.address,
connection.data.service,
strerror(errno),
@ -316,11 +314,11 @@ main(int argc, char* argv[])
continue;
}
}
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;
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;
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
} else if (len == -1)
@ -343,14 +341,8 @@ 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);
char save[sizeof(buf.recv.buffer)];
strcpy(save, buf.recv.buffer);
printf("%s\r\n", save);
printf("%s\r\n", buf.recv.buffer);
if (Tok_mesg(buf.recv.buffer, &buffer) == 1) {
char logpath[UIRCD_LIMITS_PATH];
if (get_log_path(logpath, sizeof(logpath), &connection, &buffer, true, false) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, save);
}
connection.info.l_message = ctime;
char datebuf[25];
if (buffer.tags.time.value == NULL) {