data.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. }
  724. void purge(snac *snac)
  725. /* do the purge */
  726. {
  727. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  728. time_t mt = time(NULL) - tpd * 24 * 3600;
  729. char *p, *v;
  730. xs *t_spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
  731. xs *t_list = xs_glob(t_spec, 0, 0);
  732. p = t_list;
  733. while (xs_list_iter(&p, &v)) {
  734. if (mtime(v) < mt) {
  735. /* older than the minimum time: delete it */
  736. unlink(v);
  737. snac_debug(snac, 1, xs_fmt("purged %s", v));
  738. }
  739. }
  740. xs *a_spec = xs_fmt("%s/actors/" "*.json", snac->basedir);
  741. xs *a_list = xs_glob(a_spec, 0, 0);
  742. p = a_list;
  743. while (xs_list_iter(&p, &v)) {
  744. if (mtime(v) < mt) {
  745. /* older than the minimum time: delete it */
  746. unlink(v);
  747. snac_debug(snac, 1, xs_fmt("purged %s", v));
  748. }
  749. }
  750. }