From 5a3a07a9e64f4677290062712f9e9a3299061971 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 12 Sep 2019 21:55:07 +0100 Subject: Log server messages to file named .log. --- TODO | 2 -- blabouncer.c | 9 ++++++++- logging.c | 54 ++++++++++++++++++++++++++++++++++++++++-------------- logging.h | 14 +++++++++----- message.c | 14 +++++++------- 5 files changed, 64 insertions(+), 29 deletions(-) diff --git a/TODO b/TODO index 8558408..7a0223c 100644 --- a/TODO +++ b/TODO @@ -8,8 +8,6 @@ PM replay chat in a channel (or perhaps a random channel?) e.g. replay on 06/09/ Ensure replayed lines don't exceed IRC message maximum length due to inserted time/datestamp. -Log server messages to file. - Is there a way to log nick changes to the normal log despite not tracking nicks in each channel? (We do track channel names themselves.) Change Makefile to not leave separate .o files lying around. diff --git a/blabouncer.c b/blabouncer.c index 7052117..d94a165 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -235,7 +235,13 @@ int processircmessage(SSL *server_ssl, char *str, int source, struct client *cli // =============================================> - debugprint(DEBUG_FULL, "Done with processircmessage(), didn't process anything.\n"); + debugprint(DEBUG_FULL, "Done with processircmessage(), didn't process anything, logging to network log.\n"); + + + // Write to normal log if logging enabled + if (settings->logging) { + logline(str, ircdstate, settings->basedir, LOG_NETWORK); + } // If we got here then we didn't process anything free(strcopyPtr); @@ -338,6 +344,7 @@ int processrawstring(SSL *server_ssl, char *str, int source, struct client *clie for (int i = 0; i < messagecount; i++) { if (messages[i][0] != '\0') { debugprint(DEBUG_FULL, "Message %d (\"%s\") remains unprocessed...\n", i, messages[i]); + switch(source) { case SOURCE_SERVER: // If message(s) were from the real IRC server // Relay/send to all clients ("except" = 0 because this should send to all clients) diff --git a/logging.c b/logging.c index d661a2b..ff41911 100644 --- a/logging.c +++ b/logging.c @@ -18,9 +18,9 @@ #include "logging.h" // Write the line 'str' to the relevant log file such as -// '#channel.log' or 'nickname.log'. 'ournick' is our own -// nick and is used to determine which log file to write to -// if the type is LOG_PRIVMSG. +// '#channel.log' or 'nickname.log'. 'ircdstate->ircnick' +// is our own nick and is used to determine which log file +// to write to if the type is LOG_PRIVMSG. // 'basedir' is the directory in which the 'logs' directory // will be created in which logs are to be written. // @@ -37,11 +37,14 @@ // If LOG_TOPIC then it expects a string in the format: // :nick!bar@baz TOPIC #channel :bla bla bla // +// If LOG_NETWORK then it just logs the string verbatim in +// a file named 'ircdstate->ircdname'.log. +// // With the ":foo!bar@baz "prefix being important for all // types. // // Returns 1 on success or 0 on failure. -int logline(char *str, char *ournick, char *basedir, int type) { +int logline(char *str, struct ircdstate *ircdstate, char *basedir, int type) { // Filename to write to, gets built as we go char filename[MAXCHAR]; @@ -69,15 +72,19 @@ int logline(char *str, char *ournick, char *basedir, int type) { // If LOG_TOPIC: // This gets us the prefix (containing the topic setting nick), the TOPIC command (not needed), // the channel whose topic was set, and the rest of the string intact (which is the new topic). - for (int i = 0; i < 3; i++) { - // Try to split - if ((token = strsep(&str, " ")) == NULL) { - debugprint(DEBUG_CRIT, "Error splitting string for logging, returning!\n"); - return 0; + // If LOG_NETWORK: + // Don't do this at all since we want to log the whole string. + if (type != LOG_NETWORK) { + for (int i = 0; i < 3; i++) { + // Try to split + if ((token = strsep(&str, " ")) == NULL) { + debugprint(DEBUG_CRIT, "Error splitting string for logging, returning!\n"); + return 0; + } + // Copy into the token array (strlen + 1 to get the NULL terminator) + strncpy(tokens[i], token, strlen(token) + 1); + debugprint(DEBUG_FULL, "logline(): extracted '%s'.\n", tokens[i]); } - // Copy into the token array (strlen + 1 to get the NULL terminator) - strncpy(tokens[i], token, strlen(token) + 1); - debugprint(DEBUG_FULL, "logline(): extracted '%s'.\n", tokens[i]); } // Make "filename safe" copies of from and to names to ensure filename ends up being safe @@ -104,7 +111,7 @@ int logline(char *str, char *ournick, char *basedir, int type) { // Build the log filename // If the message was sent to us, then log it in the sender's log file - if (strncmp(tokens[2], ournick, strlen(tokens[0])) == 0) { + if (strncmp(tokens[2], ircdstate->ircnick, strlen(tokens[0])) == 0) { if (!snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, from)) { debugprint(DEBUG_CRIT, "Error while preparing log filename for from name, returning!\n"); return 0; @@ -198,6 +205,25 @@ int logline(char *str, char *ournick, char *basedir, int type) { break; + case LOG_NETWORK: + // Make sure we actually have an ircdname, if not, don't try to continue + if (strlen(ircdstate->ircdname) < 1) { + debugprint(DEBUG_CRIT, "logline(): No ircdname (yet?), returning.\n"); + return 0; + } + + if (!snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, ircdstate->ircdname)) { + debugprint(DEBUG_CRIT, "Error while preparing log filename for network, returning!\n"); + return 0; + } + + debugprint(DEBUG_FULL, "logline(): Logging network log for '%s' in filename '%s'.\n", str, filename); + + // Copy source message verbatim to destination + strcpy(line, str); + + break; + default : debugprint(DEBUG_CRIT, "logline(): Unknown log type '%d', returning 0.\n", type); return 0; @@ -258,7 +284,7 @@ int logline(char *str, char *ournick, char *basedir, int type) { return 0; } } - } else if (type == LOG_JOINPART || type == LOG_TOPIC) { + } else if (type == LOG_JOINPART || type == LOG_TOPIC || type == LOG_NETWORK) { // Prepend the time string char line2[MAXCHAR]; if (!snprintf(line2, MAXCHAR, "%s %s", timestr, line)) { diff --git a/logging.h b/logging.h index 441a500..6371f8a 100644 --- a/logging.h +++ b/logging.h @@ -36,14 +36,15 @@ #define LOG_PRIVMSG 0 #define LOG_JOINPART 1 #define LOG_TOPIC 2 +#define LOG_NETWORK 3 #define DEBUG_CRIT 0 #define DEBUG_SOME 1 #define DEBUG_FULL 2 // Write the line 'str' to the relevant log file such as -// '#channel.log' or 'nickname.log'. 'ournick' is our own -// nick and is used to determine which log file to write to -// if the type is LOG_PRIVMSG. +// '#channel.log' or 'nickname.log'. 'ircdstate->ircnick' +// is our own nick and is used to determine which log file +// to write to if the type is LOG_PRIVMSG. // 'basedir' is the directory in which the 'logs' directory // will be created in which logs are to be written. // @@ -59,11 +60,14 @@ // // If LOG_TOPIC then it expects a string in the format: // :nick!bar@baz TOPIC #channel :bla bla bla - +// +// If LOG_NETWORK then it just logs the string verbatim in +// a file named 'ircdstate->ircdname'.log. +// // With the ":foo!bar@baz "prefix being important for all // types. // // Returns 1 on success or 0 on failure. -int logline(char *str, char *ournick, char *basedir, int type); +int logline(char *str, struct ircdstate *ircdstate, char *basedir, int type); #endif diff --git a/message.c b/message.c index 3036eb2..5cfff91 100644 --- a/message.c +++ b/message.c @@ -195,7 +195,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(str, ircdstate->ircnick, settings->basedir, LOG_JOINPART); + logline(str, ircdstate, settings->basedir, LOG_JOINPART); } free(prefixcopy); @@ -229,7 +229,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(str, ircdstate->ircnick, settings->basedir, LOG_JOINPART); + logline(str, ircdstate, settings->basedir, LOG_JOINPART); } free(prefixcopy); @@ -345,7 +345,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(str, ircdstate->ircnick, settings->basedir, LOG_TOPIC); + logline(str, ircdstate, settings->basedir, LOG_TOPIC); } free(topiccopy); @@ -367,7 +367,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(str, ircdstate->ircnick, settings->basedir, LOG_PRIVMSG); + logline(str, ircdstate, settings->basedir, LOG_PRIVMSG); } return 1; @@ -717,7 +717,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(str, ircdstate->ircnick, settings->basedir, LOG_PRIVMSG); + logline(str, ircdstate, settings->basedir, LOG_PRIVMSG); } return 1; @@ -1039,7 +1039,7 @@ int processclientmessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(outgoingmsg, ircdstate->ircnick, settings->basedir, LOG_PRIVMSG); + logline(outgoingmsg, ircdstate, settings->basedir, LOG_PRIVMSG); } return 1; @@ -1148,7 +1148,7 @@ int processclientmessage(SSL *server_ssl, char *str, struct client *clients, int // Write to normal log if logging enabled if (settings->logging) { - logline(fullmsg, ircdstate->ircnick, settings->basedir, LOG_PRIVMSG); + logline(fullmsg, ircdstate, settings->basedir, LOG_PRIVMSG); } // If it's a CTCP VERSION response then only send to the server (CTCP requests are delimited with \1) -- cgit v1.2.3