data.c 22 KB

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