summaryrefslogtreecommitdiff
path: root/replay.c
diff options
context:
space:
mode:
Diffstat (limited to 'replay.c')
-rw-r--r--replay.c46
1 files changed, 44 insertions, 2 deletions
diff --git a/replay.c b/replay.c
index 8c37d4c..36817fb 100644
--- a/replay.c
+++ b/replay.c
@@ -154,7 +154,8 @@ int relaylines(int seconds) {
if (fp == NULL) {
printf("error: could not open replay log '%s'.\n", filename);
- exit(1);
+ // Assume the file just doesn't exist yet - TODO - Interpret error codes to see what happened.
+ return 0;
}
// Get the current time for comparison later
@@ -196,7 +197,7 @@ int readrelayline(int seconds, int linenum, char *str) {
if (fp == NULL) {
printf("error: could not open replay log '%s'.\n", filename);
- return 1;
+ exit(1);
}
// Get the current time for comparison later
@@ -229,3 +230,44 @@ int readrelayline(int seconds, int linenum, char *str) {
// If we got here something went wrong
return 0;
}
+
+// Write the line 'str' to the relay log file after prepending it with
+// the current unixtime timestamp.
+// Expects a string in the format:
+// :foo!bar@baz PRIVMSG foo :[17:41:41] hello world
+// With the ":foo!bar@baz "prefix being important.
+// Returns 1 on success or 0 on failure.
+int writerelayline(char *str) {
+ FILE *fp;
+ char line[MAXCHAR];
+ char* filename = "replay.log";
+
+ int bytes = 0;
+
+ fp = fopen(filename, "a");
+
+ if (fp == NULL) {
+ printf("error: could not open replay log '%s' for writing.\n", filename);
+ exit(1);
+ }
+
+ // Get the current time and manipulate it into a C string
+ time_t timenow = time(NULL);
+ int timenowlen = snprintf(NULL, 0, "%ld", timenow);
+ char timenowstr[timenowlen + 1]; // TODO - Make this Year 2038 proof.
+ snprintf(timenowstr, timenowlen + 1, "%ld", timenow);
+
+ // Prepend the unixtime timestamp
+ snprintf(line, MAXCHAR, "%s %s", timenowstr, str);
+
+ printf("Complete replay log string to write: '%s', length '%ld'.\n", line, strlen(line));
+
+ // Write complete line to file
+ if ((bytes = fprintf(fp, line)) < 0) {
+ printf("error: could not write to replay log file.\n");
+ exit(1);
+ }
+
+ fclose(fp);
+ return bytes;
+}