summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-05-11 19:49:27 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-05-11 19:49:27 +0100
commit231656bf59da65036b171ce5ccc06eeadd64a05a (patch)
tree350e2c0fdbf1e368b26e90a59331e48044f4387d
parent0559bff00a6be2054194632c3543bf62af1fb56f (diff)
Implement relay log writing.
-rw-r--r--blabouncer.c19
-rw-r--r--replay.c46
2 files changed, 61 insertions, 4 deletions
diff --git a/blabouncer.c b/blabouncer.c
index 2e1f414..29b90ae 100644
--- a/blabouncer.c
+++ b/blabouncer.c
@@ -514,7 +514,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
}
}
- // Server TOPIC received? Update our local channel topic info then relay to clients
+ // Server TOPIC received? Update our local channel topic info then relay to clients.
if (strncmp(tokens[1], "TOPIC", strlen(tokens[1])) == 0) {
printf("Server TOPIC found and it is: %s with length %zd! Next token is '%s'. Updating our local channel topic info.\n", tokens[0], strlen(tokens[0]), tokens[2]);
@@ -547,6 +547,18 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
return 1;
}
+ // Server PRIVMSG received? Relay to all clients and write to replay log.
+ if (strncmp(tokens[1], "PRIVMSG", strlen(tokens[1])) == 0) {
+ printf("Server PRIVMSG found and it is: %s with length %zd! Next token is '%s'. Relaying to all clients.\n", tokens[0], strlen(tokens[0]), tokens[2]);
+
+ sendtoallclients(clientsockfd, fdmax, arr_clients, str, sourcefd);
+
+ // Write to relay log
+ writerelayline(str);
+
+ return 1;
+ }
+
// Don't return if we got here because this means we didn't process something above
}
@@ -701,7 +713,7 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
return 1;
}
- // If PRIVMSG received, send to server, but also reformat and send to all other clients
+ // If PRIVMSG received, send to server, but also reformat and send to all other clients and log to replay file.
if (strncmp(tokens[0], "PRIVMSG", strlen(tokens[0])) == 0) {
printf("Client PRIVMSG found and it is: %s with length %zd! Sending to server then back to other clients...\n", tokens[0], strlen(tokens[0]));
// Send original request straight to server
@@ -717,6 +729,9 @@ int processircmessage(int *serversockfd, int *clientsockfd, char *str, int sourc
// Send to all except source client
sendtoallclients(clientsockfd, fdmax, arr_clients, outgoingmsg, sourcefd);
+ // Write to relay log
+ writerelayline(outgoingmsg);
+
return 1;
}
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;
+}