summaryrefslogtreecommitdiff
path: root/replay.c
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-06-11 22:54:50 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-06-11 22:54:50 +0100
commit511e258e901e5248e1706609ba1099507fd750ae (patch)
tree4c7ee92fbe3634e7c78579657a023948a1a6bbae /replay.c
parent9db9fb396aaf601bd00f2b62face2693307a0e16 (diff)
Implement configurable auto replay modes. So far there is "none", "time" (auto replay the last X seconds), and "lastspoke" (auto replay everything since you last spoke).
Diffstat (limited to 'replay.c')
-rw-r--r--replay.c89
1 files changed, 85 insertions, 4 deletions
diff --git a/replay.c b/replay.c
index 4850a22..3a40e16 100644
--- a/replay.c
+++ b/replay.c
@@ -188,7 +188,7 @@ int replaylines(int seconds, char *basedir) {
}
// If the line is within range of the requested time, count it
- if (timestamp > timenow - seconds) {
+ if (timestamp >= timenow - seconds) {
numlines++;
}
}
@@ -216,7 +216,7 @@ int readreplayline(int seconds, int linenum, char *str, char *basedir) {
fp = fopen(filename, "r");
if (fp == NULL) {
- debugprint(DEBUG_CRIT, "error: could not open replay log '%s'.\n", filename);
+ debugprint(DEBUG_CRIT, "error: readreplayline(): could not open replay log '%s'.\n", filename);
fclose(fp);
return 0;
}
@@ -228,13 +228,13 @@ int readreplayline(int seconds, int linenum, char *str, char *basedir) {
// Read the timestamp from each line
int timestamp = gettimestamp(line);
if (timestamp < 1) {
- debugprint(DEBUG_CRIT, "Error reading timestamp from replay log file.\n");
+ debugprint(DEBUG_CRIT, "readreplayline(): Error reading timestamp from replay log file.\n");
fclose(fp);
return 0;
}
// If the line is within range of the requested time...
- if (timestamp > timenow - seconds) {
+ if (timestamp >= timenow - seconds) {
// ...and it is the current requested line then return it
if (count == linenum) {
// Insert our formatted [HH:MM:SS] timestamp into the message
@@ -254,6 +254,87 @@ int readreplayline(int seconds, int linenum, char *str, char *basedir) {
return 0;
}
+// Returns the number of seconds ago that 'nick' last spoke, or -1 if there is a problem.
+// 'basedir' is the directory in which to find 'replay.log'.
+int lastspokesecondsago(char *nick, char *basedir) {
+ FILE *fp;
+ char str[MAXCHAR];
+ char filename[PATH_MAX];
+
+ // Build path
+ snprintf(filename, PATH_MAX, "%s/replay.log", basedir);
+
+ int lastspoketime = 0; // When 'nick' last spoke
+
+ // Get the current time for comparison later
+ int timenow = (int)time(NULL);
+
+ fp = fopen(filename, "r");
+
+ if (fp == NULL) {
+ printf("error: replaylineslastspoke(): could not open replay log '%s'.\n", filename);
+ // Assume the file just doesn't exist yet - TODO - Interpret error codes to see what happened.
+ fclose(fp);
+ return 0;
+ }
+
+ while (fgets(str, MAXCHAR, fp) != NULL) {
+ // Split the line up to determine if it was a PRIVMSG sent by the requested 'nick'
+ // TODO - This may also be terribly inefficient
+
+ // Copy to a temporary string
+ char *strcopy = strdup(str);
+ // Keep track of initial pointer for free()ing later
+ char *strcopyPtr = strcopy;
+
+ // Build array of each space-separated token, only need three (<timestamp> :<nick>!<user>@<host> PRIVMSG)
+ char tokens[3][MAXDATASIZE + TIMELEN]; // Make IRC message length + our unix timestamp
+ char *token;
+ int counter = 0;
+ for (int i = 0; i < 3; i++) {
+ // Try to split
+ if ((token = strsep(&strcopy, " ")) == NULL) {
+ debugprint(DEBUG_CRIT, "replaylineslastspoke(): error splitting string on iteration '%d', returning -1!\n", i);
+ return -1;
+ }
+ // Copy into the token array (strlen + 1 to get the NULL terminator)
+ strncpy(tokens[i], token, strlen(token) + 1);
+ counter++;
+ }
+ free(strcopyPtr);
+
+ // Make sure there were at least three tokens
+ if (counter < 3) {
+ debugprint(DEBUG_CRIT, "replaylineslastspoke(): not enough tokens on line, only '%d', returning -1!\n", counter);
+ return -1;
+ }
+
+ // Make sure it started with a valid timestamp
+ int timestamp = gettimestamp(tokens[0]);
+ if (timestamp < 0) {
+ debugprint(DEBUG_CRIT, "replaylineslastspoke(): line didn't start with a timestamp, returning -1!\n", counter);
+ }
+
+ // Is it a PRIVMSG?
+ if (strncmp(tokens[2], "PRIVMSG", strlen("PRIVMSG"))) {
+ // Not a PRIVMSG, continue
+ continue;
+ }
+
+ // Was it said by our 'nick'?
+ extractnickfromprefix(tokens[1]);
+ if (strncmp(tokens[1], nick, strlen(nick))) {
+ // Not our 'nick', continue
+ continue;
+ }
+
+ lastspoketime = timestamp;
+ }
+
+ fclose(fp);
+ return timenow - lastspoketime;
+}
+
// Write the line 'str' to the replay log file after prepending it with
// the current unixtime timestamp. 'basedir' is the directory in which
// to write to 'replay.log'.