aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Uhlin <markus@nifty-networks.net>2025-10-23 00:57:22 +0200
committerMarkus Uhlin <markus@nifty-networks.net>2025-10-23 00:57:22 +0200
commit50d70dd56ae732d1fda97de4cb6b1dbb06d9f12d (patch)
tree57899d8fc15ecf04a88ffb9e6e76681e26b088c8
parent103a867aa63e0b8ef139b6d55affb94901ba4e5b (diff)
Settings
-rw-r--r--FICS/build.mk2
-rw-r--r--FICS/settings.cpp126
-rw-r--r--FICS/settings.h14
3 files changed, 142 insertions, 0 deletions
diff --git a/FICS/build.mk b/FICS/build.mk
index 7355421..5ace5c7 100644
--- a/FICS/build.mk
+++ b/FICS/build.mk
@@ -32,6 +32,7 @@ OBJS = $(SRC_DIR)adminproc.o\
$(SRC_DIR)rating_conv.o\
$(SRC_DIR)ratings.o\
$(SRC_DIR)rmalloc.o\
+ $(SRC_DIR)settings.o\
$(SRC_DIR)shutdown.o\
$(SRC_DIR)sought.o\
$(SRC_DIR)talkproc.o\
@@ -68,6 +69,7 @@ SRCS = $(SRC_DIR)adminproc.c\
$(SRC_DIR)rating_conv.c\
$(SRC_DIR)ratings.c\
$(SRC_DIR)rmalloc.c\
+ $(SRC_DIR)settings.cpp\
$(SRC_DIR)shutdown.c\
$(SRC_DIR)sought.cpp\
$(SRC_DIR)talkproc.c\
diff --git a/FICS/settings.cpp b/FICS/settings.cpp
new file mode 100644
index 0000000..1436c6f
--- /dev/null
+++ b/FICS/settings.cpp
@@ -0,0 +1,126 @@
+// SPDX-FileCopyrightText: 2025 Markus Uhlin <maxxe@rpblc.net>
+// SPDX-License-Identifier: ISC
+
+#include <err.h>
+#include <errno.h>
+
+#include <string>
+#include <vector>
+
+#include "copyfile.h"
+#include "interpreter.h"
+#include "settings.h"
+
+struct setting {
+ std::string name;
+ enum setting_type type;
+ std::string value;
+
+ setting()
+ {
+ this->name.assign("");
+ this->type = STYPE_STRING;
+ this->value.assign("");
+ }
+
+ setting(const char *p_name, enum setting_type p_type,
+ const char *p_value)
+ {
+ this->name.assign(p_name);
+ this->type = p_type;
+ this->value.assign(p_value);
+ }
+};
+
+static std::vector<setting> settings;
+
+#if defined(__cplusplus) && __cplusplus >= 201103L
+#define SET_PB(obj) settings.emplace_back(obj)
+#else
+#define SET_PB(obj) settings.push_back(obj)
+#endif
+
+void
+settings_init(void)
+{
+ struct setting s1("server_hostname", STYPE_STRING, "");
+ struct setting s2("server_name", STYPE_STRING, "Xfics");
+ struct setting s3("server_location", STYPE_STRING, "");
+ struct setting s4("HADMINHANDLE", STYPE_STRING, "");
+ struct setting s5("HADMINEMAIL", STYPE_STRING, "");
+ struct setting s6("REGMAIL", STYPE_STRING, "");
+
+ SET_PB(s1);
+ SET_PB(s2);
+ SET_PB(s3);
+ SET_PB(s4);
+ SET_PB(s5);
+ SET_PB(s6);
+}
+
+void
+settings_deinit(void)
+{
+ fprintf(stderr, "FICS: Deinitialized the conf settings\n");
+}
+
+static bool
+is_recognized_setting(const char *name)
+{
+ if (name == nullptr || strings_match(name, ""))
+ return false;
+ for (auto it = settings.begin(); it != settings.end(); ++it) {
+ if (strings_match((*it).name.c_str(), name))
+ return true;
+ }
+ return false;
+}
+
+static int
+install_setting(const char *name, const char *value)
+{
+ if (name == nullptr || value == nullptr)
+ return EINVAL;
+ for (auto it = settings.begin(); it != settings.end(); ++it) {
+ if (strings_match((*it).name.c_str(), name)) {
+ (*it).name.assign(value);
+ return 0;
+ }
+ }
+ return ENOENT;
+}
+
+const char *
+settings_get(const char *set_name)
+{
+ if (set_name == nullptr || strings_match(set_name, ""))
+ return ("");
+ for (auto it = settings.begin(); it != settings.end(); ++it) {
+ if (strings_match((*it).name.c_str(), set_name))
+ return (*it).value.c_str();
+ }
+ return ("");
+}
+
+void
+settings_read_conf(const char *path)
+{
+ FILE *fp = nullptr;
+
+ if (!is_regular_file(path)) {
+ errx(1, "%s: either the config file is nonexistent"
+ " -- or it isn't a regular file", __func__);
+ } else if ((fp = fopen(path, "r")) == nullptr) {
+ err(1, "%s: fopen", __func__);
+ }
+
+ Interpreter_processAllLines(fp, path, is_recognized_setting,
+ install_setting);
+
+ if (feof(fp))
+ fclose(fp);
+ else if (ferror(fp))
+ errx(1, "%s: %s", __func__, g_fgets_nullret_err1);
+ else
+ errx(1, "%s: %s", __func__, g_fgets_nullret_err2);
+}
diff --git a/FICS/settings.h b/FICS/settings.h
new file mode 100644
index 0000000..4a1eae0
--- /dev/null
+++ b/FICS/settings.h
@@ -0,0 +1,14 @@
+#ifndef SETTINGS_H
+#define SETTINGS_H
+
+#include "common.h"
+
+__FICS_BEGIN_DECLS
+void settings_init(void);
+void settings_deinit(void);
+
+const char *settings_get(const char *set_name);
+void settings_read_conf(const char *path);
+__FICS_END_DECLS
+
+#endif