data.c 30 KB

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