data.c 29 KB

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