data.c 22 KB

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