data.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  638. /* enqueues an input message */
  639. {
  640. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  641. xs *ntid = tid(retries * 60 * qrt);
  642. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  643. xs *tfn = xs_fmt("%s.tmp", fn);
  644. FILE *f;
  645. if ((f = fopen(tfn, "w")) != NULL) {
  646. xs *qmsg = xs_dict_new();
  647. xs *rn = xs_number_new(retries);
  648. xs *j;
  649. qmsg = xs_dict_append(qmsg, "type", "input");
  650. qmsg = xs_dict_append(qmsg, "object", msg);
  651. qmsg = xs_dict_append(qmsg, "req", req);
  652. qmsg = xs_dict_append(qmsg, "retries", rn);
  653. j = xs_json_dumps_pp(qmsg, 4);
  654. fwrite(j, strlen(j), 1, f);
  655. fclose(f);
  656. rename(tfn, fn);
  657. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  658. }
  659. }
  660. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  661. /* enqueues an output message for an actor */
  662. {
  663. if (strcmp(actor, snac->actor) == 0) {
  664. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  665. return;
  666. }
  667. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  668. xs *ntid = tid(retries * 60 * qrt);
  669. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  670. xs *tfn = xs_fmt("%s.tmp", fn);
  671. FILE *f;
  672. if ((f = fopen(tfn, "w")) != NULL) {
  673. xs *qmsg = xs_dict_new();
  674. xs *rn = xs_number_new(retries);
  675. xs *j;
  676. qmsg = xs_dict_append(qmsg, "type", "output");
  677. qmsg = xs_dict_append(qmsg, "actor", actor);
  678. qmsg = xs_dict_append(qmsg, "object", msg);
  679. qmsg = xs_dict_append(qmsg, "retries", rn);
  680. j = xs_json_dumps_pp(qmsg, 4);
  681. fwrite(j, strlen(j), 1, f);
  682. fclose(f);
  683. rename(tfn, fn);
  684. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  685. }
  686. }
  687. d_char *queue(snac *snac)
  688. /* returns a list with filenames that can be dequeued */
  689. {
  690. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  691. d_char *list = xs_list_new();
  692. glob_t globbuf;
  693. time_t t = time(NULL);
  694. if (glob(spec, 0, NULL, &globbuf) == 0) {
  695. int n;
  696. char *p;
  697. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  698. /* get the retry time from the basename */
  699. char *bn = strrchr(p, '/');
  700. time_t t2 = atol(bn + 1);
  701. if (t2 > t)
  702. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  703. else {
  704. list = xs_list_append(list, p);
  705. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  706. }
  707. }
  708. }
  709. globfree(&globbuf);
  710. return list;
  711. }
  712. d_char *dequeue(snac *snac, char *fn)
  713. /* dequeues a message */
  714. {
  715. FILE *f;
  716. d_char *obj = NULL;
  717. if ((f = fopen(fn, "r")) != NULL) {
  718. /* delete right now */
  719. unlink(fn);
  720. xs *j = xs_readall(f);
  721. obj = xs_json_loads(j);
  722. fclose(f);
  723. }
  724. return obj;
  725. }