2.94
data.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_hex.h"
6#include "xs_io.h"
7#include "xs_json.h"
8#include "xs_openssl.h"
9#include "xs_glob.h"
10#include "xs_set.h"
11#include "xs_time.h"
12#include "xs_regex.h"
13#include "xs_match.h"
14#include "xs_unicode.h"
15#include "xs_random.h"
16#include "xs_po.h"
17#include "xs_http.h"
18
19#include "snac.h"
20
21#include <time.h>
22#include <sys/stat.h>
23#include <sys/file.h>
24#include <sys/time.h>
25#include <fcntl.h>
26#include <pthread.h>
27
28double disk_layout = 2.7;
29
30/* storage serializer */
31pthread_mutex_t data_mutex = {0};
32
33int snac_upgrade(xs_str **error);
34
35
36int srv_open(const char *basedir, int auto_upgrade)
37/* opens a server */
38{
39 int ret = 0;
40 xs *cfg_file = NULL;
41 FILE *f;
42 xs_str *error = NULL;
43
44 /* MUST be three letters */
45 months[0] = LL("Jan");
46 months[1] = LL("Feb");
47 months[2] = LL("Mar");
48 months[3] = LL("Apr");
49 months[4] = LL("May");
50 months[5] = LL("Jun");
51 months[6] = LL("Jul");
52 months[7] = LL("Aug");
53 months[8] = LL("Sep");
54 months[9] = LL("Oct");
55 months[10] = LL("Nov");
56 months[11] = LL("Dec");
57
58 pthread_mutex_init(&data_mutex, NULL);
59
60 srv_basedir = xs_str_new(basedir);
61
62 if (xs_endswith(srv_basedir, "/"))
63 srv_basedir = xs_crop_i(srv_basedir, 0, -1);
64
65 cfg_file = xs_fmt("%s/server.json", basedir);
66
67 if ((f = fopen(cfg_file, "r")) == NULL)
68 error = xs_fmt("ERROR: cannot open '%s'", cfg_file);
69 else {
70 /* read full config file */
71 srv_config = xs_json_load(f);
72 fclose(f);
73
74 /* parse */
75
76 if (srv_config == NULL)
77 error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
78 else {
79 const char *host;
80 const char *prefix;
81 const char *dbglvl;
82 const char *proto;
83
84 host = xs_dict_get(srv_config, "host");
85 prefix = xs_dict_get(srv_config, "prefix");
86 dbglvl = xs_dict_get(srv_config, "dbglevel");
87 proto = xs_dict_get_def(srv_config, "protocol", "https");
88
89 if (host == NULL || prefix == NULL)
90 error = xs_str_new("ERROR: cannot get server data");
91 else {
92 srv_baseurl = xs_fmt("%s:/" "/%s%s", proto, host, prefix);
93
94 dbglevel = (int) xs_number_get(dbglvl);
95
96 if ((dbglvl = getenv("DEBUG")) != NULL) {
97 dbglevel = atoi(dbglvl);
98 error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
99 }
100
101 if (auto_upgrade)
102 ret = snac_upgrade(&error);
103 else if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
104 error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
105 else if (!check_strip_tool())
106 error = xs_fmt("ERROR: strip_exif enabled but commands not found or working");
107 else
108 ret = 1;
109 }
110
111 }
112 }
113
114 if (error != NULL)
115 srv_log(error);
116
117 if (!ret)
118 return ret;
119
120 /* create the queue/ subdir, just in case */
121 xs *qdir = xs_fmt("%s/queue", srv_basedir);
122 mkdirx(qdir);
123
124 xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
125 mkdirx(ibdir);
126
127 xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
128 mkdirx(tmpdir);
129
130 xs *faildir = xs_fmt("%s/failure", srv_basedir);
131 mkdirx(faildir);
132
133#ifdef __APPLE__
134/* Apple uses st_atimespec instead of st_atim etc */
135#define st_atim st_atimespec
136#define st_ctim st_ctimespec
137#define st_mtim st_mtimespec
138#endif
139
140 sbox_enter(srv_basedir);
141
142 /* read (and drop) emojis.json, possibly creating it */
143 xs_free(emojis());
144
145 /* if style.css does not exist, create it */
146 xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
147
148 if (mtime(css_fn) == 0) {
149 srv_log(xs_fmt("Writing style.css"));
150 write_default_css();
151 }
152
153 /* create the proxy token seed */
154 {
155 char rnd[16];
156 xs_rnd_buf(rnd, sizeof(rnd));
157
158 srv_proxy_token_seed = xs_hex_enc(rnd, sizeof(rnd));
159 }
160
161 /* ensure user directories include important subdirectories */
162 xs *users = user_list();
163 const char *uid;
164
165 xs_list_foreach(users, uid) {
166 xs *impdir = xs_fmt("%s/user/%s/import", srv_basedir, uid);
167 xs *expdir = xs_fmt("%s/user/%s/export", srv_basedir, uid);
168
169 mkdirx(impdir);
170 mkdirx(expdir);
171 }
172
173 /* languages */
174 srv_langs = xs_dict_new();
175 srv_langs = xs_dict_set(srv_langs, "en", xs_stock(XSTYPE_NULL));
176
177 xs *l_dir = xs_fmt("%s/lang/", srv_basedir);
178 mkdirx(l_dir);
179
180 l_dir = xs_str_cat(l_dir, "*.po");
181 xs *pos = xs_glob(l_dir, 0, 0);
182 const char *po;
183
184 xs_list_foreach(pos, po) {
185 xs *d = xs_po_to_dict(po);
186
187 if (xs_is_dict(d)) {
188 xs *l = xs_split(po, "/");
189 xs *id = xs_dup(xs_list_get(l, -1));
190 id = xs_replace_i(id, ".po", "");
191
192 srv_langs = xs_dict_set(srv_langs, id, d);
193 }
194 }
195
196 return ret;
197}
198
199
200void srv_free(void)
201{
202 xs_free(srv_basedir);
203 xs_free(srv_config);
204 xs_free(srv_baseurl);
205
206 pthread_mutex_destroy(&data_mutex);
207}
208
209
210void user_free(snac *snac)
211/* frees a user snac */
212{
213 xs_free(snac->uid);
214 xs_free(snac->basedir);
215 xs_free(snac->config);
216 xs_free(snac->config_o);
217 xs_free(snac->key);
218 xs_free(snac->links);
219 xs_free(snac->actor);
220 xs_free(snac->actor_alt);
221 xs_free(snac->md5);
222}
223
224
225int user_open(snac *user, const char *uid)
226/* opens a user */
227{
228 int ret = 0;
229
230 *user = (snac){0};
231
232 if (validate_uid(uid)) {
233 xs *cfg_file = NULL;
234 FILE *f;
235
236 xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
237
238 if (mtime(t) == 0.0) {
239 /* user folder does not exist; try with a different case */
240 xs *lcuid = xs_tolower_i(xs_dup(uid));
241 xs *ulist = user_list();
242 xs_list *p = ulist;
243 const xs_str *v;
244
245 while (xs_list_iter(&p, &v)) {
246 xs *v2 = xs_tolower_i(xs_dup(v));
247
248 if (strcmp(lcuid, v2) == 0) {
249 user->uid = xs_dup(v);
250 break;
251 }
252 }
253 }
254 else
255 user->uid = xs_str_new(uid);
256
257 if (user->uid == NULL)
258 return ret;
259
260 user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
261
262 cfg_file = xs_fmt("%s/user.json", user->basedir);
263
264 if ((f = fopen(cfg_file, "r")) != NULL) {
265 /* read full config file */
266 user->config = xs_json_load(f);
267 fclose(f);
268
269 if (user->config != NULL) {
270 xs *key_file = xs_fmt("%s/key.json", user->basedir);
271
272 if ((f = fopen(key_file, "r")) != NULL) {
273 user->key = xs_json_load(f);
274 fclose(f);
275
276 if (user->key != NULL) {
277 user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
278 user->actor_alt = xs_fmt("%s/users/%s", srv_baseurl ,user->uid);
279 user->md5 = xs_md5_hex(user->actor, strlen(user->actor));
280
281 /* everything is ok right now */
282 ret = 1;
283
284 /* does it have a configuration override? */
285 xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
286 if ((f = fopen(cfg_file_o, "r")) != NULL) {
287 user->config_o = xs_json_load(f);
288 fclose(f);
289
290 if (user->config_o == NULL)
291 srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
292 }
293
294 if (user->config_o == NULL)
295 user->config_o = xs_dict_new();
296 }
297 else
298 srv_log(xs_fmt("error parsing '%s'", key_file));
299 }
300 else
301 srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
302 }
303 else
304 srv_log(xs_fmt("error parsing '%s'", cfg_file));
305
306 user->tz = xs_dict_get_def(user->config, "tz", "UTC");
307 }
308 else
309 srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
310
311 /* verified links */
312 xs *links_file = xs_fmt("%s/links.json", user->basedir);
313
314 if ((f = fopen(links_file, "r")) != NULL) {
315 user->links = xs_json_load(f);
316 fclose(f);
317 }
318 }
319 else
320 srv_debug(2, xs_fmt("invalid user '%s'", uid));
321
322 if (!ret)
323 user_free(user);
324
325 return ret;
326}
327
328
329xs_list *user_list(void)
330/* returns the list of user ids */
331{
332 xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
333 return xs_glob(spec, 1, 0);
334}
335
336
337int user_open_by_md5(snac *snac, const char *md5)
338/* iterates all users searching by md5 */
339{
340 xs *ulist = user_list();
341 xs_list *p = ulist;
342 const xs_str *v;
343
344 while (xs_list_iter(&p, &v)) {
345 user_open(snac, v);
346
347 if (strcmp(snac->md5, md5) == 0)
348 return 1;
349
350 user_free(snac);
351 }
352
353 return 0;
354}
355
356
357int user_persist(snac *snac, int publish)
358/* store user */
359{
360 xs *fn = xs_fmt("%s/user.json", snac->basedir);
361 xs *bfn = xs_fmt("%s.bak", fn);
362 FILE *f;
363
364 if (publish) {
365 /* check if any of the relevant fields have really changed */
366 if ((f = fopen(fn, "r")) != NULL) {
367 xs *old = xs_json_load(f);
368 fclose(f);
369
370 if (old != NULL) {
371 int nw = 0;
372 const char *fields[] = { "header", "avatar", "name", "bio", "alias", "alias_raw",
373 "metadata", "latitude", "longitude", NULL };
374
375 for (int n = 0; fields[n]; n++) {
376 const char *of = xs_dict_get(old, fields[n]);
377 const char *nf = xs_dict_get(snac->config, fields[n]);
378
379 if (of == NULL && nf == NULL)
380 continue;
381
382 if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) {
383 nw = 1;
384 break;
385 }
386 }
387
388 if (!nw)
389 publish = 0;
390 else {
391 /* uncache the actor object */
392 object_del(snac->actor);
393 }
394 }
395 }
396 }
397
398 rename(fn, bfn);
399
400 if ((f = fopen(fn, "w")) != NULL) {
401 xs_json_dump(snac->config, 4, f);
402 fclose(f);
403 }
404 else
405 rename(bfn, fn);
406
407 history_del(snac, "timeline.html_");
408 timeline_touch(snac);
409
410 if (publish) {
411 xs *a_msg = msg_actor(snac);
412 xs *u_msg = msg_update(snac, a_msg);
413
414 enqueue_message(snac, u_msg);
415
416 enqueue_verify_links(snac);
417 }
418
419 return 0;
420}
421
422
423double mtime_nl(const char *fn, int *n_link)
424/* returns the mtime and number of links of a file or directory, or 0.0 */
425{
426 struct stat st;
427 double r = 0.0;
428 int n = 0;
429
430 if (fn && stat(fn, &st) != -1) {
431 r = (double) st.st_mtim.tv_sec;
432 n = st.st_nlink;
433 }
434
435 if (n_link)
436 *n_link = n;
437
438 return r;
439}
440
441
442#define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
443
444double f_ctime(const char *fn)
445/* returns the ctime of a file or directory, or 0.0 */
446{
447 struct stat st;
448 double r = 0.0;
449
450 if (fn && stat(fn, &st) != -1) {
451 /* return the lowest of ctime and mtime;
452 there are operations that change the ctime, like link() */
453 r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
454 }
455
456 return r;
457}
458
459
460int is_md5_hex(const char *md5)
461{
462 return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
463}
464
465
466/** database 2.1+ **/
467
468/** indexes **/
469
470
471int index_add_md5(const char *fn, const char *md5)
472/* adds an md5 to an index */
473{
474 int status = HTTP_STATUS_CREATED;
475 FILE *f;
476
477 if (!is_md5_hex(md5)) {
478 srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
479 return HTTP_STATUS_BAD_REQUEST;
480 }
481
482 pthread_mutex_lock(&data_mutex);
483
484 if ((f = fopen(fn, "a")) != NULL) {
485 flock(fileno(f), LOCK_EX);
486
487 /* ensure the position is at the end after getting the lock */
488 fseek(f, 0, SEEK_END);
489
490 fprintf(f, "%s\n", md5);
491 fclose(f);
492 }
493 else
494 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
495
496 pthread_mutex_unlock(&data_mutex);
497
498 return status;
499}
500
501
502int index_add(const char *fn, const char *id)
503/* adds an id to an index */
504{
505 xs *md5 = xs_md5_hex(id, strlen(id));
506 return index_add_md5(fn, md5);
507}
508
509
510int index_del_md5(const char *fn, const char *md5)
511/* deletes an md5 from an index */
512{
513 int status = HTTP_STATUS_NOT_FOUND;
514 FILE *f;
515
516 pthread_mutex_lock(&data_mutex);
517
518 if ((f = fopen(fn, "r+")) != NULL) {
519 char line[256];
520
521 while (fgets(line, sizeof(line), f) != NULL) {
522 line[MD5_HEX_SIZE - 1] = '\0';
523
524 if (strcmp(line, md5) == 0) {
525 /* found! just rewind, overwrite it with garbage
526 and an eventual call to index_gc() will clean it
527 [yes: this breaks index_len()] */
528 fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
529 fwrite("-", 1, 1, f);
530 status = HTTP_STATUS_OK;
531
532 break;
533 }
534 }
535
536 fclose(f);
537 }
538 else
539 status = HTTP_STATUS_GONE;
540
541 pthread_mutex_unlock(&data_mutex);
542
543 return status;
544}
545
546
547int index_del(const char *fn, const char *id)
548/* deletes an id from an index */
549{
550 xs *md5 = xs_md5_hex(id, strlen(id));
551 return index_del_md5(fn, md5);
552}
553
554
555int index_gc(const char *fn)
556/* garbage-collects an index, deleting objects that are not here */
557{
558 FILE *i, *o;
559 int gc = -1;
560
561 pthread_mutex_lock(&data_mutex);
562
563 if ((i = fopen(fn, "r")) != NULL) {
564 xs *nfn = xs_fmt("%s.new", fn);
565 char line[256];
566
567 if ((o = fopen(nfn, "w")) != NULL) {
568 gc = 0;
569
570 while (fgets(line, sizeof(line), i) != NULL) {
571 line[MD5_HEX_SIZE - 1] = '\0';
572
573 if (line[0] != '-' && object_here_by_md5(line))
574 fprintf(o, "%s\n", line);
575 else
576 gc++;
577 }
578
579 fclose(o);
580
581 xs *ofn = xs_fmt("%s.bak", fn);
582
583 unlink(ofn);
584 link(fn, ofn);
585 rename(nfn, fn);
586 }
587
588 fclose(i);
589 }
590
591 pthread_mutex_unlock(&data_mutex);
592
593 return gc;
594}
595
596
597int index_in_md5(const char *fn, const char *md5)
598/* checks if the md5 is already in the index */
599{
600 FILE *f;
601 int ret = 0;
602
603 if ((f = fopen(fn, "r")) != NULL) {
604 flock(fileno(f), LOCK_SH);
605
606 char line[256];
607
608 while (!ret && fgets(line, sizeof(line), f) != NULL) {
609 line[MD5_HEX_SIZE - 1] = '\0';
610
611 if (strcmp(line, md5) == 0)
612 ret = 1;
613 }
614
615 fclose(f);
616 }
617
618 return ret;
619}
620
621
622int index_in(const char *fn, const char *id)
623/* checks if the object id is already in the index */
624{
625 xs *md5 = xs_md5_hex(id, strlen(id));
626 return index_in_md5(fn, md5);
627}
628
629
630int index_first(const char *fn, char md5[MD5_HEX_SIZE])
631/* reads the first entry of an index */
632{
633 FILE *f;
634 int ret = 0;
635
636 if ((f = fopen(fn, "r")) != NULL) {
637 if (fread(md5, MD5_HEX_SIZE, 1, f)) {
638 md5[MD5_HEX_SIZE - 1] = '\0';
639 ret = 1;
640 }
641
642 fclose(f);
643 }
644
645 return ret;
646}
647
648
649int index_len(const char *fn)
650/* returns the number of elements in an index */
651{
652 struct stat st;
653 int len = 0;
654
655 if (stat(fn, &st) != -1)
656 len = st.st_size / MD5_HEX_SIZE;
657
658 return len;
659}
660
661
662xs_list *index_list(const char *fn, int max)
663/* returns an index as a list */
664{
665 xs_list *list = xs_list_new();
666 FILE *f;
667 int n = 0;
668
669 if ((f = fopen(fn, "r")) != NULL) {
670 flock(fileno(f), LOCK_SH);
671
672 char line[256];
673
674 while (n < max && fgets(line, sizeof(line), f) != NULL) {
675 if (line[0] != '-') {
676 line[MD5_HEX_SIZE - 1] = '\0';
677 list = xs_list_append(list, line);
678 n++;
679 }
680 }
681
682 fclose(f);
683 }
684
685 return list;
686}
687
688
689int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
690/* reads the next entry of a desc index */
691{
692 for (;;) {
693 /* move backwards 2 entries */
694 if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
695 return 0;
696
697 /* read and md5 */
698 if (!fread(md5, MD5_HEX_SIZE, 1, f))
699 return 0;
700
701 if (md5[0] != '-')
702 break;
703 }
704
705 md5[MD5_HEX_SIZE - 1] = '\0';
706
707 return 1;
708}
709
710
711int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
712/* reads the first entry of a desc index */
713{
714 /* try to position at the end and then back to the first element */
715 if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
716 return 0;
717
718 /* try to read an md5 */
719 if (!fread(md5, MD5_HEX_SIZE, 1, f))
720 return 0;
721
722 /* null-terminate */
723 md5[MD5_HEX_SIZE - 1] = '\0';
724
725 /* deleted? retry next */
726 if (md5[0] == '-')
727 return index_desc_next(f, md5);
728
729 return 1;
730}
731
732int index_asc_first(FILE *f,char md5[MD5_HEX_SIZE], const char *seek_md5)
733/* reads the first entry of an ascending index, starting from a given md5 */
734{
735 fseek(f, SEEK_SET, 0);
736 while (fread(md5, MD5_HEX_SIZE, 1, f)) {
737 md5[MD5_HEX_SIZE - 1] = '\0';
738 if (strcmp(md5,seek_md5) == 0) {
739 return index_asc_next(f, md5);
740 }
741 }
742 return 0;
743}
744
745int index_asc_next(FILE *f, char md5[MD5_HEX_SIZE])
746/* reads the next entry of an ascending index */
747{
748 for (;;) {
749 /* read an md5 */
750 if (!fread(md5, MD5_HEX_SIZE, 1, f))
751 return 0;
752
753 /* deleted, skip */
754 if (md5[0] != '-')
755 break;
756 }
757
758 md5[MD5_HEX_SIZE - 1] = '\0';
759
760 return 1;
761}
762
763
764xs_list *index_list_desc(const char *fn, int skip, int show)
765/* returns an index as a list, in reverse order */
766{
767 xs_list *list = xs_list_new();
768 FILE *f;
769
770 if ((f = fopen(fn, "r")) != NULL) {
771 char md5[MD5_HEX_SIZE];
772
773 if (index_desc_first(f, md5, skip)) {
774 int n = 1;
775
776 do {
777 list = xs_list_append(list, md5);
778 } while (n++ < show && index_desc_next(f, md5));
779 }
780
781 fclose(f);
782 }
783
784 return list;
785}
786
787
788/** objects **/
789
790static xs_str *_object_fn_by_md5(const char *md5, const char *func)
791{
792 xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
793 xs_str *ret;
794 int ok = 1;
795
796 /* an object deleted from an index; fail but don't bark */
797 if (md5[0] == '-')
798 ok = 0;
799 else
800 if (!is_md5_hex(md5)) {
801 srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
802 ok = 0;
803 }
804
805 if (ok) {
806 mkdirx(bfn);
807 ret = xs_fmt("%s/%s.json", bfn, md5);
808 }
809 else
810 ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
811
812 return ret;
813}
814
815
816static xs_str *_object_fn(const char *id)
817{
818 xs *md5 = xs_md5_hex(id, strlen(id));
819 return _object_fn_by_md5(md5, "_object_fn");
820}
821
822
823int object_here_by_md5(const char *id)
824/* checks if an object is already downloaded */
825{
826 xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
827 return mtime(fn) > 0.0;
828}
829
830
831int object_here(const char *id)
832/* checks if an object is already downloaded */
833{
834 xs *fn = _object_fn(id);
835 return mtime(fn) > 0.0;
836}
837
838
839int object_get_by_md5(const char *md5, xs_dict **obj)
840/* returns a stored object, optionally of the requested type */
841{
842 int status = HTTP_STATUS_NOT_FOUND;
843 xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
844 FILE *f;
845
846 if ((f = fopen(fn, "r")) != NULL) {
847 *obj = xs_json_load(f);
848 fclose(f);
849
850 if (*obj)
851 status = HTTP_STATUS_OK;
852 }
853 else
854 *obj = NULL;
855
856 return status;
857}
858
859
860int object_get(const char *id, xs_dict **obj)
861/* returns a stored object, optionally of the requested type */
862{
863 xs *md5 = xs_md5_hex(id, strlen(id));
864 return object_get_by_md5(md5, obj);
865}
866
867
868int _object_add(const char *id, const xs_dict *obj, int ow)
869/* stores an object */
870{
871 int status = HTTP_STATUS_CREATED; /* Created */
872 xs *fn = _object_fn(id);
873 FILE *f;
874
875 if (mtime(fn) > 0.0) {
876 if (!ow) {
877 /* object already here */
878 srv_debug(1, xs_fmt("object_add object already here %s", id));
879 return HTTP_STATUS_NO_CONTENT;
880 }
881 else
882 status = HTTP_STATUS_OK;
883 }
884
885 if ((f = fopen(fn, "w")) != NULL) {
886 flock(fileno(f), LOCK_EX);
887
888 xs_json_dump(obj, 4, f);
889 fclose(f);
890
891 /* does this object has a parent? */
892 const char *in_reply_to = get_in_reply_to(obj);
893
894 if (!xs_is_null(in_reply_to) && *in_reply_to) {
895 /* update the children index of the parent */
896 xs *c_idx = _object_fn(in_reply_to);
897
898 c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
899
900 if (!index_in(c_idx, id)) {
901 index_add(c_idx, id);
902 srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
903 }
904 else
905 srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
906
907 /* create a one-element index with the parent */
908 xs *p_idx = xs_replace(fn, ".json", "_p.idx");
909
910 if (mtime(p_idx) == 0.0) {
911 index_add(p_idx, in_reply_to);
912 srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
913 }
914 }
915 }
916 else {
917 srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
918 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
919 }
920
921 srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
922
923 return status;
924}
925
926
927int object_add(const char *id, const xs_dict *obj)
928/* stores an object */
929{
930 return _object_add(id, obj, 0);
931}
932
933
934int object_add_ow(const char *id, const xs_dict *obj)
935/* stores an object (overwriting allowed) */
936{
937 return _object_add(id, obj, 1);
938}
939
940
941int object_del_by_md5(const char *md5)
942/* deletes an object by its md5 */
943{
944 int status = HTTP_STATUS_NOT_FOUND;
945 xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
946
947 if (unlink(fn) != -1) {
948 status = HTTP_STATUS_OK;
949
950 /* also delete associated indexes */
951 xs *spec = xs_dup(fn);
952 spec = xs_replace_i(spec, ".json", "*.idx");
953 xs *files = xs_glob(spec, 0, 0);
954 char *p;
955 const char *v;
956
957 p = files;
958 while (xs_list_iter(&p, &v)) {
959 srv_debug(1, xs_fmt("object_del index %s", v));
960 unlink(v);
961 }
962 }
963
964 srv_debug(1, xs_fmt("object_del %s %d", fn, status));
965
966 return status;
967}
968
969
970int object_del(const char *id)
971/* deletes an object */
972{
973 xs *md5 = xs_md5_hex(id, strlen(id));
974 return object_del_by_md5(md5);
975}
976
977
978int object_del_if_unref(const char *id)
979/* deletes an object if its n_links < 2 */
980{
981 xs *fn = _object_fn(id);
982 int n_links;
983 int ret = 0;
984
985 if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
986 ret = object_del(id);
987
988 return ret;
989}
990
991
992double object_ctime_by_md5(const char *md5)
993{
994 xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
995 return f_ctime(fn);
996}
997
998
999double object_ctime(const char *id)
1000{
1001 xs *md5 = xs_md5_hex(id, strlen(id));
1002 return object_ctime_by_md5(md5);
1003}
1004
1005
1006double object_mtime_by_md5(const char *md5)
1007{
1008 xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
1009 return mtime(fn);
1010}
1011
1012
1013double object_mtime(const char *id)
1014{
1015 xs *md5 = xs_md5_hex(id, strlen(id));
1016 return object_mtime_by_md5(md5);
1017}
1018
1019
1020void object_touch(const char *id)
1021{
1022 xs *md5 = xs_md5_hex(id, strlen(id));
1023 xs *fn = _object_fn_by_md5(md5, "object_touch");
1024
1025 if (mtime(fn))
1026 utimes(fn, NULL);
1027}
1028
1029
1030xs_str *_object_index_fn(const char *id, const char *idxsfx)
1031/* returns the filename of an object's index */
1032{
1033 xs_str *fn = _object_fn(id);
1034 return xs_replace_i(fn, ".json", idxsfx);
1035}
1036
1037
1038int object_likes_len(const char *id)
1039/* returns the number of likes (without reading the index) */
1040{
1041 xs *fn = _object_index_fn(id, "_l.idx");
1042 return index_len(fn);
1043}
1044
1045
1046int object_announces_len(const char *id)
1047/* returns the number of announces (without reading the index) */
1048{
1049 xs *fn = _object_index_fn(id, "_a.idx");
1050 return index_len(fn);
1051}
1052
1053
1054int object_emojireacts_len(const char *id)
1055/* returns the number of emojireacts (without reading the index) */
1056{
1057 xs *fn = _object_index_fn(id, "_e.idx");
1058 return index_len(fn);
1059}
1060
1061
1062xs_list *object_children(const char *id)
1063/* returns the list of an object's children */
1064{
1065 xs *fn = _object_index_fn(id, "_c.idx");
1066 return index_list(fn, XS_ALL);
1067}
1068
1069
1070xs_list *object_get_emoji_reacts(const char *id)
1071/* returns the list of an object's emoji reactions */
1072{
1073 xs *fn = _object_index_fn(id, "_e.idx");
1074 return index_list(fn, XS_ALL);
1075}
1076
1077
1078xs_list *object_likes(const char *id)
1079{
1080 xs *fn = _object_index_fn(id, "_l.idx");
1081 return index_list(fn, XS_ALL);
1082}
1083
1084
1085xs_list *object_announces(const char *id)
1086{
1087 xs *fn = _object_index_fn(id, "_a.idx");
1088 return index_list(fn, XS_ALL);
1089}
1090
1091
1092int object_parent(const char *md5, char parent[MD5_HEX_SIZE])
1093/* returns the object parent, if any */
1094{
1095 xs *fn = _object_fn_by_md5(md5, "object_parent");
1096
1097 fn = xs_replace_i(fn, ".json", "_p.idx");
1098 return index_first(fn, parent);
1099}
1100
1101
1102int object_admire(const char *id, const char *actor, int like)
1103/* actor likes or announces this object */
1104{
1105 int status = HTTP_STATUS_OK;
1106 xs *fn = _object_fn(id);
1107
1108 fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
1109
1110 if (!index_in(fn, actor)) {
1111 status = index_add(fn, actor);
1112
1113 srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
1114 }
1115
1116 return status;
1117}
1118
1119
1120int object_unadmire(const char *id, const char *actor, int like)
1121/* actor retrives their likes, announces or emojis this object */
1122{
1123 switch (like) {
1124 case 0:
1125 like = 'a';
1126 break;
1127 case 1:
1128 like = 'l';
1129 break;
1130 case 2:
1131 like = 'e';
1132 break;
1133 }
1134 int status;
1135 xs *fn = _object_fn(id);
1136
1137 char sfx[7] = "_x.idx";
1138 sfx[1] = like;
1139
1140 fn = xs_replace_i(fn, ".json", sfx);
1141
1142 status = index_del(fn, actor);
1143
1144 if (valid_status(status))
1145 index_gc(fn);
1146
1147 srv_debug(0,
1148 xs_fmt("object_unadmire (%s) %s %s %d", like >= 'e' ?
1149 (like == 'l' ? "Like" : "EmojiReact") : "Announce" , actor, fn, status));
1150
1151 return status;
1152}
1153
1154int object_emoji_react(const char *mid, const char *eid)
1155/* actor reacts w/ an emoji */
1156{
1157 int status = HTTP_STATUS_OK;
1158 xs *fn = _object_fn(mid);
1159
1160 fn = xs_replace_i(fn, ".json", "_e.idx");
1161
1162 if (!index_in(fn, eid)) {
1163 status = index_add(fn, eid);
1164
1165 srv_debug(1, xs_fmt("object_emoji_react (%s) added %s to %s", "EmojiReact", eid, fn));
1166 }
1167
1168 return status;
1169}
1170
1171
1172int object_rm_emoji_react(const char *mid, const char *eid)
1173/* actor retrives their emoji reaction */
1174{
1175 int status;
1176 xs *fn = _object_fn(mid);
1177
1178 fn = xs_replace_i(fn, ".json", "_e.idx");
1179
1180 status = index_del(fn, eid);
1181 object_del(eid);
1182
1183 if (valid_status(status))
1184 index_gc(fn);
1185
1186 srv_debug(0,
1187 xs_fmt("object_unadmire (EmojiReact) %s %s %d", eid, fn, status));
1188
1189 return status;
1190}
1191
1192
1193xs_str *object_user_cache_fn_by_md5(snac *user, const char *md5, const char *cachedir)
1194{
1195 return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
1196}
1197
1198
1199xs_str *object_user_cache_fn(snac *user, const char *id, const char *cachedir)
1200{
1201 xs *md5 = xs_md5_hex(id, strlen(id));
1202 return object_user_cache_fn_by_md5(user, md5, cachedir);
1203}
1204
1205
1206xs_str *object_user_cache_index_fn(snac *user, const char *cachedir)
1207{
1208 return xs_fmt("%s/%s.idx", user->basedir, cachedir);
1209}
1210
1211
1212int _object_user_cache(snac *user, const char *id, const char *cachedir, int del)
1213/* adds or deletes from a user cache */
1214{
1215 xs *ofn = _object_fn(id);
1216 xs *cfn = object_user_cache_fn(user, id, cachedir);
1217 xs *idx = object_user_cache_index_fn(user, cachedir);
1218 int ret;
1219
1220 if (del) {
1221 ret = unlink(cfn);
1222 index_del(idx, id);
1223 }
1224 else {
1225 /* create the subfolder, if it does not exist */
1226 xs *dir = xs_fmt("%s/%s/", user->basedir, cachedir);
1227 mkdirx(dir);
1228
1229 if ((ret = link(ofn, cfn)) != -1)
1230 index_add(idx, id);
1231 }
1232
1233 return ret;
1234}
1235
1236
1237int object_user_cache_add(snac *user, const char *id, const char *cachedir)
1238/* caches an object into a user cache */
1239{
1240 return _object_user_cache(user, id, cachedir, 0);
1241}
1242
1243
1244int object_user_cache_del(snac *user, const char *id, const char *cachedir)
1245/* deletes an object from a user cache */
1246{
1247 return _object_user_cache(user, id, cachedir, 1);
1248}
1249
1250
1251int object_user_cache_in(snac *user, const char *id, const char *cachedir)
1252/* checks if an object is stored in a cache */
1253{
1254 xs *cfn = object_user_cache_fn(user, id, cachedir);
1255 return !!(mtime(cfn) != 0.0);
1256}
1257
1258
1259int object_user_cache_in_by_md5(snac *user, const char *md5, const char *cachedir)
1260/* checks if an object is stored in a cache */
1261{
1262 xs *cfn = object_user_cache_fn_by_md5(user, md5, cachedir);
1263 return !!(mtime(cfn) != 0.0);
1264}
1265
1266
1267xs_list *object_user_cache_list(snac *user, const char *cachedir, int max, int inv)
1268/* returns the objects in a cache as a list */
1269{
1270 xs *idx = xs_fmt("%s/%s.idx", user->basedir, cachedir);
1271 return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
1272}
1273
1274
1275/** specialized functions **/
1276
1277/** followers **/
1278
1279int follower_add(snac *snac, const char *actor)
1280/* adds a follower */
1281{
1282 int ret = object_user_cache_add(snac, actor, "followers");
1283
1284 snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
1285
1286 return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
1287}
1288
1289
1290int follower_del(snac *snac, const char *actor)
1291/* deletes a follower */
1292{
1293 int ret = object_user_cache_del(snac, actor, "followers");
1294
1295 snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
1296
1297 return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
1298}
1299
1300
1301int follower_check(snac *snac, const char *actor)
1302/* checks if someone is a follower */
1303{
1304 return object_user_cache_in(snac, actor, "followers");
1305}
1306
1307
1308int follower_list_len(snac *snac)
1309/* returns the number of followers */
1310{
1311 xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
1312 return xs_list_len(list);
1313}
1314
1315
1316xs_list *follower_list(snac *snac)
1317/* returns the list of followers */
1318{
1319 xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
1320 xs_list *fwers = xs_list_new();
1321 char *p;
1322 const char *v;
1323
1324 /* resolve the list of md5 to be a list of actors */
1325 p = list;
1326 while (xs_list_iter(&p, &v)) {
1327 xs *a_obj = NULL;
1328
1329 if (valid_status(object_get_by_md5(v, &a_obj))) {
1330 const char *actor = xs_dict_get(a_obj, "id");
1331
1332 if (!xs_is_null(actor)) {
1333 /* check if the actor is still cached */
1334 xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
1335
1336 if (mtime(fn) > 0.0)
1337 fwers = xs_list_append(fwers, actor);
1338 }
1339 }
1340 }
1341
1342 return fwers;
1343}
1344
1345
1346/** pending followers **/
1347
1348int pending_add(snac *user, const char *actor, const xs_dict *msg)
1349/* stores the follow message for later confirmation */
1350{
1351 xs *dir = xs_fmt("%s/pending", user->basedir);
1352 xs *md5 = xs_md5_hex(actor, strlen(actor));
1353 xs *fn = xs_fmt("%s/%s.json", dir, md5);
1354 FILE *f;
1355
1356 mkdirx(dir);
1357
1358 if ((f = fopen(fn, "w")) == NULL)
1359 return -1;
1360
1361 xs_json_dump(msg, 4, f);
1362 fclose(f);
1363
1364 return 0;
1365}
1366
1367
1368int pending_check(snac *user, const char *actor)
1369/* checks if there is a pending follow confirmation for the actor */
1370{
1371 xs *md5 = xs_md5_hex(actor, strlen(actor));
1372 xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1373
1374 return mtime(fn) != 0;
1375}
1376
1377
1378xs_dict *pending_get(snac *user, const char *actor)
1379/* returns the pending follow confirmation for the actor */
1380{
1381 xs *md5 = xs_md5_hex(actor, strlen(actor));
1382 xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1383 xs_dict *msg = NULL;
1384 FILE *f;
1385
1386 if ((f = fopen(fn, "r")) != NULL) {
1387 msg = xs_json_load(f);
1388 fclose(f);
1389 }
1390
1391 return msg;
1392}
1393
1394
1395void pending_del(snac *user, const char *actor)
1396/* deletes a pending follow confirmation for the actor */
1397{
1398 xs *md5 = xs_md5_hex(actor, strlen(actor));
1399 xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
1400
1401 unlink(fn);
1402}
1403
1404
1405xs_list *pending_list(snac *user)
1406/* returns a list of pending follow confirmations */
1407{
1408 xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
1409 xs *l = xs_glob(spec, 0, 0);
1410 xs_list *r = xs_list_new();
1411 const char *v;
1412
1413 xs_list_foreach(l, v) {
1414 FILE *f;
1415 xs *msg = NULL;
1416
1417 if ((f = fopen(v, "r")) == NULL)
1418 continue;
1419
1420 msg = xs_json_load(f);
1421 fclose(f);
1422
1423 if (msg == NULL)
1424 continue;
1425
1426 const char *actor = xs_dict_get(msg, "actor");
1427
1428 if (xs_type(actor) == XSTYPE_STRING)
1429 r = xs_list_append(r, actor);
1430 }
1431
1432 return r;
1433}
1434
1435
1436int pending_count(snac *user)
1437/* returns the number of pending follow confirmations */
1438{
1439 xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
1440 xs *l = xs_glob(spec, 0, 0);
1441
1442 return xs_list_len(l);
1443}
1444
1445
1446int is_msg_mine(snac *user, const char *id)
1447/* returns true if a post id is by the given user */
1448{
1449 int ret = 0;
1450
1451 if (xs_is_string(id)) {
1452 xs *s1 = xs_fmt("%s/", user->actor);
1453 ret = xs_startswith(id, s1);
1454 }
1455
1456 return ret;
1457}
1458
1459
1460/** timeline **/
1461
1462double timeline_mtime(snac *snac)
1463{
1464 xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1465 return mtime(fn);
1466}
1467
1468
1469int timeline_touch(snac *snac)
1470/* changes the date of the timeline index */
1471{
1472 xs *fn = xs_fmt("%s/private.idx", snac->basedir);
1473 return utimes(fn, NULL);
1474}
1475
1476
1477xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
1478/* get the filename of an entry by md5 from any timeline */
1479{
1480 xs_str *fn = NULL;
1481
1482 if (is_md5_hex(md5)) {
1483 fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
1484
1485 if (mtime(fn) == 0.0) {
1486 fn = xs_free(fn);
1487 fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
1488
1489 if (mtime(fn) == 0.0)
1490 fn = xs_free(fn);
1491 }
1492 }
1493
1494 return fn;
1495}
1496
1497
1498int timeline_here_by_md5(snac *snac, const char *md5)
1499/* checks if an object is in the user cache */
1500{
1501 xs *fn = timeline_fn_by_md5(snac, md5);
1502
1503 return !(fn == NULL);
1504}
1505
1506
1507int timeline_here(snac *user, const char *id)
1508{
1509 xs *md5 = xs_md5_hex(id, strlen(id));
1510
1511 return timeline_here_by_md5(user, md5);
1512}
1513
1514
1515int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
1516/* gets a message from the timeline */
1517{
1518 int status = HTTP_STATUS_NOT_FOUND;
1519 FILE *f = NULL;
1520
1521 xs *fn = timeline_fn_by_md5(snac, md5);
1522
1523 if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
1524 *msg = xs_json_load(f);
1525 fclose(f);
1526
1527 if (*msg != NULL)
1528 status = HTTP_STATUS_OK;
1529 }
1530
1531 return status;
1532}
1533
1534
1535int timeline_del(snac *snac, const char *id)
1536/* deletes a message from the timeline */
1537{
1538 /* delete from the user's caches */
1539 object_user_cache_del(snac, id, "public");
1540 object_user_cache_del(snac, id, "private");
1541
1542 unpin(snac, id);
1543 unbookmark(snac, id);
1544
1545 /* try to delete the object if it's not used elsewhere */
1546 return object_del_if_unref(id);
1547}
1548
1549
1550void timeline_update_indexes(snac *snac, const char *id)
1551/* updates the indexes */
1552{
1553 object_user_cache_add(snac, id, "private");
1554
1555 if (is_msg_mine(snac, id)) {
1556 xs *msg = NULL;
1557
1558 if (valid_status(object_get(id, &msg))) {
1559 const int scope = get_msg_visibility(msg);
1560 /* if its ours and is public, also store in public */
1561 if (scope == SCOPE_PUBLIC) {
1562 if (object_user_cache_add(snac, id, "public") >= 0) {
1563 /* also add it to the instance public timeline */
1564 xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
1565 index_add(ipt, id);
1566 }
1567 else
1568 srv_debug(1, xs_fmt("Not added to public instance index %s, visibility %d", id, scope));
1569 }
1570 else
1571 /* also add it to public, it will be discarded later */
1572 object_user_cache_add(snac, id, "public");
1573
1574 /* if `keep_replied_posts` is `true` and a local post is a reply to a remote post, add the parent remote post to the public */
1575 if (xs_is_true(xs_dict_get(srv_config, "keep_replied_posts"))) {
1576 const char *in_reply_to = get_in_reply_to(msg);
1577
1578 if (xs_is_string(in_reply_to) && *in_reply_to &&
1579 !is_msg_mine(snac, in_reply_to)) {
1580 if (object_user_cache_add(snac, in_reply_to, "public") >= 0) {
1581 snac_debug(snac, 1,
1582 xs_fmt("keep_replied_posts: added parent %s to public", in_reply_to));
1583 }
1584 }
1585 }
1586 }
1587 }
1588 else {
1589 /* if `keep_replied_me` is `true` and a remote post is a reply to one of local posts, add it to public */
1590 if (xs_is_true(xs_dict_get(srv_config, "keep_replied_me"))) {
1591 xs *msg = NULL;
1592
1593 if (valid_status(object_get(id, &msg))) {
1594 const char *in_reply_to = get_in_reply_to(msg);
1595
1596 if (xs_is_string(in_reply_to) && *in_reply_to &&
1597 is_msg_mine(snac, in_reply_to)) {
1598 if (object_user_cache_add(snac, id, "public") >= 0) {
1599 snac_debug(snac, 1,
1600 xs_fmt("keep_replied_me: added reply %s to public", id));
1601 }
1602 }
1603 }
1604 }
1605 }
1606}
1607
1608
1609int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
1610/* adds a message to the timeline */
1611{
1612 int ret = object_add(id, o_msg);
1613 timeline_update_indexes(snac, id);
1614
1615 tag_index(id, o_msg);
1616
1617 list_distribute(snac, NULL, o_msg);
1618
1619 snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
1620
1621 return ret;
1622}
1623
1624
1625int timeline_emoji_react(const char *act, const char *id, const xs_dict *msg_o)
1626/* adds an emoji reaction to a message */
1627{
1628 xs *msg = xs_dup(msg_o);
1629 msg = xs_dict_append(msg, "attributedTo", act);
1630 msg = xs_dict_set(msg, "type", "EmojiReact");
1631 const char *emote_id = xs_dict_get(msg, "id");
1632
1633 int ret = object_add(emote_id, msg);
1634 if (ret == HTTP_STATUS_OK || ret == HTTP_STATUS_CREATED)
1635 ret = object_emoji_react(id, emote_id);
1636
1637 return ret;
1638}
1639
1640
1641int timeline_admire(snac *snac, const char *id,
1642 const char *admirer, int like, const xs_dict *msg)
1643/* updates a timeline entry with a new admiration or emoji reaction */
1644{
1645 int ret;
1646 const char *content = xs_dict_get_path(msg, "content");
1647 const char *type = xs_dict_get_path(msg, "type");
1648
1649 /* if we are admiring this, add to both timelines, and store for later */
1650 if (!like && strcmp(admirer, snac->actor) == 0) {
1651 object_user_cache_add(snac, id, "public");
1652 object_user_cache_add(snac, id, "private");
1653 }
1654
1655 if (strcmp(admirer, snac->actor) == 0)
1656 object_user_cache_add(snac, id, "admire");
1657
1658 /* use utf <3 as a like, as it is ugly */
1659 if (type && xs_match(type, "Like|EmojiReact|Emoji") &&
1660 content && strcmp(content, "❤") != 0) {
1661 ret = timeline_emoji_react(snac->actor, id, msg);
1662 snac_debug(snac, 1, xs_fmt("timeline_emoji_react %s", id));
1663 }
1664
1665 else {
1666 ret = object_admire(id, admirer, like);
1667 snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
1668 like ? "Like" : "Announce", id, admirer));
1669 }
1670
1671 return ret;
1672}
1673
1674
1675xs_list *timeline_top_level(snac *snac, const xs_list *list)
1676/* returns the top level md5 entries from this index */
1677{
1678 xs_set seen;
1679 const xs_str *v;
1680
1681 xs_set_init(&seen);
1682
1683 int c = 0;
1684 while (xs_list_next(list, &v, &c)) {
1685 char line[MD5_HEX_SIZE] = "";
1686
1687 strncpy(line, v, sizeof(line));
1688
1689 for (;;) {
1690 char line2[MD5_HEX_SIZE];
1691
1692 /* if it doesn't have a parent, use this */
1693 if (!object_parent(line, line2))
1694 break;
1695
1696 /* well, there is a parent... but is it here? */
1697 if (!timeline_here_by_md5(snac, line2))
1698 break;
1699
1700 /* it's here! try again with its own parent */
1701 strncpy(line, line2, sizeof(line));
1702 }
1703
1704 xs_set_add(&seen, line);
1705 }
1706
1707 return xs_set_result(&seen);
1708}
1709
1710
1711xs_str *user_index_fn(snac *user, const char *idx_name)
1712/* returns the filename of a user index */
1713{
1714 return xs_fmt("%s/%s.idx", user->basedir, idx_name);
1715}
1716
1717
1718xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show, int *more)
1719/* returns a timeline (with all entries) */
1720{
1721 xs *idx = user_index_fn(user, idx_name);
1722
1723 /* if a more flag is sent, request one more */
1724 xs_list *lst = index_list_desc(idx, skip, show + (more != NULL ? 1 : 0));
1725
1726 if (more != NULL) {
1727 if (xs_list_len(lst) > show) {
1728 *more = 1;
1729 lst = xs_list_del(lst, -1);
1730 }
1731 else
1732 *more = 0;
1733 }
1734
1735 return lst;
1736}
1737
1738
1739xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show, int *more)
1740/* returns a timeline (only top level entries) */
1741{
1742 int c_max;
1743
1744 /* maximum number of items in the timeline */
1745 c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
1746
1747 /* never more timeline entries than the configured maximum */
1748 if (show > c_max)
1749 show = c_max;
1750
1751 xs *list = timeline_simple_list(snac, idx_name, skip, show, more);
1752
1753 return timeline_top_level(snac, list);
1754}
1755
1756
1757void timeline_add_mark(snac *user)
1758/* adds an "already seen" mark to the private timeline */
1759{
1760 xs *fn = xs_fmt("%s/private.idx", user->basedir);
1761 char last_entry[MD5_HEX_SIZE] = "";
1762 FILE *f;
1763
1764 /* get the last entry in the index */
1765 if ((f = fopen(fn, "r")) != NULL) {
1766 index_desc_first(f, last_entry, 0);
1767 fclose(f);
1768 }
1769
1770 /* is the last entry *not* a mark? */
1771 if (strcmp(last_entry, MD5_ALREADY_SEEN_MARK) != 0) {
1772 /* add it */
1773 index_add_md5(fn, MD5_ALREADY_SEEN_MARK);
1774 }
1775}
1776
1777
1778xs_str *instance_index_fn(void)
1779{
1780 return xs_fmt("%s/public.idx", srv_basedir);
1781}
1782
1783
1784xs_list *timeline_instance_list(int skip, int show)
1785/* returns the timeline for the full instance */
1786{
1787 xs *idx = instance_index_fn();
1788 xs *lst = index_list_desc(idx, skip, show);
1789
1790 /* make the list unique */
1791 xs_set rep;
1792 xs_set_init(&rep);
1793 const char *md5;
1794
1795 xs_list_foreach(lst, md5)
1796 xs_set_add(&rep, md5);
1797
1798 return xs_set_result(&rep);
1799}
1800
1801
1802/** following **/
1803
1804/* this needs special treatment and cannot use the object db as is,
1805 with a link to a cached author, because we need the Follow object
1806 in case we need to unfollow (Undo + original Follow) */
1807
1808xs_str *_following_fn(snac *snac, const char *actor)
1809{
1810 xs *md5 = xs_md5_hex(actor, strlen(actor));
1811 return xs_fmt("%s/following/%s.json", snac->basedir, md5);
1812}
1813
1814
1815int following_add(snac *snac, const char *actor, const xs_dict *msg)
1816/* adds to the following list */
1817{
1818 int ret = HTTP_STATUS_CREATED;
1819 xs *fn = _following_fn(snac, actor);
1820 FILE *f;
1821 xs *p_object = NULL;
1822
1823 if (valid_status(following_get(snac, actor, &p_object))) {
1824 /* object already exists; if it's of type Accept,
1825 the actor is already being followed and confirmed,
1826 so do nothing */
1827 const char *type = xs_dict_get(p_object, "type");
1828
1829 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1830 snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
1831 return HTTP_STATUS_OK;
1832 }
1833 }
1834
1835 if ((f = fopen(fn, "w")) != NULL) {
1836 xs_json_dump(msg, 4, f);
1837 fclose(f);
1838
1839 /* get the filename of the actor object */
1840 xs *actor_fn = _object_fn(actor);
1841
1842 /* increase its reference count */
1843 fn = xs_replace_i(fn, ".json", "_a.json");
1844 link(actor_fn, fn);
1845 }
1846 else
1847 ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
1848
1849 snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
1850
1851 return ret;
1852}
1853
1854
1855int following_del(snac *snac, const char *actor)
1856/* we're not following this actor any longer */
1857{
1858 xs *fn = _following_fn(snac, actor);
1859
1860 snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
1861
1862 unlink(fn);
1863
1864 /* also delete the reference to the author */
1865 fn = xs_replace_i(fn, ".json", "_a.json");
1866 unlink(fn);
1867
1868 return HTTP_STATUS_OK;
1869}
1870
1871
1872int following_check(snac *snac, const char *actor)
1873/* checks if we are following this actor */
1874{
1875 xs *fn = _following_fn(snac, actor);
1876
1877 return !!(mtime(fn) != 0.0);
1878}
1879
1880
1881int following_get(snac *snac, const char *actor, xs_dict **data)
1882/* returns the 'Follow' object */
1883{
1884 xs *fn = _following_fn(snac, actor);
1885 FILE *f;
1886 int status = HTTP_STATUS_OK;
1887
1888 if ((f = fopen(fn, "r")) != NULL) {
1889 *data = xs_json_load(f);
1890 fclose(f);
1891 }
1892 else
1893 status = HTTP_STATUS_NOT_FOUND;
1894
1895 return status;
1896}
1897
1898
1899int following_list_len(snac *snac)
1900/* returns number of people being followed */
1901{
1902 xs *spec = xs_fmt("%s/following/" "*_a.json", snac->basedir);
1903 xs *glist = xs_glob(spec, 0, 0);
1904 return xs_list_len(glist);
1905}
1906
1907
1908xs_list *following_list(snac *snac)
1909/* returns the list of people being followed */
1910{
1911 xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
1912 xs *glist = xs_glob(spec, 0, 0);
1913 xs_list *p;
1914 const xs_str *v;
1915 xs_list *list = xs_list_new();
1916
1917 /* iterate the list of files */
1918 p = glist;
1919 while (xs_list_iter(&p, &v)) {
1920 FILE *f;
1921
1922 /* load the follower data */
1923 if ((f = fopen(v, "r")) != NULL) {
1924 xs *o = xs_json_load(f);
1925 fclose(f);
1926
1927 if (o != NULL) {
1928 const char *type = xs_dict_get(o, "type");
1929
1930 if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
1931 const char *actor = xs_dict_get(o, "actor");
1932
1933 if (!xs_is_null(actor)) {
1934 list = xs_list_append(list, actor);
1935
1936 /* check if there is a link to the actor object */
1937 xs *v2 = xs_replace(v, ".json", "_a.json");
1938
1939 if (mtime(v2) == 0.0) {
1940 /* no; add a link to it */
1941 xs *actor_fn = _object_fn(actor);
1942 link(actor_fn, v2);
1943 }
1944 }
1945 }
1946 }
1947 }
1948 }
1949
1950 return list;
1951}
1952
1953
1954xs_str *_muted_fn(snac *snac, const char *actor)
1955{
1956 xs *md5 = xs_md5_hex(actor, strlen(actor));
1957 return xs_fmt("%s/muted/%s", snac->basedir, md5);
1958}
1959
1960
1961void mute(snac *snac, const char *actor)
1962/* mutes a moron */
1963{
1964 xs *fn = _muted_fn(snac, actor);
1965 FILE *f;
1966
1967 if ((f = fopen(fn, "w")) != NULL) {
1968 fprintf(f, "%s\n", actor);
1969 fclose(f);
1970
1971 snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
1972 }
1973}
1974
1975
1976void unmute(snac *snac, const char *actor)
1977/* actor is no longer a moron */
1978{
1979 xs *fn = _muted_fn(snac, actor);
1980
1981 unlink(fn);
1982
1983 snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
1984}
1985
1986
1987int is_muted(snac *snac, const char *actor)
1988/* check if someone is muted */
1989{
1990 xs *fn = _muted_fn(snac, actor);
1991
1992 return !!(mtime(fn) != 0.0);
1993}
1994
1995
1996xs_list *muted_list(snac *user)
1997/* returns the list (actor URLs) of the muted morons */
1998{
1999 xs_list *l = xs_list_new();
2000 xs *spec = xs_fmt("%s/muted/" "*", user->basedir);
2001 xs *files = xs_glob(spec, 0, 0);
2002 const char *fn;
2003
2004 xs_list_foreach(files, fn) {
2005 FILE *f;
2006
2007 if ((f = fopen(fn, "r")) != NULL) {
2008 xs *actor = xs_strip_i(xs_readline(f));
2009 fclose(f);
2010
2011 l = xs_list_append(l, actor);
2012 }
2013 }
2014
2015 return l;
2016}
2017
2018/** emojis react **/
2019
2020xs_str *emoji_reacted(snac *user, const char *id)
2021/* returns the emoji an user reacted to a message */
2022{
2023 xs *emojis = object_get_emoji_reacts(id);
2024 int c = 0;
2025 const char *v;
2026
2027 while (xs_list_next(emojis, &v, &c)) {
2028 xs *msg = NULL;
2029 if (object_get_by_md5(v, &msg)) {
2030 const xs_val *act = xs_dict_get(msg, "actor");
2031 if (act && strcmp(act, user->actor) == 0)
2032 return xs_dup(xs_dict_get(msg, "content"));
2033 }
2034 }
2035 return NULL;
2036}
2037
2038/** bookmarking **/
2039
2040int is_bookmarked(snac *user, const char *id)
2041/* returns true if this note is bookmarked */
2042{
2043 return object_user_cache_in(user, id, "bookmark");
2044}
2045
2046
2047int bookmark(snac *user, const char *id)
2048/* bookmarks a post */
2049{
2050 if (is_bookmarked(user, id))
2051 return -3;
2052
2053 return object_user_cache_add(user, id, "bookmark");
2054}
2055
2056
2057int unbookmark(snac *user, const char *id)
2058/* unbookmarks a post */
2059{
2060 return object_user_cache_del(user, id, "bookmark");
2061}
2062
2063
2064xs_list *bookmark_list(snac *user)
2065/* return the lists of bookmarked posts */
2066{
2067 return object_user_cache_list(user, "bookmark", XS_ALL, 1);
2068}
2069
2070
2071xs_str *bookmark_index_fn(snac *user)
2072{
2073 return object_user_cache_index_fn(user, "bookmark");
2074}
2075
2076
2077/** pinning **/
2078
2079int is_pinned(snac *user, const char *id)
2080/* returns true if this note is pinned */
2081{
2082 return object_user_cache_in(user, id, "pinned");
2083}
2084
2085
2086int is_pinned_by_md5(snac *user, const char *md5)
2087{
2088 return object_user_cache_in_by_md5(user, md5, "pinned");
2089}
2090
2091
2092int pin(snac *user, const char *id)
2093/* pins a message */
2094{
2095 int ret = -2;
2096
2097 if (is_msg_mine(user, id)) {
2098 if (is_pinned(user, id))
2099 ret = -3;
2100 else
2101 ret = object_user_cache_add(user, id, "pinned");
2102 }
2103
2104 return ret;
2105}
2106
2107
2108int unpin(snac *user, const char *id)
2109/* unpin a message */
2110{
2111 return object_user_cache_del(user, id, "pinned");
2112}
2113
2114
2115xs_list *pinned_list(snac *user)
2116/* return the lists of pinned posts */
2117{
2118 return object_user_cache_list(user, "pinned", XS_ALL, 1);
2119}
2120
2121
2122/** drafts **/
2123
2124int is_draft(snac *user, const char *id)
2125/* returns true if this note is a draft */
2126{
2127 return object_user_cache_in(user, id, "draft");
2128}
2129
2130
2131void draft_del(snac *user, const char *id)
2132/* delete a message from the draft cache */
2133{
2134 object_user_cache_del(user, id, "draft");
2135}
2136
2137
2138void draft_add(snac *user, const char *id, const xs_dict *msg)
2139/* store the message as a draft */
2140{
2141 /* delete from the index, in case it was already there */
2142 draft_del(user, id);
2143
2144 /* overwrite object */
2145 object_add_ow(id, msg);
2146
2147 /* [re]add to the index */
2148 object_user_cache_add(user, id, "draft");
2149}
2150
2151
2152xs_list *draft_list(snac *user)
2153/* return the lists of drafts */
2154{
2155 return object_user_cache_list(user, "draft", XS_ALL, 1);
2156}
2157
2158
2159/** scheduled posts **/
2160
2161int is_scheduled(snac *user, const char *id)
2162/* returns true if this note is scheduled for future sending */
2163{
2164 return object_user_cache_in(user, id, "sched");
2165}
2166
2167
2168void schedule_del(snac *user, const char *id)
2169/* deletes an scheduled post */
2170{
2171 object_user_cache_del(user, id, "sched");
2172}
2173
2174
2175void schedule_add(snac *user, const char *id, const xs_dict *msg)
2176/* schedules this post for later */
2177{
2178 /* delete from the index, in case it was already there */
2179 schedule_del(user, id);
2180
2181 /* overwrite object */
2182 object_add_ow(id, msg);
2183
2184 /* [re]add to the index */
2185 object_user_cache_add(user, id, "sched");
2186}
2187
2188
2189xs_list *scheduled_list(snac *user)
2190/* return the list of scheduled posts */
2191{
2192 return object_user_cache_list(user, "sched", XS_ALL, 1);
2193}
2194
2195
2196void scheduled_process(snac *user)
2197/* processes the scheduled list, sending those ready to be sent */
2198{
2199 xs *posts = scheduled_list(user);
2200 const char *md5;
2201 xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
2202
2203 xs_list_foreach(posts, md5) {
2204 xs *msg = NULL;
2205
2206 if (valid_status(object_get_by_md5(md5, &msg))) {
2207 if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
2208 /* due date! */
2209 const char *id = xs_dict_get(msg, "id");
2210
2211 timeline_add(user, id, msg);
2212
2213 xs *c_msg = msg_create(user, msg);
2214 enqueue_message(user, c_msg);
2215
2216 schedule_del(user, id);
2217 }
2218 }
2219 }
2220}
2221
2222
2223/** hiding **/
2224
2225xs_str *_hidden_fn(snac *snac, const char *id)
2226{
2227 xs *md5 = xs_md5_hex(id, strlen(id));
2228 return xs_fmt("%s/hidden/%s", snac->basedir, md5);
2229}
2230
2231
2232void hide(snac *snac, const char *id)
2233/* hides an object and its children (if it's a post) */
2234{
2235 xs *fn = _hidden_fn(snac, id);
2236 FILE *f;
2237
2238 if ((f = fopen(fn, "w")) != NULL) {
2239 fprintf(f, "%s\n", id);
2240 fclose(f);
2241
2242 snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
2243
2244 /* hide all the children */
2245 xs *chld = object_children(id);
2246 char *p;
2247 const char *v;
2248
2249 p = chld;
2250 while (xs_list_iter(&p, &v)) {
2251 xs *co = NULL;
2252
2253 /* resolve to get the id */
2254 if (valid_status(object_get_by_md5(v, &co))) {
2255 const char *id = xs_dict_get(co, "id");
2256 if (id != NULL)
2257 hide(snac, id);
2258 }
2259 }
2260 }
2261}
2262
2263
2264int is_hidden(snac *snac, const char *id)
2265/* check is id is hidden */
2266{
2267 xs *fn = _hidden_fn(snac, id);
2268
2269 return !!(mtime(fn) != 0.0);
2270}
2271
2272
2273int unhide(snac *user, const char *id)
2274/* unhides an object */
2275{
2276 xs *fn = _hidden_fn(user, id);
2277 return unlink(fn);
2278}
2279
2280
2281int actor_add(const char *actor, const xs_dict *msg)
2282/* adds an actor */
2283{
2284 return object_add_ow(actor, msg);
2285}
2286
2287
2288int actor_get(const char *actor, xs_dict **data)
2289/* returns an already downloaded actor */
2290{
2291 int status = HTTP_STATUS_OK;
2292 xs_dict *d = NULL;
2293
2294 if (xs_startswith(actor, srv_baseurl)) {
2295 /* it's a (possible) local user */
2296 xs *l = xs_split(actor, "/");
2297 const char *uid = xs_list_get(l, -1);
2298 snac user;
2299
2300 if (!xs_is_null(uid) && user_open(&user, uid)) {
2301 if (data)
2302 *data = msg_actor(&user);
2303
2304 user_free(&user);
2305 return HTTP_STATUS_OK;
2306 }
2307 else
2308 return HTTP_STATUS_NOT_FOUND;
2309 }
2310
2311 /* read the object */
2312 if (!valid_status(status = object_get(actor, &d))) {
2313 d = xs_free(d);
2314 return status;
2315 }
2316
2317 /* if the object is corrupted, discard it */
2318 if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
2319 srv_debug(1, xs_fmt("corrupted actor object %s", actor));
2320 d = xs_free(d);
2321 return HTTP_STATUS_NOT_FOUND;
2322 }
2323
2324 if (data)
2325 *data = d;
2326 else
2327 d = xs_free(d);
2328
2329 xs *fn = _object_fn(actor);
2330 double max_time;
2331
2332 /* maximum time for the actor data to be considered stale */
2333 max_time = 3600.0 * 36.0;
2334
2335 if (mtime(fn) + max_time < (double) time(NULL)) {
2336 /* actor data exists but also stinks */
2337 status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
2338 }
2339
2340 return status;
2341}
2342
2343
2344int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
2345/* gets an actor and requests a refresh if it's stale */
2346{
2347 int status = actor_get(actor, data);
2348
2349 if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
2350 enqueue_actor_refresh(user, actor, 0);
2351
2352 return status;
2353}
2354
2355
2356/** user limiting (announce blocks) **/
2357
2358int limited(snac *user, const char *id, int cmd)
2359/* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
2360{
2361 int ret = 0;
2362 xs *dir = xs_fmt("%s/limited", user->basedir);
2363 xs *md5 = xs_md5_hex(id, strlen(id));
2364 xs *fn = xs_fmt("%s/%s", dir, md5);
2365
2366 switch (cmd) {
2367 case 0: /** check **/
2368 ret = !!(mtime(fn) > 0.0);
2369 break;
2370
2371 case 1: /** limit **/
2372 mkdirx(dir);
2373
2374 if (mtime(fn) > 0.0)
2375 ret = -1;
2376 else {
2377 FILE *f;
2378
2379 if ((f = fopen(fn, "w")) != NULL) {
2380 fprintf(f, "%s\n", id);
2381 fclose(f);
2382 }
2383 else
2384 ret = -2;
2385 }
2386 break;
2387
2388 case 2: /** unlimit **/
2389 if (mtime(fn) > 0.0)
2390 ret = unlink(fn);
2391 else
2392 ret = -1;
2393 break;
2394 }
2395
2396 return ret;
2397}
2398
2399
2400/** tag indexing **/
2401
2402void tag_index(const char *id, const xs_dict *obj)
2403/* update the tag indexes for this object */
2404{
2405 const xs_list *tags = xs_dict_get(obj, "tag");
2406 xs *md5_id = xs_md5_hex(id, strlen(id));
2407
2408 if (get_msg_visibility(obj) != SCOPE_PUBLIC)
2409 return;
2410
2411 if (xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
2412 xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
2413
2414 mkdirx(g_tag_dir);
2415
2416 const xs_dict *v;
2417 int ct = 0;
2418 while (xs_list_next(tags, &v, &ct)) {
2419 const char *type = xs_dict_get(v, "type");
2420 const char *name = xs_dict_get(v, "name");
2421
2422 if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
2423 while (*name == '#' || *name == '@')
2424 name++;
2425
2426 if (*name == '\0')
2427 continue;
2428
2429 xs* name_cased = xs_utf8_to_lower((xs_str *)name);
2430
2431 xs *md5_tag = xs_md5_hex(name_cased, strlen(name_cased));
2432 xs *tag_dir = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
2433 mkdirx(tag_dir);
2434
2435 xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
2436
2437 if (!index_in_md5(g_tag_idx, md5_id))
2438 index_add_md5(g_tag_idx, md5_id);
2439
2440 FILE *f;
2441 xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
2442 if ((f = fopen(g_tag_name, "w")) != NULL) {
2443 fprintf(f, "%s\n", name_cased);
2444 fclose(f);
2445 }
2446
2447 srv_debug(1, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
2448 }
2449 }
2450 }
2451}
2452
2453
2454xs_str *tag_fn(const char *tag)
2455{
2456 if (*tag == '#')
2457 tag++;
2458
2459 xs *lw_tag = xs_utf8_to_lower(tag);
2460 xs *md5 = xs_md5_hex(lw_tag, strlen(lw_tag));
2461
2462 return xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
2463}
2464
2465
2466xs_list *tag_search(const char *tag, int skip, int show)
2467/* returns the list of posts tagged with tag */
2468{
2469 xs *idx = tag_fn(tag);
2470
2471 return index_list_desc(idx, skip, show);
2472}
2473
2474
2475/** lists **/
2476
2477xs_val *list_maint(snac *user, const char *list, int op)
2478/* list maintenance */
2479{
2480 xs_val *l = NULL;
2481
2482 switch (op) {
2483 case 0: /** list of lists **/
2484 {
2485 FILE *f;
2486 xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
2487 xs *ls = xs_glob(spec, 0, 0);
2488 int c = 0;
2489 const char *v;
2490
2491 l = xs_list_new();
2492
2493 while (xs_list_next(ls, &v, &c)) {
2494 if ((f = fopen(v, "r")) != NULL) {
2495 xs *title = xs_readline(f);
2496 fclose(f);
2497
2498 title = xs_strip_i(title);
2499
2500 xs *v2 = xs_replace(v, ".id", "");
2501 xs *l2 = xs_split(v2, "/");
2502
2503 /* return [ list_id, list_title ] */
2504 xs *tmp_list = xs_list_append(xs_list_new(), xs_list_get(l2, -1), title);
2505 l = xs_list_append(l, tmp_list);
2506 }
2507 }
2508 }
2509
2510 break;
2511
2512 case 1: /** create new list (list is the name) **/
2513 {
2514 xs *lol = list_maint(user, NULL, 0);
2515 int c = 0;
2516 const xs_list *v;
2517 int add = 1;
2518
2519 /* check if this list name already exists */
2520 while (xs_list_next(lol, &v, &c)) {
2521 if (strcmp(xs_list_get(v, 1), list) == 0) {
2522 add = 0;
2523
2524 l = xs_dup(xs_list_get(v, 0));
2525
2526 break;
2527 }
2528 }
2529
2530 if (add) {
2531 FILE *f;
2532 xs *dir = xs_fmt("%s/list/", user->basedir);
2533
2534 l = xs_fmt("%010x", time(NULL));
2535
2536 mkdirx(dir);
2537
2538 xs *fn = xs_fmt("%s%s.id", dir, l);
2539
2540 if ((f = fopen(fn, "w")) != NULL) {
2541 fprintf(f, "%s\n", list);
2542 fclose(f);
2543 }
2544 }
2545 }
2546
2547 break;
2548
2549 case 2: /** delete list (list is the id) **/
2550 {
2551 if (xs_is_hex(list)) {
2552 xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
2553 unlink(fn);
2554
2555 fn = xs_replace_i(fn, ".id", ".lst");
2556 unlink(fn);
2557
2558 fn = xs_replace_i(fn, ".lst", ".idx");
2559 unlink(fn);
2560
2561 fn = xs_str_cat(fn, ".bak");
2562 unlink(fn);
2563 }
2564 }
2565
2566 break;
2567
2568 case 3: /** get list name **/
2569 if (xs_is_hex(list)) {
2570 FILE *f;
2571 xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
2572
2573 if ((f = fopen(fn, "r")) != NULL) {
2574 l = xs_strip_i(xs_readline(f));
2575 fclose(f);
2576 }
2577 }
2578
2579 break;
2580
2581 case 4: /** find list id by name **/
2582 if (xs_is_string(list)) {
2583 xs *lol = list_maint(user, NULL, 0);
2584 const xs_list *li;
2585
2586 xs_list_foreach(lol, li) {
2587 if (strcmp(list, xs_list_get(li, 1)) == 0) {
2588 l = xs_dup(xs_list_get(li, 0));
2589 break;
2590 }
2591 }
2592 }
2593 }
2594
2595 return l;
2596}
2597
2598
2599xs_str *list_timeline_fn(snac *user, const char *list)
2600{
2601 return xs_fmt("%s/list/%s.idx", user->basedir, list);
2602}
2603
2604
2605xs_list *list_timeline(snac *user, const char *list, int skip, int show)
2606/* returns the timeline of a list */
2607{
2608 xs_list *l = NULL;
2609
2610 if (!xs_is_hex(list))
2611 return NULL;
2612
2613 xs *fn = list_timeline_fn(user, list);
2614
2615 if (mtime(fn) > 0.0)
2616 l = index_list_desc(fn, skip, show);
2617 else
2618 l = xs_list_new();
2619
2620 return l;
2621}
2622
2623
2624xs_val *list_members(snac *user, const char *list, const char *actor_md5, int op)
2625/* list member management */
2626{
2627 xs_val *l = NULL;
2628
2629 if (!xs_is_hex(list))
2630 return NULL;
2631
2632 if (actor_md5 != NULL && !xs_is_hex(actor_md5))
2633 return NULL;
2634
2635 xs *fn = xs_fmt("%s/list/%s.lst", user->basedir, list);
2636
2637 switch (op) {
2638 case 0: /** list members **/
2639 l = index_list(fn, XS_ALL);
2640
2641 break;
2642
2643 case 1: /** append actor to list **/
2644 if (xs_is_string(actor_md5) && xs_is_hex(actor_md5)) {
2645 if (!index_in_md5(fn, actor_md5))
2646 index_add_md5(fn, actor_md5);
2647 }
2648
2649 break;
2650
2651 case 2: /** delete actor from list **/
2652 if (xs_is_string(actor_md5) && xs_is_hex(actor_md5))
2653 index_del_md5(fn, actor_md5);
2654
2655 break;
2656
2657 default:
2658 srv_log(xs_fmt("ERROR: list_content: bad op %d", op));
2659 break;
2660 }
2661
2662 return l;
2663}
2664
2665
2666void list_distribute(snac *user, const char *who, const xs_dict *post)
2667/* distributes the post to all appropriate lists */
2668{
2669 const char *id = xs_dict_get(post, "id");
2670
2671 /* if who is not set, use the attributedTo in the message */
2672 if (xs_is_null(who))
2673 who = get_atto(post);
2674
2675 if (xs_type(who) == XSTYPE_STRING && xs_type(id) == XSTYPE_STRING) {
2676 xs *a_md5 = xs_md5_hex(who, strlen(who));
2677 xs *i_md5 = xs_md5_hex(id, strlen(id));
2678 xs *spec = xs_fmt("%s/list/" "*.lst", user->basedir);
2679 xs *ls = xs_glob(spec, 0, 0);
2680 int c = 0;
2681 const char *v;
2682
2683 while (xs_list_next(ls, &v, &c)) {
2684 /* is the actor in this list? */
2685 if (index_in_md5(v, a_md5)) {
2686 /* it is; add post md5 to its timeline */
2687 xs *idx = xs_replace(v, ".lst", ".idx");
2688 index_add_md5(idx, i_md5);
2689
2690 snac_debug(user, 1, xs_fmt("listed post %s in %s", id, idx));
2691 }
2692 }
2693 }
2694}
2695
2696
2697/** static data **/
2698
2699static int _load_raw_file(const char *fn, xs_val **data, int *size,
2700 const char *inm, xs_str **etag)
2701/* loads a cached file */
2702{
2703 int status = HTTP_STATUS_NOT_FOUND;
2704
2705 if (fn) {
2706 double tm = mtime(fn);
2707
2708 if (tm > 0.0) {
2709 /* file exists; build the etag */
2710 xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
2711
2712 /* if if-none-match is set, check if it's the same */
2713 if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
2714 /* client has the newest version */
2715 status = HTTP_STATUS_NOT_MODIFIED;
2716 }
2717 else {
2718 /* newer or never downloaded; read the full file */
2719 struct stat sb;
2720 FILE *f;
2721
2722 if (lstat(fn, &sb) == 0 && (sb.st_mode&S_IFMT) == S_IFREG) {
2723 if ((f = fopen(fn, "rb")) != NULL) {
2724 *size = XS_ALL;
2725 *data = xs_read(f, size);
2726 fclose(f);
2727
2728 status = HTTP_STATUS_OK;
2729 }
2730 }
2731 }
2732
2733 /* if caller wants the etag, return it */
2734 if (etag != NULL)
2735 *etag = xs_dup(e);
2736
2737 srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
2738 }
2739 }
2740
2741 return status;
2742}
2743
2744
2745xs_str *_static_fn(snac *snac, const char *id)
2746/* gets the filename for a static file */
2747{
2748 if (strstr(id, ".."))
2749 return NULL;
2750 else
2751 return xs_fmt("%s/static/%s", snac->basedir, id);
2752}
2753
2754
2755int static_get(snac *snac, const char *id, xs_val **data, int *size,
2756 const char *inm, xs_str **etag)
2757/* returns static content */
2758{
2759 xs *fn = _static_fn(snac, id);
2760
2761 return _load_raw_file(fn, data, size, inm, etag);
2762}
2763
2764
2765void static_put(snac *snac, const char *id, const char *data, int size)
2766/* writes status content */
2767{
2768 xs *fn = _static_fn(snac, id);
2769 FILE *f;
2770
2771 if (fn && (f = fopen(fn, "wb")) != NULL) {
2772 fwrite(data, size, 1, f);
2773 fclose(f);
2774
2775 strip_media(fn);
2776 }
2777}
2778
2779
2780void static_put_meta(snac *snac, const char *id, const char *str)
2781/* puts metadata (i.e. a media description string) to id */
2782{
2783 xs *fn = _static_fn(snac, id);
2784
2785 if (fn) {
2786 fn = xs_str_cat(fn, ".txt");
2787 FILE *f;
2788
2789 if ((f = fopen(fn, "w")) != NULL) {
2790 fprintf(f, "%s\n", str);
2791 fclose(f);
2792 }
2793 }
2794}
2795
2796
2797xs_str *static_get_meta(snac *snac, const char *id)
2798/* gets metadata from a media */
2799{
2800 xs *fn = _static_fn(snac, id);
2801 xs_str *r = NULL;
2802
2803 if (fn) {
2804 fn = xs_str_cat(fn, ".txt");
2805 FILE *f;
2806
2807 if ((f = fopen(fn, "r")) != NULL) {
2808 r = xs_strip_i(xs_readline(f));
2809 fclose(f);
2810 }
2811 }
2812 else
2813 r = xs_str_new("");
2814
2815 return r;
2816}
2817
2818
2819/** history **/
2820
2821xs_str *_history_fn(snac *snac, const char *id)
2822/* gets the filename for the history */
2823{
2824 if (strchr(id, '/'))
2825 return NULL;
2826 else
2827 return xs_fmt("%s/history/%s", snac->basedir, id);
2828}
2829
2830
2831double history_mtime(snac *snac, const char *id)
2832{
2833 double t = 0.0;
2834 xs *fn = _history_fn(snac, id);
2835
2836 if (fn != NULL)
2837 t = mtime(fn);
2838
2839 return t;
2840}
2841
2842
2843void history_add(snac *snac, const char *id, const char *content, int size,
2844 xs_str **etag)
2845/* adds something to the history */
2846{
2847 xs *fn = _history_fn(snac, id);
2848 FILE *f;
2849
2850 if (fn && (f = fopen(fn, "w")) != NULL) {
2851 fwrite(content, size, 1, f);
2852 fclose(f);
2853
2854 if (etag) {
2855 double tm = mtime(fn);
2856 *etag = xs_fmt("W/\"snac-%.0lf\"", tm);
2857 }
2858 }
2859}
2860
2861
2862int history_get(snac *snac, const char *id, xs_str **content, int *size,
2863 const char *inm, xs_str **etag)
2864{
2865 xs *fn = _history_fn(snac, id);
2866
2867 return _load_raw_file(fn, content, size, inm, etag);
2868}
2869
2870
2871int history_del(snac *snac, const char *id)
2872{
2873 xs *fn = _history_fn(snac, id);
2874
2875 if (fn)
2876 return unlink(fn);
2877 else
2878 return -1;
2879}
2880
2881
2882xs_list *history_list(snac *snac)
2883{
2884 xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
2885
2886 return xs_glob(spec, 1, 1);
2887}
2888
2889
2890void lastlog_write(snac *snac, const char *source)
2891/* writes the last time the user logged in */
2892{
2893 xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
2894 FILE *f;
2895
2896 if ((f = fopen(fn, "w")) != NULL) {
2897 fprintf(f, "%lf %s\n", ftime(), source);
2898 fclose(f);
2899 }
2900}
2901
2902
2903/** inbox collection **/
2904
2905void inbox_add(const char *inbox)
2906/* collects a shared inbox */
2907{
2908 /* don't collect ourselves */
2909 if (xs_startswith(inbox, srv_baseurl))
2910 return;
2911
2912 xs *md5 = xs_md5_hex(inbox, strlen(inbox));
2913 xs *fn = xs_fmt("%s/inbox/%s", srv_basedir, md5);
2914 FILE *f;
2915
2916 if ((f = fopen(fn, "w")) != NULL) {
2917 fprintf(f, "%s\n", inbox);
2918 fclose(f);
2919 }
2920}
2921
2922
2923void inbox_add_by_actor(const xs_dict *actor)
2924/* collects an actor's shared inbox, if it has one */
2925{
2926 const char *v;
2927
2928 if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
2929 !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
2930 /* only collect this inbox if its instance is not blocked */
2931 if (!is_instance_blocked(v))
2932 inbox_add(v);
2933 }
2934}
2935
2936
2937xs_list *inbox_list(void)
2938/* returns the collected inboxes as a list */
2939{
2940 xs_list *ibl = xs_list_new();
2941 xs *spec = xs_fmt("%s/inbox/" "*", srv_basedir);
2942 xs *files = xs_glob(spec, 0, 0);
2943 const xs_val *v;
2944
2945 xs_list_foreach(files, v) {
2946 FILE *f;
2947
2948 if ((f = fopen(v, "r")) != NULL) {
2949 xs *line = xs_readline(f);
2950
2951 if (line && *line) {
2952 line = xs_strip_i(line);
2953
2954 if (!is_instance_blocked(line))
2955 ibl = xs_list_append(ibl, line);
2956 }
2957
2958 fclose(f);
2959 }
2960 }
2961
2962 return ibl;
2963}
2964
2965
2966/** instance-wide operations **/
2967
2968xs_str *_instance_block_fn(const char *instance)
2969{
2970 xs *s = xs_replace(instance, "http:/" "/", "");
2971 xs *s1 = xs_replace(s, "https:/" "/", "");
2972 xs *l = xs_split(s1, "/");
2973 const char *p = xs_list_get(l, 0);
2974 xs *md5 = xs_md5_hex(p, strlen(p));
2975
2976 return xs_fmt("%s/block/%s", srv_basedir, md5);
2977}
2978
2979
2980int is_instance_blocked(const char *instance)
2981{
2982 xs *fn = _instance_block_fn(instance);
2983
2984 return !!(mtime(fn) != 0.0);
2985}
2986
2987
2988int instance_block(const char *instance)
2989/* blocks a full instance */
2990{
2991 int ret;
2992
2993 /* create the subdir */
2994 xs *dir = xs_fmt("%s/block/", srv_basedir);
2995 mkdirx(dir);
2996
2997 if (!is_instance_blocked(instance)) {
2998 xs *fn = _instance_block_fn(instance);
2999 FILE *f;
3000
3001 if ((f = fopen(fn, "w")) != NULL) {
3002 fprintf(f, "%s\n", instance);
3003 fclose(f);
3004
3005 ret = 0;
3006 }
3007 else
3008 ret = -1;
3009 }
3010 else
3011 ret = -2;
3012
3013 return ret;
3014}
3015
3016
3017int instance_unblock(const char *instance)
3018/* unblocks a full instance */
3019{
3020 int ret;
3021
3022 if (is_instance_blocked(instance)) {
3023 xs *fn = _instance_block_fn(instance);
3024 ret = unlink(fn);
3025 }
3026 else
3027 ret = -2;
3028
3029 return ret;
3030}
3031
3032
3033/** operations by content **/
3034
3035int content_match(const char *file, const xs_dict *msg)
3036/* checks if a message's content matches any of the regexes in file */
3037/* file format: one regex per line */
3038{
3039 xs *fn = xs_fmt("%s/%s", srv_basedir, file);
3040 FILE *f;
3041 int r = 0;
3042 const char *v = xs_dict_get(msg, "content");
3043
3044 if (xs_type(v) == XSTYPE_STRING && *v) {
3045 if ((f = fopen(fn, "r")) != NULL) {
3046 srv_debug(1, xs_fmt("content_match: loading regexes from %s", fn));
3047
3048 /* massage content (strip HTML tags, etc.) */
3049 xs *c1 = xs_regex_replace(v, "<[^>]+>", " ");
3050 c1 = xs_regex_replace_i(c1, " {2,}", " ");
3051 xs *c = xs_utf8_to_lower(c1);
3052
3053 while (!r && !feof(f)) {
3054 xs *rx = xs_strip_i(xs_readline(f));
3055
3056 if (*rx && xs_regex_match(c, rx)) {
3057 srv_debug(1, xs_fmt("content_match: match for '%s'", rx));
3058 r = 1;
3059 }
3060 }
3061
3062 fclose(f);
3063 }
3064 }
3065
3066 return r;
3067}
3068
3069
3070xs_list *content_search(snac *user, const char *regex,
3071 int priv, int skip, int show, int max_secs, int *timeout)
3072/* returns a list of posts which content matches the regex */
3073{
3074 if (regex == NULL || *regex == '\0')
3075 return xs_list_new();
3076
3077 xs *i_regex = xs_utf8_to_lower(regex);
3078
3079 xs_set seen;
3080
3081 xs_set_init(&seen);
3082
3083 if (max_secs == 0)
3084 max_secs = 3;
3085
3086 time_t t = time(NULL) + max_secs;
3087 *timeout = 0;
3088
3089 /* iterate all timelines simultaneously */
3090 xs_list *tls[3] = {0};
3091 const char *md5s[3] = {0};
3092 int c[3] = {0};
3093
3094 tls[0] = timeline_simple_list(user, "public", 0, XS_ALL, NULL); /* public */
3095 tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
3096 tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL, NULL) : xs_list_new(); /* private or none */
3097
3098 /* first positioning */
3099 for (int n = 0; n < 3; n++)
3100 xs_list_next(tls[n], &md5s[n], &c[n]);
3101
3102 show += skip;
3103
3104 while (show > 0) {
3105 /* timeout? */
3106 if (time(NULL) > t) {
3107 *timeout = 1;
3108 break;
3109 }
3110
3111 /* find the newest post */
3112 int newest = -1;
3113 double mtime = 0.0;
3114
3115 for (int n = 0; n < 3; n++) {
3116 if (md5s[n] != NULL) {
3117 xs *fn = _object_fn_by_md5(md5s[n], "content_search");
3118 double mt;
3119
3120 while ((mt = mtime(fn)) == 0 && md5s[n] != NULL) {
3121 /* object is not here: move to the next one */
3122 if (xs_list_next(tls[n], &md5s[n], &c[n])) {
3123 xs_free(fn);
3124 fn = _object_fn_by_md5(md5s[n], "content_search_2");
3125 }
3126 else
3127 md5s[n] = NULL;
3128 }
3129
3130 if (mt > mtime) {
3131 newest = n;
3132 mtime = mt;
3133 }
3134 }
3135 }
3136
3137 if (newest == -1)
3138 break;
3139
3140 const char *md5 = md5s[newest];
3141
3142 /* advance the chosen timeline */
3143 if (!xs_list_next(tls[newest], &md5s[newest], &c[newest]))
3144 md5s[newest] = NULL;
3145
3146 xs *post = NULL;
3147
3148 if (!valid_status(object_get_by_md5(md5, &post)))
3149 continue;
3150
3151 if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE))
3152 continue;
3153
3154 const char *id = xs_dict_get(post, "id");
3155
3156 if (id == NULL || is_hidden(user, id))
3157 continue;
3158
3159 /* recalculate the md5 id to be sure it's not repeated
3160 (it may have been searched by the "url" field instead of "id") */
3161 xs *new_md5 = xs_md5_hex(id, strlen(id));
3162 md5 = new_md5;
3163
3164 /* test for the post URL */
3165 if (strcmp(id, regex) == 0) {
3166 if (xs_set_add(&seen, md5) == 1)
3167 show--;
3168
3169 continue;
3170 }
3171
3172 /* test for the alternate post id */
3173 const char *url = xs_dict_get(post, "url");
3174 if (xs_type(url) == XSTYPE_STRING && strcmp(url, regex) == 0) {
3175 if (xs_set_add(&seen, md5) == 1)
3176 show--;
3177
3178 continue;
3179 }
3180
3181 xs *c = xs_str_new(NULL);
3182 const char *content = xs_dict_get(post, "content");
3183 const char *name = xs_dict_get(post, "name");
3184 const char *atto = get_atto(post);
3185
3186 if (!xs_is_null(content))
3187 c = xs_str_cat(c, content);
3188 if (!xs_is_null(name))
3189 c = xs_str_cat(c, " ", name);
3190 if (!xs_is_null(atto))
3191 c = xs_str_cat(c, " ", atto);
3192
3193 /* add alt-texts from attachments */
3194 const xs_list *atts = xs_dict_get(post, "attachment");
3195 int tc = 0;
3196 const xs_dict *att;
3197
3198 while (xs_list_next(atts, &att, &tc)) {
3199 const char *name = xs_dict_get(att, "name");
3200
3201 if (name != NULL)
3202 c = xs_str_cat(c, " ", name);
3203 }
3204
3205 /* strip HTML */
3206 c = xs_regex_replace_i(c, "<[^>]+>", " ");
3207 c = xs_regex_replace_i(c, " {2,}", " ");
3208
3209 /* convert to lowercase */
3210 xs *lc = xs_utf8_to_lower(c);
3211
3212 /* apply regex */
3213 if (xs_regex_match(lc, i_regex)) {
3214 if (xs_set_add(&seen, md5) == 1)
3215 show--;
3216 }
3217 }
3218
3219 xs_list *r = xs_set_result(&seen);
3220
3221 if (skip) {
3222 /* BAD */
3223 while (skip--) {
3224 r = xs_list_del(r, 0);
3225 }
3226 }
3227
3228 xs_free(tls[0]);
3229 xs_free(tls[1]);
3230 xs_free(tls[2]);
3231
3232 return r;
3233}
3234
3235
3236int actor_failure(const char *actor, int op)
3237/* actor failure maintenance */
3238{
3239 int ret = 0;
3240
3241 xs *md5 = xs_md5_hex(actor, strlen(actor));
3242 xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
3243
3244 switch (op) {
3245 case 0: /** check **/
3246 if (mtime(fn))
3247 ret = -1;
3248
3249 break;
3250
3251 case 1: /** register a failure **/
3252 if (mtime(fn) == 0.0) {
3253 FILE *f;
3254
3255 /* only create once, as the date will be used */
3256 if ((f = fopen(fn, "w")) != NULL) {
3257 fprintf(f, "%s\n", actor);
3258 fclose(f);
3259 }
3260 }
3261
3262 break;
3263
3264 case 2: /** clear a failure **/
3265 /* called whenever a message comes from this instance */
3266 unlink(fn);
3267
3268 break;
3269 }
3270
3271 return ret;
3272}
3273
3274
3275int instance_failure(const char *url, int op)
3276/* do some checks and accounting on instance failures */
3277{
3278 int ret = 0;
3279 xs *l = xs_split(url, "/");
3280 const char *hostname = xs_list_get(l, 2);
3281 double mt;
3282
3283 if (!xs_is_string(hostname))
3284 return 0;
3285
3286 xs *md5 = xs_md5_hex(hostname, strlen(hostname));
3287 xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
3288
3289 switch (op) {
3290 case 0: /** check **/
3291 if ((mt = mtime(fn)) != 0.0) {
3292 /* grace time */
3293 double seconds_failing = xs_number_get(xs_dict_get_def(srv_config, "max_failing_days", "15"))
3294 * (24 * 60 * 60);
3295
3296 if ((double)time(NULL) - mt > seconds_failing)
3297 ret = -1;
3298 }
3299
3300 break;
3301
3302 case 1: /** register a failure **/
3303 if (mtime(fn) == 0.0) {
3304 FILE *f;
3305
3306 /* only create once, as the date will be used */
3307 if ((f = fopen(fn, "w")) != NULL) {
3308 fprintf(f, "%s\n", hostname);
3309 fclose(f);
3310 }
3311 }
3312
3313 break;
3314
3315 case 2: /** clear a failure **/
3316 /* called whenever a message comes from this instance */
3317 unlink(fn);
3318
3319 break;
3320 }
3321
3322 return ret;
3323}
3324
3325
3326int grave(const char *objid, int op)
3327/* the graveyeard of deleted objects */
3328{
3329 int ret = 0;
3330 xs *dir = xs_fmt("%s/grave", srv_basedir);
3331 xs *md5 = xs_md5_hex(objid, strlen(objid));
3332 xs *fn = xs_fmt("%s/%s", dir, md5);
3333 FILE *f;
3334
3335 switch (op) {
3336 case 0: /** check **/
3337 ret = mtime(fn) > 0.0 ? 1 : 0;
3338 break;
3339
3340 case 1: /** add **/
3341 mkdirx(dir);
3342
3343 if ((f = fopen(fn, "w")) != NULL) {
3344 fprintf(f, "%s\n", objid);
3345 fclose(f);
3346 }
3347
3348 break;
3349
3350 case 2: /** del **/
3351 unlink(fn);
3352 break;
3353 }
3354
3355 return ret;
3356}
3357
3358
3359/** notifications **/
3360
3361xs_str *notify_check_time(snac *snac, int reset)
3362/* gets or resets the latest notification check time */
3363{
3364 xs_str *t = NULL;
3365 xs *fn = xs_fmt("%s/notifydate.txt", snac->basedir);
3366 FILE *f;
3367
3368 if (reset) {
3369 if ((f = fopen(fn, "w")) != NULL) {
3370 t = tid(0);
3371 fprintf(f, "%s\n", t);
3372 fclose(f);
3373 }
3374 }
3375 else {
3376 if ((f = fopen(fn, "r")) != NULL) {
3377 t = xs_readline(f);
3378 fclose(f);
3379 }
3380 else
3381 /* never set before */
3382 t = xs_fmt("%16.6f", 0.0);
3383 }
3384
3385 return t;
3386}
3387
3388xs_dict *markers_get(snac *snac, const xs_list *markers)
3389{
3390 xs *data = NULL;
3391 xs_dict *returns = xs_dict_new();
3392 xs *fn = xs_fmt("%s/markers.json", snac->basedir);
3393 const xs_str *v = NULL;
3394 FILE *f;
3395
3396 if ((f = fopen(fn, "r")) != NULL) {
3397 data = xs_json_load(f);
3398 fclose(f);
3399 }
3400
3401 if (xs_is_null(data))
3402 data = xs_dict_new();
3403
3404 xs_list_foreach(markers, v) {
3405 const xs_dict *mark = xs_dict_get(data, v);
3406 if (!xs_is_null(mark)) {
3407 returns = xs_dict_append(returns, v, mark);
3408 }
3409 }
3410 return returns;
3411}
3412
3413xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
3414/* gets or sets notification marker */
3415{
3416 xs *data = NULL;
3417 xs_dict *written = xs_dict_new();
3418 xs *fn = xs_fmt("%s/markers.json", snac->basedir);
3419 FILE *f;
3420
3421 if ((f = fopen(fn, "r")) != NULL) {
3422 data = xs_json_load(f);
3423 fclose(f);
3424 }
3425
3426 if (xs_is_null(data))
3427 data = xs_dict_new();
3428
3429 if (!xs_is_null(home_marker)) {
3430 xs *home = xs_dict_new();
3431 xs *s_tid = tid(0);
3432 home = xs_dict_append(home, "last_read_id", home_marker);
3433 home = xs_dict_append(home, "version", xs_stock(0));
3434 home = xs_dict_append(home, "updated_at", s_tid);
3435 data = xs_dict_set(data, "home", home);
3436 written = xs_dict_append(written, "home", home);
3437 }
3438
3439 if (!xs_is_null(notify_marker)) {
3440 xs *notify = xs_dict_new();
3441 xs *s_tid = tid(0);
3442 notify = xs_dict_append(notify, "last_read_id", notify_marker);
3443 notify = xs_dict_append(notify, "version", xs_stock(0));
3444 notify = xs_dict_append(notify, "updated_at", s_tid);
3445 data = xs_dict_set(data, "notifications", notify);
3446 written = xs_dict_append(written, "notifications", notify);
3447 }
3448
3449 if ((f = fopen(fn, "w")) != NULL) {
3450 xs_json_dump(data, 4, f);
3451 fclose(f);
3452 }
3453
3454 return written;
3455}
3456
3457void notify_add(snac *snac, const char *type, const char *utype,
3458 const char *actor, const char *objid, const xs_dict *msg)
3459/* adds a new notification */
3460{
3461 xs *ntid = tid(0);
3462 xs *fn = xs_fmt("%s/notify/", snac->basedir);
3463 xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
3464 FILE *f;
3465
3466 /* create the directory */
3467 mkdirx(fn);
3468 fn = xs_str_cat(fn, ntid);
3469 fn = xs_str_cat(fn, ".json");
3470
3471 xs *noti = xs_dict_new();
3472
3473 noti = xs_dict_append(noti, "id", ntid);
3474 noti = xs_dict_append(noti, "type", type);
3475 noti = xs_dict_append(noti, "utype", utype);
3476 noti = xs_dict_append(noti, "actor", actor);
3477 noti = xs_dict_append(noti, "date", date);
3478 noti = xs_dict_append(noti, "msg", msg);
3479
3480 if (!xs_is_null(objid))
3481 noti = xs_dict_append(noti, "objid", objid);
3482
3483 if ((f = fopen(fn, "w")) != NULL) {
3484 xs_json_dump(noti, 4, f);
3485 fclose(f);
3486 }
3487
3488 /* add it to the index if it already exists */
3489 xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3490
3491 if (mtime(idx) != 0.0) {
3492 pthread_mutex_lock(&data_mutex);
3493
3494 if ((f = fopen(idx, "a")) != NULL) {
3495 fprintf(f, "%-32s\n", ntid);
3496 fclose(f);
3497 }
3498
3499 pthread_mutex_unlock(&data_mutex);
3500 }
3501
3502 if (!xs_is_true(xs_dict_get(srv_config, "disable_notify_webhook")))
3503 enqueue_notify_webhook(snac, noti, 0);
3504}
3505
3506
3507xs_dict *notify_get(snac *snac, const char *id)
3508/* gets a notification */
3509{
3510 /* base file */
3511 xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
3512
3513 /* strip spaces and add extension */
3514 fn = xs_strip_i(fn);
3515 fn = xs_str_cat(fn, ".json");
3516
3517 FILE *f;
3518 xs_dict *out = NULL;
3519
3520 if ((f = fopen(fn, "r")) != NULL) {
3521 out = xs_json_load(f);
3522 fclose(f);
3523 }
3524
3525 return out;
3526}
3527
3528
3529xs_list *notify_list(snac *snac, int skip, int show)
3530/* returns a list of notification ids */
3531{
3532 xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3533
3534 if (mtime(idx) == 0.0) {
3535 /* create the index from scratch */
3536 FILE *f;
3537
3538 pthread_mutex_lock(&data_mutex);
3539
3540 if ((f = fopen(idx, "w")) != NULL) {
3541 xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
3542 xs *lst = xs_glob(spec, 1, 0);
3543 xs_list *p = lst;
3544 const char *v;
3545
3546 while (xs_list_iter(&p, &v)) {
3547 char *p = strrchr(v, '.');
3548 if (p) {
3549 *p = '\0';
3550 fprintf(f, "%-32s\n", v);
3551 }
3552 }
3553
3554 fclose(f);
3555 }
3556
3557 pthread_mutex_unlock(&data_mutex);
3558 }
3559
3560 return index_list_desc(idx, skip, show);
3561}
3562
3563
3564xs_list *notify_filter_list(snac *snac, xs_list *notifs)
3565/* apply user-defined notification filter to IDs */
3566{
3567 const xs_dict *n_filter = xs_dict_get(snac->config, "notify_filter");
3568 if (!n_filter) {
3569 return xs_dup(notifs);
3570 }
3571 const xs_val *n_def = xs_stock( XSTYPE_TRUE );
3572 int n_likes_on = xs_is_true(xs_dict_get_def(n_filter, "likes", n_def));
3573 int n_reacts_on = xs_is_true(xs_dict_get_def(n_filter, "reacts", n_def));
3574 int n_ments_on = xs_is_true(xs_dict_get_def(n_filter, "mentions", n_def));
3575 int n_ann_on = xs_is_true(xs_dict_get_def(n_filter, "announces", n_def));
3576 int n_fol_on = xs_is_true(xs_dict_get_def(n_filter, "follows", n_def));
3577 int n_unfol_on = xs_is_true(xs_dict_get_def(n_filter, "unfollows", n_def));
3578 int n_folreq_on = xs_is_true(xs_dict_get_def(n_filter, "folreqs", n_def));
3579 int n_blocks_on = xs_is_true(xs_dict_get_def(n_filter, "blocks", n_def));
3580 int n_polls_on = xs_is_true(xs_dict_get_def(n_filter, "polls", n_def));
3581
3582 const xs_str *v;
3583 xs_list *flt = xs_list_new();
3584
3585 xs_list_foreach(notifs, v) {
3586 xs *noti = notify_get(snac, v);
3587
3588 if (noti == NULL)
3589 continue;
3590
3591 const char *type = xs_dict_get(noti, "type");
3592 const char *utype = xs_dict_get(noti, "utype");
3593 const char *actor_id = xs_dict_get(noti, "actor");
3594 if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
3595 continue;
3596 if (strcmp(type, "Create") == 0 && !n_ments_on)
3597 continue;
3598 if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0 && !n_polls_on)
3599 continue;
3600 if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0 && !n_unfol_on)
3601 continue;
3602 if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
3603 if (strcmp(type, "Like") == 0 && !n_likes_on)
3604 continue;
3605 if (strcmp(type, "EmojiReact") == 0 && !n_reacts_on)
3606 continue;
3607 }
3608 if (strcmp(type, "Follow") == 0) {
3609 if (pending_check(snac, actor_id)) {
3610 if (!n_folreq_on)
3611 continue;
3612 }
3613 else
3614 if (!n_fol_on)
3615 continue;
3616 }
3617 if (strcmp(type, "Block") == 0 && !n_blocks_on)
3618 continue;
3619 if (strcmp(type, "Announce") == 0 && !n_ann_on)
3620 continue;
3621 flt = xs_list_append(flt, v);
3622 }
3623 return flt;
3624}
3625
3626
3627int notify_new_num(snac *snac)
3628/* counts the number of new notifications */
3629{
3630 xs *t = notify_check_time(snac, 0);
3631 xs *lst_unfilt = notify_list(snac, 0, XS_ALL);
3632 xs *lst = notify_filter_list(snac, lst_unfilt);
3633 int cnt = 0;
3634
3635 xs_list *p = lst;
3636 const xs_str *v;
3637
3638 while (xs_list_iter(&p, &v)) {
3639 xs *id = xs_strip_i(xs_dup(v));
3640
3641 /* old? count no more */
3642 if (strcmp(id, t) < 0)
3643 break;
3644
3645 cnt++;
3646 }
3647
3648 return cnt;
3649}
3650
3651
3652void notify_clear(snac *snac)
3653/* clears all notifications */
3654{
3655 xs *spec = xs_fmt("%s/notify/" "*", snac->basedir);
3656 xs *lst = xs_glob(spec, 0, 0);
3657 xs_list *p = lst;
3658 const xs_str *v;
3659
3660 while (xs_list_iter(&p, &v))
3661 unlink(v);
3662
3663 xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
3664
3665 if (mtime(idx) != 0.0) {
3666 pthread_mutex_lock(&data_mutex);
3667 truncate(idx, 0);
3668 pthread_mutex_unlock(&data_mutex);
3669 }
3670}
3671
3672
3673/** the queue **/
3674
3675static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
3676/* writes safely to the queue */
3677{
3678 xs *tfn = xs_fmt("%s.tmp", fn);
3679 FILE *f;
3680
3681 if ((f = fopen(tfn, "w")) != NULL) {
3682 xs_json_dump(msg, 4, f);
3683 fclose(f);
3684
3685 rename(tfn, fn);
3686 }
3687
3688 return msg;
3689}
3690
3691
3692static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
3693/* creates a queue message */
3694{
3695 int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
3696 xs *ntid = tid(retries * 60 * qrt);
3697 xs *rn = xs_number_new(retries);
3698
3699 xs_dict *qmsg = xs_dict_new();
3700
3701 qmsg = xs_dict_append(qmsg, "type", type);
3702 qmsg = xs_dict_append(qmsg, "message", msg);
3703 qmsg = xs_dict_append(qmsg, "retries", rn);
3704 qmsg = xs_dict_append(qmsg, "ntid", ntid);
3705
3706 return qmsg;
3707}
3708
3709
3710void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
3711/* enqueues an input message */
3712{
3713 xs *qmsg = _new_qmsg("input", msg, retries);
3714 const char *ntid = xs_dict_get(qmsg, "ntid");
3715 xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
3716
3717 qmsg = xs_dict_append(qmsg, "req", req);
3718
3719 qmsg = _enqueue_put(fn, qmsg);
3720
3721 snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
3722}
3723
3724
3725void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
3726/* enqueues an input message from the shared input */
3727{
3728 xs *qmsg = _new_qmsg("input", msg, retries);
3729 const char *ntid = xs_dict_get(qmsg, "ntid");
3730 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3731
3732 qmsg = xs_dict_append(qmsg, "req", req);
3733
3734 qmsg = _enqueue_put(fn, qmsg);
3735
3736 srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
3737}
3738
3739
3740void enqueue_output_raw(const char *keyid, const char *seckey,
3741 const xs_dict *msg, const xs_str *inbox,
3742 int retries, int p_status)
3743/* enqueues an output message to an inbox */
3744{
3745 xs *qmsg = _new_qmsg("output", msg, retries);
3746 const char *ntid = xs_dict_get(qmsg, "ntid");
3747 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3748
3749 xs *ns = xs_number_new(p_status);
3750 qmsg = xs_dict_append(qmsg, "p_status", ns);
3751
3752 qmsg = xs_dict_append(qmsg, "inbox", inbox);
3753 qmsg = xs_dict_append(qmsg, "keyid", keyid);
3754 qmsg = xs_dict_append(qmsg, "seckey", seckey);
3755
3756 /* if it's to be sent right now, bypass the disk queue and post the job */
3757 if (retries == 0 && p_state != NULL)
3758 job_post(qmsg, 0);
3759 else {
3760 qmsg = _enqueue_put(fn, qmsg);
3761 srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
3762 }
3763}
3764
3765
3766void enqueue_output(snac *snac, const xs_dict *msg,
3767 const xs_str *inbox, int retries, int p_status)
3768/* enqueues an output message to an inbox */
3769{
3770 if (is_msg_mine(snac, inbox)) {
3771 snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
3772 return;
3773 }
3774
3775 const char *seckey = xs_dict_get(snac->key, "secret");
3776
3777 enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
3778}
3779
3780
3781void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
3782 const xs_str *actor, int retries)
3783/* enqueues an output message for an actor */
3784{
3785 xs *inbox = get_actor_inbox(actor, 1);
3786
3787 if (!xs_is_null(inbox))
3788 enqueue_output(snac, msg, inbox, retries, 0);
3789 else
3790 snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
3791}
3792
3793
3794void enqueue_email(const xs_dict *msg, int retries)
3795/* enqueues an email message to be sent */
3796{
3797 xs *qmsg = _new_qmsg("email", msg, retries);
3798 const char *ntid = xs_dict_get(qmsg, "ntid");
3799 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3800
3801 qmsg = _enqueue_put(fn, qmsg);
3802
3803 srv_debug(1, xs_fmt("enqueue_email %d", retries));
3804}
3805
3806
3807void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
3808/* enqueues a message to be sent via Telegram */
3809{
3810 xs *qmsg = _new_qmsg("telegram", msg, 0);
3811 const char *ntid = xs_dict_get(qmsg, "ntid");
3812 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3813
3814 qmsg = xs_dict_append(qmsg, "bot", bot);
3815 qmsg = xs_dict_append(qmsg, "chat_id", chat_id);
3816
3817 qmsg = _enqueue_put(fn, qmsg);
3818
3819 srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
3820}
3821
3822void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
3823/* enqueues a message to be sent via ntfy */
3824{
3825 xs *qmsg = _new_qmsg("ntfy", msg, 0);
3826 const char *ntid = xs_dict_get(qmsg, "ntid");
3827 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3828
3829 qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
3830 qmsg = xs_dict_append(qmsg, "ntfy_token", ntfy_token);
3831
3832
3833 qmsg = _enqueue_put(fn, qmsg);
3834
3835 srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
3836}
3837
3838void enqueue_message(snac *snac, const xs_dict *msg)
3839/* enqueues an output message */
3840{
3841 xs *qmsg = _new_qmsg("message", msg, 0);
3842 const char *ntid = xs_dict_get(qmsg, "ntid");
3843 xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
3844
3845 qmsg = _enqueue_put(fn, qmsg);
3846
3847 snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
3848}
3849
3850
3851void enqueue_close_question(snac *user, const char *id, int end_secs)
3852/* enqueues the closing of a question */
3853{
3854 xs *qmsg = _new_qmsg("close_question", id, 0);
3855 xs *ntid = tid(end_secs);
3856 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3857
3858 qmsg = xs_dict_set(qmsg, "ntid", ntid);
3859
3860 qmsg = _enqueue_put(fn, qmsg);
3861
3862 snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
3863}
3864
3865
3866void enqueue_object_request(snac *user, const char *id, int forward_secs)
3867/* enqueues the request of an object in the future */
3868{
3869 if (forward_secs < 0) {
3870 /* set it to a somewhat random delay */
3871 forward_secs = xs_hash_func(user->uid, strlen(user->uid)) % -forward_secs;
3872 }
3873
3874 xs *qmsg = _new_qmsg("object_request", id, 0);
3875 xs *ntid = tid(forward_secs);
3876 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3877
3878 qmsg = xs_dict_set(qmsg, "ntid", ntid);
3879
3880 qmsg = _enqueue_put(fn, qmsg);
3881
3882 snac_debug(user, 0, xs_fmt("enqueue_object_request %s %d", id, forward_secs));
3883}
3884
3885
3886void enqueue_verify_links(snac *user)
3887/* enqueues a link verification */
3888{
3889 xs *qmsg = _new_qmsg("verify_links", "", 0);
3890 const char *ntid = xs_dict_get(qmsg, "ntid");
3891 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3892
3893 qmsg = _enqueue_put(fn, qmsg);
3894
3895 snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
3896}
3897
3898
3899void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs)
3900/* enqueues an actor refresh */
3901{
3902 if (forward_secs < 0) {
3903 /* set it to a somewhat random delay */
3904 forward_secs = xs_hash_func(user->uid, strlen(user->uid)) % -forward_secs;
3905 }
3906
3907 xs *qmsg = _new_qmsg("actor_refresh", "", 0);
3908 xs *ntid = tid(forward_secs);
3909 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3910
3911 qmsg = xs_dict_set(qmsg, "ntid", ntid);
3912 qmsg = xs_dict_append(qmsg, "actor", actor);
3913
3914 qmsg = _enqueue_put(fn, qmsg);
3915
3916 snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s %d", actor, forward_secs));
3917}
3918
3919
3920void enqueue_webmention(const xs_dict *msg)
3921/* enqueues a webmention for the post */
3922{
3923 xs *qmsg = _new_qmsg("webmention", msg, 0);
3924 const char *ntid = xs_dict_get(qmsg, "ntid");
3925 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
3926
3927 qmsg = _enqueue_put(fn, qmsg);
3928
3929 srv_debug(1, xs_fmt("enqueue_webmention"));
3930}
3931
3932
3933void enqueue_notify_webhook(snac *user, const xs_dict *noti, int retries)
3934/* enqueues a notification webhook */
3935{
3936 const char *webhook = xs_dict_get(user->config, "notify_webhook");
3937
3938 if (xs_is_string(webhook) && xs_match(webhook, "https://*|http://*")) { /** **/
3939 xs *msg = xs_dup(noti);
3940
3941 /* add more data */
3942 msg = xs_dict_set(msg, "target", user->actor);
3943 msg = xs_dict_set(msg, "uid", user->uid);
3944 msg = xs_dict_set(msg, "basedir", srv_basedir);
3945 msg = xs_dict_set(msg, "baseurl", srv_baseurl);
3946
3947 xs *actor_obj = NULL;
3948
3949 if (valid_status(object_get(xs_dict_get(noti, "actor"), &actor_obj)) && actor_obj)
3950 msg = xs_dict_set(msg, "account", actor_obj);
3951
3952 /* if this post is a reply, also add the inReplyTo object */
3953 const char *in_reply_to = xs_dict_get_path(msg, "msg.object.inReplyTo");
3954
3955 if (xs_is_string(in_reply_to)) {
3956 xs *irt_obj = NULL;
3957
3958 if (valid_status(object_get(in_reply_to, &irt_obj)))
3959 msg = xs_dict_set(msg, "reply", irt_obj);
3960 }
3961
3962 xs *qmsg = _new_qmsg("notify_webhook", msg, retries);
3963 const char *ntid = xs_dict_get(qmsg, "ntid");
3964 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3965
3966 qmsg = _enqueue_put(fn, qmsg);
3967
3968 snac_debug(user, 1, xs_fmt("notify_webhook"));
3969 }
3970}
3971
3972
3973void enqueue_collect_replies(snac *user, const char *post)
3974/* enqueues a collect replies request */
3975{
3976 xs *qmsg = _new_qmsg("collect_replies", post, 0);
3977 const char *ntid = xs_dict_get(qmsg, "ntid");
3978 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3979
3980 qmsg = _enqueue_put(fn, qmsg);
3981
3982 snac_debug(user, 1, xs_fmt("enqueue_collect_replies %s", post));
3983}
3984
3985
3986void enqueue_collect_outbox(snac *user, const char *actor_id)
3987/* enqueues a collect outbox request */
3988{
3989 xs *qmsg = _new_qmsg("collect_outbox", actor_id, 0);
3990 const char *ntid = xs_dict_get(qmsg, "ntid");
3991 xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
3992
3993 qmsg = _enqueue_put(fn, qmsg);
3994
3995 snac_debug(user, 1, xs_fmt("enqueue_collect_outbox %s", actor_id));
3996}
3997
3998
3999void enqueue_fsck(void)
4000/* enqueues an fsck */
4001{
4002 xs *qmsg = _new_qmsg("fsck", "", 0);
4003 const char *ntid = xs_dict_get(qmsg, "ntid");
4004 xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
4005
4006 qmsg = _enqueue_put(fn, qmsg);
4007}
4008
4009
4010int was_question_voted(snac *user, const char *id)
4011/* returns true if the user voted in this poll */
4012{
4013 xs *children = object_children(id);
4014 int voted = 0;
4015 xs_list *p;
4016 const xs_str *md5;
4017
4018 p = children;
4019 while (xs_list_iter(&p, &md5)) {
4020 xs *obj = NULL;
4021
4022 if (valid_status(object_get_by_md5(md5, &obj))) {
4023 const char *atto = get_atto(obj);
4024 if (atto && strcmp(atto, user->actor) == 0 &&
4025 !xs_is_null(xs_dict_get(obj, "name"))) {
4026 voted = 1;
4027 break;
4028 }
4029 }
4030 }
4031
4032 return voted;
4033}
4034
4035
4036xs_list *user_queue(snac *snac)
4037/* returns a list with filenames that can be dequeued */
4038{
4039 xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
4040 xs_list *list = xs_list_new();
4041 time_t t = time(NULL);
4042 xs_list *p;
4043 const xs_val *v;
4044
4045 xs *fns = xs_glob(spec, 0, 0);
4046
4047 p = fns;
4048 while (xs_list_iter(&p, &v)) {
4049 /* get the retry time from the basename */
4050 char *bn = strrchr(v, '/');
4051 time_t t2 = atol(bn + 1);
4052
4053 if (t2 > t)
4054 snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
4055 else {
4056 list = xs_list_append(list, v);
4057 snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
4058 }
4059 }
4060
4061 return list;
4062}
4063
4064
4065xs_list *queue(void)
4066/* returns a list with filenames that can be dequeued */
4067{
4068 xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
4069 xs_list *list = xs_list_new();
4070 time_t t = time(NULL);
4071 xs_list *p;
4072 const xs_val *v;
4073
4074 xs *fns = xs_glob(spec, 0, 0);
4075
4076 p = fns;
4077 while (xs_list_iter(&p, &v)) {
4078 /* get the retry time from the basename */
4079 char *bn = strrchr(v, '/');
4080 time_t t2 = atol(bn + 1);
4081
4082 if (t2 > t)
4083 srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
4084 else {
4085 list = xs_list_append(list, v);
4086 srv_debug(2, xs_fmt("queue ready for %s", v));
4087 }
4088 }
4089
4090 return list;
4091}
4092
4093
4094xs_dict *queue_get(const char *fn)
4095/* gets a file from a queue */
4096{
4097 FILE *f;
4098 xs_dict *obj = NULL;
4099
4100 if ((f = fopen(fn, "r")) != NULL) {
4101 obj = xs_json_load(f);
4102 fclose(f);
4103 }
4104
4105 return obj;
4106}
4107
4108
4109xs_dict *dequeue(const char *fn)
4110/* dequeues a message */
4111{
4112 xs_dict *obj = queue_get(fn);
4113
4114 unlink(fn);
4115
4116 return obj;
4117}
4118
4119
4120/** the purge **/
4121
4122static int _purge_file(const char *fn, time_t mt)
4123/* purge fn if it's older than days */
4124{
4125 int ret = 0;
4126
4127 if (mtime(fn) < mt) {
4128 /* older than the minimum time: delete it */
4129 unlink(fn);
4130 srv_debug(2, xs_fmt("purged %s", fn));
4131 ret = 1;
4132 }
4133
4134 return ret;
4135}
4136
4137
4138static void _purge_dir(const char *dir, int days)
4139/* purges all files in a directory older than days */
4140{
4141 int cnt = 0;
4142
4143 if (days) {
4144 time_t mt = time(NULL) - days * 24 * 3600;
4145 xs *spec = xs_fmt("%s/" "*", dir);
4146 xs *list = xs_glob(spec, 0, 0);
4147 xs_list *p;
4148 const xs_str *v;
4149
4150 p = list;
4151 while (xs_list_iter(&p, &v))
4152 cnt += _purge_file(v, mt);
4153
4154 srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
4155 }
4156}
4157
4158
4159static void _purge_user_subdir(snac *snac, const char *subdir, int days)
4160/* purges all files in a user subdir older than days */
4161{
4162 xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
4163
4164 _purge_dir(u_subdir, days);
4165}
4166
4167
4168void purge_server(void)
4169/* purge global server data */
4170{
4171 xs *spec = xs_fmt("%s/object/??", srv_basedir);
4172 xs *dirs = xs_glob(spec, 0, 0);
4173 xs_list *p;
4174 const xs_str *v;
4175 int cnt = 0;
4176 int icnt = 0;
4177
4178 time_t mt = time(NULL) - 7 * 24 * 3600;
4179
4180 p = dirs;
4181 while (xs_list_iter(&p, &v)) {
4182 xs_list *p2;
4183 const xs_str *v2;
4184
4185 {
4186 xs *spec2 = xs_fmt("%s/" "*.json", v);
4187 xs *files = xs_glob(spec2, 0, 0);
4188
4189 p2 = files;
4190 while (xs_list_iter(&p2, &v2)) {
4191 int n_link;
4192
4193 /* old and with no hard links? */
4194 if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
4195 xs *s1 = xs_replace(v2, ".json", "");
4196 xs *l = xs_split(s1, "/");
4197 const char *md5 = xs_list_get(l, -1);
4198
4199 object_del_by_md5(md5);
4200 cnt++;
4201 }
4202 }
4203 }
4204
4205 {
4206 /* look for stray indexes */
4207 xs *speci = xs_fmt("%s/" "*_?.idx", v);
4208 xs *idxfs = xs_glob(speci, 0, 0);
4209
4210 p2 = idxfs;
4211 while (xs_list_iter(&p2, &v2)) {
4212 /* old enough to consider? */
4213 if (mtime(v2) < mt) {
4214 /* check if the indexed object is here */
4215 xs *o = xs_dup(v2);
4216 char *ext = strchr(o, '_');
4217
4218 if (ext) {
4219 *ext = '\0';
4220 o = xs_str_cat(o, ".json");
4221
4222 if (mtime(o) == 0.0) {
4223 /* delete */
4224 unlink(v2);
4225 srv_debug(1, xs_fmt("purged %s", v2));
4226 icnt++;
4227 }
4228 }
4229 }
4230 }
4231
4232 /* delete index backups */
4233 xs *specb = xs_fmt("%s/" "*.bak", v);
4234 xs *bakfs = xs_glob(specb, 0, 0);
4235
4236 p2 = bakfs;
4237 while (xs_list_iter(&p2, &v2)) {
4238 unlink(v2);
4239 srv_debug(1, xs_fmt("purged %s", v2));
4240 }
4241 }
4242 }
4243
4244 /* purge collected inboxes */
4245 xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
4246 _purge_dir(ib_dir, 7);
4247
4248 /* purge the instance timeline */
4249 xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
4250 int itl_gc = index_gc(itl_fn);
4251
4252 /* purge tag indexes */
4253 xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
4254 xs *tag_dirs = xs_glob(tag_spec, 0, 0);
4255 p = tag_dirs;
4256
4257 int tag_gc = 0;
4258 while (xs_list_iter(&p, &v)) {
4259 xs *spec2 = xs_fmt("%s/" "*.idx", v);
4260 xs *files = xs_glob(spec2, 0, 0);
4261 xs_list *p2;
4262 const xs_str *v2;
4263
4264 p2 = files;
4265 while (xs_list_iter(&p2, &v2)) {
4266 tag_gc += index_gc(v2);
4267 xs *bak = xs_fmt("%s.bak", v2);
4268 unlink(bak);
4269
4270 if (index_len(v2) == 0) {
4271 /* there are no longer any entry with this tag;
4272 purge it completely */
4273 unlink(v2);
4274 xs *dottag = xs_replace(v2, ".idx", ".tag");
4275 unlink(dottag);
4276 }
4277 }
4278 }
4279
4280 srv_debug(1, xs_fmt("purge: global "
4281 "(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
4282}
4283
4284
4285void delete_purged_posts(snac *user, int days)
4286/* enqueues Delete activities for local purged messages */
4287{
4288 if (days == 0)
4289 return;
4290
4291 time_t mt = time(NULL) - days * 24 * 3600;
4292 xs *spec = xs_fmt("%s/public/" "*.json", user->basedir);
4293 xs *list = xs_glob(spec, 0, 0);
4294 const char *v;
4295
4296 xs_list_foreach(list, v) {
4297 if (mtime(v) < mt) {
4298 /* to be purged; is it a Note by us? */
4299 FILE *f;
4300
4301 if ((f = fopen(v, "r")) != NULL) {
4302 xs *msg = xs_json_load(f);
4303 fclose(f);
4304
4305 if (xs_is_dict(msg)) {
4306 const char *id = xs_dict_get(msg, "id");
4307
4308 if (xs_is_string(id) && is_msg_mine(user, id)) {
4309 xs *d_msg = msg_delete(user, id);
4310
4311 enqueue_message(user, d_msg);
4312
4313 snac_log(user, xs_fmt("enqueued Delete for purged message %s", id));
4314 }
4315 }
4316 }
4317 }
4318 }
4319}
4320
4321
4322void purge_static(snac *user)
4323/* purges the static directory of a user */
4324{
4325 xs *spec = xs_fmt("%s/static/""*", user->basedir);
4326 xs *fns = xs_glob(spec, 1, 0);
4327 xs *files = xs_dict_new();
4328 const char *k, *v;
4329 int cnt = 0;
4330
4331 /* fill a dict with the static resource urls */
4332 xs_list_foreach(fns, v) {
4333 if (strcmp(v, "style.css") == 0) {
4334 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4335 continue;
4336 }
4337
4338 xs *s = xs_fmt("%s/s/%s", user->actor, v);
4339
4340 xs *fn = xs_fmt("%s/static/%s", user->basedir, v);
4341 struct stat st;
4342
4343 /* exclude non-files */
4344 if (stat(fn, &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG) {
4345 snac_debug(user, 2, xs_fmt("purge_static: excluding non-file '%s'", v));
4346 continue;
4347 }
4348
4349 files = xs_dict_set(files, s, v);
4350 cnt++;
4351 }
4352
4353 /* don't purge avatars nor headers */
4354 if (xs_is_string(v = xs_dict_get(user->config, "avatar")) && xs_dict_get(files, v)) {
4355 files = xs_dict_del(files, v);
4356 cnt--;
4357
4358 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4359 }
4360 if (xs_is_string(v = xs_dict_get(user->config, "header")) && xs_dict_get(files, v)) {
4361 files = xs_dict_del(files, v);
4362 cnt--;
4363
4364 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4365 }
4366 if (xs_is_string(v = xs_dict_get(srv_config, "favicon_url")) && xs_dict_get(files, v)) {
4367 files = xs_dict_del(files, v);
4368 cnt--;
4369
4370 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4371 }
4372
4373 /* exclude emojis served from the static directory */
4374 xs *emo = emojis();
4375 xs_dict_foreach(emo, k, v) {
4376 if (xs_dict_get(files, v)) {
4377 files = xs_dict_del(files, v);
4378 cnt--;
4379
4380 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
4381 }
4382 }
4383
4384 if (cnt <= 0)
4385 return;
4386
4387 xs *tl = timeline_simple_list(user, "public", 0, XS_ALL, NULL);
4388 const char *md5;
4389
4390 xs_list_foreach(tl, md5) {
4391 if (cnt <= 0)
4392 break;
4393
4394 xs *msg = NULL;
4395
4396 if (!valid_status(object_get_by_md5(md5, &msg)))
4397 continue;
4398
4399 if (!is_msg_mine(user, xs_dict_get_def(msg, "id", "")))
4400 continue;
4401
4402 const xs_list *atts = xs_dict_get(msg, "attachment");
4403 if (!xs_is_list(atts))
4404 continue;
4405
4406 const xs_dict *d;
4407
4408 xs_list_foreach(atts, d) {
4409 const char *url;
4410
4411 if (xs_is_dict(d) && xs_is_string(url = xs_dict_get(d, "url"))) {
4412 if (xs_dict_get(files, url)) {
4413 files = xs_dict_del(files, url);
4414 cnt--;
4415
4416 snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", url));
4417
4418 xs *alt_text = xs_fmt("%s.txt", url);
4419
4420 if (xs_dict_get(files, alt_text)) {
4421 files = xs_dict_del(files, alt_text);
4422 cnt--;
4423
4424 snac_debug(user, 2, xs_fmt("purge_static: excluding alt_text '%s'", alt_text));
4425 }
4426 }
4427 }
4428 }
4429 }
4430
4431 /* what is left in the files dict is not referenced anywhere */
4432 xs_dict_foreach(files, k, v) {
4433 xs *s = xs_fmt("%s/static/%s", user->basedir, v);
4434 snac_debug(user, 1, xs_fmt("purge_static: %s", s));
4435 unlink(s);
4436 }
4437}
4438
4439
4440void purge_user(snac *snac)
4441/* do the purge for this user */
4442{
4443 int priv_days, pub_days, user_days = 0;
4444 const char *v;
4445 int n;
4446
4447 priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
4448 pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
4449
4450 if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
4451 (v = xs_dict_get(snac->config, "purge_days")) != NULL)
4452 user_days = xs_number_get(v);
4453
4454 if (user_days) {
4455 /* override admin settings only if they are lesser */
4456 if (priv_days == 0 || user_days < priv_days)
4457 priv_days = user_days;
4458
4459 if (pub_days == 0 || user_days < pub_days)
4460 pub_days = user_days;
4461 }
4462
4463 if (xs_is_true(xs_dict_get(srv_config, "propagate_local_purge")))
4464 delete_purged_posts(snac, pub_days);
4465
4466 _purge_user_subdir(snac, "hidden", priv_days);
4467 _purge_user_subdir(snac, "private", priv_days);
4468 _purge_user_subdir(snac, "pending", priv_days);
4469
4470 _purge_user_subdir(snac, "public", pub_days);
4471 _purge_user_subdir(snac, "admire", pub_days);
4472
4473 const char *idxs[] = { "followers.idx", "private.idx", "public.idx", "pinned.idx",
4474 "bookmark.idx", "draft.idx", "sched.idx", "admire.idx", NULL };
4475
4476 for (n = 0; idxs[n]; n++) {
4477 xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
4478 int gc = index_gc(idx);
4479 srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
4480 }
4481
4482 /* purge lists */
4483 {
4484 xs *spec = xs_fmt("%s/list/" "*.idx", snac->basedir);
4485 xs *lol = xs_glob(spec, 0, 0);
4486 int c = 0;
4487 const char *v;
4488
4489 while (xs_list_next(lol, &v, &c)) {
4490 int gc = index_gc(v);
4491 srv_debug(1, xs_fmt("purge: %s %d", v, gc));
4492 }
4493 }
4494
4495 /* purge static directories, if so desired */
4496 if (xs_is_true(xs_dict_get(srv_config, "purge_static")))
4497 purge_static(snac);
4498
4499 /* unrelated to purging, but it's a janitorial process, so what the hell */
4500 verify_links(snac);
4501}
4502
4503
4504void purge_all(void)
4505/* purge all users */
4506{
4507 snac snac;
4508 xs *list = user_list();
4509 char *p;
4510 const char *uid;
4511
4512 p = list;
4513 while (xs_list_iter(&p, &uid)) {
4514 if (user_open(&snac, uid)) {
4515 purge_user(&snac);
4516 user_free(&snac);
4517 }
4518 }
4519
4520 purge_server();
4521
4522#ifndef NO_MASTODON_API
4523 mastoapi_purge();
4524#endif
4525}
4526
4527
4528/** archive **/
4529
4530void srv_archive(const char *direction, const char *url, xs_dict *req,
4531 const char *payload, int p_size,
4532 int status, xs_dict *headers,
4533 const char *body, int b_size)
4534/* archives a connection */
4535{
4536 /* obsessive archiving */
4537 xs *date = tid(0);
4538 xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
4539 FILE *f;
4540
4541 if (mkdirx(dir) != -1) {
4542 xs *meta_fn = xs_fmt("%s/_META", dir);
4543
4544 if ((f = fopen(meta_fn, "w")) != NULL) {
4545 xs *j1 = xs_json_dumps(req, 4);
4546 xs *j2 = xs_json_dumps(headers, 4);
4547
4548 fprintf(f, "dir: %s\n", direction);
4549
4550 if (url)
4551 fprintf(f, "url: %s\n", url);
4552
4553 fprintf(f, "req: %s\n", j1);
4554 fprintf(f, "p_size: %d\n", p_size);
4555 fprintf(f, "status: %d\n", status);
4556 fprintf(f, "response: %s\n", j2);
4557 fprintf(f, "b_size: %d\n", b_size);
4558 fclose(f);
4559 }
4560
4561 if (p_size && payload) {
4562 xs *payload_fn = NULL;
4563 xs *payload_fn_raw = NULL;
4564 const char *v = xs_dict_get(req, "content-type");
4565
4566 if (v && xs_str_in(v, "json") != -1) {
4567 payload_fn = xs_fmt("%s/payload.json", dir);
4568
4569 if ((f = fopen(payload_fn, "w")) != NULL) {
4570 xs *v1 = xs_json_loads(payload);
4571 xs *j1 = NULL;
4572
4573 if (v1 != NULL)
4574 j1 = xs_json_dumps(v1, 4);
4575
4576 if (j1 != NULL)
4577 fwrite(j1, strlen(j1), 1, f);
4578 else
4579 fwrite(payload, p_size, 1, f);
4580
4581 fclose(f);
4582 }
4583 }
4584
4585 payload_fn_raw = xs_fmt("%s/payload", dir);
4586
4587 if ((f = fopen(payload_fn_raw, "w")) != NULL) {
4588 fwrite(payload, p_size, 1, f);
4589 fclose(f);
4590 }
4591 }
4592
4593 if (b_size && body) {
4594 xs *body_fn = NULL;
4595 const char *v = xs_dict_get(headers, "content-type");
4596
4597 if (v && xs_str_in(v, "json") != -1) {
4598 body_fn = xs_fmt("%s/body.json", dir);
4599
4600 if ((f = fopen(body_fn, "w")) != NULL) {
4601 xs *v1 = xs_json_loads(body);
4602 xs *j1 = NULL;
4603
4604 if (v1 != NULL)
4605 j1 = xs_json_dumps(v1, 4);
4606
4607 if (j1 != NULL)
4608 fwrite(j1, strlen(j1), 1, f);
4609 else
4610 fwrite(body, b_size, 1, f);
4611
4612 fclose(f);
4613 }
4614 }
4615 else {
4616 body_fn = xs_fmt("%s/body", dir);
4617
4618 if ((f = fopen(body_fn, "w")) != NULL) {
4619 fwrite(body, b_size, 1, f);
4620 fclose(f);
4621 }
4622 }
4623 }
4624 }
4625}
4626
4627
4628void srv_archive_error(const char *prefix, const xs_str *err,
4629 const xs_dict *req, const xs_val *data)
4630/* archives an error */
4631{
4632 xs *ntid = tid(0);
4633 xs *fn = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
4634 FILE *f;
4635
4636 if ((f = fopen(fn, "w")) != NULL) {
4637 fprintf(f, "Error: %s\n", err);
4638
4639 if (req) {
4640 fprintf(f, "Request headers:\n");
4641
4642 xs_json_dump(req, 4, f);
4643
4644 fprintf(f, "\n");
4645 }
4646
4647 if (data) {
4648 fprintf(f, "Data:\n");
4649
4650 if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
4651 xs_json_dump(data, 4, f);
4652 }
4653 else
4654 fprintf(f, "%s", data);
4655
4656 fprintf(f, "\n");
4657 }
4658
4659 fclose(f);
4660 }
4661}
4662
4663
4664void srv_archive_qitem(const char *prefix, xs_dict *q_item)
4665/* archives a q_item in the error folder */
4666{
4667 xs *ntid = tid(0);
4668 xs *fn = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
4669 FILE *f;
4670
4671 if ((f = fopen(fn, "w")) != NULL) {
4672 xs_json_dump(q_item, 4, f);
4673 fclose(f);
4674 }
4675}
4676
4677
4678t_announcement *announcement(const double after)
4679/* returns announcement text or NULL if none exists or it is olde than "after" */
4680{
4681 static const long int MAX_SIZE = 2048;
4682 static t_announcement a = {
4683 .text = NULL,
4684 .timestamp = 0.0,
4685 };
4686 static xs_str *fn = NULL;
4687 if (fn == NULL)
4688 fn = xs_fmt("%s/announcement.txt", srv_basedir);
4689
4690 const double ts = mtime(fn);
4691
4692 /* file does not exist or other than what was requested */
4693 if (ts == 0.0 || ts <= after)
4694 return NULL;
4695
4696 /* nothing changed, just return the current announcement */
4697 if (a.text != NULL && ts <= a.timestamp)
4698 return &a;
4699
4700 /* read and store new announcement */
4701 FILE *f;
4702
4703 if ((f = fopen(fn, "r")) != NULL) {
4704 fseek (f, 0, SEEK_END);
4705 const long int length = ftell(f);
4706
4707 if (length > MAX_SIZE) {
4708 /* this is probably unintentional */
4709 srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
4710 }
4711 else
4712 if (length > 0) {
4713 fseek (f, 0, SEEK_SET);
4714 char *buffer = malloc(length + 1);
4715 if (buffer) {
4716 fread(buffer, 1, length, f);
4717 buffer[length] = '\0';
4718
4719 free(a.text);
4720 a.text = buffer;
4721 a.timestamp = ts;
4722 }
4723 else {
4724 srv_log("Error allocating memory for announcement");
4725 }
4726 }
4727 else {
4728 /* an empty file means no announcement */
4729 free(a.text);
4730 a.text = NULL;
4731 a.timestamp = 0.0;
4732 }
4733
4734 fclose (f);
4735 }
4736
4737 if (a.text != NULL)
4738 return &a;
4739
4740 return NULL;
4741}
4742
4743
4744xs_str *make_url(const char *href, const char *proxy, int by_token)
4745/* makes an URL, possibly including proxying */
4746{
4747 xs_str *url = NULL;
4748
4749 if (proxy && !xs_startswith(href, srv_baseurl)) {
4750 xs *p = NULL;
4751
4752 if (by_token) {
4753 xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, proxy);
4754 xs *tk = xs_md5_hex(tks, strlen(tks));
4755
4756 p = xs_fmt("%s/y/%s/", proxy, tk);
4757 }
4758 else
4759 p = xs_fmt("%s/x/", proxy);
4760
4761 url = xs_replace(href, "https:/" "/", p);
4762 }
4763 else
4764 url = xs_dup(href);
4765
4766 return url;
4767}
4768
4769
4770/** bad login throttle **/
4771
4772xs_str *_badlogin_fn(const char *addr)
4773{
4774 xs *md5 = xs_md5_hex(addr, strlen(addr));
4775 xs *dir = xs_fmt("%s/badlogin", srv_basedir);
4776
4777 mkdirx(dir);
4778
4779 return xs_fmt("%s/%s", dir, md5);
4780}
4781
4782
4783int _badlogin_read(const char *fn, int *failures)
4784/* reads a badlogin file */
4785{
4786 int ok = 0;
4787 FILE *f;
4788
4789 pthread_mutex_lock(&data_mutex);
4790
4791 if ((f = fopen(fn, "r")) != NULL) {
4792 xs *l = xs_readline(f);
4793 fclose(f);
4794
4795 if (sscanf(l, "%d", failures) == 1)
4796 ok = 1;
4797 }
4798
4799 pthread_mutex_unlock(&data_mutex);
4800
4801 return ok;
4802}
4803
4804
4805int badlogin_check(const char *user, const char *addr)
4806/* checks if this address is authorized to try a login */
4807{
4808 int valid = 1;
4809
4810 if (xs_type(addr) == XSTYPE_STRING) {
4811 xs *fn = _badlogin_fn(addr);
4812 double mt = mtime(fn);
4813
4814 if (mt > 0) {
4815 int badlogin_expire = xs_number_get(xs_dict_get_def(srv_config,
4816 "badlogin_expire", "300"));
4817
4818 mt += badlogin_expire;
4819
4820 /* if file is expired, delete and give pass */
4821 if (mt < time(NULL)) {
4822 srv_debug(1, xs_fmt("Login from %s for %s allowed again", addr, user));
4823 unlink(fn);
4824 }
4825 else {
4826 int failures;
4827
4828 if (_badlogin_read(fn, &failures)) {
4829 int badlogin_max = xs_number_get(xs_dict_get_def(srv_config,
4830 "badlogin_retries", "5"));
4831
4832 if (failures >= badlogin_max) {
4833 valid = 0;
4834
4835 xs *d = xs_str_iso_date((time_t) mt);
4836
4837 srv_debug(1,
4838 xs_fmt("Login from %s for %s forbidden until %s", addr, user, d));
4839 }
4840 }
4841 }
4842 }
4843 }
4844
4845 return valid;
4846}
4847
4848
4849void badlogin_inc(const char *user, const char *addr)
4850/* increments a bad login from this address */
4851{
4852 if (xs_type(addr) == XSTYPE_STRING) {
4853 int failures = 0;
4854 xs *fn = _badlogin_fn(addr);
4855 FILE *f;
4856
4857 _badlogin_read(fn, &failures);
4858
4859 pthread_mutex_lock(&data_mutex);
4860
4861 if ((f = fopen(fn, "w")) != NULL) {
4862 failures++;
4863
4864 fprintf(f, "%d %s %s\n", failures, addr, user);
4865 fclose(f);
4866
4867 srv_log(xs_fmt("Registered %d login failure(s) from %s for %s", failures, addr, user));
4868 }
4869
4870 pthread_mutex_unlock(&data_mutex);
4871 }
4872}
4873
4874
4875/** language strings **/
4876
4877const char *lang_str(const char *str, const snac *user)
4878/* returns a translated string */
4879{
4880 const char *n_str = str;
4881
4882 if (user && xs_is_dict(user->lang) && xs_is_string(str)) {
4883 n_str = xs_dict_get(user->lang, str);
4884
4885 if (xs_is_null(n_str) || *n_str == '\0')
4886 n_str = str;
4887 }
4888
4889 return n_str;
4890}
4891
4892
4893/** integrity checks **/
4894
4895void data_fsck(void)
4896{
4897 xs *list = user_list();
4898 const char *uid;
4899
4900 xs_list_foreach(list, uid) {
4901 snac user;
4902
4903 if (!user_open(&user, uid))
4904 continue;
4905
4906 {
4907 /* iterate all private posts and check that non-public posts
4908 from this user are also linked into the public directory,
4909 to avoid the don't-fucking-delete-my-own-private-posts purge bug */
4910
4911 xs *priv_spec = xs_fmt("%s/private/""*.json", user.basedir);
4912 xs *posts = xs_glob(priv_spec, 0, 0);
4913 const char *priv_fn;
4914
4915 xs_list_foreach(posts, priv_fn) {
4916 xs *pub_fn = xs_replace(priv_fn, "/private/", "/public/");
4917
4918 /* already there? look no more */
4919 if (mtime(pub_fn))
4920 continue;
4921
4922 /* read the post */
4923 FILE *f;
4924 if ((f = fopen(priv_fn, "r")) == NULL)
4925 continue;
4926
4927 xs *post = xs_json_load(f);
4928 fclose(f);
4929
4930 if (!xs_is_dict(post))
4931 continue;
4932
4933 const char *attr_to = get_atto(post);
4934
4935 if (!xs_is_string(attr_to) || strcmp(attr_to, user.actor) != 0) {
4936 /* not from this user */
4937 continue;
4938 }
4939
4940 /* link */
4941 snac_debug(&user, 1, xs_fmt("fsck: fixed missing link %s", xs_dict_get(post, "id")));
4942 link(priv_fn, pub_fn);
4943 }
4944 }
4945
4946 user_free(&user);
4947 }
4948}