data.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 *xs_glob_n(const char *spec, int basename, int reverse, int max)
  110. /* does a globbing and returns the found files */
  111. {
  112. glob_t globbuf;
  113. d_char *list = xs_list_new();
  114. if (glob(spec, 0, NULL, &globbuf) == 0) {
  115. int n;
  116. if (max > globbuf.gl_pathc)
  117. max = globbuf.gl_pathc;
  118. for (n = 0; n < max; n++) {
  119. char *p;
  120. if (reverse)
  121. p = globbuf.gl_pathv[globbuf.gl_pathc - n - 1];
  122. else
  123. p = globbuf.gl_pathv[n];
  124. if (p != NULL) {
  125. if (basename) {
  126. if ((p = strrchr(p, '/')) == NULL)
  127. continue;
  128. p++;
  129. }
  130. list = xs_list_append(list, p);
  131. }
  132. }
  133. }
  134. globfree(&globbuf);
  135. return list;
  136. }
  137. #define xs_glob(spec, basename, reverse) xs_glob_n(spec, basename, reverse, 0xfffffff)
  138. d_char *user_list(void)
  139. /* returns the list of user ids */
  140. {
  141. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  142. return xs_glob(spec, 1, 0);
  143. }
  144. double mtime(char *fn)
  145. /* returns the mtime of a file or directory, or 0.0 */
  146. {
  147. struct stat st;
  148. double r = 0.0;
  149. if (fn && stat(fn, &st) != -1)
  150. r = (double)st.st_mtim.tv_sec;
  151. return r;
  152. }
  153. d_char *_follower_fn(snac *snac, char *actor)
  154. {
  155. xs *md5 = xs_md5_hex(actor, strlen(actor));
  156. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  157. }
  158. int follower_add(snac *snac, char *actor, char *msg)
  159. /* adds a follower */
  160. {
  161. int ret = 201; /* created */
  162. xs *fn = _follower_fn(snac, actor);
  163. FILE *f;
  164. if ((f = fopen(fn, "w")) != NULL) {
  165. xs *j = xs_json_dumps_pp(msg, 4);
  166. fwrite(j, 1, strlen(j), f);
  167. fclose(f);
  168. }
  169. else
  170. ret = 500;
  171. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  172. return ret;
  173. }
  174. int follower_del(snac *snac, char *actor)
  175. /* deletes a follower */
  176. {
  177. int status = 200;
  178. xs *fn = _follower_fn(snac, actor);
  179. if (fn != NULL)
  180. unlink(fn);
  181. else
  182. status = 404;
  183. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  184. return status;
  185. }
  186. int follower_check(snac *snac, char *actor)
  187. /* checks if someone is a follower */
  188. {
  189. xs *fn = _follower_fn(snac, actor);
  190. return !!(mtime(fn) != 0.0);
  191. }
  192. d_char *follower_list(snac *snac)
  193. /* returns the list of followers */
  194. {
  195. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  196. xs *glist = xs_glob(spec, 0, 0);
  197. char *p, *v;
  198. d_char *list = xs_list_new();
  199. /* iterate the list of files */
  200. p = glist;
  201. while (xs_list_iter(&p, &v)) {
  202. FILE *f;
  203. /* load the follower data */
  204. if ((f = fopen(v, "r")) != NULL) {
  205. xs *j = xs_readall(f);
  206. fclose(f);
  207. if (j != NULL) {
  208. xs *o = xs_json_loads(j);
  209. if (o != NULL)
  210. list = xs_list_append(list, o);
  211. }
  212. }
  213. }
  214. return list;
  215. }
  216. double timeline_mtime(snac *snac)
  217. {
  218. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  219. return mtime(fn);
  220. }
  221. d_char *_timeline_find_fn(snac *snac, char *id)
  222. /* returns the file name of a timeline entry by its id */
  223. {
  224. xs *md5 = xs_md5_hex(id, strlen(id));
  225. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  226. xs *list = NULL;
  227. d_char *fn = NULL;
  228. list = xs_glob(spec, 0, 0);
  229. /* if there is something, get the first one */
  230. if (xs_list_len(list) > 0)
  231. fn = xs_str_new(xs_list_get(list, 0));
  232. return fn;
  233. }
  234. int timeline_here(snac *snac, char *id)
  235. /* checks if an object is already downloaded */
  236. {
  237. xs *fn = _timeline_find_fn(snac, id);
  238. return fn != NULL;
  239. }
  240. d_char *timeline_find(snac *snac, char *id)
  241. /* gets a message from the timeline by id */
  242. {
  243. xs *fn = _timeline_find_fn(snac, id);
  244. d_char *msg = NULL;
  245. if (fn != NULL) {
  246. FILE *f;
  247. if ((f = fopen(fn, "r")) != NULL) {
  248. xs *j = xs_readall(f);
  249. msg = xs_json_loads(j);
  250. fclose(f);
  251. }
  252. }
  253. return msg;
  254. }
  255. void timeline_del(snac *snac, char *id)
  256. /* deletes a message from the timeline */
  257. {
  258. xs *fn = _timeline_find_fn(snac, id);
  259. if (fn != NULL) {
  260. xs *lfn = NULL;
  261. unlink(fn);
  262. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  263. /* try to delete also from the local timeline */
  264. lfn = xs_replace(fn, "/timeline/", "/local/");
  265. if (unlink(lfn) != -1)
  266. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  267. }
  268. }
  269. d_char *timeline_get(snac *snac, char *fn)
  270. /* gets a timeline entry by file name */
  271. {
  272. d_char *d = NULL;
  273. FILE *f;
  274. if ((f = fopen(fn, "r")) != NULL) {
  275. xs *j = xs_readall(f);
  276. d = xs_json_loads(j);
  277. fclose(f);
  278. }
  279. return d;
  280. }
  281. d_char *_timeline_list(snac *snac, char *directory, int max)
  282. /* returns a list of the timeline filenames */
  283. {
  284. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  285. int c_max;
  286. /* maximum number of items in the timeline */
  287. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  288. /* never more timeline entries than the configured maximum */
  289. if (max > c_max)
  290. max = c_max;
  291. return xs_glob_n(spec, 0, 1, max);
  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. d_char *history_list(snac *snac)
  659. {
  660. d_char *list;
  661. xs *spec;
  662. glob_t globbuf;
  663. list = xs_list_new();
  664. spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  665. if (glob(spec, 0, NULL, &globbuf) == 0) {
  666. int n;
  667. char *fn;
  668. for (n = 0; (fn = globbuf.gl_pathv[n]) != NULL; n++) {
  669. char *p;
  670. if ((p = strrchr(fn, '/')) != NULL) {
  671. *p++ = '\0';
  672. if (*p != '_')
  673. list = xs_list_append(list, p);
  674. }
  675. }
  676. }
  677. globfree(&globbuf);
  678. return list;
  679. }
  680. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  681. /* enqueues an input message */
  682. {
  683. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  684. xs *ntid = tid(retries * 60 * qrt);
  685. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  686. xs *tfn = xs_fmt("%s.tmp", fn);
  687. FILE *f;
  688. if ((f = fopen(tfn, "w")) != NULL) {
  689. xs *qmsg = xs_dict_new();
  690. xs *rn = xs_number_new(retries);
  691. xs *j;
  692. qmsg = xs_dict_append(qmsg, "type", "input");
  693. qmsg = xs_dict_append(qmsg, "object", msg);
  694. qmsg = xs_dict_append(qmsg, "req", req);
  695. qmsg = xs_dict_append(qmsg, "retries", rn);
  696. j = xs_json_dumps_pp(qmsg, 4);
  697. fwrite(j, strlen(j), 1, f);
  698. fclose(f);
  699. rename(tfn, fn);
  700. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  701. }
  702. }
  703. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  704. /* enqueues an output message for an actor */
  705. {
  706. if (strcmp(actor, snac->actor) == 0) {
  707. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  708. return;
  709. }
  710. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  711. xs *ntid = tid(retries * 60 * qrt);
  712. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  713. xs *tfn = xs_fmt("%s.tmp", fn);
  714. FILE *f;
  715. if ((f = fopen(tfn, "w")) != NULL) {
  716. xs *qmsg = xs_dict_new();
  717. xs *rn = xs_number_new(retries);
  718. xs *j;
  719. qmsg = xs_dict_append(qmsg, "type", "output");
  720. qmsg = xs_dict_append(qmsg, "actor", actor);
  721. qmsg = xs_dict_append(qmsg, "object", msg);
  722. qmsg = xs_dict_append(qmsg, "retries", rn);
  723. j = xs_json_dumps_pp(qmsg, 4);
  724. fwrite(j, strlen(j), 1, f);
  725. fclose(f);
  726. rename(tfn, fn);
  727. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  728. }
  729. }
  730. d_char *queue(snac *snac)
  731. /* returns a list with filenames that can be dequeued */
  732. {
  733. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  734. d_char *list = xs_list_new();
  735. glob_t globbuf;
  736. time_t t = time(NULL);
  737. if (glob(spec, 0, NULL, &globbuf) == 0) {
  738. int n;
  739. char *p;
  740. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  741. /* get the retry time from the basename */
  742. char *bn = strrchr(p, '/');
  743. time_t t2 = atol(bn + 1);
  744. if (t2 > t)
  745. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  746. else {
  747. list = xs_list_append(list, p);
  748. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  749. }
  750. }
  751. }
  752. globfree(&globbuf);
  753. return list;
  754. }
  755. d_char *dequeue(snac *snac, char *fn)
  756. /* dequeues a message */
  757. {
  758. FILE *f;
  759. d_char *obj = NULL;
  760. if ((f = fopen(fn, "r")) != NULL) {
  761. /* delete right now */
  762. unlink(fn);
  763. xs *j = xs_readall(f);
  764. obj = xs_json_loads(j);
  765. fclose(f);
  766. }
  767. return obj;
  768. }