--- router/router.c.orig 2012-03-09 01:28:54.000000000 +0400 +++ router/router.c 2012-06-07 09:54:52.822401769 +0400 @@ -1131,61 +1131,38 @@ int message_log(nad_t nad, router_t r, const unsigned char *msg_from, const unsigned char *msg_to) { time_t t; - char *time_pos; - int time_sz; + struct tm *time_pos; + char timestamp[25]; struct stat filestat; FILE *message_file; short int new_msg_file = 0; int i; int nad_body_len = 0; - long int nad_body_start = 0; - int body_count; char *nad_body = NULL; - char body[MAX_MESSAGE*2]; + int elem; assert((int) (nad != NULL)); - /* timestamp */ - t = time(NULL); - time_pos = ctime(&t); - time_sz = strlen(time_pos); - /* chop off the \n */ - time_pos[time_sz-1]=' '; - // Find the message body - for (i = 0; NAD_ENAME_L(nad, i) > 0; i++) - { - if((NAD_ENAME_L(nad, i) == 4) && (strncmp("body", NAD_ENAME(nad, i), 4) == 0)) - { - nad_body_len = NAD_CDATA_L(nad, i); - if (nad_body_len > 0) { - nad_body = NAD_CDATA(nad, i); - } else { - log_write(r->log, LOG_NOTICE, "message_log received a message with empty body"); - return 0; - } - break; - } + elem = nad_find_elem(nad, 0, -1, "message", 1); + if (elem >= 0) { + elem = nad_find_elem(nad, elem, -1, "body", 1); } // Don't log anything if we found no NAD body - if (nad_body == NULL) { + if (elem == -1) { return 0; } - // Store original pointer address so that we know when to stop iterating through nad_body - nad_body_start = nad_body; + nad_body_len = NAD_CDATA_L(nad, elem); + nad_body = NAD_CDATA(nad, elem); - // replace line endings with "\n" - for (body_count = 0; (nad_body < nad_body_start + nad_body_len) && (body_count < (MAX_MESSAGE*2)-3); nad_body++) { - if (*nad_body == '\n') { - body[body_count++] = '\\'; - body[body_count++] = 'n'; - } else { - body[body_count++] = *nad_body; + // temporary replace line endings with 0x01, ASCII: SOH + for (i = 0; i < nad_body_len; i++) { + if (nad_body[i] == '\n') { + nad_body[i] = 0x01; } } - body[body_count] = '\0'; // Log our message umask((mode_t) 0077); @@ -1193,8 +1170,7 @@ new_msg_file = 1; } - if ((message_file = fopen(r->message_logging_file, "a")) == NULL) - { + if ((message_file = fopen(r->message_logging_file, "a")) == NULL) { log_write(r->log, LOG_ERR, "Unable to open message log for writing: %s", strerror(errno)); return 1; } @@ -1206,16 +1182,31 @@ return 1; } fprintf(message_file, "# See router.xml for logging options.\n"); - fprintf(message_file, "# Format: (Date)(From JID)(To JID)(Message Body)\n"); + fprintf(message_file, "# Format: DateTime FromJID ToJID MessageBody\n"); } - if (! fprintf(message_file, "%s\t%s\t%s\t%s\n", time_pos, msg_from, msg_to, body)) - { - log_write(r->log, LOG_ERR, "Unable to write to message log: %s", strerror(errno)); - return 1; + /* ISO8601 timestamp */ + t = time(NULL); + time_pos = localtime(&t); + if (strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S%z", time_pos) == 0) { + log_write(r->log, LOG_ERR, "strftime failed: %s", strerror(errno)); } + elem = fprintf(message_file, "%s %s %s %.*s\n", timestamp, msg_from, msg_to, nad_body_len, nad_body); + fclose(message_file); + // revert line endings + for (i = 0; i < nad_body_len; i++) { + if (nad_body[i] == 0x01) { + nad_body[i] = '\n'; + } + } + + if (!elem) { + log_write(r->log, LOG_ERR, "Unable to write to message log: %s", strerror(errno)); + return 1; + } + return 0; }