data.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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_debug(snac, 1, 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 (if not already set) */
  428. if (!like && xs_is_null(xs_dict_get(meta, "referrer")))
  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"), like ? NULL : admirer);
  440. snac_debug(snac, 1, 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, const 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, const 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. void static_put(snac *snac, const char *id, const char *data, int size)
  603. /* writes status content */
  604. {
  605. xs *fn = _static_fn(snac, id);
  606. FILE *f;
  607. if ((f = fopen(fn, "wb")) != NULL) {
  608. fwrite(data, size, 1, f);
  609. fclose(f);
  610. }
  611. }
  612. d_char *_history_fn(snac *snac, char *id)
  613. /* gets the filename for the history */
  614. {
  615. return xs_fmt("%s/history/%s", snac->basedir, id);
  616. }
  617. double history_mtime(snac *snac, char * id)
  618. {
  619. double t = 0.0;
  620. xs *fn = _history_fn(snac, id);
  621. if (fn != NULL)
  622. t = mtime(fn);
  623. return t;
  624. }
  625. void history_add(snac *snac, char *id, char *content, int size)
  626. /* adds something to the history */
  627. {
  628. xs *fn = _history_fn(snac, id);
  629. FILE *f;
  630. if ((f = fopen(fn, "w")) != NULL) {
  631. fwrite(content, size, 1, f);
  632. fclose(f);
  633. }
  634. }
  635. d_char *history_get(snac *snac, char *id)
  636. {
  637. d_char *content = NULL;
  638. xs *fn = _history_fn(snac, id);
  639. FILE *f;
  640. if ((f = fopen(fn, "r")) != NULL) {
  641. content = xs_readall(f);
  642. fclose(f);
  643. }
  644. return content;
  645. }
  646. int history_del(snac *snac, char *id)
  647. {
  648. xs *fn = _history_fn(snac, id);
  649. return unlink(fn);
  650. }
  651. d_char *history_list(snac *snac)
  652. {
  653. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  654. return xs_glob(spec, 1, 0);
  655. }
  656. static int _enqueue_put(char *fn, char *msg)
  657. /* writes safely to the queue */
  658. {
  659. int ret = 1;
  660. xs *tfn = xs_fmt("%s.tmp", fn);
  661. FILE *f;
  662. if ((f = fopen(tfn, "w")) != NULL) {
  663. xs *j = xs_json_dumps_pp(msg, 4);
  664. fwrite(j, strlen(j), 1, f);
  665. fclose(f);
  666. rename(tfn, fn);
  667. }
  668. else
  669. ret = 0;
  670. return ret;
  671. }
  672. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  673. /* enqueues an input message */
  674. {
  675. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  676. xs *ntid = tid(retries * 60 * qrt);
  677. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  678. xs *qmsg = xs_dict_new();
  679. xs *rn = xs_number_new(retries);
  680. qmsg = xs_dict_append(qmsg, "type", "input");
  681. qmsg = xs_dict_append(qmsg, "object", msg);
  682. qmsg = xs_dict_append(qmsg, "req", req);
  683. qmsg = xs_dict_append(qmsg, "retries", rn);
  684. _enqueue_put(fn, qmsg);
  685. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  686. }
  687. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  688. /* enqueues an output message for an actor */
  689. {
  690. if (strcmp(actor, snac->actor) == 0) {
  691. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  692. return;
  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 *qmsg = xs_dict_new();
  698. xs *rn = xs_number_new(retries);
  699. qmsg = xs_dict_append(qmsg, "type", "output");
  700. qmsg = xs_dict_append(qmsg, "actor", actor);
  701. qmsg = xs_dict_append(qmsg, "object", msg);
  702. qmsg = xs_dict_append(qmsg, "retries", rn);
  703. _enqueue_put(fn, qmsg);
  704. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  705. }
  706. d_char *queue(snac *snac)
  707. /* returns a list with filenames that can be dequeued */
  708. {
  709. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  710. d_char *list = xs_list_new();
  711. glob_t globbuf;
  712. time_t t = time(NULL);
  713. if (glob(spec, 0, NULL, &globbuf) == 0) {
  714. int n;
  715. char *p;
  716. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  717. /* get the retry time from the basename */
  718. char *bn = strrchr(p, '/');
  719. time_t t2 = atol(bn + 1);
  720. if (t2 > t)
  721. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  722. else {
  723. list = xs_list_append(list, p);
  724. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  725. }
  726. }
  727. }
  728. globfree(&globbuf);
  729. return list;
  730. }
  731. d_char *dequeue(snac *snac, char *fn)
  732. /* dequeues a message */
  733. {
  734. FILE *f;
  735. d_char *obj = NULL;
  736. if ((f = fopen(fn, "r")) != NULL) {
  737. /* delete right now */
  738. unlink(fn);
  739. xs *j = xs_readall(f);
  740. obj = xs_json_loads(j);
  741. fclose(f);
  742. }
  743. return obj;
  744. }
  745. void purge(snac *snac)
  746. /* do the purge */
  747. {
  748. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  749. time_t mt = time(NULL) - tpd * 24 * 3600;
  750. char *p, *v;
  751. xs *t_spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
  752. xs *t_list = xs_glob(t_spec, 0, 0);
  753. p = t_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. xs *a_spec = xs_fmt("%s/actors/" "*.json", snac->basedir);
  762. xs *a_list = xs_glob(a_spec, 0, 0);
  763. p = a_list;
  764. while (xs_list_iter(&p, &v)) {
  765. if (mtime(v) < mt) {
  766. /* older than the minimum time: delete it */
  767. unlink(v);
  768. snac_debug(snac, 1, xs_fmt("purged %s", v));
  769. }
  770. }
  771. }