data.c 24 KB

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