From 8b70aa21da96d9dcad641c3fe2787307d5610036 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 28 Oct 2019 19:21:44 -0400 Subject: Add option to save and load ring contents across restarts --- bounce.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'bounce.c') diff --git a/bounce.c b/bounce.c index 65789ab..4e7458c 100644 --- a/bounce.c +++ b/bounce.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -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]; -- cgit 1.4.1