data.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. int following_get(snac *snac, char *actor, d_char **data)
  499. /* returns the 'Follow' object */
  500. {
  501. xs *fn = _following_fn(snac, actor);
  502. FILE *f;
  503. int status = 200;
  504. if ((f = fopen(fn, "r")) != NULL) {
  505. xs *j = xs_readall(f);
  506. fclose(f);
  507. *data = xs_json_loads(j);
  508. }
  509. else
  510. status = 404;
  511. return status;
  512. }
  513. d_char *_muted_fn(snac *snac, char *actor)
  514. {
  515. xs *md5 = xs_md5_hex(actor, strlen(actor));
  516. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  517. }
  518. void mute(snac *snac, char *actor)
  519. /* mutes a moron */
  520. {
  521. xs *fn = _muted_fn(snac, actor);
  522. FILE *f;
  523. if ((f = fopen(fn, "w")) != NULL) {
  524. fprintf(f, "%s\n", actor);
  525. fclose(f);
  526. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  527. }
  528. }
  529. void unmute(snac *snac, char *actor)
  530. /* actor is no longer a moron */
  531. {
  532. xs *fn = _muted_fn(snac, actor);
  533. unlink(fn);
  534. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  535. }
  536. int is_muted(snac *snac, char *actor)
  537. /* check if someone is muted */
  538. {
  539. xs *fn = _muted_fn(snac, actor);
  540. return !!(mtime(fn) != 0.0);
  541. }
  542. d_char *_actor_fn(snac *snac, char *actor)
  543. /* returns the file name for an actor */
  544. {
  545. xs *md5 = xs_md5_hex(actor, strlen(actor));
  546. return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
  547. }
  548. int actor_add(snac *snac, char *actor, char *msg)
  549. /* adds an actor */
  550. {
  551. int ret = 201; /* created */
  552. xs *fn = _actor_fn(snac, actor);
  553. FILE *f;
  554. if ((f = fopen(fn, "w")) != NULL) {
  555. xs *j = xs_json_dumps_pp(msg, 4);
  556. fwrite(j, 1, strlen(j), f);
  557. fclose(f);
  558. }
  559. else
  560. ret = 500;
  561. snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn));
  562. return ret;
  563. }
  564. int actor_get(snac *snac, char *actor, d_char **data)
  565. /* returns an already downloaded actor */
  566. {
  567. xs *fn = _actor_fn(snac, actor);
  568. double t;
  569. double max_time;
  570. int status;
  571. FILE *f;
  572. t = mtime(fn);
  573. /* no mtime? there is nothing here */
  574. if (t == 0.0)
  575. return 404;
  576. /* maximum time for the actor data to be considered stale */
  577. max_time = 3600.0 * 36.0;
  578. if (t + max_time < (double) time(NULL)) {
  579. /* actor data exists but also stinks */
  580. if ((f = fopen(fn, "a")) != NULL) {
  581. /* write a blank at the end to 'touch' the file */
  582. fwrite(" ", 1, 1, f);
  583. fclose(f);
  584. }
  585. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  586. }
  587. else {
  588. /* it's still valid */
  589. status = 200;
  590. }
  591. if ((f = fopen(fn, "r")) != NULL) {
  592. xs *j = xs_readall(f);
  593. fclose(f);
  594. if (data)
  595. *data = xs_json_loads(j);
  596. }
  597. else
  598. status = 500;
  599. return status;
  600. }
  601. d_char *_static_fn(snac *snac, char *id)
  602. /* gets the filename for a static file */
  603. {
  604. return xs_fmt("%s/static/%s", snac->basedir, id);
  605. }
  606. int static_get(snac *snac, char *id, d_char **data, int *size)
  607. /* returns static content */
  608. {
  609. xs *fn = _static_fn(snac, id);
  610. FILE *f;
  611. int status = 404;
  612. *size = 0xfffffff;
  613. if ((f = fopen(fn, "rb")) != NULL) {
  614. *data = xs_read(f, size);
  615. status = 200;
  616. }
  617. return status;
  618. }
  619. d_char *_history_fn(snac *snac, char *id)
  620. /* gets the filename for the history */
  621. {
  622. return xs_fmt("%s/history/%s", snac->basedir, id);
  623. }
  624. double history_mtime(snac *snac, char * id)
  625. {
  626. double t = 0.0;
  627. xs *fn = _history_fn(snac, id);
  628. if (fn != NULL)
  629. t = mtime(fn);
  630. return t;
  631. }
  632. void history_add(snac *snac, char *id, char *content, int size)
  633. /* adds something to the history */
  634. {
  635. xs *fn = _history_fn(snac, id);
  636. FILE *f;
  637. if ((f = fopen(fn, "w")) != NULL) {
  638. fwrite(content, size, 1, f);
  639. fclose(f);
  640. }
  641. }
  642. d_char *history_get(snac *snac, char *id)
  643. {
  644. d_char *content = NULL;
  645. xs *fn = _history_fn(snac, id);
  646. FILE *f;
  647. if ((f = fopen(fn, "r")) != NULL) {
  648. content = xs_readall(f);
  649. fclose(f);
  650. }
  651. return content;
  652. }
  653. int history_del(snac *snac, char *id)
  654. {
  655. xs *fn = _history_fn(snac, id);
  656. return unlink(fn);
  657. }
  658. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  659. /* enqueues an input message */
  660. {
  661. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  662. xs *ntid = tid(retries * 60 * qrt);
  663. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  664. xs *tfn = xs_fmt("%s.tmp", fn);
  665. FILE *f;
  666. if ((f = fopen(tfn, "w")) != NULL) {
  667. xs *qmsg = xs_dict_new();
  668. xs *rn = xs_number_new(retries);
  669. xs *j;
  670. qmsg = xs_dict_append(qmsg, "type", "input");
  671. qmsg = xs_dict_append(qmsg, "object", msg);
  672. qmsg = xs_dict_append(qmsg, "req", req);
  673. qmsg = xs_dict_append(qmsg, "retries", rn);
  674. j = xs_json_dumps_pp(qmsg, 4);
  675. fwrite(j, strlen(j), 1, f);
  676. fclose(f);
  677. rename(tfn, fn);
  678. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  679. }
  680. }
  681. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  682. /* enqueues an output message for an actor */
  683. {
  684. if (strcmp(actor, snac->actor) == 0) {
  685. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  686. return;
  687. }
  688. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  689. xs *ntid = tid(retries * 60 * qrt);
  690. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  691. xs *tfn = xs_fmt("%s.tmp", fn);
  692. FILE *f;
  693. if ((f = fopen(tfn, "w")) != NULL) {
  694. xs *qmsg = xs_dict_new();
  695. xs *rn = xs_number_new(retries);
  696. xs *j;
  697. qmsg = xs_dict_append(qmsg, "type", "output");
  698. qmsg = xs_dict_append(qmsg, "actor", actor);
  699. qmsg = xs_dict_append(qmsg, "object", msg);
  700. qmsg = xs_dict_append(qmsg, "retries", rn);
  701. j = xs_json_dumps_pp(qmsg, 4);
  702. fwrite(j, strlen(j), 1, f);
  703. fclose(f);
  704. rename(tfn, fn);
  705. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  706. }
  707. }
  708. d_char *queue(snac *snac)
  709. /* returns a list with filenames that can be dequeued */
  710. {
  711. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  712. d_char *list = xs_list_new();
  713. glob_t globbuf;
  714. time_t t = time(NULL);
  715. if (glob(spec, 0, NULL, &globbuf) == 0) {
  716. int n;
  717. char *p;
  718. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  719. /* get the retry time from the basename */
  720. char *bn = strrchr(p, '/');
  721. time_t t2 = atol(bn + 1);
  722. if (t2 > t)
  723. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  724. else {
  725. list = xs_list_append(list, p);
  726. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  727. }
  728. }
  729. }
  730. globfree(&globbuf);
  731. return list;
  732. }
  733. d_char *dequeue(snac *snac, char *fn)
  734. /* dequeues a message */
  735. {
  736. FILE *f;
  737. d_char *obj = NULL;
  738. if ((f = fopen(fn, "r")) != NULL) {
  739. /* delete right now */
  740. unlink(fn);
  741. xs *j = xs_readall(f);
  742. obj = xs_json_loads(j);
  743. fclose(f);
  744. }
  745. return obj;
  746. }