data.c 24 KB

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