data.c 31 KB

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