diff options
Diffstat (limited to 'logging.c')
-rw-r--r-- | logging.c | 54 |
1 files changed, 40 insertions, 14 deletions
@@ -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)) { |