data.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_openssl.h"
  7. #include "xs_glob.h"
  8. #include "xs_set.h"
  9. #include "snac.h"
  10. #include <time.h>
  11. #include <sys/stat.h>
  12. #include <sys/file.h>
  13. #include <fcntl.h>
  14. double disk_layout = 2.7;
  15. int snac_upgrade(d_char **error);
  16. int srv_open(char *basedir, int auto_upgrade)
  17. /* opens a server */
  18. {
  19. int ret = 0;
  20. xs *cfg_file = NULL;
  21. FILE *f;
  22. d_char *error = NULL;
  23. srv_basedir = xs_str_new(basedir);
  24. if (xs_endswith(srv_basedir, "/"))
  25. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  26. cfg_file = xs_fmt("%s/server.json", basedir);
  27. if ((f = fopen(cfg_file, "r")) == NULL)
  28. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  29. else {
  30. xs *cfg_data;
  31. /* read full config file */
  32. cfg_data = xs_readall(f);
  33. fclose(f);
  34. /* parse */
  35. srv_config = xs_json_loads(cfg_data);
  36. if (srv_config == NULL)
  37. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  38. else {
  39. char *host;
  40. char *prefix;
  41. char *dbglvl;
  42. host = xs_dict_get(srv_config, "host");
  43. prefix = xs_dict_get(srv_config, "prefix");
  44. dbglvl = xs_dict_get(srv_config, "dbglevel");
  45. if (host == NULL || prefix == NULL)
  46. error = xs_str_new("ERROR: cannot get server data");
  47. else {
  48. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  49. dbglevel = (int) xs_number_get(dbglvl);
  50. if ((dbglvl = getenv("DEBUG")) != NULL) {
  51. dbglevel = atoi(dbglvl);
  52. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  53. }
  54. if (auto_upgrade)
  55. ret = snac_upgrade(&error);
  56. else {
  57. if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
  58. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  59. else
  60. ret = 1;
  61. }
  62. }
  63. }
  64. }
  65. if (error != NULL)
  66. srv_log(error);
  67. /* create the queue/ subdir, just in case */
  68. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  69. mkdir(qdir, DIR_PERM);
  70. #ifdef __OpenBSD__
  71. char *v = xs_dict_get(srv_config, "disable_openbsd_security");
  72. if (v && xs_type(v) == XSTYPE_TRUE) {
  73. srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
  74. }
  75. else {
  76. srv_debug(1, xs_fmt("Calling unveil()"));
  77. unveil(basedir, "rwc");
  78. unveil("/usr/sbin/sendmail", "x");
  79. unveil("/etc/resolv.conf", "r");
  80. unveil("/etc/hosts", "r");
  81. unveil("/etc/ssl/openssl.cnf", "r");
  82. unveil("/etc/ssl/cert.pem", "r");
  83. unveil("/usr/share/zoneinfo", "r");
  84. unveil(NULL, NULL);
  85. srv_debug(1, xs_fmt("Calling pledge()"));
  86. pledge("stdio rpath wpath cpath flock inet proc exec dns", NULL);
  87. }
  88. #endif /* __OpenBSD__ */
  89. return ret;
  90. }
  91. void srv_free(void)
  92. {
  93. xs_free(srv_basedir);
  94. xs_free(srv_config);
  95. xs_free(srv_baseurl);
  96. }
  97. void user_free(snac *snac)
  98. /* frees a user snac */
  99. {
  100. xs_free(snac->uid);
  101. xs_free(snac->basedir);
  102. xs_free(snac->config);
  103. xs_free(snac->key);
  104. xs_free(snac->actor);
  105. xs_free(snac->md5);
  106. }
  107. int user_open(snac *snac, const char *uid)
  108. /* opens a user */
  109. {
  110. int ret = 0;
  111. memset(snac, '\0', sizeof(struct _snac));
  112. if (validate_uid(uid)) {
  113. xs *cfg_file;
  114. FILE *f;
  115. snac->uid = xs_str_new(uid);
  116. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  117. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  118. if ((f = fopen(cfg_file, "r")) != NULL) {
  119. xs *cfg_data;
  120. /* read full config file */
  121. cfg_data = xs_readall(f);
  122. fclose(f);
  123. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  124. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  125. if ((f = fopen(key_file, "r")) != NULL) {
  126. xs *key_data;
  127. key_data = xs_readall(f);
  128. fclose(f);
  129. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  130. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  131. snac->md5 = xs_md5_hex(snac->actor, strlen(snac->actor));
  132. ret = 1;
  133. }
  134. else
  135. srv_log(xs_fmt("cannot parse '%s'", key_file));
  136. }
  137. else
  138. srv_log(xs_fmt("error opening '%s'", key_file));
  139. }
  140. else
  141. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  142. }
  143. else
  144. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  145. }
  146. else
  147. srv_debug(1, xs_fmt("invalid user '%s'", uid));
  148. if (!ret)
  149. user_free(snac);
  150. return ret;
  151. }
  152. d_char *user_list(void)
  153. /* returns the list of user ids */
  154. {
  155. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  156. return xs_glob(spec, 1, 0);
  157. }
  158. double mtime_nl(const char *fn, int *n_link)
  159. /* returns the mtime and number of links of a file or directory, or 0.0 */
  160. {
  161. struct stat st;
  162. double r = 0.0;
  163. int n = 0;
  164. if (fn && stat(fn, &st) != -1) {
  165. r = (double) st.st_mtim.tv_sec;
  166. n = st.st_nlink;
  167. }
  168. if (n_link)
  169. *n_link = n;
  170. return r;
  171. }
  172. /** database 2.1+ **/
  173. /** indexes **/
  174. int index_add_md5(const char *fn, const char *md5)
  175. /* adds an md5 to an index */
  176. {
  177. int status = 201; /* Created */
  178. FILE *f;
  179. if ((f = fopen(fn, "a")) != NULL) {
  180. flock(fileno(f), LOCK_EX);
  181. /* ensure the position is at the end after getting the lock */
  182. fseek(f, 0, SEEK_END);
  183. fprintf(f, "%s\n", md5);
  184. fclose(f);
  185. }
  186. else
  187. status = 500;
  188. return status;
  189. }
  190. int index_add(const char *fn, const char *id)
  191. /* adds an id to an index */
  192. {
  193. xs *md5 = xs_md5_hex(id, strlen(id));
  194. return index_add_md5(fn, md5);
  195. }
  196. int index_del_md5(const char *fn, const char *md5)
  197. /* deletes an md5 from an index */
  198. {
  199. int status = 404;
  200. FILE *i, *o;
  201. if ((i = fopen(fn, "r")) != NULL) {
  202. flock(fileno(i), LOCK_EX);
  203. xs *nfn = xs_fmt("%s.new", fn);
  204. char line[256];
  205. if ((o = fopen(nfn, "w")) != NULL) {
  206. while (fgets(line, sizeof(line), i) != NULL) {
  207. line[32] = '\0';
  208. if (memcmp(line, md5, 32) != 0)
  209. fprintf(o, "%s\n", line);
  210. }
  211. fclose(o);
  212. xs *ofn = xs_fmt("%s.bak", fn);
  213. link(fn, ofn);
  214. rename(nfn, fn);
  215. }
  216. else
  217. status = 500;
  218. fclose(i);
  219. }
  220. else
  221. status = 500;
  222. return status;
  223. }
  224. int index_del(const char *fn, const char *id)
  225. /* deletes an id from an index */
  226. {
  227. xs *md5 = xs_md5_hex(id, strlen(id));
  228. return index_del_md5(fn, md5);
  229. }
  230. int index_in_md5(const char *fn, const char *md5)
  231. /* checks if the md5 is already in the index */
  232. {
  233. FILE *f;
  234. int ret = 0;
  235. if ((f = fopen(fn, "r")) != NULL) {
  236. flock(fileno(f), LOCK_SH);
  237. char line[256];
  238. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  239. line[32] = '\0';
  240. if (strcmp(line, md5) == 0)
  241. ret = 1;
  242. }
  243. fclose(f);
  244. }
  245. return ret;
  246. }
  247. int index_in(const char *fn, const char *id)
  248. /* checks if the object id is already in the index */
  249. {
  250. xs *md5 = xs_md5_hex(id, strlen(id));
  251. return index_in_md5(fn, md5);
  252. }
  253. int index_first(const char *fn, char *line, int size)
  254. /* reads the first entry of an index */
  255. {
  256. FILE *f;
  257. int ret = 0;
  258. if ((f = fopen(fn, "r")) != NULL) {
  259. flock(fileno(f), LOCK_SH);
  260. if (fgets(line, size, f) != NULL) {
  261. line[32] = '\0';
  262. ret = 1;
  263. }
  264. fclose(f);
  265. }
  266. return ret;
  267. }
  268. int index_len(const char *fn)
  269. /* returns the number of elements in an index */
  270. {
  271. struct stat st;
  272. int len = 0;
  273. if (stat(fn, &st) != -1)
  274. len = st.st_size / 33;
  275. return len;
  276. }
  277. d_char *index_list(const char *fn, int max)
  278. /* returns an index as a list */
  279. {
  280. d_char *list = NULL;
  281. FILE *f;
  282. int n = 0;
  283. if ((f = fopen(fn, "r")) != NULL) {
  284. flock(fileno(f), LOCK_SH);
  285. char line[256];
  286. list = xs_list_new();
  287. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  288. line[32] = '\0';
  289. list = xs_list_append(list, line);
  290. n++;
  291. }
  292. fclose(f);
  293. }
  294. return list;
  295. }
  296. d_char *index_list_desc(const char *fn, int skip, int show)
  297. /* returns an index as a list, in reverse order */
  298. {
  299. d_char *list = NULL;
  300. FILE *f;
  301. int n = 0;
  302. if ((f = fopen(fn, "r")) != NULL) {
  303. flock(fileno(f), LOCK_SH);
  304. char line[256];
  305. list = xs_list_new();
  306. /* move to the end minus one entry (or more, if skipping entries) */
  307. if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
  308. while (n < show && fgets(line, sizeof(line), f) != NULL) {
  309. line[32] = '\0';
  310. list = xs_list_append(list, line);
  311. n++;
  312. /* move backwards 2 entries */
  313. if (fseek(f, -66, SEEK_CUR) == -1)
  314. break;
  315. }
  316. }
  317. fclose(f);
  318. }
  319. return list;
  320. }
  321. /** objects **/
  322. d_char *_object_fn_by_md5(const char *md5)
  323. {
  324. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  325. mkdir(bfn, DIR_PERM);
  326. return xs_fmt("%s/%s.json", bfn, md5);
  327. }
  328. d_char *_object_fn(const char *id)
  329. {
  330. xs *md5 = xs_md5_hex(id, strlen(id));
  331. return _object_fn_by_md5(md5);
  332. }
  333. int object_here_by_md5(char *id)
  334. /* checks if an object is already downloaded */
  335. {
  336. xs *fn = _object_fn_by_md5(id);
  337. return mtime(fn) > 0.0;
  338. }
  339. int object_here(char *id)
  340. /* checks if an object is already downloaded */
  341. {
  342. xs *fn = _object_fn(id);
  343. return mtime(fn) > 0.0;
  344. }
  345. int object_get_by_md5(const char *md5, xs_dict **obj)
  346. /* returns a stored object, optionally of the requested type */
  347. {
  348. int status = 404;
  349. xs *fn = _object_fn_by_md5(md5);
  350. FILE *f;
  351. if ((f = fopen(fn, "r")) != NULL) {
  352. flock(fileno(f), LOCK_SH);
  353. xs *j = xs_readall(f);
  354. fclose(f);
  355. *obj = xs_json_loads(j);
  356. if (*obj)
  357. status = 200;
  358. }
  359. else
  360. *obj = NULL;
  361. return status;
  362. }
  363. int object_get(const char *id, xs_dict **obj)
  364. /* returns a stored object, optionally of the requested type */
  365. {
  366. xs *md5 = xs_md5_hex(id, strlen(id));
  367. return object_get_by_md5(md5, obj);
  368. }
  369. int _object_add(const char *id, d_char *obj, int ow)
  370. /* stores an object */
  371. {
  372. int status = 201; /* Created */
  373. xs *fn = _object_fn(id);
  374. FILE *f;
  375. if (!ow && mtime(fn) > 0.0) {
  376. /* object already here */
  377. srv_debug(1, xs_fmt("object_add object already here %s", id));
  378. return 204; /* No content */
  379. }
  380. if ((f = fopen(fn, "w")) != NULL) {
  381. flock(fileno(f), LOCK_EX);
  382. xs *j = xs_json_dumps_pp(obj, 4);
  383. fwrite(j, strlen(j), 1, f);
  384. fclose(f);
  385. /* does this object has a parent? */
  386. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  387. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  388. /* update the children index of the parent */
  389. xs *c_idx = _object_fn(in_reply_to);
  390. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  391. if (!index_in(c_idx, id)) {
  392. index_add(c_idx, id);
  393. srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
  394. }
  395. else
  396. srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
  397. /* create a one-element index with the parent */
  398. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  399. if (mtime(p_idx) == 0.0) {
  400. index_add(p_idx, in_reply_to);
  401. srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  402. }
  403. }
  404. }
  405. else {
  406. srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
  407. status = 500;
  408. }
  409. srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
  410. return status;
  411. }
  412. int object_add(const char *id, d_char *obj)
  413. /* stores an object */
  414. {
  415. return _object_add(id, obj, 0);
  416. }
  417. int object_add_ow(const char *id, d_char *obj)
  418. /* stores an object (overwriting allowed) */
  419. {
  420. return _object_add(id, obj, 1);
  421. }
  422. int object_del_by_md5(const char *md5)
  423. /* deletes an object by its md5 */
  424. {
  425. int status = 404;
  426. xs *fn = _object_fn_by_md5(md5);
  427. if (fn != NULL && unlink(fn) != -1) {
  428. status = 200;
  429. /* also delete associated indexes */
  430. xs *spec = xs_dup(fn);
  431. spec = xs_replace_i(spec, ".json", "*.idx");
  432. xs *files = xs_glob(spec, 0, 0);
  433. char *p, *v;
  434. p = files;
  435. while (xs_list_iter(&p, &v)) {
  436. srv_debug(1, xs_fmt("object_del index %s", v));
  437. unlink(v);
  438. }
  439. }
  440. srv_debug(1, xs_fmt("object_del %s %d", fn, status));
  441. return status;
  442. }
  443. int object_del(const char *id)
  444. /* deletes an object */
  445. {
  446. xs *md5 = xs_md5_hex(id, strlen(id));
  447. return object_del_by_md5(md5);
  448. }
  449. int object_del_if_unref(const char *id)
  450. /* deletes an object if its n_links < 2 */
  451. {
  452. xs *fn = _object_fn(id);
  453. int n_links;
  454. int ret = 0;
  455. if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
  456. ret = object_del(id);
  457. return ret;
  458. }
  459. d_char *_object_index_fn(const char *id, const char *idxsfx)
  460. /* returns the filename of an object's index */
  461. {
  462. d_char *fn = _object_fn(id);
  463. return xs_replace_i(fn, ".json", idxsfx);
  464. }
  465. int object_likes_len(const char *id)
  466. /* returns the number of likes (without reading the index) */
  467. {
  468. xs *fn = _object_index_fn(id, "_l.idx");
  469. return index_len(fn);
  470. }
  471. int object_announces_len(const char *id)
  472. /* returns the number of announces (without reading the index) */
  473. {
  474. xs *fn = _object_index_fn(id, "_a.idx");
  475. return index_len(fn);
  476. }
  477. d_char *object_children(const char *id)
  478. /* returns the list of an object's children */
  479. {
  480. xs *fn = _object_index_fn(id, "_c.idx");
  481. return index_list(fn, XS_ALL);
  482. }
  483. d_char *object_likes(const char *id)
  484. {
  485. xs *fn = _object_index_fn(id, "_l.idx");
  486. return index_list(fn, XS_ALL);
  487. }
  488. d_char *object_announces(const char *id)
  489. {
  490. xs *fn = _object_index_fn(id, "_a.idx");
  491. return index_list(fn, XS_ALL);
  492. }
  493. int object_parent(const char *id, char *buf, int size)
  494. /* returns the object parent, if any */
  495. {
  496. xs *fn = _object_fn_by_md5(id);
  497. fn = xs_replace_i(fn, ".json", "_p.idx");
  498. return index_first(fn, buf, size);
  499. }
  500. int object_admire(const char *id, const char *actor, int like)
  501. /* actor likes or announces this object */
  502. {
  503. int status = 200;
  504. xs *fn = _object_fn(id);
  505. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  506. if (!index_in(fn, actor)) {
  507. status = index_add(fn, actor);
  508. srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  509. }
  510. return status;
  511. }
  512. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  513. /* adds or deletes from a user cache */
  514. {
  515. xs *ofn = _object_fn(id);
  516. xs *l = xs_split(ofn, "/");
  517. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  518. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  519. int ret;
  520. if (del) {
  521. if ((ret = unlink(cfn)) != -1)
  522. index_del(idx, id);
  523. }
  524. else {
  525. if ((ret = link(ofn, cfn)) != -1)
  526. index_add(idx, id);
  527. }
  528. return ret;
  529. }
  530. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  531. /* caches an object into a user cache */
  532. {
  533. return _object_user_cache(snac, id, cachedir, 0);
  534. }
  535. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  536. /* deletes an object from a user cache */
  537. {
  538. return _object_user_cache(snac, id, cachedir, 1);
  539. }
  540. int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
  541. /* checks if an object is stored in a cache */
  542. {
  543. xs *md5 = xs_md5_hex(id, strlen(id));
  544. xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
  545. return !!(mtime(cfn) != 0.0);
  546. }
  547. d_char *object_user_cache_list(snac *snac, const char *cachedir, int max)
  548. /* returns the objects in a cache as a list */
  549. {
  550. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  551. return index_list(idx, max);
  552. }
  553. /** specialized functions **/
  554. /** followers **/
  555. int follower_add(snac *snac, const char *actor)
  556. /* adds a follower */
  557. {
  558. int ret = object_user_cache_add(snac, actor, "followers");
  559. snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
  560. return ret == -1 ? 500 : 200;
  561. }
  562. int follower_del(snac *snac, const char *actor)
  563. /* deletes a follower */
  564. {
  565. int ret = object_user_cache_del(snac, actor, "followers");
  566. snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
  567. return ret == -1 ? 404 : 200;
  568. }
  569. int follower_check(snac *snac, const char *actor)
  570. /* checks if someone is a follower */
  571. {
  572. return object_user_cache_in(snac, actor, "followers");
  573. }
  574. d_char *follower_list(snac *snac)
  575. /* returns the list of followers */
  576. {
  577. xs *list = object_user_cache_list(snac, "followers", XS_ALL);
  578. d_char *fwers = xs_list_new();
  579. char *p, *v;
  580. /* resolve the list of md5 to be a list of actors */
  581. p = list;
  582. while (xs_list_iter(&p, &v)) {
  583. xs *a_obj = NULL;
  584. if (valid_status(object_get_by_md5(v, &a_obj))) {
  585. char *actor = xs_dict_get(a_obj, "id");
  586. if (!xs_is_null(actor))
  587. fwers = xs_list_append(fwers, actor);
  588. }
  589. }
  590. return fwers;
  591. }
  592. /** timeline **/
  593. double timeline_mtime(snac *snac)
  594. {
  595. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  596. return mtime(fn);
  597. }
  598. int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
  599. /* gets a message from the timeline */
  600. {
  601. int status = 404;
  602. FILE *f = NULL;
  603. /* try to open from the private cache first */
  604. xs *prfn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
  605. if ((f = fopen(prfn, "r")) == NULL) {
  606. /* try now the public one */
  607. xs *pufn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
  608. f = fopen(pufn, "r");
  609. }
  610. if (f != NULL) {
  611. flock(fileno(f), LOCK_SH);
  612. xs *j = xs_readall(f);
  613. fclose(f);
  614. if ((*msg = xs_json_loads(j)) != NULL)
  615. status = 200;
  616. }
  617. return status;
  618. }
  619. int timeline_del(snac *snac, char *id)
  620. /* deletes a message from the timeline */
  621. {
  622. /* delete from the user's caches */
  623. object_user_cache_del(snac, id, "public");
  624. object_user_cache_del(snac, id, "private");
  625. /* try to delete the object if it's not used elsewhere */
  626. return object_del_if_unref(id);
  627. }
  628. void timeline_update_indexes(snac *snac, const char *id)
  629. /* updates the indexes */
  630. {
  631. object_user_cache_add(snac, id, "private");
  632. if (xs_startswith(id, snac->actor)) {
  633. xs *msg = NULL;
  634. if (valid_status(object_get(id, &msg))) {
  635. /* if its ours and is public, also store in public */
  636. if (is_msg_public(snac, msg))
  637. object_user_cache_add(snac, id, "public");
  638. }
  639. }
  640. }
  641. int timeline_add(snac *snac, char *id, char *o_msg)
  642. /* adds a message to the timeline */
  643. {
  644. int ret = object_add(id, o_msg);
  645. timeline_update_indexes(snac, id);
  646. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  647. return ret;
  648. }
  649. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  650. /* updates a timeline entry with a new admiration */
  651. {
  652. /* if we are admiring this, add to both timelines */
  653. if (!like && strcmp(admirer, snac->actor) == 0) {
  654. object_user_cache_add(snac, id, "public");
  655. object_user_cache_add(snac, id, "private");
  656. }
  657. object_admire(id, admirer, like);
  658. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  659. like ? "Like" : "Announce", id, admirer));
  660. }
  661. d_char *timeline_top_level(d_char *list)
  662. /* returns the top level md5 entries from this index */
  663. {
  664. xs_set seen;
  665. char *p, *v;
  666. xs_set_init(&seen);
  667. p = list;
  668. while (xs_list_iter(&p, &v)) {
  669. char line[256] = "";
  670. strcpy(line, v);
  671. for (;;) {
  672. char line2[256];
  673. /* if it doesn't have a parent, use this */
  674. if (!object_parent(line, line2, sizeof(line2)))
  675. break;
  676. /* well, there is a parent... but if it's not there, use this */
  677. if (!object_here_by_md5(line2))
  678. break;
  679. /* it's here! try again with its own parent */
  680. strcpy(line, line2);
  681. }
  682. xs_set_add(&seen, line);
  683. }
  684. return xs_set_result(&seen);
  685. }
  686. d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show)
  687. /* returns a timeline (with all entries) */
  688. {
  689. int c_max;
  690. /* maximum number of items in the timeline */
  691. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  692. /* never more timeline entries than the configured maximum */
  693. if (show > c_max)
  694. show = c_max;
  695. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
  696. return index_list_desc(idx, skip, show);
  697. }
  698. d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show)
  699. /* returns a timeline (only top level entries) */
  700. {
  701. xs *list = timeline_simple_list(snac, idx_name, skip, show);
  702. return timeline_top_level(list);
  703. }
  704. /** following **/
  705. /* this needs special treatment and cannot use the object db as is,
  706. with a link to a cached author, because we need the Follow object
  707. in case we need to unfollow (Undo + original Follow) */
  708. d_char *_following_fn(snac *snac, char *actor)
  709. {
  710. xs *md5 = xs_md5_hex(actor, strlen(actor));
  711. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  712. }
  713. int following_add(snac *snac, char *actor, char *msg)
  714. /* adds to the following list */
  715. {
  716. int ret = 201; /* created */
  717. xs *fn = _following_fn(snac, actor);
  718. FILE *f;
  719. if ((f = fopen(fn, "w")) != NULL) {
  720. xs *j = xs_json_dumps_pp(msg, 4);
  721. fwrite(j, 1, strlen(j), f);
  722. fclose(f);
  723. }
  724. else
  725. ret = 500;
  726. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  727. return ret;
  728. }
  729. int following_del(snac *snac, char *actor)
  730. /* we're not following this actor any longer */
  731. {
  732. xs *fn = _following_fn(snac, actor);
  733. unlink(fn);
  734. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  735. return 200;
  736. }
  737. int following_check(snac *snac, char *actor)
  738. /* checks if we are following this actor */
  739. {
  740. xs *fn = _following_fn(snac, actor);
  741. return !!(mtime(fn) != 0.0);
  742. }
  743. int following_get(snac *snac, char *actor, d_char **data)
  744. /* returns the 'Follow' object */
  745. {
  746. xs *fn = _following_fn(snac, actor);
  747. FILE *f;
  748. int status = 200;
  749. if ((f = fopen(fn, "r")) != NULL) {
  750. xs *j = xs_readall(f);
  751. fclose(f);
  752. *data = xs_json_loads(j);
  753. }
  754. else
  755. status = 404;
  756. return status;
  757. }
  758. d_char *following_list(snac *snac)
  759. /* returns the list of people being followed */
  760. {
  761. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  762. xs *glist = xs_glob(spec, 0, 0);
  763. char *p, *v;
  764. d_char *list = xs_list_new();
  765. /* iterate the list of files */
  766. p = glist;
  767. while (xs_list_iter(&p, &v)) {
  768. FILE *f;
  769. /* load the follower data */
  770. if ((f = fopen(v, "r")) != NULL) {
  771. xs *j = xs_readall(f);
  772. fclose(f);
  773. if (j != NULL) {
  774. xs *o = xs_json_loads(j);
  775. if (o != NULL) {
  776. char *type = xs_dict_get(o, "type");
  777. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  778. char *actor = xs_dict_get(o, "actor");
  779. if (!xs_is_null(actor))
  780. list = xs_list_append(list, actor);
  781. }
  782. }
  783. }
  784. }
  785. }
  786. return list;
  787. }
  788. d_char *_muted_fn(snac *snac, char *actor)
  789. {
  790. xs *md5 = xs_md5_hex(actor, strlen(actor));
  791. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  792. }
  793. void mute(snac *snac, char *actor)
  794. /* mutes a moron */
  795. {
  796. xs *fn = _muted_fn(snac, actor);
  797. FILE *f;
  798. if ((f = fopen(fn, "w")) != NULL) {
  799. fprintf(f, "%s\n", actor);
  800. fclose(f);
  801. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  802. }
  803. }
  804. void unmute(snac *snac, char *actor)
  805. /* actor is no longer a moron */
  806. {
  807. xs *fn = _muted_fn(snac, actor);
  808. unlink(fn);
  809. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  810. }
  811. int is_muted(snac *snac, char *actor)
  812. /* check if someone is muted */
  813. {
  814. xs *fn = _muted_fn(snac, actor);
  815. return !!(mtime(fn) != 0.0);
  816. }
  817. d_char *_hidden_fn(snac *snac, const char *id)
  818. {
  819. xs *md5 = xs_md5_hex(id, strlen(id));
  820. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  821. }
  822. void hide(snac *snac, const char *id)
  823. /* hides a message tree */
  824. {
  825. xs *fn = _hidden_fn(snac, id);
  826. FILE *f;
  827. if ((f = fopen(fn, "w")) != NULL) {
  828. fprintf(f, "%s\n", id);
  829. fclose(f);
  830. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  831. /* hide all the children */
  832. xs *chld = object_children(id);
  833. char *p, *v;
  834. p = chld;
  835. while (xs_list_iter(&p, &v)) {
  836. xs *co = NULL;
  837. /* resolve to get the id */
  838. if (valid_status(object_get_by_md5(v, &co))) {
  839. if ((v = xs_dict_get(co, "id")) != NULL)
  840. hide(snac, v);
  841. }
  842. }
  843. }
  844. }
  845. int is_hidden(snac *snac, const char *id)
  846. /* check is id is hidden */
  847. {
  848. xs *fn = _hidden_fn(snac, id);
  849. return !!(mtime(fn) != 0.0);
  850. }
  851. int actor_add(snac *snac, const char *actor, d_char *msg)
  852. /* adds an actor */
  853. {
  854. return object_add_ow(actor, msg);
  855. }
  856. int actor_get(snac *snac, const char *actor, d_char **data)
  857. /* returns an already downloaded actor */
  858. {
  859. int status = 200;
  860. d_char *d;
  861. if (strcmp(actor, snac->actor) == 0) {
  862. /* this actor */
  863. if (data)
  864. *data = msg_actor(snac);
  865. return status;
  866. }
  867. /* read the object */
  868. if (!valid_status(status = object_get(actor, &d)))
  869. return status;
  870. if (data)
  871. *data = d;
  872. else
  873. d = xs_free(d);
  874. xs *fn = _object_fn(actor);
  875. double max_time;
  876. /* maximum time for the actor data to be considered stale */
  877. max_time = 3600.0 * 36.0;
  878. if (mtime(fn) + max_time < (double) time(NULL)) {
  879. /* actor data exists but also stinks */
  880. FILE *f;
  881. if ((f = fopen(fn, "a")) != NULL) {
  882. /* write a blank at the end to 'touch' the file */
  883. fwrite(" ", 1, 1, f);
  884. fclose(f);
  885. }
  886. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  887. }
  888. return status;
  889. }
  890. d_char *_static_fn(snac *snac, const char *id)
  891. /* gets the filename for a static file */
  892. {
  893. return xs_fmt("%s/static/%s", snac->basedir, id);
  894. }
  895. int static_get(snac *snac, const char *id, d_char **data, int *size)
  896. /* returns static content */
  897. {
  898. xs *fn = _static_fn(snac, id);
  899. FILE *f;
  900. int status = 404;
  901. *size = XS_ALL;
  902. if ((f = fopen(fn, "rb")) != NULL) {
  903. *data = xs_read(f, size);
  904. fclose(f);
  905. status = 200;
  906. }
  907. return status;
  908. }
  909. void static_put(snac *snac, const char *id, const char *data, int size)
  910. /* writes status content */
  911. {
  912. xs *fn = _static_fn(snac, id);
  913. FILE *f;
  914. if ((f = fopen(fn, "wb")) != NULL) {
  915. fwrite(data, size, 1, f);
  916. fclose(f);
  917. }
  918. }
  919. d_char *_history_fn(snac *snac, char *id)
  920. /* gets the filename for the history */
  921. {
  922. return xs_fmt("%s/history/%s", snac->basedir, id);
  923. }
  924. double history_mtime(snac *snac, char * id)
  925. {
  926. double t = 0.0;
  927. xs *fn = _history_fn(snac, id);
  928. if (fn != NULL)
  929. t = mtime(fn);
  930. return t;
  931. }
  932. void history_add(snac *snac, char *id, char *content, int size)
  933. /* adds something to the history */
  934. {
  935. xs *fn = _history_fn(snac, id);
  936. FILE *f;
  937. if ((f = fopen(fn, "w")) != NULL) {
  938. fwrite(content, size, 1, f);
  939. fclose(f);
  940. }
  941. }
  942. d_char *history_get(snac *snac, char *id)
  943. {
  944. d_char *content = NULL;
  945. xs *fn = _history_fn(snac, id);
  946. FILE *f;
  947. if ((f = fopen(fn, "r")) != NULL) {
  948. content = xs_readall(f);
  949. fclose(f);
  950. }
  951. return content;
  952. }
  953. int history_del(snac *snac, char *id)
  954. {
  955. xs *fn = _history_fn(snac, id);
  956. return unlink(fn);
  957. }
  958. d_char *history_list(snac *snac)
  959. {
  960. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  961. return xs_glob(spec, 1, 0);
  962. }
  963. /** the queue **/
  964. static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
  965. /* writes safely to the queue */
  966. {
  967. xs *tfn = xs_fmt("%s.tmp", fn);
  968. FILE *f;
  969. if ((f = fopen(tfn, "w")) != NULL) {
  970. xs *j = xs_json_dumps_pp(msg, 4);
  971. fwrite(j, strlen(j), 1, f);
  972. fclose(f);
  973. rename(tfn, fn);
  974. }
  975. return msg;
  976. }
  977. static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
  978. /* creates a queue message */
  979. {
  980. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  981. xs *ntid = tid(retries * 60 * qrt);
  982. xs *rn = xs_number_new(retries);
  983. xs_dict *qmsg = xs_dict_new();
  984. qmsg = xs_dict_append(qmsg, "type", type);
  985. qmsg = xs_dict_append(qmsg, "message", msg);
  986. qmsg = xs_dict_append(qmsg, "retries", rn);
  987. qmsg = xs_dict_append(qmsg, "ntid", ntid);
  988. return qmsg;
  989. }
  990. void enqueue_input(snac *snac, xs_dict *msg, xs_dict *req, int retries)
  991. /* enqueues an input message */
  992. {
  993. xs *qmsg = _new_qmsg("input", msg, retries);
  994. char *ntid = xs_dict_get(qmsg, "ntid");
  995. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  996. qmsg = xs_dict_append(qmsg, "req", req);
  997. qmsg = _enqueue_put(fn, qmsg);
  998. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  999. }
  1000. void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries)
  1001. /* enqueues an output message to an inbox */
  1002. {
  1003. if (xs_startswith(inbox, snac->actor)) {
  1004. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  1005. return;
  1006. }
  1007. xs *qmsg = _new_qmsg("output", msg, retries);
  1008. char *ntid = xs_dict_get(qmsg, "ntid");
  1009. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1010. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1011. qmsg = xs_dict_append(qmsg, "keyid", snac->actor);
  1012. qmsg = xs_dict_append(qmsg, "seckey", xs_dict_get(snac->key, "secret"));
  1013. qmsg = _enqueue_put(fn, qmsg);
  1014. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1015. }
  1016. void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries)
  1017. /* enqueues an output message for an actor */
  1018. {
  1019. xs *inbox = get_actor_inbox(snac, actor);
  1020. if (!xs_is_null(inbox))
  1021. enqueue_output(snac, msg, inbox, retries);
  1022. else
  1023. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1024. }
  1025. void enqueue_email(xs_str *msg, int retries)
  1026. /* enqueues an email message to be sent */
  1027. {
  1028. xs *qmsg = _new_qmsg("email", msg, retries);
  1029. char *ntid = xs_dict_get(qmsg, "ntid");
  1030. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1031. qmsg = _enqueue_put(fn, qmsg);
  1032. srv_debug(1, xs_fmt("enqueue_email %d", retries));
  1033. }
  1034. void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
  1035. /* enqueues a message to be sent via Telegram */
  1036. {
  1037. xs *qmsg = _new_qmsg("telegram", msg, 0);
  1038. char *ntid = xs_dict_get(qmsg, "ntid");
  1039. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1040. qmsg = xs_dict_append(qmsg, "bot", bot);
  1041. qmsg = xs_dict_append(qmsg, "chat_id", chat_id);
  1042. qmsg = _enqueue_put(fn, qmsg);
  1043. srv_debug(1, xs_fmt("enqueue_email %s %s", bot, chat_id));
  1044. }
  1045. void enqueue_message(snac *snac, xs_dict *msg)
  1046. /* enqueues an output message */
  1047. {
  1048. xs *qmsg = _new_qmsg("message", msg, 0);
  1049. char *ntid = xs_dict_get(qmsg, "ntid");
  1050. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1051. qmsg = _enqueue_put(fn, qmsg);
  1052. snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
  1053. }
  1054. xs_list *user_queue(snac *snac)
  1055. /* returns a list with filenames that can be dequeued */
  1056. {
  1057. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1058. xs_list *list = xs_list_new();
  1059. time_t t = time(NULL);
  1060. xs_list *p;
  1061. xs_val *v;
  1062. xs *fns = xs_glob(spec, 0, 0);
  1063. p = fns;
  1064. while (xs_list_iter(&p, &v)) {
  1065. /* get the retry time from the basename */
  1066. char *bn = strrchr(v, '/');
  1067. time_t t2 = atol(bn + 1);
  1068. if (t2 > t)
  1069. snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
  1070. else {
  1071. list = xs_list_append(list, v);
  1072. snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
  1073. }
  1074. }
  1075. return list;
  1076. }
  1077. xs_list *queue(void)
  1078. /* returns a list with filenames that can be dequeued */
  1079. {
  1080. xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
  1081. xs_list *list = xs_list_new();
  1082. time_t t = time(NULL);
  1083. xs_list *p;
  1084. xs_val *v;
  1085. xs *fns = xs_glob(spec, 0, 0);
  1086. p = fns;
  1087. while (xs_list_iter(&p, &v)) {
  1088. /* get the retry time from the basename */
  1089. char *bn = strrchr(v, '/');
  1090. time_t t2 = atol(bn + 1);
  1091. if (t2 > t)
  1092. srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1093. else {
  1094. list = xs_list_append(list, v);
  1095. srv_debug(2, xs_fmt("queue ready for %s", v));
  1096. }
  1097. }
  1098. return list;
  1099. }
  1100. xs_dict *dequeue(const char *fn)
  1101. /* dequeues a message */
  1102. {
  1103. FILE *f;
  1104. xs_dict *obj = NULL;
  1105. if ((f = fopen(fn, "r")) != NULL) {
  1106. /* delete right now */
  1107. unlink(fn);
  1108. xs *j = xs_readall(f);
  1109. obj = xs_json_loads(j);
  1110. fclose(f);
  1111. }
  1112. return obj;
  1113. }
  1114. /** the purge **/
  1115. static void _purge_file(const char *fn, time_t mt)
  1116. /* purge fn if it's older than days */
  1117. {
  1118. if (mtime(fn) < mt) {
  1119. /* older than the minimum time: delete it */
  1120. unlink(fn);
  1121. srv_debug(1, xs_fmt("purged %s", fn));
  1122. }
  1123. }
  1124. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1125. /* purges all files in subdir older than days */
  1126. {
  1127. if (days) {
  1128. time_t mt = time(NULL) - days * 24 * 3600;
  1129. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1130. xs *list = xs_glob(spec, 0, 0);
  1131. char *p, *v;
  1132. p = list;
  1133. while (xs_list_iter(&p, &v))
  1134. _purge_file(v, mt);
  1135. }
  1136. }
  1137. void purge_server(void)
  1138. /* purge global server data */
  1139. {
  1140. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1141. xs *dirs = xs_glob(spec, 0, 0);
  1142. char *p, *v;
  1143. time_t mt = time(NULL) - 7 * 24 * 3600;
  1144. p = dirs;
  1145. while (xs_list_iter(&p, &v)) {
  1146. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1147. xs *files = xs_glob(spec2, 0, 0);
  1148. char *p2, *v2;
  1149. p2 = files;
  1150. while (xs_list_iter(&p2, &v2)) {
  1151. int n_link;
  1152. /* old and with no hard links? */
  1153. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1154. xs *s1 = xs_replace(v2, ".json", "");
  1155. xs *l = xs_split(s1, "/");
  1156. char *md5 = xs_list_get(l, -1);
  1157. object_del_by_md5(md5);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. void purge_user(snac *snac)
  1163. /* do the purge for this user */
  1164. {
  1165. int priv_days, pub_days, user_days = 0;
  1166. char *v;
  1167. priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1168. pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1169. if ((v = xs_dict_get(snac->config, "purge_days")) != NULL)
  1170. user_days = xs_number_get(v);
  1171. if (user_days) {
  1172. /* override admin settings only if they are lesser */
  1173. if (priv_days == 0 || user_days < priv_days)
  1174. priv_days = user_days;
  1175. if (pub_days == 0 || user_days < pub_days)
  1176. pub_days = user_days;
  1177. }
  1178. _purge_subdir(snac, "hidden", priv_days);
  1179. _purge_subdir(snac, "private", priv_days);
  1180. _purge_subdir(snac, "public", pub_days);
  1181. }
  1182. void purge_all(void)
  1183. /* purge all users */
  1184. {
  1185. snac snac;
  1186. xs *list = user_list();
  1187. char *p, *uid;
  1188. p = list;
  1189. while (xs_list_iter(&p, &uid)) {
  1190. if (user_open(&snac, uid)) {
  1191. purge_user(&snac);
  1192. user_free(&snac);
  1193. }
  1194. }
  1195. purge_server();
  1196. }