data.c 29 KB

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