data.c 24 KB

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