data.c 27 KB

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