data.c 23 KB

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