data.c 37 KB

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