From aab7a7000cff14afe4cb331721ad55dc139f80c7 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sun, 15 Sep 2019 15:11:55 +0100 Subject: Log nick changes to the normal log file(s). --- TODO | 2 -- logging.c | 23 ++++++++++++++++++++++- logging.h | 5 +++++ message.c | 32 ++++++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 32bfa2c..b0eedee 100644 --- a/TODO +++ b/TODO @@ -1,3 +1 @@ All the TODOs sprinkled throughout the code! - -Log nick changes to the normal log. diff --git a/logging.c b/logging.c index f6fa23e..6935673 100644 --- a/logging.c +++ b/logging.c @@ -48,6 +48,10 @@ // channel log file. The caller probably has to call logline() // multiple times for each channel the nick was in. // +// If LOG_NICK then it expects a string in the format: +// :oldnick!bar@baz NICK :newnick +// Same manual 'channelname' prepending as LOG_QUIT above. +// // With the ":foo!bar@baz "prefix being important for all // types. // @@ -263,6 +267,23 @@ int logline(char *str, struct ircdstate *ircdstate, char *basedir, int type) { break; + case LOG_NICK: + // Extract old nick from the prefix + extractnickfromprefix(tokens[1]); + // Strip colon from new nick + stripprefix(str); + + // Build a friendly message (e.g. "oldnick is now known as newnick") + snprintf(line, MAXCHAR, "%s is now known as %s", tokens[1], str); + + // Build the log filename + if (!snprintf(filename, MAXCHAR, "%s/logs/%s.log", basedir, tokens[0])) { + debugprint(DEBUG_CRIT, "logline(): Error while preparing log filename for nick, returning!\n"); + return 0; + } + + break; + default : debugprint(DEBUG_CRIT, "logline(): Unknown log type '%d', returning 0.\n", type); return 0; @@ -323,7 +344,7 @@ int logline(char *str, struct ircdstate *ircdstate, char *basedir, int type) { return 0; } } - } else if (type == LOG_JOINPART || type == LOG_TOPIC || type == LOG_NETWORK || type == LOG_QUIT) { + } else if (type == LOG_JOINPART || type == LOG_TOPIC || type == LOG_NETWORK || type == LOG_QUIT || type == LOG_NICK) { // Prepend the time string char line2[MAXCHAR]; if (!snprintf(line2, MAXCHAR, "%s %s", timestr, line)) { diff --git a/logging.h b/logging.h index 726a490..c2b6d4f 100644 --- a/logging.h +++ b/logging.h @@ -38,6 +38,7 @@ #define LOG_TOPIC 2 #define LOG_NETWORK 3 #define LOG_QUIT 4 +#define LOG_NICK 5 #define DEBUG_CRIT 0 #define DEBUG_SOME 1 #define DEBUG_FULL 2 @@ -73,6 +74,10 @@ // channel log file. The caller probably has to call logline() // multiple times for each channel the nick was in. // +// If LOG_NICK then it expects a string in the format: +// channelname :oldnick!bar@baz NICK :newnick +// Same manual 'channelname' prepending as LOG_QUIT above. +// // With the ":foo!bar@baz "prefix being important for all // types. // diff --git a/message.c b/message.c index 869a1ce..a7bef1e 100644 --- a/message.c +++ b/message.c @@ -270,7 +270,7 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int writereplayline(str, settings->basedir); } - // Get each channel the QUITting user was in, and log the quit from that channel + // Get each channel the QUITting user was in, and log the quit from that channel if enabled if (settings->logging) { char quitnick[MAXNICKLENGTH]; strcpy(quitnick, tokens[0]); @@ -475,11 +475,6 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int free(prefixcopy); } - // Update old nick to the new nick in our local channel struct - if (!updatenickinallchannels(tokens[0], tokens[2], channels)) { - debugprint(DEBUG_CRIT, "Failed to update old nick to new nick in channels.\n"); - } - // Relay to all clients sendtoallclients(clients, str, sourcefd, settings); @@ -488,8 +483,29 @@ int processservermessage(SSL *server_ssl, char *str, struct client *clients, int writereplayline(str, settings->basedir); } - // TODO - 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.) + // Get each channel the old nick was in, and log the NICK change in that channel if enabled + if (settings->logging) { + char oldnick[MAXNICKLENGTH]; + strcpy(oldnick, tokens[0]); + extractnickfromprefix(oldnick); + for (int i = 0; i < MAXCHANNELS; i++) { + if (channels[i].name[0]) { + for (int j = 0; j < MAXCHANNICKS; j++) { + if (strlen(channels[i].nicks[j]) == strlen(oldnick) && !strcmp(channels[i].nicks[j], oldnick)) { + char logstring[MAXDATASIZE]; + snprintf(logstring, MAXDATASIZE, "%s %s", channels[i].name, str); + logline(logstring, ircdstate, settings->basedir, LOG_NICK); + break; + } + } + } + } + } + + // Update old nick to the new nick in our local channel struct + if (!updatenickinallchannels(tokens[0], tokens[2], channels)) { + debugprint(DEBUG_CRIT, "Failed to update old nick to new nick in channels.\n"); + } free(svrprefixcopy); return 1; -- cgit v1.2.3