From 968cee422ab1d61b4234127892d75f0497d8d8c2 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 16 May 2019 20:59:51 +0100 Subject: Add a configurable base directory for things like logs, defaulting to $HOME/.blabouncer/. --- replay.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'replay.c') diff --git a/replay.c b/replay.c index 3c2c920..a898a7b 100644 --- a/replay.c +++ b/replay.c @@ -137,10 +137,14 @@ void formattime(char *str) { } // Return the number of lines in the replay log since 'seconds' seconds ago, or -1 if there a problem. -int replaylines(int seconds) { +// 'basedir' is the directory in which to find 'replay.log'. +int replaylines(int seconds, char *basedir) { FILE *fp; char str[MAXCHAR]; - char* filename = "replay.log"; + char filename[PATH_MAX]; + + // Build path + snprintf(filename, PATH_MAX, "%s/replay.log", basedir); int numlines = 0; @@ -175,14 +179,18 @@ int replaylines(int seconds) { } // 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. +// 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]". // 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) { +int readreplayline(int seconds, int linenum, char *str, char *basedir) { FILE *fp; char line[MAXCHAR]; - char* filename = "replay.log"; + char filename[PATH_MAX]; + + // Build path + snprintf(filename, PATH_MAX, "%s/replay.log", basedir); int count = 0; @@ -226,15 +234,19 @@ int readreplayline(int seconds, int linenum, char *str) { } // Write the line 'str' to the replay log file after prepending it with -// the current unixtime timestamp. +// the current unixtime timestamp. 'basedir' is the directory in which +// to write to 'replay.log'. // Expects a string in the format: // :from!bar@baz PRIVMSG to :hello world // With the ":foo!bar@baz "prefix being important. // Returns 1 on success or 0 on failure. -int writereplayline(char *str) { +int writereplayline(char *str, char *basedir) { FILE *fp; char line[MAXCHAR]; - char* filename = "replay.log"; + char filename[PATH_MAX]; + + // Build path + snprintf(filename, PATH_MAX, "%s/replay.log", basedir); int bytes = 0; -- cgit v1.2.3