about summary refs log tree commit diff
path: root/bounce.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-10-28 19:21:44 -0400
committerJune McEnroe <june@causal.agency>2019-10-28 19:21:44 -0400
commit8b70aa21da96d9dcad641c3fe2787307d5610036 (patch)
treebf2e42559dcaf7fec495a0f47a4ff0470e0b7eb8 /bounce.c
parentUse "producer/consumer" consistently in ring code (diff)
downloadpounce-8b70aa21da96d9dcad641c3fe2787307d5610036.tar.gz
pounce-8b70aa21da96d9dcad641c3fe2787307d5610036.zip
Add option to save and load ring contents across restarts
Diffstat (limited to 'bounce.c')
-rw-r--r--bounce.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/bounce.c b/bounce.c
index 65789ab..4e7458c 100644
--- a/bounce.c
+++ b/bounce.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sysexits.h>
 #include <tls.h>
 #include <unistd.h>
@@ -88,11 +89,20 @@ static char *sensitive(char *arg) {
 	return value;
 }
 
+static FILE *saveFile;
+static void exitSave(void) {
+	int error = ringSave(saveFile);
+	if (error) warn("fwrite");
+	error = fclose(saveFile);
+	if (error) warn("fclose");
+}
+
 int main(int argc, char *argv[]) {
 	const char *localHost = "localhost";
 	const char *localPort = "6697";
 	char certPath[PATH_MAX] = "";
 	char privPath[PATH_MAX] = "";
+	const char *save = NULL;
 
 	bool insecure = false;
 	const char *host = NULL;
@@ -107,7 +117,8 @@ int main(int argc, char *argv[]) {
 	const char *quit = "connection reset by purr";
 
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "!A:C:H:K:NP:Q:W:a:h:j:n:p:r:u:vw:"))) {
+	const char *opts = "!A:C:H:K:NP:Q:W:a:f:h:j:n:p:r:u:vw:";
+	while (0 < (opt = getopt(argc, argv, opts))) {
 		switch (opt) {
 			break; case '!': insecure = true;
 			break; case 'A': away = optarg;
@@ -119,6 +130,7 @@ int main(int argc, char *argv[]) {
 			break; case 'Q': quit = optarg;
 			break; case 'W': clientPass = sensitive(optarg);
 			break; case 'a': auth = sensitive(optarg);
+			break; case 'f': save = optarg;
 			break; case 'h': host = optarg;
 			break; case 'j': join = optarg;
 			break; case 'n': nick = optarg;
@@ -146,6 +158,19 @@ int main(int argc, char *argv[]) {
 	if (!user) user = nick;
 	if (!real) real = nick;
 
+	if (save) {
+		umask(0066);
+		saveFile = fopen(save, "a+");
+		if (!saveFile) err(EX_CANTCREAT, "%s", save);
+
+		rewind(saveFile);
+		ringLoad(saveFile);
+
+		int error = ftruncate(fileno(saveFile), 0);
+		if (error) err(EX_IOERR, "ftruncate");
+		atexit(exitSave);
+	}
+
 	listenConfig(certPath, privPath);
 
 	int bind[8];