data.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. int _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. xs *pfn = NULL;
  315. xs *p_msg = NULL;
  316. FILE *f;
  317. if (!xs_is_null(parent)) {
  318. /* get the parent */
  319. pfn = _timeline_find_fn(snac, parent);
  320. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  321. xs *j;
  322. char *v;
  323. j = xs_readall(f);
  324. fclose(f);
  325. p_msg = xs_json_loads(j);
  326. if ((v = xs_dict_get(p_msg, "_snac")) != NULL) {
  327. /* is parent hidden? */
  328. if ((v = xs_dict_get(v, "hidden")) && xs_type(v) == XSTYPE_TRUE) {
  329. snac_debug(snac, 1,
  330. xs_fmt("_timeline_write dropping due to hidden parent %s (%s)", id, parent));
  331. return 0;
  332. }
  333. }
  334. }
  335. }
  336. /* write the message */
  337. if ((f = fopen(fn, "w")) != NULL) {
  338. xs *j = xs_json_dumps_pp(msg, 4);
  339. fwrite(j, strlen(j), 1, f);
  340. fclose(f);
  341. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  342. }
  343. /* related to this user? link to local timeline */
  344. if (xs_startswith(id, snac->actor) ||
  345. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  346. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  347. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  348. link(fn, lfn);
  349. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  350. }
  351. if (p_msg != NULL) {
  352. /* update the parent, adding this id to its children list */
  353. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  354. xs *children = xs_dup(xs_dict_get(meta, "children"));
  355. /* add the child if it's not already there */
  356. if (xs_list_in(children, id) == -1)
  357. children = xs_list_append(children, id);
  358. /* re-store */
  359. meta = xs_dict_set(meta, "children", children);
  360. p_msg = xs_dict_set(p_msg, "_snac", meta);
  361. xs *nfn = _timeline_new_fn(snac, parent);
  362. if ((f = fopen(nfn, "w")) != NULL) {
  363. xs *j = xs_json_dumps_pp(p_msg, 4);
  364. fwrite(j, strlen(j), 1, f);
  365. fclose(f);
  366. unlink(pfn);
  367. snac_debug(snac, 1,
  368. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  369. /* try to do the same with the local */
  370. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  371. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  372. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  373. link(nfn, nlfn);
  374. snac_debug(snac, 1,
  375. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  376. }
  377. }
  378. else
  379. return 0;
  380. /* now iterate all parents up, just renaming the files */
  381. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  382. int max_levels = 10;
  383. while (!xs_is_null(grampa)) {
  384. xs *gofn = _timeline_find_fn(snac, grampa);
  385. if (gofn == NULL)
  386. break;
  387. /* create the new filename */
  388. xs *gnfn = _timeline_new_fn(snac, grampa);
  389. rename(gofn, gnfn);
  390. snac_debug(snac, 1,
  391. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  392. /* try to do the same with the local */
  393. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  394. if (unlink(golfn) != -1) {
  395. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  396. link(gnfn, gnlfn);
  397. snac_debug(snac, 1,
  398. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  399. }
  400. /* now open it and get its own parent */
  401. if ((f = fopen(gnfn, "r")) != NULL) {
  402. xs *j = xs_readall(f);
  403. fclose(f);
  404. xs *g_msg = xs_json_loads(j);
  405. char *meta = xs_dict_get(g_msg, "_snac");
  406. char *p = xs_dict_get(meta, "parent");
  407. xs_free(grampa);
  408. grampa = xs_dup(p);
  409. }
  410. else
  411. break;
  412. if (--max_levels == 0) {
  413. snac_log(snac, xs_dup("_timeline_write maximum grampa levels reached"));
  414. break;
  415. }
  416. }
  417. }
  418. return 1;
  419. }
  420. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  421. /* adds a message to the timeline */
  422. {
  423. xs *pfn = _timeline_find_fn(snac, id);
  424. int ret = 0;
  425. if (pfn != NULL) {
  426. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  427. return 0;
  428. }
  429. xs *msg = xs_dup(o_msg);
  430. xs *md;
  431. /* add new metadata */
  432. md = xs_json_loads("{"
  433. "\"children\": [],"
  434. "\"liked_by\": [],"
  435. "\"announced_by\": [],"
  436. "\"version\": \"" USER_AGENT "\","
  437. "\"referrer\": null,"
  438. "\"parent\": null"
  439. "}");
  440. if (!xs_is_null(parent))
  441. md = xs_dict_set(md, "parent", parent);
  442. if (!xs_is_null(referrer))
  443. md = xs_dict_set(md, "referrer", referrer);
  444. msg = xs_dict_set(msg, "_snac", md);
  445. if ((ret = _timeline_write(snac, id, msg, parent, referrer)))
  446. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  447. return ret;
  448. }
  449. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  450. /* updates a timeline entry with a new admiration */
  451. {
  452. xs *ofn = _timeline_find_fn(snac, id);
  453. FILE *f;
  454. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  455. xs *j1 = xs_readall(f);
  456. fclose(f);
  457. xs *msg = xs_json_loads(j1);
  458. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  459. xs *list;
  460. if (like)
  461. list = xs_dup(xs_dict_get(meta, "liked_by"));
  462. else
  463. list = xs_dup(xs_dict_get(meta, "announced_by"));
  464. /* add the admirer if it's not already there */
  465. if (xs_list_in(list, admirer) == -1)
  466. list = xs_list_append(list, admirer);
  467. /* set the admirer as the referrer (if not already set or it's us) */
  468. if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
  469. strcmp(admirer, snac->actor) == 0))
  470. meta = xs_dict_set(meta, "referrer", admirer);
  471. /* re-store */
  472. if (like)
  473. meta = xs_dict_set(meta, "liked_by", list);
  474. else
  475. meta = xs_dict_set(meta, "announced_by", list);
  476. msg = xs_dict_set(msg, "_snac", meta);
  477. unlink(ofn);
  478. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  479. unlink(ofn);
  480. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  481. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  482. like ? "Like" : "Announce", id, admirer));
  483. }
  484. else
  485. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  486. }
  487. int timeline_hide(snac *snac, char *id, int hide)
  488. /* hides/unhides a timeline entry */
  489. {
  490. int ret = 0;
  491. xs *fn = _timeline_find_fn(snac, id);
  492. FILE *f;
  493. if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
  494. xs *s1 = xs_readall(f);
  495. xs *msg = xs_json_loads(s1);
  496. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  497. xs *hdn = xs_val_new(hide ? XSTYPE_TRUE : XSTYPE_FALSE);
  498. char *p, *v;
  499. fclose(f);
  500. /* if it's already in this hidden state, we're done */
  501. if ((v = xs_dict_get(meta, "hidden")) && xs_type(v) == xs_type(hdn))
  502. return ret;
  503. meta = xs_dict_set(meta, "hidden", hdn);
  504. msg = xs_dict_set(msg, "_snac", meta);
  505. if ((f = fopen(fn, "w")) != NULL) {
  506. xs *j1 = xs_json_dumps_pp(msg, 4);
  507. fwrite(j1, strlen(j1), 1, f);
  508. fclose(f);
  509. snac_debug(snac, 1, xs_fmt("timeline_hide %d %s", hide, id));
  510. /* now hide the children */
  511. p = xs_dict_get(meta, "children");
  512. while (xs_list_iter(&p, &v))
  513. timeline_hide(snac, v, hide);
  514. ret = 1;
  515. }
  516. }
  517. return ret;
  518. }
  519. d_char *_following_fn(snac *snac, char *actor)
  520. {
  521. xs *md5 = xs_md5_hex(actor, strlen(actor));
  522. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  523. }
  524. int following_add(snac *snac, char *actor, char *msg)
  525. /* adds to the following list */
  526. {
  527. int ret = 201; /* created */
  528. xs *fn = _following_fn(snac, actor);
  529. FILE *f;
  530. if ((f = fopen(fn, "w")) != NULL) {
  531. xs *j = xs_json_dumps_pp(msg, 4);
  532. fwrite(j, 1, strlen(j), f);
  533. fclose(f);
  534. }
  535. else
  536. ret = 500;
  537. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  538. return ret;
  539. }
  540. int following_del(snac *snac, char *actor)
  541. /* someone is no longer following us */
  542. {
  543. xs *fn = _following_fn(snac, actor);
  544. unlink(fn);
  545. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  546. return 200;
  547. }
  548. int following_check(snac *snac, char *actor)
  549. /* checks if someone is following us */
  550. {
  551. xs *fn = _following_fn(snac, actor);
  552. return !!(mtime(fn) != 0.0);
  553. }
  554. int following_get(snac *snac, char *actor, d_char **data)
  555. /* returns the 'Follow' object */
  556. {
  557. xs *fn = _following_fn(snac, actor);
  558. FILE *f;
  559. int status = 200;
  560. if ((f = fopen(fn, "r")) != NULL) {
  561. xs *j = xs_readall(f);
  562. fclose(f);
  563. *data = xs_json_loads(j);
  564. }
  565. else
  566. status = 404;
  567. return status;
  568. }
  569. d_char *following_list(snac *snac)
  570. /* returns the list of people being followed */
  571. {
  572. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  573. xs *glist = xs_glob(spec, 0, 0);
  574. char *p, *v;
  575. d_char *list = xs_list_new();
  576. /* iterate the list of files */
  577. p = glist;
  578. while (xs_list_iter(&p, &v)) {
  579. FILE *f;
  580. /* load the follower data */
  581. if ((f = fopen(v, "r")) != NULL) {
  582. xs *j = xs_readall(f);
  583. fclose(f);
  584. if (j != NULL) {
  585. xs *o = xs_json_loads(j);
  586. if (o != NULL) {
  587. char *type = xs_dict_get(o, "type");
  588. if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
  589. list = xs_list_append(list, o);
  590. }
  591. }
  592. }
  593. }
  594. return list;
  595. }
  596. d_char *_muted_fn(snac *snac, char *actor)
  597. {
  598. xs *md5 = xs_md5_hex(actor, strlen(actor));
  599. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  600. }
  601. void mute(snac *snac, char *actor)
  602. /* mutes a moron */
  603. {
  604. xs *fn = _muted_fn(snac, actor);
  605. FILE *f;
  606. if ((f = fopen(fn, "w")) != NULL) {
  607. fprintf(f, "%s\n", actor);
  608. fclose(f);
  609. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  610. }
  611. }
  612. void unmute(snac *snac, char *actor)
  613. /* actor is no longer a moron */
  614. {
  615. xs *fn = _muted_fn(snac, actor);
  616. unlink(fn);
  617. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  618. }
  619. int is_muted(snac *snac, char *actor)
  620. /* check if someone is muted */
  621. {
  622. xs *fn = _muted_fn(snac, actor);
  623. return !!(mtime(fn) != 0.0);
  624. }
  625. d_char *_actor_fn(snac *snac, char *actor)
  626. /* returns the file name for an actor */
  627. {
  628. xs *md5 = xs_md5_hex(actor, strlen(actor));
  629. return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
  630. }
  631. int actor_add(snac *snac, char *actor, char *msg)
  632. /* adds an actor */
  633. {
  634. int ret = 201; /* created */
  635. xs *fn = _actor_fn(snac, actor);
  636. FILE *f;
  637. if ((f = fopen(fn, "w")) != NULL) {
  638. xs *j = xs_json_dumps_pp(msg, 4);
  639. fwrite(j, 1, strlen(j), f);
  640. fclose(f);
  641. }
  642. else
  643. ret = 500;
  644. snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn));
  645. return ret;
  646. }
  647. int actor_get(snac *snac, char *actor, d_char **data)
  648. /* returns an already downloaded actor */
  649. {
  650. xs *fn = _actor_fn(snac, actor);
  651. double t;
  652. double max_time;
  653. int status;
  654. FILE *f;
  655. if (strcmp(actor, snac->actor) == 0) {
  656. if (data)
  657. *data = msg_actor(snac);
  658. return 200;
  659. }
  660. t = mtime(fn);
  661. /* no mtime? there is nothing here */
  662. if (t == 0.0)
  663. return 404;
  664. /* maximum time for the actor data to be considered stale */
  665. max_time = 3600.0 * 36.0;
  666. if (t + max_time < (double) time(NULL)) {
  667. /* actor data exists but also stinks */
  668. if ((f = fopen(fn, "a")) != NULL) {
  669. /* write a blank at the end to 'touch' the file */
  670. fwrite(" ", 1, 1, f);
  671. fclose(f);
  672. }
  673. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  674. }
  675. else {
  676. /* it's still valid */
  677. status = 200;
  678. }
  679. if (data) {
  680. if ((f = fopen(fn, "r")) != NULL) {
  681. xs *j = xs_readall(f);
  682. fclose(f);
  683. *data = xs_json_loads(j);
  684. }
  685. else
  686. status = 500;
  687. }
  688. return status;
  689. }
  690. d_char *_static_fn(snac *snac, const char *id)
  691. /* gets the filename for a static file */
  692. {
  693. return xs_fmt("%s/static/%s", snac->basedir, id);
  694. }
  695. int static_get(snac *snac, const char *id, d_char **data, int *size)
  696. /* returns static content */
  697. {
  698. xs *fn = _static_fn(snac, id);
  699. FILE *f;
  700. int status = 404;
  701. *size = 0xfffffff;
  702. if ((f = fopen(fn, "rb")) != NULL) {
  703. *data = xs_read(f, size);
  704. fclose(f);
  705. status = 200;
  706. }
  707. return status;
  708. }
  709. void static_put(snac *snac, const char *id, const char *data, int size)
  710. /* writes status content */
  711. {
  712. xs *fn = _static_fn(snac, id);
  713. FILE *f;
  714. if ((f = fopen(fn, "wb")) != NULL) {
  715. fwrite(data, size, 1, f);
  716. fclose(f);
  717. }
  718. }
  719. d_char *_history_fn(snac *snac, char *id)
  720. /* gets the filename for the history */
  721. {
  722. return xs_fmt("%s/history/%s", snac->basedir, id);
  723. }
  724. double history_mtime(snac *snac, char * id)
  725. {
  726. double t = 0.0;
  727. xs *fn = _history_fn(snac, id);
  728. if (fn != NULL)
  729. t = mtime(fn);
  730. return t;
  731. }
  732. void history_add(snac *snac, char *id, char *content, int size)
  733. /* adds something to the history */
  734. {
  735. xs *fn = _history_fn(snac, id);
  736. FILE *f;
  737. if ((f = fopen(fn, "w")) != NULL) {
  738. fwrite(content, size, 1, f);
  739. fclose(f);
  740. }
  741. }
  742. d_char *history_get(snac *snac, char *id)
  743. {
  744. d_char *content = NULL;
  745. xs *fn = _history_fn(snac, id);
  746. FILE *f;
  747. if ((f = fopen(fn, "r")) != NULL) {
  748. content = xs_readall(f);
  749. fclose(f);
  750. }
  751. return content;
  752. }
  753. int history_del(snac *snac, char *id)
  754. {
  755. xs *fn = _history_fn(snac, id);
  756. return unlink(fn);
  757. }
  758. d_char *history_list(snac *snac)
  759. {
  760. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  761. return xs_glob(spec, 1, 0);
  762. }
  763. static int _enqueue_put(char *fn, char *msg)
  764. /* writes safely to the queue */
  765. {
  766. int ret = 1;
  767. xs *tfn = xs_fmt("%s.tmp", fn);
  768. FILE *f;
  769. if ((f = fopen(tfn, "w")) != NULL) {
  770. xs *j = xs_json_dumps_pp(msg, 4);
  771. fwrite(j, strlen(j), 1, f);
  772. fclose(f);
  773. rename(tfn, fn);
  774. }
  775. else
  776. ret = 0;
  777. return ret;
  778. }
  779. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  780. /* enqueues an input message */
  781. {
  782. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  783. xs *ntid = tid(retries * 60 * qrt);
  784. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  785. xs *qmsg = xs_dict_new();
  786. xs *rn = xs_number_new(retries);
  787. qmsg = xs_dict_append(qmsg, "type", "input");
  788. qmsg = xs_dict_append(qmsg, "object", msg);
  789. qmsg = xs_dict_append(qmsg, "req", req);
  790. qmsg = xs_dict_append(qmsg, "retries", rn);
  791. _enqueue_put(fn, qmsg);
  792. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  793. }
  794. void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
  795. /* enqueues an output message to an inbox */
  796. {
  797. if (xs_startswith(inbox, snac->actor)) {
  798. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  799. return;
  800. }
  801. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  802. xs *ntid = tid(retries * 60 * qrt);
  803. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  804. xs *qmsg = xs_dict_new();
  805. xs *rn = xs_number_new(retries);
  806. qmsg = xs_dict_append(qmsg, "type", "output");
  807. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  808. qmsg = xs_dict_append(qmsg, "object", msg);
  809. qmsg = xs_dict_append(qmsg, "retries", rn);
  810. _enqueue_put(fn, qmsg);
  811. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  812. }
  813. void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
  814. /* enqueues an output message for an actor */
  815. {
  816. xs *inbox = get_actor_inbox(snac, actor);
  817. if (!xs_is_null(inbox))
  818. enqueue_output(snac, msg, inbox, retries);
  819. else
  820. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  821. }
  822. void enqueue_email(snac *snac, char *msg, int retries)
  823. /* enqueues an email message to be sent */
  824. {
  825. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  826. xs *ntid = tid(retries * 60 * qrt);
  827. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  828. xs *qmsg = xs_dict_new();
  829. xs *rn = xs_number_new(retries);
  830. qmsg = xs_dict_append(qmsg, "type", "email");
  831. qmsg = xs_dict_append(qmsg, "message", msg);
  832. qmsg = xs_dict_append(qmsg, "retries", rn);
  833. _enqueue_put(fn, qmsg);
  834. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  835. }
  836. d_char *queue(snac *snac)
  837. /* returns a list with filenames that can be dequeued */
  838. {
  839. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  840. d_char *list = xs_list_new();
  841. time_t t = time(NULL);
  842. char *p, *v;
  843. xs *fns = xs_glob(spec, 0, 0);
  844. p = fns;
  845. while (xs_list_iter(&p, &v)) {
  846. /* get the retry time from the basename */
  847. char *bn = strrchr(v, '/');
  848. time_t t2 = atol(bn + 1);
  849. if (t2 > t)
  850. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  851. else {
  852. list = xs_list_append(list, v);
  853. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  854. }
  855. }
  856. return list;
  857. }
  858. d_char *dequeue(snac *snac, char *fn)
  859. /* dequeues a message */
  860. {
  861. FILE *f;
  862. d_char *obj = NULL;
  863. if ((f = fopen(fn, "r")) != NULL) {
  864. /* delete right now */
  865. unlink(fn);
  866. xs *j = xs_readall(f);
  867. obj = xs_json_loads(j);
  868. fclose(f);
  869. }
  870. return obj;
  871. }
  872. static void _purge_subdir(snac *snac, const char *subdir, int days)
  873. /* purges all files in subdir older than days */
  874. {
  875. if (days) {
  876. time_t mt = time(NULL) - days * 24 * 3600;
  877. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, subdir);
  878. xs *list = xs_glob(spec, 0, 0);
  879. char *p, *v;
  880. p = list;
  881. while (xs_list_iter(&p, &v)) {
  882. if (mtime(v) < mt) {
  883. /* older than the minimum time: delete it */
  884. unlink(v);
  885. snac_debug(snac, 1, xs_fmt("purged %s", v));
  886. }
  887. }
  888. }
  889. }
  890. void purge(snac *snac)
  891. /* do the purge */
  892. {
  893. int days;
  894. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  895. _purge_subdir(snac, "timeline", days);
  896. _purge_subdir(snac, "actors", days);
  897. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  898. _purge_subdir(snac, "local", days);
  899. }
  900. void purge_all(void)
  901. /* purge all users */
  902. {
  903. snac snac;
  904. xs *list = user_list();
  905. char *p, *uid;
  906. p = list;
  907. while (xs_list_iter(&p, &uid)) {
  908. if (user_open(&snac, uid)) {
  909. purge(&snac);
  910. user_free(&snac);
  911. }
  912. }
  913. }