data.c 28 KB

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