data.c 38 KB

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