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