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