data.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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, 0755);
  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, 0755);
  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_message(snac *snac, xs_dict *msg)
  1035. /* enqueues an output message */
  1036. {
  1037. xs *qmsg = _new_qmsg("message", msg, 0);
  1038. char *ntid = xs_dict_get(qmsg, "ntid");
  1039. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1040. qmsg = _enqueue_put(fn, qmsg);
  1041. snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
  1042. }
  1043. xs_list *user_queue(snac *snac)
  1044. /* returns a list with filenames that can be dequeued */
  1045. {
  1046. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1047. xs_list *list = xs_list_new();
  1048. time_t t = time(NULL);
  1049. xs_list *p;
  1050. xs_val *v;
  1051. xs *fns = xs_glob(spec, 0, 0);
  1052. p = fns;
  1053. while (xs_list_iter(&p, &v)) {
  1054. /* get the retry time from the basename */
  1055. char *bn = strrchr(v, '/');
  1056. time_t t2 = atol(bn + 1);
  1057. if (t2 > t)
  1058. snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
  1059. else {
  1060. list = xs_list_append(list, v);
  1061. snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
  1062. }
  1063. }
  1064. return list;
  1065. }
  1066. xs_list *queue(void)
  1067. /* returns a list with filenames that can be dequeued */
  1068. {
  1069. xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
  1070. xs_list *list = xs_list_new();
  1071. time_t t = time(NULL);
  1072. xs_list *p;
  1073. xs_val *v;
  1074. xs *fns = xs_glob(spec, 0, 0);
  1075. p = fns;
  1076. while (xs_list_iter(&p, &v)) {
  1077. /* get the retry time from the basename */
  1078. char *bn = strrchr(v, '/');
  1079. time_t t2 = atol(bn + 1);
  1080. if (t2 > t)
  1081. srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1082. else {
  1083. list = xs_list_append(list, v);
  1084. srv_debug(2, xs_fmt("queue ready for %s", v));
  1085. }
  1086. }
  1087. return list;
  1088. }
  1089. xs_dict *dequeue(const char *fn)
  1090. /* dequeues a message */
  1091. {
  1092. FILE *f;
  1093. xs_dict *obj = NULL;
  1094. if ((f = fopen(fn, "r")) != NULL) {
  1095. /* delete right now */
  1096. unlink(fn);
  1097. xs *j = xs_readall(f);
  1098. obj = xs_json_loads(j);
  1099. fclose(f);
  1100. }
  1101. return obj;
  1102. }
  1103. /** the purge **/
  1104. static void _purge_file(const char *fn, time_t mt)
  1105. /* purge fn if it's older than days */
  1106. {
  1107. if (mtime(fn) < mt) {
  1108. /* older than the minimum time: delete it */
  1109. unlink(fn);
  1110. srv_debug(1, xs_fmt("purged %s", fn));
  1111. }
  1112. }
  1113. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1114. /* purges all files in subdir older than days */
  1115. {
  1116. if (days) {
  1117. time_t mt = time(NULL) - days * 24 * 3600;
  1118. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1119. xs *list = xs_glob(spec, 0, 0);
  1120. char *p, *v;
  1121. p = list;
  1122. while (xs_list_iter(&p, &v))
  1123. _purge_file(v, mt);
  1124. }
  1125. }
  1126. void purge_server(void)
  1127. /* purge global server data */
  1128. {
  1129. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1130. xs *dirs = xs_glob(spec, 0, 0);
  1131. char *p, *v;
  1132. time_t mt = time(NULL) - 7 * 24 * 3600;
  1133. p = dirs;
  1134. while (xs_list_iter(&p, &v)) {
  1135. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1136. xs *files = xs_glob(spec2, 0, 0);
  1137. char *p2, *v2;
  1138. p2 = files;
  1139. while (xs_list_iter(&p2, &v2)) {
  1140. int n_link;
  1141. /* old and with no hard links? */
  1142. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1143. xs *s1 = xs_replace(v2, ".json", "");
  1144. xs *l = xs_split(s1, "/");
  1145. char *md5 = xs_list_get(l, -1);
  1146. object_del_by_md5(md5);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. void purge_user(snac *snac)
  1152. /* do the purge for this user */
  1153. {
  1154. int priv_days, pub_days, user_days = 0;
  1155. char *v;
  1156. priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1157. pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1158. if ((v = xs_dict_get(snac->config, "purge_days")) != NULL)
  1159. user_days = xs_number_get(v);
  1160. if (user_days) {
  1161. /* override admin settings only if they are lesser */
  1162. if (priv_days == 0 || user_days < priv_days)
  1163. priv_days = user_days;
  1164. if (pub_days == 0 || user_days < pub_days)
  1165. pub_days = user_days;
  1166. }
  1167. _purge_subdir(snac, "hidden", priv_days);
  1168. _purge_subdir(snac, "private", priv_days);
  1169. _purge_subdir(snac, "public", pub_days);
  1170. }
  1171. void purge_all(void)
  1172. /* purge all users */
  1173. {
  1174. snac snac;
  1175. xs *list = user_list();
  1176. char *p, *uid;
  1177. p = list;
  1178. while (xs_list_iter(&p, &uid)) {
  1179. if (user_open(&snac, uid)) {
  1180. purge_user(&snac);
  1181. user_free(&snac);
  1182. }
  1183. }
  1184. purge_server();
  1185. }