data.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. double mtime(char *fn)
  130. /* returns the mtime of a file or directory, or 0.0 */
  131. {
  132. struct stat st;
  133. double r = 0.0;
  134. if (fn && stat(fn, &st) != -1)
  135. r = (double)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. double timeline_mtime(snac *snac)
  203. {
  204. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  205. return mtime(fn);
  206. }
  207. d_char *_timeline_find_fn(snac *snac, char *id)
  208. /* returns the file name of a timeline entry by its id */
  209. {
  210. xs *md5 = xs_md5_hex(id, strlen(id));
  211. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  212. glob_t globbuf;
  213. d_char *fn = NULL;
  214. if (glob(spec, 0, NULL, &globbuf) == 0 && globbuf.gl_pathc) {
  215. /* get just the first file */
  216. fn = xs_str_new(globbuf.gl_pathv[0]);
  217. }
  218. globfree(&globbuf);
  219. return fn;
  220. }
  221. int timeline_here(snac *snac, char *id)
  222. /* checks if an object is already downloaded */
  223. {
  224. xs *fn = _timeline_find_fn(snac, id);
  225. return fn != NULL;
  226. }
  227. d_char *timeline_find(snac *snac, char *id)
  228. /* gets a message from the timeline by id */
  229. {
  230. xs *fn = _timeline_find_fn(snac, id);
  231. d_char *msg = NULL;
  232. if (fn != NULL) {
  233. FILE *f;
  234. if ((f = fopen(fn, "r")) != NULL) {
  235. xs *j = xs_readall(f);
  236. msg = xs_json_loads(j);
  237. fclose(f);
  238. }
  239. }
  240. return msg;
  241. }
  242. void timeline_del(snac *snac, char *id)
  243. /* deletes a message from the timeline */
  244. {
  245. xs *fn = _timeline_find_fn(snac, id);
  246. if (fn != NULL) {
  247. xs *lfn = NULL;
  248. unlink(fn);
  249. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  250. /* try to delete also from the local timeline */
  251. lfn = xs_replace(fn, "/timeline/", "/local/");
  252. if (unlink(lfn) != -1)
  253. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  254. }
  255. }
  256. d_char *timeline_get(snac *snac, char *fn)
  257. /* gets a timeline entry by file name */
  258. {
  259. d_char *d = NULL;
  260. FILE *f;
  261. if ((f = fopen(fn, "r")) != NULL) {
  262. xs *j = xs_readall(f);
  263. d = xs_json_loads(j);
  264. fclose(f);
  265. }
  266. return d;
  267. }
  268. d_char *_timeline_list(snac *snac, char *directory, int max)
  269. /* returns a list of the timeline filenames */
  270. {
  271. d_char *list;
  272. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  273. glob_t globbuf;
  274. int c_max;
  275. /* maximum number of items in the timeline */
  276. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  277. if (max > c_max)
  278. max = c_max;
  279. list = xs_list_new();
  280. /* get the list in reverse order */
  281. if (glob(spec, 0, NULL, &globbuf) == 0) {
  282. int n;
  283. if (max > globbuf.gl_pathc)
  284. max = globbuf.gl_pathc;
  285. for (n = 0; n < max; n++) {
  286. char *fn = globbuf.gl_pathv[globbuf.gl_pathc - n - 1];
  287. list = xs_list_append(list, fn);
  288. }
  289. }
  290. globfree(&globbuf);
  291. return list;
  292. }
  293. d_char *timeline_list(snac *snac, int max)
  294. {
  295. return _timeline_list(snac, "timeline", max);
  296. }
  297. d_char *local_list(snac *snac, int max)
  298. {
  299. return _timeline_list(snac, "local", max);
  300. }
  301. d_char *_timeline_new_fn(snac *snac, char *id)
  302. /* creates a new filename */
  303. {
  304. xs *ntid = tid(0);
  305. xs *md5 = xs_md5_hex(id, strlen(id));
  306. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  307. }
  308. void _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  309. /* writes a timeline entry and refreshes the ancestors */
  310. {
  311. xs *fn = _timeline_new_fn(snac, id);
  312. FILE *f;
  313. if ((f = fopen(fn, "w")) != NULL) {
  314. xs *j = xs_json_dumps_pp(msg, 4);
  315. fwrite(j, strlen(j), 1, f);
  316. fclose(f);
  317. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  318. }
  319. /* related to this user? link to local timeline */
  320. if (xs_startswith(id, snac->actor) ||
  321. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  322. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  323. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  324. link(fn, lfn);
  325. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  326. }
  327. if (!xs_is_null(parent)) {
  328. /* update the parent, adding this id to its children list */
  329. xs *pfn = _timeline_find_fn(snac, parent);
  330. xs *p_msg = NULL;
  331. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  332. xs *j;
  333. j = xs_readall(f);
  334. fclose(f);
  335. p_msg = xs_json_loads(j);
  336. }
  337. if (p_msg == NULL)
  338. return;
  339. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  340. xs *children = xs_dup(xs_dict_get(meta, "children"));
  341. /* add the child if it's not already there */
  342. if (xs_list_in(children, id) == -1)
  343. children = xs_list_append(children, id);
  344. /* re-store */
  345. meta = xs_dict_set(meta, "children", children);
  346. p_msg = xs_dict_set(p_msg, "_snac", meta);
  347. xs *nfn = _timeline_new_fn(snac, parent);
  348. if ((f = fopen(nfn, "w")) != NULL) {
  349. xs *j = xs_json_dumps_pp(p_msg, 4);
  350. fwrite(j, strlen(j), 1, f);
  351. fclose(f);
  352. unlink(pfn);
  353. snac_debug(snac, 1,
  354. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  355. /* try to do the same with the local */
  356. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  357. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  358. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  359. link(nfn, nlfn);
  360. snac_debug(snac, 1,
  361. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  362. }
  363. }
  364. else
  365. return;
  366. /* now iterate all parents up, just renaming the files */
  367. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  368. while (!xs_is_null(grampa)) {
  369. xs *gofn = _timeline_find_fn(snac, grampa);
  370. if (gofn == NULL)
  371. break;
  372. /* create the new filename */
  373. xs *gnfn = _timeline_new_fn(snac, grampa);
  374. rename(gofn, gnfn);
  375. snac_debug(snac, 1,
  376. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  377. /* try to do the same with the local */
  378. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  379. if (unlink(golfn) != -1) {
  380. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  381. link(gnfn, gnlfn);
  382. snac_debug(snac, 1,
  383. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  384. }
  385. /* now open it and get its own parent */
  386. if ((f = fopen(gnfn, "r")) != NULL) {
  387. xs *j = xs_readall(f);
  388. fclose(f);
  389. xs *g_msg = xs_json_loads(j);
  390. d_char *meta = xs_dict_get(g_msg, "_snac");
  391. d_char *p = xs_dict_get(meta, "parent");
  392. free(grampa);
  393. grampa = xs_dup(p);
  394. }
  395. }
  396. }
  397. }
  398. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  399. /* adds a message to the timeline */
  400. {
  401. xs *pfn = _timeline_find_fn(snac, id);
  402. if (pfn != NULL) {
  403. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  404. return 0;
  405. }
  406. xs *msg = xs_dup(o_msg);
  407. xs *md;
  408. /* add new metadata */
  409. md = xs_json_loads("{"
  410. "\"children\": [],"
  411. "\"liked_by\": [],"
  412. "\"announced_by\": [],"
  413. "\"version\": \"" USER_AGENT "\","
  414. "\"referrer\": null,"
  415. "\"parent\": null"
  416. "}");
  417. if (!xs_is_null(parent))
  418. md = xs_dict_set(md, "parent", parent);
  419. if (!xs_is_null(referrer))
  420. md = xs_dict_set(md, "referrer", referrer);
  421. msg = xs_dict_set(msg, "_snac", md);
  422. _timeline_write(snac, id, msg, parent, referrer);
  423. snac_log(snac, xs_fmt("timeline_add %s", id));
  424. return 1;
  425. }
  426. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  427. /* updates a timeline entry with a new admiration */
  428. {
  429. xs *ofn = _timeline_find_fn(snac, id);
  430. FILE *f;
  431. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  432. xs *j1 = xs_readall(f);
  433. fclose(f);
  434. xs *msg = xs_json_loads(j1);
  435. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  436. xs *list;
  437. if (like)
  438. list = xs_dup(xs_dict_get(meta, "liked_by"));
  439. else
  440. list = xs_dup(xs_dict_get(meta, "announced_by"));
  441. /* add the admirer if it's not already there */
  442. if (xs_list_in(list, admirer) == -1)
  443. list = xs_list_append(list, admirer);
  444. /* set the admirer as the referrer */
  445. if (!like)
  446. meta = xs_dict_set(meta, "referrer", admirer);
  447. /* re-store */
  448. if (like)
  449. meta = xs_dict_set(meta, "liked_by", list);
  450. else
  451. meta = xs_dict_set(meta, "announced_by", list);
  452. msg = xs_dict_set(msg, "_snac", meta);
  453. unlink(ofn);
  454. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  455. unlink(ofn);
  456. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), admirer);
  457. snac_log(snac, xs_fmt("timeline_admire (%s) %s %s",
  458. like ? "Like" : "Announce", id, admirer));
  459. }
  460. else
  461. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  462. }
  463. d_char *_following_fn(snac *snac, char *actor)
  464. {
  465. xs *md5 = xs_md5_hex(actor, strlen(actor));
  466. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  467. }
  468. int following_add(snac *snac, char *actor, char *msg)
  469. /* adds to the following list */
  470. {
  471. int ret = 201; /* created */
  472. xs *fn = _following_fn(snac, actor);
  473. FILE *f;
  474. if ((f = fopen(fn, "w")) != NULL) {
  475. xs *j = xs_json_dumps_pp(msg, 4);
  476. fwrite(j, 1, strlen(j), f);
  477. fclose(f);
  478. }
  479. else
  480. ret = 500;
  481. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  482. return ret;
  483. }
  484. int following_del(snac *snac, char *actor)
  485. /* someone is no longer following us */
  486. {
  487. xs *fn = _following_fn(snac, actor);
  488. unlink(fn);
  489. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  490. return 200;
  491. }
  492. int following_check(snac *snac, char *actor)
  493. /* checks if someone is following us */
  494. {
  495. xs *fn = _following_fn(snac, actor);
  496. return !!(mtime(fn) != 0.0);
  497. }
  498. d_char *_muted_fn(snac *snac, char *actor)
  499. {
  500. xs *md5 = xs_md5_hex(actor, strlen(actor));
  501. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  502. }
  503. void mute(snac *snac, char *actor)
  504. /* mutes a moron */
  505. {
  506. xs *fn = _muted_fn(snac, actor);
  507. FILE *f;
  508. if ((f = fopen(fn, "w")) != NULL) {
  509. fprintf(f, "%s\n", actor);
  510. fclose(f);
  511. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  512. }
  513. }
  514. void unmute(snac *snac, char *actor)
  515. /* actor is no longer a moron */
  516. {
  517. xs *fn = _muted_fn(snac, actor);
  518. unlink(fn);
  519. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  520. }
  521. int is_muted(snac *snac, char *actor)
  522. /* check if someone is muted */
  523. {
  524. xs *fn = _muted_fn(snac, actor);
  525. return !!(mtime(fn) != 0.0);
  526. }
  527. d_char *_actor_fn(snac *snac, char *actor)
  528. /* returns the file name for an actor */
  529. {
  530. xs *md5 = xs_md5_hex(actor, strlen(actor));
  531. return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
  532. }
  533. int actor_add(snac *snac, char *actor, char *msg)
  534. /* adds an actor */
  535. {
  536. int ret = 201; /* created */
  537. xs *fn = _actor_fn(snac, actor);
  538. FILE *f;
  539. if ((f = fopen(fn, "w")) != NULL) {
  540. xs *j = xs_json_dumps_pp(msg, 4);
  541. fwrite(j, 1, strlen(j), f);
  542. fclose(f);
  543. }
  544. else
  545. ret = 500;
  546. snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn));
  547. return ret;
  548. }
  549. int actor_get(snac *snac, char *actor, d_char **data)
  550. /* returns an already downloaded actor */
  551. {
  552. xs *fn = _actor_fn(snac, actor);
  553. double t;
  554. double max_time;
  555. int status;
  556. FILE *f;
  557. t = mtime(fn);
  558. /* no mtime? there is nothing here */
  559. if (t == 0.0)
  560. return 404;
  561. /* maximum time for the actor data to be considered stale */
  562. max_time = 3600.0 * 36.0;
  563. if (t + max_time < (double) time(NULL)) {
  564. /* actor data exists but also stinks */
  565. if ((f = fopen(fn, "a")) != NULL) {
  566. /* write a blank at the end to 'touch' the file */
  567. fwrite(" ", 1, 1, f);
  568. fclose(f);
  569. }
  570. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  571. }
  572. else {
  573. /* it's still valid */
  574. status = 200;
  575. }
  576. if ((f = fopen(fn, "r")) != NULL) {
  577. xs *j = xs_readall(f);
  578. fclose(f);
  579. *data = xs_json_loads(j);
  580. }
  581. else
  582. status = 500;
  583. return status;
  584. }
  585. d_char *_static_fn(snac *snac, 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, 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. status = 200;
  600. }
  601. return status;
  602. }
  603. d_char *_history_fn(snac *snac, char *id)
  604. /* gets the filename for the history */
  605. {
  606. return xs_fmt("%s/history/%s", snac->basedir, id);
  607. }
  608. double history_mtime(snac *snac, char * id)
  609. {
  610. double t = 0.0;
  611. xs *fn = _history_fn(snac, id);
  612. if (fn != NULL)
  613. t = mtime(fn);
  614. return t;
  615. }
  616. void history_add(snac *snac, char *id, char *content, int size)
  617. /* adds something to the history */
  618. {
  619. xs *fn = _history_fn(snac, id);
  620. FILE *f;
  621. if ((f = fopen(fn, "w")) != NULL) {
  622. fwrite(content, size, 1, f);
  623. fclose(f);
  624. }
  625. }
  626. d_char *history_get(snac *snac, char *id)
  627. {
  628. d_char *content = NULL;
  629. xs *fn = _history_fn(snac, id);
  630. FILE *f;
  631. if ((f = fopen(fn, "r")) != NULL) {
  632. content = xs_readall(f);
  633. fclose(f);
  634. }
  635. return content;
  636. }
  637. int history_del(snac *snac, char *id)
  638. {
  639. xs *fn = _history_fn(snac, id);
  640. return unlink(fn);
  641. }
  642. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  643. /* enqueues an input message */
  644. {
  645. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  646. xs *ntid = tid(retries * 60 * qrt);
  647. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  648. xs *tfn = xs_fmt("%s.tmp", fn);
  649. FILE *f;
  650. if ((f = fopen(tfn, "w")) != NULL) {
  651. xs *qmsg = xs_dict_new();
  652. xs *rn = xs_number_new(retries);
  653. xs *j;
  654. qmsg = xs_dict_append(qmsg, "type", "input");
  655. qmsg = xs_dict_append(qmsg, "object", msg);
  656. qmsg = xs_dict_append(qmsg, "req", req);
  657. qmsg = xs_dict_append(qmsg, "retries", rn);
  658. j = xs_json_dumps_pp(qmsg, 4);
  659. fwrite(j, strlen(j), 1, f);
  660. fclose(f);
  661. rename(tfn, fn);
  662. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  663. }
  664. }
  665. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  666. /* enqueues an output message for an actor */
  667. {
  668. if (strcmp(actor, snac->actor) == 0) {
  669. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  670. return;
  671. }
  672. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  673. xs *ntid = tid(retries * 60 * qrt);
  674. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  675. xs *tfn = xs_fmt("%s.tmp", fn);
  676. FILE *f;
  677. if ((f = fopen(tfn, "w")) != NULL) {
  678. xs *qmsg = xs_dict_new();
  679. xs *rn = xs_number_new(retries);
  680. xs *j;
  681. qmsg = xs_dict_append(qmsg, "type", "output");
  682. qmsg = xs_dict_append(qmsg, "actor", actor);
  683. qmsg = xs_dict_append(qmsg, "object", msg);
  684. qmsg = xs_dict_append(qmsg, "retries", rn);
  685. j = xs_json_dumps_pp(qmsg, 4);
  686. fwrite(j, strlen(j), 1, f);
  687. fclose(f);
  688. rename(tfn, fn);
  689. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  690. }
  691. }
  692. d_char *queue(snac *snac)
  693. /* returns a list with filenames that can be dequeued */
  694. {
  695. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  696. d_char *list = xs_list_new();
  697. glob_t globbuf;
  698. time_t t = time(NULL);
  699. if (glob(spec, 0, NULL, &globbuf) == 0) {
  700. int n;
  701. char *p;
  702. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  703. /* get the retry time from the basename */
  704. char *bn = strrchr(p, '/');
  705. time_t t2 = atol(bn + 1);
  706. if (t2 > t)
  707. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  708. else {
  709. list = xs_list_append(list, p);
  710. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  711. }
  712. }
  713. }
  714. globfree(&globbuf);
  715. return list;
  716. }
  717. d_char *dequeue(snac *snac, char *fn)
  718. /* dequeues a message */
  719. {
  720. FILE *f;
  721. d_char *obj = NULL;
  722. if ((f = fopen(fn, "r")) != NULL) {
  723. /* delete right now */
  724. unlink(fn);
  725. xs *j = xs_readall(f);
  726. obj = xs_json_loads(j);
  727. fclose(f);
  728. }
  729. return obj;
  730. }