diff options
Diffstat (limited to 'replay.c')
| -rw-r--r-- | replay.c | 42 | 
1 files changed, 28 insertions, 14 deletions
| @@ -83,16 +83,21 @@ int striptimestamp(char *str) {  // 1557592901 :foo!bar@baz PRIVMSG foo :hello world  // And convert it to:  // :foo!bar@baz PRIVMSG foo :[17:41:41] hello world +// Or the same but e.g. [DD/MM/YY HH:MM:SS] if replaydates == 1.  // Only inserts the formatted time for PRIVMSGs at the moment (and maybe only needs to!). -void formattime(char *str) { +void formattime(char *str, int replaydates) {    // Extract the timestamp for conversion into [HH:MM:SS]    char timestr[TIMELEN];    sprintf(timestr, "%d", gettimestamp(str)); // Convert int time to string time    struct tm tm;    strptime(timestr, "%s", &tm); -  char timestampf[TIMELEN]; // Formatted timestamp -  // Convert into [HH:MM:SS] -  strftime(timestampf, TIMELEN, "[%H:%M:%S]", &tm); +  char timestampf[DATETIMELEN]; // Formatted timestamp +  // Convert into [HH:MM:SS] or e.g. [DD/MM/YY HH:MM:SS] +  if (replaydates) { +    strftime(timestampf, DATETIMELEN, "[%x %H:%M:%S]", &tm); +  } else { +    strftime(timestampf, DATETIMELEN, "[%H:%M:%S]", &tm); +  }    // Strip the original unixtimestamp    striptimestamp(str); @@ -144,22 +149,30 @@ void formattime(char *str) {    // Second bit (:foo!bar@baz PRIVMSG foo :[HH:MM:SS])    int j = 0; -  for (int i = realpos; i < TIMELEN + realpos - 1; i++) { // -1 to avoid the null char from timestampf +  for (size_t i = realpos; i < strlen(timestampf) + realpos; i++) {      newline[i] = timestampf[j];      j++;    }    // Insert a space after the formatted timestamp -  newline[TIMELEN + realpos - 1] = ' '; +  newline[strlen(timestampf) + realpos] = ' '; +  newline[strlen(timestampf) + realpos + 1] = '\0'; -  // Final bit (the original PRIVMSG contents) -  for (int i = TIMELEN + realpos; i < len + TIMELEN; i++) { -    newline[i] = str[i - TIMELEN]; +  // Record length of temporary newline string and original real message (after the colon) +  size_t newlinelen = strlen(newline); +  size_t msglen = strlen(str) - realpos - 2; // - 2 to ignore leading colon and null terminator + +  // Append the real message to the temporary newline string +  for (size_t i = 0; i < strlen(str) - realpos - 2; i++) { +    newline[newlinelen + i] = str[realpos + i];    } +  // Null terminate it +  newline[newlinelen + msglen] = '\0'; +    // Copy the whole thing back to str and null terminate -  strncpy(str, newline, len + TIMELEN); -  str[len + TIMELEN] = '\0'; +  strncpy(str, newline, len + strlen(timestampf)); +  str[len + strlen(timestampf)] = '\0';  }  // Return the number of lines in the replay log since 'seconds' seconds ago, or -1 if there a problem. @@ -208,10 +221,11 @@ int replaylines(int seconds, char *basedir) {  // Set 'str' to the line in the log with a timestamp of greater than 'seconds'  // seconds ago, plus however many lines 'linenum' is set to.  'basedir' is the  // directory in which to find 'replay.log'. -// Also modify the line to include a timestamp in the form "[HH:MM:SS]". +// Also modify the line to include a timestamp in the form "[HH:MM:SS]", or [DD/MM/YY HH:MM:SS] +// if replaydates == 1.  // Returns 1 on success, or 0 on failure.  // TODO - This is horribly inefficient since it re-reads the entire file each call, rewrite this! -int readreplayline(int seconds, int linenum, char *str, char *basedir) { +int readreplayline(int seconds, int linenum, char *str, char *basedir, int replaydates) {    FILE *fp;    char line[MAXCHAR];    char filename[PATH_MAX]; @@ -246,7 +260,7 @@ int readreplayline(int seconds, int linenum, char *str, char *basedir) {        // ...and it is the current requested line then return it        if (count == linenum) {          // Insert our formatted [HH:MM:SS] timestamp into the message -        formattime(line); +        formattime(line, replaydates);          strncpy(str, line, strlen(line));          str[strlen(line)] = '\0'; | 
