data.c 27 KB

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