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