data.c 29 KB

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