data.c 24 KB

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