data.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. int list_add_md5(const char *fn, const char *md5)
  212. /* adds an md5 to a list */
  213. {
  214. int status = 200;
  215. FILE *f;
  216. if ((f = fopen(fn, "a")) != NULL) {
  217. flock(fileno(f), LOCK_EX);
  218. fprintf(f, "%s\n", md5);
  219. fclose(f);
  220. }
  221. else
  222. status = 500;
  223. return status;
  224. }
  225. d_char *_follower_fn(snac *snac, char *actor)
  226. {
  227. xs *md5 = xs_md5_hex(actor, strlen(actor));
  228. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  229. }
  230. int follower_add(snac *snac, char *actor, char *msg)
  231. /* adds a follower */
  232. {
  233. int ret = 201; /* created */
  234. xs *fn = _follower_fn(snac, actor);
  235. FILE *f;
  236. if ((f = fopen(fn, "w")) != NULL) {
  237. xs *j = xs_json_dumps_pp(msg, 4);
  238. fwrite(j, 1, strlen(j), f);
  239. fclose(f);
  240. }
  241. else
  242. ret = 500;
  243. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  244. return ret;
  245. }
  246. int follower_del(snac *snac, char *actor)
  247. /* deletes a follower */
  248. {
  249. int status = 200;
  250. xs *fn = _follower_fn(snac, actor);
  251. if (fn != NULL)
  252. unlink(fn);
  253. else
  254. status = 404;
  255. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  256. return status;
  257. }
  258. int follower_check(snac *snac, char *actor)
  259. /* checks if someone is a follower */
  260. {
  261. xs *fn = _follower_fn(snac, actor);
  262. return !!(mtime(fn) != 0.0);
  263. }
  264. d_char *follower_list(snac *snac)
  265. /* returns the list of followers */
  266. {
  267. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  268. xs *glist = xs_glob(spec, 0, 0);
  269. char *p, *v;
  270. d_char *list = xs_list_new();
  271. /* iterate the list of files */
  272. p = glist;
  273. while (xs_list_iter(&p, &v)) {
  274. FILE *f;
  275. /* load the follower data */
  276. if ((f = fopen(v, "r")) != NULL) {
  277. xs *j = xs_readall(f);
  278. fclose(f);
  279. if (j != NULL) {
  280. xs *o = xs_json_loads(j);
  281. if (o != NULL)
  282. list = xs_list_append(list, o);
  283. }
  284. }
  285. }
  286. return list;
  287. }
  288. double timeline_mtime(snac *snac)
  289. {
  290. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  291. return mtime(fn);
  292. }
  293. d_char *_timeline_find_fn(snac *snac, char *id)
  294. /* returns the file name of a timeline entry by its id */
  295. {
  296. xs *md5 = xs_md5_hex(id, strlen(id));
  297. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  298. xs *list = NULL;
  299. d_char *fn = NULL;
  300. int l;
  301. list = xs_glob(spec, 0, 0);
  302. l = xs_list_len(list);
  303. /* if there is something, get the first one */
  304. if (l > 0) {
  305. fn = xs_str_new(xs_list_get(list, 0));
  306. if (l > 1)
  307. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  308. }
  309. return fn;
  310. }
  311. int timeline_here(snac *snac, char *id)
  312. /* checks if an object is already downloaded */
  313. {
  314. xs *fn = _timeline_find_fn(snac, id);
  315. return fn != NULL;
  316. }
  317. d_char *timeline_find(snac *snac, char *id)
  318. /* gets a message from the timeline by id */
  319. {
  320. xs *fn = _timeline_find_fn(snac, id);
  321. d_char *msg = NULL;
  322. if (fn != NULL) {
  323. FILE *f;
  324. if ((f = fopen(fn, "r")) != NULL) {
  325. xs *j = xs_readall(f);
  326. msg = xs_json_loads(j);
  327. fclose(f);
  328. }
  329. }
  330. return msg;
  331. }
  332. int timeline_del(snac *snac, char *id)
  333. /* deletes a message from the timeline */
  334. {
  335. int ret = 404;
  336. xs *fn = _timeline_find_fn(snac, id);
  337. if (fn != NULL) {
  338. xs *lfn = NULL;
  339. unlink(fn);
  340. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  341. /* try to delete also from the local timeline */
  342. lfn = xs_replace(fn, "/timeline/", "/local/");
  343. if (unlink(lfn) != -1)
  344. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  345. ret = 200;
  346. }
  347. return ret;
  348. }
  349. d_char *timeline_get(snac *snac, char *fn)
  350. /* gets a timeline entry by file name */
  351. {
  352. d_char *d = NULL;
  353. FILE *f;
  354. if ((f = fopen(fn, "r")) != NULL) {
  355. xs *j = xs_readall(f);
  356. d = xs_json_loads(j);
  357. fclose(f);
  358. }
  359. return d;
  360. }
  361. d_char *_timeline_list(snac *snac, char *directory, int max)
  362. /* returns a list of the timeline filenames */
  363. {
  364. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  365. int c_max;
  366. /* maximum number of items in the timeline */
  367. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  368. /* never more timeline entries than the configured maximum */
  369. if (max > c_max)
  370. max = c_max;
  371. return xs_glob_n(spec, 0, 1, max);
  372. }
  373. d_char *timeline_list(snac *snac, int max)
  374. {
  375. return _timeline_list(snac, "timeline", max);
  376. }
  377. d_char *local_list(snac *snac, int max)
  378. {
  379. return _timeline_list(snac, "local", max);
  380. }
  381. d_char *_timeline_new_fn(snac *snac, char *id)
  382. /* creates a new filename */
  383. {
  384. xs *ntid = tid(0);
  385. xs *md5 = xs_md5_hex(id, strlen(id));
  386. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  387. }
  388. int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  389. /* writes a timeline entry and refreshes the ancestors */
  390. {
  391. xs *fn = _timeline_new_fn(snac, id);
  392. xs *pfn = NULL;
  393. xs *p_msg = NULL;
  394. FILE *f;
  395. if (!xs_is_null(parent)) {
  396. /* get the parent */
  397. pfn = _timeline_find_fn(snac, parent);
  398. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  399. xs *j;
  400. char *v;
  401. j = xs_readall(f);
  402. fclose(f);
  403. p_msg = xs_json_loads(j);
  404. if ((v = xs_dict_get(p_msg, "_snac")) != NULL) {
  405. /* is parent hidden? */
  406. if ((v = xs_dict_get(v, "hidden")) && xs_type(v) == XSTYPE_TRUE) {
  407. snac_debug(snac, 1,
  408. xs_fmt("_timeline_write dropping due to hidden parent %s (%s)", id, parent));
  409. return 0;
  410. }
  411. }
  412. }
  413. }
  414. /* write the message */
  415. if ((f = fopen(fn, "w")) != NULL) {
  416. xs *j = xs_json_dumps_pp(msg, 4);
  417. fwrite(j, strlen(j), 1, f);
  418. fclose(f);
  419. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  420. }
  421. /* related to this user? link to local timeline */
  422. if (xs_startswith(id, snac->actor) ||
  423. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  424. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  425. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  426. link(fn, lfn);
  427. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  428. }
  429. if (p_msg != NULL) {
  430. /* update the parent, adding this id to its children list */
  431. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  432. xs *children = xs_dup(xs_dict_get(meta, "children"));
  433. /* add the child if it's not already there */
  434. if (xs_list_in(children, id) == -1)
  435. children = xs_list_append(children, id);
  436. /* re-store */
  437. meta = xs_dict_set(meta, "children", children);
  438. p_msg = xs_dict_set(p_msg, "_snac", meta);
  439. xs *nfn = _timeline_new_fn(snac, parent);
  440. if ((f = fopen(nfn, "w")) != NULL) {
  441. xs *j = xs_json_dumps_pp(p_msg, 4);
  442. fwrite(j, strlen(j), 1, f);
  443. fclose(f);
  444. unlink(pfn);
  445. snac_debug(snac, 1,
  446. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  447. /* try to do the same with the local */
  448. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  449. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  450. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  451. link(nfn, nlfn);
  452. snac_debug(snac, 1,
  453. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  454. }
  455. }
  456. else
  457. return 0;
  458. /* now iterate all parents up, just renaming the files */
  459. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  460. int max_levels = 20;
  461. while (!xs_is_null(grampa)) {
  462. xs *gofn = _timeline_find_fn(snac, grampa);
  463. if (gofn == NULL)
  464. break;
  465. /* create the new filename */
  466. xs *gnfn = _timeline_new_fn(snac, grampa);
  467. rename(gofn, gnfn);
  468. snac_debug(snac, 1,
  469. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  470. /* try to do the same with the local */
  471. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  472. if (unlink(golfn) != -1) {
  473. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  474. link(gnfn, gnlfn);
  475. snac_debug(snac, 1,
  476. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  477. }
  478. /* now open it and get its own parent */
  479. if ((f = fopen(gnfn, "r")) != NULL) {
  480. xs *j = xs_readall(f);
  481. fclose(f);
  482. xs *g_msg = xs_json_loads(j);
  483. char *meta = xs_dict_get(g_msg, "_snac");
  484. char *p = xs_dict_get(meta, "parent");
  485. xs_free(grampa);
  486. grampa = xs_dup(p);
  487. }
  488. else
  489. break;
  490. if (--max_levels == 0) {
  491. snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached"));
  492. break;
  493. }
  494. }
  495. }
  496. return 1;
  497. }
  498. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  499. /* adds a message to the timeline */
  500. {
  501. xs *pfn = _timeline_find_fn(snac, id);
  502. int ret = 0;
  503. if (pfn != NULL) {
  504. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  505. return 0;
  506. }
  507. xs *msg = xs_dup(o_msg);
  508. xs *md;
  509. /* add new metadata */
  510. md = xs_json_loads("{"
  511. "\"children\": [],"
  512. "\"liked_by\": [],"
  513. "\"announced_by\": [],"
  514. "\"version\": \"" USER_AGENT "\","
  515. "\"referrer\": null,"
  516. "\"parent\": null"
  517. "}");
  518. if (!xs_is_null(parent))
  519. md = xs_dict_set(md, "parent", parent);
  520. if (!xs_is_null(referrer))
  521. md = xs_dict_set(md, "referrer", referrer);
  522. msg = xs_dict_set(msg, "_snac", md);
  523. if ((ret = _timeline_write(snac, id, msg, parent, referrer)))
  524. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  525. return ret;
  526. }
  527. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  528. /* updates a timeline entry with a new admiration */
  529. {
  530. xs *ofn = _timeline_find_fn(snac, id);
  531. FILE *f;
  532. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  533. xs *j1 = xs_readall(f);
  534. fclose(f);
  535. xs *msg = xs_json_loads(j1);
  536. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  537. xs *list;
  538. if (like)
  539. list = xs_dup(xs_dict_get(meta, "liked_by"));
  540. else
  541. list = xs_dup(xs_dict_get(meta, "announced_by"));
  542. /* add the admirer if it's not already there */
  543. if (xs_list_in(list, admirer) == -1)
  544. list = xs_list_append(list, admirer);
  545. /* set the admirer as the referrer (if not already set or it's us) */
  546. if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
  547. strcmp(admirer, snac->actor) == 0))
  548. meta = xs_dict_set(meta, "referrer", admirer);
  549. /* re-store */
  550. if (like)
  551. meta = xs_dict_set(meta, "liked_by", list);
  552. else
  553. meta = xs_dict_set(meta, "announced_by", list);
  554. msg = xs_dict_set(msg, "_snac", meta);
  555. unlink(ofn);
  556. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  557. unlink(ofn);
  558. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  559. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  560. like ? "Like" : "Announce", id, admirer));
  561. }
  562. else
  563. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  564. }
  565. int timeline_hide(snac *snac, char *id, int hide)
  566. /* hides/unhides a timeline entry */
  567. {
  568. int ret = 0;
  569. xs *fn = _timeline_find_fn(snac, id);
  570. FILE *f;
  571. if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
  572. xs *s1 = xs_readall(f);
  573. xs *msg = xs_json_loads(s1);
  574. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  575. xs *hdn = xs_val_new(hide ? XSTYPE_TRUE : XSTYPE_FALSE);
  576. char *p, *v;
  577. fclose(f);
  578. /* if it's already in this hidden state, we're done */
  579. if ((v = xs_dict_get(meta, "hidden")) && xs_type(v) == xs_type(hdn))
  580. return ret;
  581. meta = xs_dict_set(meta, "hidden", hdn);
  582. msg = xs_dict_set(msg, "_snac", meta);
  583. if ((f = fopen(fn, "w")) != NULL) {
  584. xs *j1 = xs_json_dumps_pp(msg, 4);
  585. fwrite(j1, strlen(j1), 1, f);
  586. fclose(f);
  587. snac_debug(snac, 1, xs_fmt("timeline_hide %d %s", hide, id));
  588. /* now hide the children */
  589. p = xs_dict_get(meta, "children");
  590. while (xs_list_iter(&p, &v))
  591. timeline_hide(snac, v, hide);
  592. ret = 1;
  593. }
  594. }
  595. return ret;
  596. }
  597. d_char *_following_fn(snac *snac, char *actor)
  598. {
  599. xs *md5 = xs_md5_hex(actor, strlen(actor));
  600. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  601. }
  602. int following_add(snac *snac, char *actor, char *msg)
  603. /* adds to the following list */
  604. {
  605. int ret = 201; /* created */
  606. xs *fn = _following_fn(snac, actor);
  607. FILE *f;
  608. if ((f = fopen(fn, "w")) != NULL) {
  609. xs *j = xs_json_dumps_pp(msg, 4);
  610. fwrite(j, 1, strlen(j), f);
  611. fclose(f);
  612. }
  613. else
  614. ret = 500;
  615. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  616. return ret;
  617. }
  618. int following_del(snac *snac, char *actor)
  619. /* someone is no longer following us */
  620. {
  621. xs *fn = _following_fn(snac, actor);
  622. unlink(fn);
  623. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  624. return 200;
  625. }
  626. int following_check(snac *snac, char *actor)
  627. /* checks if someone is following us */
  628. {
  629. xs *fn = _following_fn(snac, actor);
  630. return !!(mtime(fn) != 0.0);
  631. }
  632. int following_get(snac *snac, char *actor, d_char **data)
  633. /* returns the 'Follow' object */
  634. {
  635. xs *fn = _following_fn(snac, actor);
  636. FILE *f;
  637. int status = 200;
  638. if ((f = fopen(fn, "r")) != NULL) {
  639. xs *j = xs_readall(f);
  640. fclose(f);
  641. *data = xs_json_loads(j);
  642. }
  643. else
  644. status = 404;
  645. return status;
  646. }
  647. d_char *following_list(snac *snac)
  648. /* returns the list of people being followed */
  649. {
  650. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  651. xs *glist = xs_glob(spec, 0, 0);
  652. char *p, *v;
  653. d_char *list = xs_list_new();
  654. /* iterate the list of files */
  655. p = glist;
  656. while (xs_list_iter(&p, &v)) {
  657. FILE *f;
  658. /* load the follower data */
  659. if ((f = fopen(v, "r")) != NULL) {
  660. xs *j = xs_readall(f);
  661. fclose(f);
  662. if (j != NULL) {
  663. xs *o = xs_json_loads(j);
  664. if (o != NULL) {
  665. char *type = xs_dict_get(o, "type");
  666. if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
  667. list = xs_list_append(list, o);
  668. }
  669. }
  670. }
  671. }
  672. return list;
  673. }
  674. d_char *_muted_fn(snac *snac, char *actor)
  675. {
  676. xs *md5 = xs_md5_hex(actor, strlen(actor));
  677. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  678. }
  679. void mute(snac *snac, char *actor)
  680. /* mutes a moron */
  681. {
  682. xs *fn = _muted_fn(snac, actor);
  683. FILE *f;
  684. if ((f = fopen(fn, "w")) != NULL) {
  685. fprintf(f, "%s\n", actor);
  686. fclose(f);
  687. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  688. }
  689. }
  690. void unmute(snac *snac, char *actor)
  691. /* actor is no longer a moron */
  692. {
  693. xs *fn = _muted_fn(snac, actor);
  694. unlink(fn);
  695. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  696. }
  697. int is_muted(snac *snac, char *actor)
  698. /* check if someone is muted */
  699. {
  700. xs *fn = _muted_fn(snac, actor);
  701. return !!(mtime(fn) != 0.0);
  702. }
  703. int actor_add(snac *snac, const char *actor, d_char *msg)
  704. /* adds an actor */
  705. {
  706. return object_add(actor, msg);
  707. }
  708. int actor_get(snac *snac, const char *actor, d_char **data)
  709. /* returns an already downloaded actor */
  710. {
  711. int status = 200;
  712. char *d;
  713. if (strcmp(actor, snac->actor) == 0) {
  714. /* this actor */
  715. if (data)
  716. *data = msg_actor(snac);
  717. return status;
  718. }
  719. /* read the object */
  720. if (!valid_status(status = object_get(actor, &d, "Person")))
  721. return status;
  722. if (data)
  723. *data = d;
  724. xs *fn = _object_fn_by_id(actor);
  725. double max_time;
  726. /* maximum time for the actor data to be considered stale */
  727. max_time = 3600.0 * 36.0;
  728. if (mtime(fn) + max_time < (double) time(NULL)) {
  729. /* actor data exists but also stinks */
  730. FILE *f;
  731. if ((f = fopen(fn, "a")) != NULL) {
  732. /* write a blank at the end to 'touch' the file */
  733. fwrite(" ", 1, 1, f);
  734. fclose(f);
  735. }
  736. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  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. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  947. _purge_subdir(snac, "local", days);
  948. }
  949. void purge_all(void)
  950. /* purge all users */
  951. {
  952. snac snac;
  953. xs *list = user_list();
  954. char *p, *uid;
  955. p = list;
  956. while (xs_list_iter(&p, &uid)) {
  957. if (user_open(&snac, uid)) {
  958. purge(&snac);
  959. user_free(&snac);
  960. }
  961. }
  962. }