summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorLouis Brauer <louis@openbooking.ch>2024-05-31 00:30:37 +0200
committerLouis Brauer <louis@openbooking.ch>2024-05-31 00:30:37 +0200
commitc3bcb2bd3b354bb05997347821a37506ca6cc298 (patch)
tree83e10ba950cc52c0c3e75b23ea5c5b9827020c85 /data.c
parentaf8f1ef273e457318cb48f198e73c59e57373723 (diff)
Implement instance announcements
Diffstat (limited to 'data.c')
-rw-r--r--data.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/data.c b/data.c
index e24bf16..b25ddf8 100644
--- a/data.c
+++ b/data.c
@@ -3370,3 +3370,69 @@ void srv_archive_qitem(const char *prefix, xs_dict *q_item)
fclose(f);
}
}
+
+
+t_announcement *announcement(const double after)
+/* returns announcement text or NULL if none exists or it is olde than "after" */
+{
+ static const long int MAX_SIZE = 2048;
+ static t_announcement a = {
+ .text = NULL,
+ .timestamp = 0.0,
+ };
+ static xs_str *fn = NULL;
+ if (fn == NULL)
+ fn = xs_fmt("%s/announcement.txt", srv_basedir);
+
+ const double ts = mtime(fn);
+
+ /* file does not exist or other than what was requested */
+ if (ts == 0.0 || ts <= after)
+ return NULL;
+
+ /* nothing changed, just return the current announcement */
+ if (a.text != NULL && ts <= a.timestamp)
+ return &a;
+
+ /* read and store new announcement */
+ FILE *f;
+
+ if ((f = fopen(fn, "r")) != NULL) {
+ fseek (f, 0, SEEK_END);
+ const long int length = ftell(f);
+
+ if (length > MAX_SIZE) {
+ /* this is probably unintentional */
+ srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
+ }
+ else
+ if (length > 0) {
+ fseek (f, 0, SEEK_SET);
+ char *buffer = malloc(length + 1);
+ if (buffer) {
+ fread(buffer, 1, length, f);
+ buffer[length] = '\0';
+
+ free(a.text);
+ a.text = buffer;
+ a.timestamp = ts;
+ }
+ else {
+ srv_log("Error allocating memory for announcement");
+ }
+ }
+ else {
+ /* an empty file means no announcement */
+ free(a.text);
+ a.text = NULL;
+ a.timestamp = 0.0;
+ }
+
+ fclose (f);
+ }
+
+ if (a.text != NULL)
+ return &a;
+
+ return NULL;
+}