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