data.c 37 KB

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