data.c 36 KB

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