summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorGravatar Louis Brauer2024-05-31 00:30:37 +0200
committerGravatar Louis Brauer2024-05-31 00:30:37 +0200
commitc3bcb2bd3b354bb05997347821a37506ca6cc298 (patch)
tree83e10ba950cc52c0c3e75b23ea5c5b9827020c85 /data.c
parentImplement image uploads for Tokodon (diff)
downloadpenes-snac2-c3bcb2bd3b354bb05997347821a37506ca6cc298.tar.gz
penes-snac2-c3bcb2bd3b354bb05997347821a37506ca6cc298.tar.xz
penes-snac2-c3bcb2bd3b354bb05997347821a37506ca6cc298.zip
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)
3370 fclose(f); 3370 fclose(f);
3371 } 3371 }
3372} 3372}
3373
3374
3375t_announcement *announcement(const double after)
3376/* returns announcement text or NULL if none exists or it is olde than "after" */
3377{
3378 static const long int MAX_SIZE = 2048;
3379 static t_announcement a = {
3380 .text = NULL,
3381 .timestamp = 0.0,
3382 };
3383 static xs_str *fn = NULL;
3384 if (fn == NULL)
3385 fn = xs_fmt("%s/announcement.txt", srv_basedir);
3386
3387 const double ts = mtime(fn);
3388
3389 /* file does not exist or other than what was requested */
3390 if (ts == 0.0 || ts <= after)
3391 return NULL;
3392
3393 /* nothing changed, just return the current announcement */
3394 if (a.text != NULL && ts <= a.timestamp)
3395 return &a;
3396
3397 /* read and store new announcement */
3398 FILE *f;
3399
3400 if ((f = fopen(fn, "r")) != NULL) {
3401 fseek (f, 0, SEEK_END);
3402 const long int length = ftell(f);
3403
3404 if (length > MAX_SIZE) {
3405 /* this is probably unintentional */
3406 srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
3407 }
3408 else
3409 if (length > 0) {
3410 fseek (f, 0, SEEK_SET);
3411 char *buffer = malloc(length + 1);
3412 if (buffer) {
3413 fread(buffer, 1, length, f);
3414 buffer[length] = '\0';
3415
3416 free(a.text);
3417 a.text = buffer;
3418 a.timestamp = ts;
3419 }
3420 else {
3421 srv_log("Error allocating memory for announcement");
3422 }
3423 }
3424 else {
3425 /* an empty file means no announcement */
3426 free(a.text);
3427 a.text = NULL;
3428 a.timestamp = 0.0;
3429 }
3430
3431 fclose (f);
3432 }
3433
3434 if (a.text != NULL)
3435 return &a;
3436
3437 return NULL;
3438}