data.c 27 KB

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