data.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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 <glob.h>
  11. #include <sys/stat.h>
  12. int srv_open(char *basedir)
  13. /* opens a server */
  14. {
  15. int ret = 0;
  16. xs *cfg_file = NULL;
  17. FILE *f;
  18. d_char *error = NULL;
  19. srv_basedir = xs_str_new(basedir);
  20. if (xs_endswith(srv_basedir, "/"))
  21. srv_basedir = xs_crop(srv_basedir, 0, -1);
  22. cfg_file = xs_fmt("%s/server.json", basedir);
  23. if ((f = fopen(cfg_file, "r")) == NULL)
  24. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  25. else {
  26. xs *cfg_data;
  27. /* read full config file */
  28. cfg_data = xs_readall(f);
  29. fclose(f);
  30. /* parse */
  31. srv_config = xs_json_loads(cfg_data);
  32. if (srv_config == NULL)
  33. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  34. else {
  35. char *host;
  36. char *prefix;
  37. char *dbglvl;
  38. char *layout;
  39. double f = 0.0;
  40. host = xs_dict_get(srv_config, "host");
  41. prefix = xs_dict_get(srv_config, "prefix");
  42. dbglvl = xs_dict_get(srv_config, "dbglevel");
  43. layout = xs_dict_get(srv_config, "layout");
  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 (!layout || (f = xs_number_get(layout)) != 2.0)
  54. error = xs_fmt("ERROR: unsupported old disk layout %f\n", f);
  55. else
  56. ret = 1;
  57. }
  58. }
  59. }
  60. if (ret == 0 && error != NULL)
  61. srv_log(error);
  62. return ret;
  63. }
  64. void user_free(snac *snac)
  65. /* frees a user snac */
  66. {
  67. free(snac->uid);
  68. free(snac->basedir);
  69. free(snac->config);
  70. free(snac->key);
  71. free(snac->actor);
  72. }
  73. int user_open(snac *snac, char *uid)
  74. /* opens a user */
  75. {
  76. int ret = 0;
  77. memset(snac, '\0', sizeof(struct _snac));
  78. if (validate_uid(uid)) {
  79. xs *cfg_file;
  80. FILE *f;
  81. snac->uid = xs_str_new(uid);
  82. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  83. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  84. if ((f = fopen(cfg_file, "r")) != NULL) {
  85. xs *cfg_data;
  86. /* read full config file */
  87. cfg_data = xs_readall(f);
  88. fclose(f);
  89. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  90. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  91. if ((f = fopen(key_file, "r")) != NULL) {
  92. xs *key_data;
  93. key_data = xs_readall(f);
  94. fclose(f);
  95. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  96. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  97. ret = 1;
  98. }
  99. else
  100. srv_log(xs_fmt("cannot parse '%s'", key_file));
  101. }
  102. else
  103. srv_log(xs_fmt("error opening '%s'", key_file));
  104. }
  105. else
  106. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  107. }
  108. else
  109. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  110. }
  111. else
  112. srv_log(xs_fmt("invalid user '%s'", uid));
  113. if (!ret)
  114. user_free(snac);
  115. return ret;
  116. }
  117. d_char *user_list(void)
  118. /* returns the list of user ids */
  119. {
  120. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  121. return xs_glob(spec, 1, 0);
  122. }
  123. double mtime(char *fn)
  124. /* returns the mtime of a file or directory, or 0.0 */
  125. {
  126. struct stat st;
  127. double r = 0.0;
  128. if (fn && stat(fn, &st) != -1)
  129. r = (double)st.st_mtim.tv_sec;
  130. return r;
  131. }
  132. d_char *_follower_fn(snac *snac, char *actor)
  133. {
  134. xs *md5 = xs_md5_hex(actor, strlen(actor));
  135. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  136. }
  137. int follower_add(snac *snac, char *actor, char *msg)
  138. /* adds a follower */
  139. {
  140. int ret = 201; /* created */
  141. xs *fn = _follower_fn(snac, actor);
  142. FILE *f;
  143. if ((f = fopen(fn, "w")) != NULL) {
  144. xs *j = xs_json_dumps_pp(msg, 4);
  145. fwrite(j, 1, strlen(j), f);
  146. fclose(f);
  147. }
  148. else
  149. ret = 500;
  150. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  151. return ret;
  152. }
  153. int follower_del(snac *snac, char *actor)
  154. /* deletes a follower */
  155. {
  156. int status = 200;
  157. xs *fn = _follower_fn(snac, actor);
  158. if (fn != NULL)
  159. unlink(fn);
  160. else
  161. status = 404;
  162. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  163. return status;
  164. }
  165. int follower_check(snac *snac, char *actor)
  166. /* checks if someone is a follower */
  167. {
  168. xs *fn = _follower_fn(snac, actor);
  169. return !!(mtime(fn) != 0.0);
  170. }
  171. d_char *follower_list(snac *snac)
  172. /* returns the list of followers */
  173. {
  174. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  175. xs *glist = xs_glob(spec, 0, 0);
  176. char *p, *v;
  177. d_char *list = xs_list_new();
  178. /* iterate the list of files */
  179. p = glist;
  180. while (xs_list_iter(&p, &v)) {
  181. FILE *f;
  182. /* load the follower data */
  183. if ((f = fopen(v, "r")) != NULL) {
  184. xs *j = xs_readall(f);
  185. fclose(f);
  186. if (j != NULL) {
  187. xs *o = xs_json_loads(j);
  188. if (o != NULL)
  189. list = xs_list_append(list, o);
  190. }
  191. }
  192. }
  193. return list;
  194. }
  195. double timeline_mtime(snac *snac)
  196. {
  197. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  198. return mtime(fn);
  199. }
  200. d_char *_timeline_find_fn(snac *snac, char *id)
  201. /* returns the file name of a timeline entry by its id */
  202. {
  203. xs *md5 = xs_md5_hex(id, strlen(id));
  204. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  205. xs *list = NULL;
  206. d_char *fn = NULL;
  207. int l;
  208. list = xs_glob(spec, 0, 0);
  209. l = xs_list_len(list);
  210. /* if there is something, get the first one */
  211. if (l > 0) {
  212. fn = xs_str_new(xs_list_get(list, 0));
  213. if (l > 1)
  214. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  215. }
  216. return fn;
  217. }
  218. int timeline_here(snac *snac, char *id)
  219. /* checks if an object is already downloaded */
  220. {
  221. xs *fn = _timeline_find_fn(snac, id);
  222. return fn != NULL;
  223. }
  224. d_char *timeline_find(snac *snac, char *id)
  225. /* gets a message from the timeline by id */
  226. {
  227. xs *fn = _timeline_find_fn(snac, id);
  228. d_char *msg = NULL;
  229. if (fn != NULL) {
  230. FILE *f;
  231. if ((f = fopen(fn, "r")) != NULL) {
  232. xs *j = xs_readall(f);
  233. msg = xs_json_loads(j);
  234. fclose(f);
  235. }
  236. }
  237. return msg;
  238. }
  239. void timeline_del(snac *snac, char *id)
  240. /* deletes a message from the timeline */
  241. {
  242. xs *fn = _timeline_find_fn(snac, id);
  243. if (fn != NULL) {
  244. xs *lfn = NULL;
  245. unlink(fn);
  246. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  247. /* try to delete also from the local timeline */
  248. lfn = xs_replace(fn, "/timeline/", "/local/");
  249. if (unlink(lfn) != -1)
  250. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  251. }
  252. }
  253. d_char *timeline_get(snac *snac, char *fn)
  254. /* gets a timeline entry by file name */
  255. {
  256. d_char *d = NULL;
  257. FILE *f;
  258. if ((f = fopen(fn, "r")) != NULL) {
  259. xs *j = xs_readall(f);
  260. d = xs_json_loads(j);
  261. fclose(f);
  262. }
  263. return d;
  264. }
  265. d_char *_timeline_list(snac *snac, char *directory, int max)
  266. /* returns a list of the timeline filenames */
  267. {
  268. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  269. int c_max;
  270. /* maximum number of items in the timeline */
  271. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  272. /* never more timeline entries than the configured maximum */
  273. if (max > c_max)
  274. max = c_max;
  275. return xs_glob_n(spec, 0, 1, max);
  276. }
  277. d_char *timeline_list(snac *snac, int max)
  278. {
  279. return _timeline_list(snac, "timeline", max);
  280. }
  281. d_char *local_list(snac *snac, int max)
  282. {
  283. return _timeline_list(snac, "local", max);
  284. }
  285. d_char *_timeline_new_fn(snac *snac, char *id)
  286. /* creates a new filename */
  287. {
  288. xs *ntid = tid(0);
  289. xs *md5 = xs_md5_hex(id, strlen(id));
  290. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  291. }
  292. void _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  293. /* writes a timeline entry and refreshes the ancestors */
  294. {
  295. xs *fn = _timeline_new_fn(snac, id);
  296. FILE *f;
  297. if ((f = fopen(fn, "w")) != NULL) {
  298. xs *j = xs_json_dumps_pp(msg, 4);
  299. fwrite(j, strlen(j), 1, f);
  300. fclose(f);
  301. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  302. }
  303. /* related to this user? link to local timeline */
  304. if (xs_startswith(id, snac->actor) ||
  305. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  306. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  307. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  308. link(fn, lfn);
  309. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  310. }
  311. if (!xs_is_null(parent)) {
  312. /* update the parent, adding this id to its children list */
  313. xs *pfn = _timeline_find_fn(snac, parent);
  314. xs *p_msg = NULL;
  315. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  316. xs *j;
  317. j = xs_readall(f);
  318. fclose(f);
  319. p_msg = xs_json_loads(j);
  320. }
  321. if (p_msg == NULL)
  322. return;
  323. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  324. xs *children = xs_dup(xs_dict_get(meta, "children"));
  325. /* add the child if it's not already there */
  326. if (xs_list_in(children, id) == -1)
  327. children = xs_list_append(children, id);
  328. /* re-store */
  329. meta = xs_dict_set(meta, "children", children);
  330. p_msg = xs_dict_set(p_msg, "_snac", meta);
  331. xs *nfn = _timeline_new_fn(snac, parent);
  332. if ((f = fopen(nfn, "w")) != NULL) {
  333. xs *j = xs_json_dumps_pp(p_msg, 4);
  334. fwrite(j, strlen(j), 1, f);
  335. fclose(f);
  336. unlink(pfn);
  337. snac_debug(snac, 1,
  338. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  339. /* try to do the same with the local */
  340. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  341. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  342. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  343. link(nfn, nlfn);
  344. snac_debug(snac, 1,
  345. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  346. }
  347. }
  348. else
  349. return;
  350. /* now iterate all parents up, just renaming the files */
  351. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  352. while (!xs_is_null(grampa)) {
  353. xs *gofn = _timeline_find_fn(snac, grampa);
  354. if (gofn == NULL)
  355. break;
  356. /* create the new filename */
  357. xs *gnfn = _timeline_new_fn(snac, grampa);
  358. rename(gofn, gnfn);
  359. snac_debug(snac, 1,
  360. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  361. /* try to do the same with the local */
  362. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  363. if (unlink(golfn) != -1) {
  364. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  365. link(gnfn, gnlfn);
  366. snac_debug(snac, 1,
  367. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  368. }
  369. /* now open it and get its own parent */
  370. if ((f = fopen(gnfn, "r")) != NULL) {
  371. xs *j = xs_readall(f);
  372. fclose(f);
  373. xs *g_msg = xs_json_loads(j);
  374. d_char *meta = xs_dict_get(g_msg, "_snac");
  375. d_char *p = xs_dict_get(meta, "parent");
  376. free(grampa);
  377. grampa = xs_dup(p);
  378. }
  379. }
  380. }
  381. }
  382. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  383. /* adds a message to the timeline */
  384. {
  385. xs *pfn = _timeline_find_fn(snac, id);
  386. if (pfn != NULL) {
  387. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  388. return 0;
  389. }
  390. xs *msg = xs_dup(o_msg);
  391. xs *md;
  392. /* add new metadata */
  393. md = xs_json_loads("{"
  394. "\"children\": [],"
  395. "\"liked_by\": [],"
  396. "\"announced_by\": [],"
  397. "\"version\": \"" USER_AGENT "\","
  398. "\"referrer\": null,"
  399. "\"parent\": null"
  400. "}");
  401. if (!xs_is_null(parent))
  402. md = xs_dict_set(md, "parent", parent);
  403. if (!xs_is_null(referrer))
  404. md = xs_dict_set(md, "referrer", referrer);
  405. msg = xs_dict_set(msg, "_snac", md);
  406. _timeline_write(snac, id, msg, parent, referrer);
  407. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  408. return 1;
  409. }
  410. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  411. /* updates a timeline entry with a new admiration */
  412. {
  413. xs *ofn = _timeline_find_fn(snac, id);
  414. FILE *f;
  415. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  416. xs *j1 = xs_readall(f);
  417. fclose(f);
  418. xs *msg = xs_json_loads(j1);
  419. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  420. xs *list;
  421. if (like)
  422. list = xs_dup(xs_dict_get(meta, "liked_by"));
  423. else
  424. list = xs_dup(xs_dict_get(meta, "announced_by"));
  425. /* add the admirer if it's not already there */
  426. if (xs_list_in(list, admirer) == -1)
  427. list = xs_list_append(list, admirer);
  428. /* set the admirer as the referrer (if not already set) */
  429. if (!like && xs_is_null(xs_dict_get(meta, "referrer")))
  430. meta = xs_dict_set(meta, "referrer", admirer);
  431. /* re-store */
  432. if (like)
  433. meta = xs_dict_set(meta, "liked_by", list);
  434. else
  435. meta = xs_dict_set(meta, "announced_by", list);
  436. msg = xs_dict_set(msg, "_snac", meta);
  437. unlink(ofn);
  438. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  439. unlink(ofn);
  440. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  441. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  442. like ? "Like" : "Announce", id, admirer));
  443. }
  444. else
  445. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  446. }
  447. d_char *_following_fn(snac *snac, char *actor)
  448. {
  449. xs *md5 = xs_md5_hex(actor, strlen(actor));
  450. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  451. }
  452. int following_add(snac *snac, char *actor, char *msg)
  453. /* adds to the following list */
  454. {
  455. int ret = 201; /* created */
  456. xs *fn = _following_fn(snac, actor);
  457. FILE *f;
  458. if ((f = fopen(fn, "w")) != NULL) {
  459. xs *j = xs_json_dumps_pp(msg, 4);
  460. fwrite(j, 1, strlen(j), f);
  461. fclose(f);
  462. }
  463. else
  464. ret = 500;
  465. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  466. return ret;
  467. }
  468. int following_del(snac *snac, char *actor)
  469. /* someone is no longer following us */
  470. {
  471. xs *fn = _following_fn(snac, actor);
  472. unlink(fn);
  473. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  474. return 200;
  475. }
  476. int following_check(snac *snac, char *actor)
  477. /* checks if someone is following us */
  478. {
  479. xs *fn = _following_fn(snac, actor);
  480. return !!(mtime(fn) != 0.0);
  481. }
  482. int following_get(snac *snac, char *actor, d_char **data)
  483. /* returns the 'Follow' object */
  484. {
  485. xs *fn = _following_fn(snac, actor);
  486. FILE *f;
  487. int status = 200;
  488. if ((f = fopen(fn, "r")) != NULL) {
  489. xs *j = xs_readall(f);
  490. fclose(f);
  491. *data = xs_json_loads(j);
  492. }
  493. else
  494. status = 404;
  495. return status;
  496. }
  497. d_char *_muted_fn(snac *snac, char *actor)
  498. {
  499. xs *md5 = xs_md5_hex(actor, strlen(actor));
  500. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  501. }
  502. void mute(snac *snac, char *actor)
  503. /* mutes a moron */
  504. {
  505. xs *fn = _muted_fn(snac, actor);
  506. FILE *f;
  507. if ((f = fopen(fn, "w")) != NULL) {
  508. fprintf(f, "%s\n", actor);
  509. fclose(f);
  510. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  511. }
  512. }
  513. void unmute(snac *snac, char *actor)
  514. /* actor is no longer a moron */
  515. {
  516. xs *fn = _muted_fn(snac, actor);
  517. unlink(fn);
  518. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  519. }
  520. int is_muted(snac *snac, char *actor)
  521. /* check if someone is muted */
  522. {
  523. xs *fn = _muted_fn(snac, actor);
  524. return !!(mtime(fn) != 0.0);
  525. }
  526. d_char *_actor_fn(snac *snac, char *actor)
  527. /* returns the file name for an actor */
  528. {
  529. xs *md5 = xs_md5_hex(actor, strlen(actor));
  530. return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
  531. }
  532. int actor_add(snac *snac, char *actor, char *msg)
  533. /* adds an actor */
  534. {
  535. int ret = 201; /* created */
  536. xs *fn = _actor_fn(snac, actor);
  537. FILE *f;
  538. if ((f = fopen(fn, "w")) != NULL) {
  539. xs *j = xs_json_dumps_pp(msg, 4);
  540. fwrite(j, 1, strlen(j), f);
  541. fclose(f);
  542. }
  543. else
  544. ret = 500;
  545. snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn));
  546. return ret;
  547. }
  548. int actor_get(snac *snac, char *actor, d_char **data)
  549. /* returns an already downloaded actor */
  550. {
  551. xs *fn = _actor_fn(snac, actor);
  552. double t;
  553. double max_time;
  554. int status;
  555. FILE *f;
  556. t = mtime(fn);
  557. /* no mtime? there is nothing here */
  558. if (t == 0.0)
  559. return 404;
  560. /* maximum time for the actor data to be considered stale */
  561. max_time = 3600.0 * 36.0;
  562. if (t + max_time < (double) time(NULL)) {
  563. /* actor data exists but also stinks */
  564. if ((f = fopen(fn, "a")) != NULL) {
  565. /* write a blank at the end to 'touch' the file */
  566. fwrite(" ", 1, 1, f);
  567. fclose(f);
  568. }
  569. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  570. }
  571. else {
  572. /* it's still valid */
  573. status = 200;
  574. }
  575. if ((f = fopen(fn, "r")) != NULL) {
  576. xs *j = xs_readall(f);
  577. fclose(f);
  578. if (data)
  579. *data = xs_json_loads(j);
  580. }
  581. else
  582. status = 500;
  583. return status;
  584. }
  585. d_char *_static_fn(snac *snac, const char *id)
  586. /* gets the filename for a static file */
  587. {
  588. return xs_fmt("%s/static/%s", snac->basedir, id);
  589. }
  590. int static_get(snac *snac, const char *id, d_char **data, int *size)
  591. /* returns static content */
  592. {
  593. xs *fn = _static_fn(snac, id);
  594. FILE *f;
  595. int status = 404;
  596. *size = 0xfffffff;
  597. if ((f = fopen(fn, "rb")) != NULL) {
  598. *data = xs_read(f, size);
  599. fclose(f);
  600. status = 200;
  601. }
  602. return status;
  603. }
  604. void static_put(snac *snac, const char *id, const char *data, int size)
  605. /* writes status content */
  606. {
  607. xs *fn = _static_fn(snac, id);
  608. FILE *f;
  609. if ((f = fopen(fn, "wb")) != NULL) {
  610. fwrite(data, size, 1, f);
  611. fclose(f);
  612. }
  613. }
  614. d_char *_history_fn(snac *snac, char *id)
  615. /* gets the filename for the history */
  616. {
  617. return xs_fmt("%s/history/%s", snac->basedir, id);
  618. }
  619. double history_mtime(snac *snac, char * id)
  620. {
  621. double t = 0.0;
  622. xs *fn = _history_fn(snac, id);
  623. if (fn != NULL)
  624. t = mtime(fn);
  625. return t;
  626. }
  627. void history_add(snac *snac, char *id, char *content, int size)
  628. /* adds something to the history */
  629. {
  630. xs *fn = _history_fn(snac, id);
  631. FILE *f;
  632. if ((f = fopen(fn, "w")) != NULL) {
  633. fwrite(content, size, 1, f);
  634. fclose(f);
  635. }
  636. }
  637. d_char *history_get(snac *snac, char *id)
  638. {
  639. d_char *content = NULL;
  640. xs *fn = _history_fn(snac, id);
  641. FILE *f;
  642. if ((f = fopen(fn, "r")) != NULL) {
  643. content = xs_readall(f);
  644. fclose(f);
  645. }
  646. return content;
  647. }
  648. int history_del(snac *snac, char *id)
  649. {
  650. xs *fn = _history_fn(snac, id);
  651. return unlink(fn);
  652. }
  653. d_char *history_list(snac *snac)
  654. {
  655. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  656. return xs_glob(spec, 1, 0);
  657. }
  658. static int _enqueue_put(char *fn, char *msg)
  659. /* writes safely to the queue */
  660. {
  661. int ret = 1;
  662. xs *tfn = xs_fmt("%s.tmp", fn);
  663. FILE *f;
  664. if ((f = fopen(tfn, "w")) != NULL) {
  665. xs *j = xs_json_dumps_pp(msg, 4);
  666. fwrite(j, strlen(j), 1, f);
  667. fclose(f);
  668. rename(tfn, fn);
  669. }
  670. else
  671. ret = 0;
  672. return ret;
  673. }
  674. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  675. /* enqueues an input message */
  676. {
  677. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  678. xs *ntid = tid(retries * 60 * qrt);
  679. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  680. xs *qmsg = xs_dict_new();
  681. xs *rn = xs_number_new(retries);
  682. qmsg = xs_dict_append(qmsg, "type", "input");
  683. qmsg = xs_dict_append(qmsg, "object", msg);
  684. qmsg = xs_dict_append(qmsg, "req", req);
  685. qmsg = xs_dict_append(qmsg, "retries", rn);
  686. _enqueue_put(fn, qmsg);
  687. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  688. }
  689. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  690. /* enqueues an output message for an actor */
  691. {
  692. if (strcmp(actor, snac->actor) == 0) {
  693. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  694. return;
  695. }
  696. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  697. xs *ntid = tid(retries * 60 * qrt);
  698. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  699. xs *qmsg = xs_dict_new();
  700. xs *rn = xs_number_new(retries);
  701. qmsg = xs_dict_append(qmsg, "type", "output");
  702. qmsg = xs_dict_append(qmsg, "actor", actor);
  703. qmsg = xs_dict_append(qmsg, "object", msg);
  704. qmsg = xs_dict_append(qmsg, "retries", rn);
  705. _enqueue_put(fn, qmsg);
  706. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  707. }
  708. d_char *queue(snac *snac)
  709. /* returns a list with filenames that can be dequeued */
  710. {
  711. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  712. d_char *list = xs_list_new();
  713. glob_t globbuf;
  714. time_t t = time(NULL);
  715. if (glob(spec, 0, NULL, &globbuf) == 0) {
  716. int n;
  717. char *p;
  718. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  719. /* get the retry time from the basename */
  720. char *bn = strrchr(p, '/');
  721. time_t t2 = atol(bn + 1);
  722. if (t2 > t)
  723. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  724. else {
  725. list = xs_list_append(list, p);
  726. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  727. }
  728. }
  729. }
  730. globfree(&globbuf);
  731. return list;
  732. }
  733. d_char *dequeue(snac *snac, char *fn)
  734. /* dequeues a message */
  735. {
  736. FILE *f;
  737. d_char *obj = NULL;
  738. if ((f = fopen(fn, "r")) != NULL) {
  739. /* delete right now */
  740. unlink(fn);
  741. xs *j = xs_readall(f);
  742. obj = xs_json_loads(j);
  743. fclose(f);
  744. }
  745. return obj;
  746. }
  747. void purge(snac *snac)
  748. /* do the purge */
  749. {
  750. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  751. /* purge days set to 0? disable purging */
  752. if (tpd == 0) {
  753. /* well, enjoy your data drive exploding */
  754. return;
  755. }
  756. time_t mt = time(NULL) - tpd * 24 * 3600;
  757. xs *t_spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
  758. xs *t_list = xs_glob(t_spec, 0, 0);
  759. char *p, *v;
  760. p = t_list;
  761. while (xs_list_iter(&p, &v)) {
  762. if (mtime(v) < mt) {
  763. /* older than the minimum time: delete it */
  764. unlink(v);
  765. snac_debug(snac, 1, xs_fmt("purged %s", v));
  766. }
  767. }
  768. xs *a_spec = xs_fmt("%s/actors/" "*.json", snac->basedir);
  769. xs *a_list = xs_glob(a_spec, 0, 0);
  770. p = a_list;
  771. while (xs_list_iter(&p, &v)) {
  772. if (mtime(v) < mt) {
  773. /* older than the minimum time: delete it */
  774. unlink(v);
  775. snac_debug(snac, 1, xs_fmt("purged %s", v));
  776. }
  777. }
  778. }
  779. void purge_all(void)
  780. /* purge all users */
  781. {
  782. snac snac;
  783. xs *list = user_list();
  784. char *p, *uid;
  785. p = list;
  786. while (xs_list_iter(&p, &uid)) {
  787. if (user_open(&snac, uid)) {
  788. purge(&snac);
  789. user_free(&snac);
  790. }
  791. }
  792. }