data.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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.4;
  14. int db_upgrade(d_char **error);
  15. int srv_open(char *basedir, int auto_upgrade)
  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. if (auto_upgrade)
  54. ret = db_upgrade(&error);
  55. else {
  56. if (xs_number_get(xs_dict_get(srv_config, "layout")) < db_layout)
  57. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  58. else
  59. ret = 1;
  60. }
  61. }
  62. }
  63. }
  64. if (error != NULL)
  65. srv_log(error);
  66. /* disabled temporarily; messages can't be sent (libcurl issue?) */
  67. #if 0
  68. #ifdef __OpenBSD__
  69. srv_debug(2, xs_fmt("Calling unveil()"));
  70. unveil(basedir, "rwc");
  71. unveil("/usr/sbin", "x");
  72. unveil(NULL, NULL);
  73. #endif /* __OpenBSD__ */
  74. #endif
  75. return ret;
  76. }
  77. void srv_free(void)
  78. {
  79. xs_free(srv_basedir);
  80. xs_free(srv_config);
  81. xs_free(srv_baseurl);
  82. }
  83. void user_free(snac *snac)
  84. /* frees a user snac */
  85. {
  86. xs_free(snac->uid);
  87. xs_free(snac->basedir);
  88. xs_free(snac->config);
  89. xs_free(snac->key);
  90. xs_free(snac->actor);
  91. }
  92. int user_open(snac *snac, char *uid)
  93. /* opens a user */
  94. {
  95. int ret = 0;
  96. memset(snac, '\0', sizeof(struct _snac));
  97. if (validate_uid(uid)) {
  98. xs *cfg_file;
  99. FILE *f;
  100. snac->uid = xs_str_new(uid);
  101. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  102. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  103. if ((f = fopen(cfg_file, "r")) != NULL) {
  104. xs *cfg_data;
  105. /* read full config file */
  106. cfg_data = xs_readall(f);
  107. fclose(f);
  108. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  109. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  110. if ((f = fopen(key_file, "r")) != NULL) {
  111. xs *key_data;
  112. key_data = xs_readall(f);
  113. fclose(f);
  114. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  115. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  116. ret = 1;
  117. }
  118. else
  119. srv_log(xs_fmt("cannot parse '%s'", key_file));
  120. }
  121. else
  122. srv_log(xs_fmt("error opening '%s'", key_file));
  123. }
  124. else
  125. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  126. }
  127. else
  128. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  129. }
  130. else
  131. srv_log(xs_fmt("invalid user '%s'", uid));
  132. if (!ret)
  133. user_free(snac);
  134. return ret;
  135. }
  136. d_char *user_list(void)
  137. /* returns the list of user ids */
  138. {
  139. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  140. return xs_glob(spec, 1, 0);
  141. }
  142. double mtime_nl(const char *fn, int *n_link)
  143. /* returns the mtime and number of links of a file or directory, or 0.0 */
  144. {
  145. struct stat st;
  146. double r = 0.0;
  147. int n = 0;
  148. if (fn && stat(fn, &st) != -1) {
  149. r = (double) st.st_mtim.tv_sec;
  150. n = st.st_nlink;
  151. }
  152. if (n_link)
  153. *n_link = n;
  154. return r;
  155. }
  156. /** database 2.1+ **/
  157. /** indexes **/
  158. int index_add_md5(const char *fn, const char *md5)
  159. /* adds an md5 to an index */
  160. {
  161. int status = 201; /* Created */
  162. FILE *f;
  163. if ((f = fopen(fn, "a")) != NULL) {
  164. flock(fileno(f), LOCK_EX);
  165. fprintf(f, "%s\n", md5);
  166. fclose(f);
  167. }
  168. else
  169. status = 500;
  170. return status;
  171. }
  172. int index_add(const char *fn, const char *id)
  173. /* adds an id to an index */
  174. {
  175. xs *md5 = xs_md5_hex(id, strlen(id));
  176. return index_add_md5(fn, md5);
  177. }
  178. int index_del(const char *fn, const char *md5)
  179. /* deletes an md5 from an index */
  180. {
  181. int status = 404;
  182. FILE *i, *o;
  183. if ((i = fopen(fn, "r")) != NULL) {
  184. flock(fileno(i), LOCK_EX);
  185. xs *nfn = xs_fmt("%s.new", fn);
  186. char line[256];
  187. if ((o = fopen(nfn, "w")) != NULL) {
  188. while (fgets(line, sizeof(line), i) != NULL) {
  189. line[32] = '\0';
  190. if (memcmp(line, md5, 32) != 0)
  191. fprintf(o, "%s\n", line);
  192. }
  193. fclose(o);
  194. xs *ofn = xs_fmt("%s.bak", fn);
  195. link(fn, ofn);
  196. rename(nfn, fn);
  197. }
  198. else
  199. status = 500;
  200. fclose(i);
  201. }
  202. else
  203. status = 500;
  204. return status;
  205. }
  206. int index_in_md5(const char *fn, const char *md5)
  207. /* checks if the md5 is already in the index */
  208. {
  209. FILE *f;
  210. int ret = 0;
  211. if ((f = fopen(fn, "r")) != NULL) {
  212. flock(fileno(f), LOCK_SH);
  213. char line[256];
  214. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  215. line[32] = '\0';
  216. if (strcmp(line, md5) == 0)
  217. ret = 1;
  218. }
  219. fclose(f);
  220. }
  221. return ret;
  222. }
  223. int index_in(const char *fn, const char *id)
  224. /* checks if the object id is already in the index */
  225. {
  226. xs *md5 = xs_md5_hex(id, strlen(id));
  227. return index_in_md5(fn, md5);
  228. }
  229. d_char *index_list(const char *fn, int max)
  230. /* returns an index as a list */
  231. {
  232. d_char *list = NULL;
  233. FILE *f;
  234. int n = 0;
  235. if ((f = fopen(fn, "r")) != NULL) {
  236. flock(fileno(f), LOCK_SH);
  237. char line[256];
  238. list = xs_list_new();
  239. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  240. line[32] = '\0';
  241. list = xs_list_append(list, line);
  242. n++;
  243. }
  244. fclose(f);
  245. }
  246. return list;
  247. }
  248. d_char *index_list_desc(const char *fn, int max)
  249. /* returns an index as a list, in reverse order */
  250. {
  251. d_char *list = NULL;
  252. FILE *f;
  253. int n = 0;
  254. if ((f = fopen(fn, "r")) != NULL) {
  255. flock(fileno(f), LOCK_SH);
  256. char line[256];
  257. list = xs_list_new();
  258. /* move to the end minus one entry */
  259. if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
  260. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  261. line[32] = '\0';
  262. list = xs_list_append(list, line);
  263. n++;
  264. /* move backwards 2 entries */
  265. if (fseek(f, -66, SEEK_CUR) == -1)
  266. break;
  267. }
  268. }
  269. fclose(f);
  270. }
  271. return list;
  272. }
  273. /** objects **/
  274. d_char *_object_fn_by_md5(const char *md5)
  275. {
  276. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  277. mkdir(bfn, 0755);
  278. return xs_fmt("%s/%s.json", bfn, md5);
  279. }
  280. d_char *_object_fn(const char *id)
  281. {
  282. xs *md5 = xs_md5_hex(id, strlen(id));
  283. return _object_fn_by_md5(md5);
  284. }
  285. int object_get_by_md5(const char *md5, d_char **obj, const char *type)
  286. /* returns a stored object, optionally of the requested type */
  287. {
  288. int status = 404;
  289. xs *fn = _object_fn_by_md5(md5);
  290. FILE *f;
  291. if ((f = fopen(fn, "r")) != NULL) {
  292. flock(fileno(f), LOCK_SH);
  293. xs *j = xs_readall(f);
  294. fclose(f);
  295. *obj = xs_json_loads(j);
  296. if (*obj) {
  297. status = 200;
  298. /* specific type requested? */
  299. if (!xs_is_null(type)) {
  300. char *v = xs_dict_get(*obj, "type");
  301. if (xs_is_null(v) || strcmp(v, type) != 0) {
  302. status = 404;
  303. *obj = xs_free(*obj);
  304. }
  305. }
  306. }
  307. }
  308. else
  309. *obj = NULL;
  310. return status;
  311. }
  312. int object_get(const char *id, d_char **obj, const char *type)
  313. /* returns a stored object, optionally of the requested type */
  314. {
  315. xs *md5 = xs_md5_hex(id, strlen(id));
  316. return object_get_by_md5(md5, obj, type);
  317. }
  318. int _object_add(const char *id, d_char *obj, int ow)
  319. /* stores an object */
  320. {
  321. int status = 201; /* Created */
  322. xs *fn = _object_fn(id);
  323. FILE *f;
  324. if (!ow && mtime(fn) > 0.0) {
  325. /* object already here */
  326. srv_debug(0, xs_fmt("object_add object already here %s", id));
  327. return 204; /* No content */
  328. }
  329. if ((f = fopen(fn, "w")) != NULL) {
  330. flock(fileno(f), LOCK_EX);
  331. xs *j = xs_json_dumps_pp(obj, 4);
  332. fwrite(j, strlen(j), 1, f);
  333. fclose(f);
  334. /* does this object has a parent? */
  335. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  336. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  337. /* update the children index of the parent */
  338. xs *c_idx = _object_fn(in_reply_to);
  339. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  340. index_add(c_idx, id);
  341. srv_debug(0, xs_fmt("object_add added child %s to %s", id, c_idx));
  342. /* create a one-element index with the parent */
  343. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  344. index_add(p_idx, in_reply_to);
  345. srv_debug(0, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  346. }
  347. }
  348. else
  349. status = 500;
  350. srv_debug(0, xs_fmt("object_add %s %s %d", id, fn, status));
  351. return status;
  352. }
  353. int object_add(const char *id, d_char *obj)
  354. /* stores an object */
  355. {
  356. return _object_add(id, obj, 0);
  357. }
  358. int object_add_ow(const char *id, d_char *obj)
  359. /* stores an object (overwriting allowed) */
  360. {
  361. return _object_add(id, obj, 1);
  362. }
  363. int object_del_by_md5(const char *md5)
  364. /* deletes an object by its md5 */
  365. {
  366. int status = 404;
  367. xs *fn = _object_fn_by_md5(md5);
  368. if (fn != NULL && unlink(fn) != -1) {
  369. status = 200;
  370. /* also delete associated indexes */
  371. xs *spec = xs_dup(fn);
  372. spec = xs_replace_i(spec, ".json", "*.idx");
  373. xs *files = xs_glob(spec, 0, 0);
  374. char *p, *v;
  375. p = files;
  376. while (xs_list_iter(&p, &v)) {
  377. srv_debug(0, xs_fmt("object_del index %s", v));
  378. unlink(v);
  379. }
  380. }
  381. srv_debug(0, xs_fmt("object_del %s %d", fn, status));
  382. return status;
  383. }
  384. int object_del(const char *id)
  385. /* deletes an object */
  386. {
  387. xs *md5 = xs_md5_hex(id, strlen(id));
  388. return object_del_by_md5(md5);
  389. }
  390. d_char *object_children(const char *id)
  391. /* returns the list of an object's children */
  392. {
  393. xs *fn = _object_fn(id);
  394. fn = xs_replace_i(fn, ".json", "_c.idx");
  395. return index_list(fn, XS_ALL);
  396. }
  397. int object_admire(const char *id, const char *actor, int like)
  398. /* actor likes or announces this object */
  399. {
  400. int status = 200;
  401. xs *fn = _object_fn(id);
  402. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  403. if (!index_in(fn, actor)) {
  404. status = index_add(fn, actor);
  405. srv_debug(0, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  406. }
  407. return status;
  408. }
  409. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  410. /* adds or deletes from a user cache */
  411. {
  412. xs *ofn = _object_fn(id);
  413. xs *l = xs_split(ofn, "/");
  414. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  415. return del ? unlink(cfn) : link(ofn, cfn);
  416. }
  417. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  418. /* caches an object into a user cache */
  419. {
  420. return _object_user_cache(snac, id, cachedir, 0);
  421. }
  422. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  423. /* deletes an object from a user cache */
  424. {
  425. return _object_user_cache(snac, id, cachedir, 1);
  426. }
  427. /** specialized functions **/
  428. d_char *_follower_fn(snac *snac, char *actor)
  429. {
  430. xs *md5 = xs_md5_hex(actor, strlen(actor));
  431. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  432. }
  433. int follower_add(snac *snac, char *actor, char *msg)
  434. /* adds a follower */
  435. {
  436. int ret = 201; /* created */
  437. xs *fn = _follower_fn(snac, actor);
  438. FILE *f;
  439. if ((f = fopen(fn, "w")) != NULL) {
  440. xs *j = xs_json_dumps_pp(msg, 4);
  441. fwrite(j, 1, strlen(j), f);
  442. fclose(f);
  443. }
  444. else
  445. ret = 500;
  446. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  447. return ret;
  448. }
  449. int follower_del(snac *snac, char *actor)
  450. /* deletes a follower */
  451. {
  452. int status = 200;
  453. xs *fn = _follower_fn(snac, actor);
  454. if (fn != NULL)
  455. unlink(fn);
  456. else
  457. status = 404;
  458. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  459. return status;
  460. }
  461. int follower_check(snac *snac, char *actor)
  462. /* checks if someone is a follower */
  463. {
  464. xs *fn = _follower_fn(snac, actor);
  465. return !!(mtime(fn) != 0.0);
  466. }
  467. d_char *follower_list(snac *snac)
  468. /* returns the list of followers */
  469. {
  470. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  471. xs *glist = xs_glob(spec, 0, 0);
  472. char *p, *v;
  473. d_char *list = xs_list_new();
  474. /* iterate the list of files */
  475. p = glist;
  476. while (xs_list_iter(&p, &v)) {
  477. FILE *f;
  478. /* load the follower data */
  479. if ((f = fopen(v, "r")) != NULL) {
  480. xs *j = xs_readall(f);
  481. fclose(f);
  482. if (j != NULL) {
  483. xs *o = xs_json_loads(j);
  484. if (o != NULL)
  485. list = xs_list_append(list, o);
  486. }
  487. }
  488. }
  489. return list;
  490. }
  491. double timeline_mtime(snac *snac)
  492. {
  493. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  494. return mtime(fn);
  495. }
  496. d_char *_timeline_find_fn(snac *snac, char *id)
  497. /* returns the file name of a timeline entry by its id */
  498. {
  499. xs *md5 = xs_md5_hex(id, strlen(id));
  500. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  501. xs *list = NULL;
  502. d_char *fn = NULL;
  503. int l;
  504. list = xs_glob(spec, 0, 0);
  505. l = xs_list_len(list);
  506. /* if there is something, get the first one */
  507. if (l > 0) {
  508. fn = xs_str_new(xs_list_get(list, 0));
  509. if (l > 1)
  510. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  511. }
  512. return fn;
  513. }
  514. int timeline_here(snac *snac, char *id)
  515. /* checks if an object is already downloaded */
  516. {
  517. xs *fn = _timeline_find_fn(snac, id);
  518. return fn != NULL;
  519. }
  520. d_char *timeline_find(snac *snac, char *id)
  521. /* gets a message from the timeline by id */
  522. {
  523. xs *fn = _timeline_find_fn(snac, id);
  524. d_char *msg = NULL;
  525. if (fn != NULL) {
  526. FILE *f;
  527. if ((f = fopen(fn, "r")) != NULL) {
  528. xs *j = xs_readall(f);
  529. msg = xs_json_loads(j);
  530. fclose(f);
  531. }
  532. }
  533. return msg;
  534. }
  535. int timeline_del(snac *snac, char *id)
  536. /* deletes a message from the timeline */
  537. {
  538. int ret = 404;
  539. xs *fn = _timeline_find_fn(snac, id);
  540. if (fn != NULL) {
  541. xs *lfn = NULL;
  542. unlink(fn);
  543. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  544. /* try to delete also from the local timeline */
  545. lfn = xs_replace(fn, "/timeline/", "/local/");
  546. if (unlink(lfn) != -1)
  547. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  548. ret = 200;
  549. }
  550. if (valid_status(object_del(id))) {
  551. object_user_cache_del(snac, id, "public");
  552. object_user_cache_del(snac, id, "private");
  553. }
  554. return ret;
  555. }
  556. d_char *timeline_get(snac *snac, char *fn)
  557. /* gets a timeline entry by file name */
  558. {
  559. d_char *d = NULL;
  560. FILE *f;
  561. if ((f = fopen(fn, "r")) != NULL) {
  562. xs *j = xs_readall(f);
  563. d = xs_json_loads(j);
  564. fclose(f);
  565. }
  566. return d;
  567. }
  568. d_char *_timeline_list(snac *snac, char *directory, int max)
  569. /* returns a list of the timeline filenames */
  570. {
  571. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  572. int c_max;
  573. /* maximum number of items in the timeline */
  574. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  575. /* never more timeline entries than the configured maximum */
  576. if (max > c_max)
  577. max = c_max;
  578. return xs_glob_n(spec, 0, 1, max);
  579. }
  580. d_char *timeline_list(snac *snac, int max)
  581. {
  582. return _timeline_list(snac, "timeline", max);
  583. }
  584. d_char *local_list(snac *snac, int max)
  585. {
  586. return _timeline_list(snac, "local", max);
  587. }
  588. d_char *_timeline_new_fn(snac *snac, char *id)
  589. /* creates a new filename */
  590. {
  591. xs *ntid = tid(0);
  592. xs *md5 = xs_md5_hex(id, strlen(id));
  593. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  594. }
  595. int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  596. /* writes a timeline entry and refreshes the ancestors */
  597. {
  598. xs *fn = _timeline_new_fn(snac, id);
  599. xs *pfn = NULL;
  600. xs *p_msg = NULL;
  601. FILE *f;
  602. if (!xs_is_null(parent)) {
  603. /* get the parent */
  604. pfn = _timeline_find_fn(snac, parent);
  605. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  606. xs *j;
  607. j = xs_readall(f);
  608. fclose(f);
  609. p_msg = xs_json_loads(j);
  610. }
  611. }
  612. /* write the message */
  613. if ((f = fopen(fn, "w")) != NULL) {
  614. xs *j = xs_json_dumps_pp(msg, 4);
  615. fwrite(j, strlen(j), 1, f);
  616. fclose(f);
  617. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  618. }
  619. /* related to this user? link to local timeline */
  620. if (xs_startswith(id, snac->actor) ||
  621. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  622. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  623. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  624. link(fn, lfn);
  625. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  626. }
  627. if (p_msg != NULL) {
  628. /* update the parent, adding this id to its children list */
  629. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  630. xs *children = xs_dup(xs_dict_get(meta, "children"));
  631. /* add the child if it's not already there */
  632. if (xs_list_in(children, id) == -1)
  633. children = xs_list_append(children, id);
  634. /* re-store */
  635. meta = xs_dict_set(meta, "children", children);
  636. p_msg = xs_dict_set(p_msg, "_snac", meta);
  637. xs *nfn = _timeline_new_fn(snac, parent);
  638. if ((f = fopen(nfn, "w")) != NULL) {
  639. xs *j = xs_json_dumps_pp(p_msg, 4);
  640. fwrite(j, strlen(j), 1, f);
  641. fclose(f);
  642. unlink(pfn);
  643. snac_debug(snac, 1,
  644. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  645. /* try to do the same with the local */
  646. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  647. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  648. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  649. link(nfn, nlfn);
  650. snac_debug(snac, 1,
  651. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  652. }
  653. }
  654. else
  655. return 0;
  656. /* now iterate all parents up, just renaming the files */
  657. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  658. int max_levels = 20;
  659. while (!xs_is_null(grampa)) {
  660. xs *gofn = _timeline_find_fn(snac, grampa);
  661. if (gofn == NULL)
  662. break;
  663. /* create the new filename */
  664. xs *gnfn = _timeline_new_fn(snac, grampa);
  665. rename(gofn, gnfn);
  666. snac_debug(snac, 1,
  667. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  668. /* try to do the same with the local */
  669. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  670. if (unlink(golfn) != -1) {
  671. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  672. link(gnfn, gnlfn);
  673. snac_debug(snac, 1,
  674. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  675. }
  676. /* now open it and get its own parent */
  677. if ((f = fopen(gnfn, "r")) != NULL) {
  678. xs *j = xs_readall(f);
  679. fclose(f);
  680. xs *g_msg = xs_json_loads(j);
  681. char *meta = xs_dict_get(g_msg, "_snac");
  682. char *p = xs_dict_get(meta, "parent");
  683. xs_free(grampa);
  684. grampa = xs_dup(p);
  685. }
  686. else
  687. break;
  688. if (--max_levels == 0) {
  689. snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached"));
  690. break;
  691. }
  692. }
  693. }
  694. return 1;
  695. }
  696. void timeline_update_indexes(snac *snac, const char *id)
  697. /* updates the indexes */
  698. {
  699. /* add to the private index */
  700. xs *idx = xs_fmt("%s/private.idx", snac->basedir);
  701. index_add(idx, id);
  702. object_user_cache_add(snac, id, "private");
  703. if (xs_startswith(id, snac->actor)) {
  704. /* add to the public index */
  705. idx = xs_replace_i(idx, "private", "public");
  706. index_add(idx, id);
  707. object_user_cache_add(snac, id, "public");
  708. }
  709. }
  710. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  711. /* adds a message to the timeline */
  712. {
  713. xs *pfn = _timeline_find_fn(snac, id);
  714. int ret = 0;
  715. if (pfn != NULL) {
  716. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  717. return 0;
  718. }
  719. xs *msg = xs_dup(o_msg);
  720. xs *md;
  721. /* add new metadata */
  722. md = xs_json_loads("{"
  723. "\"children\": [],"
  724. "\"liked_by\": [],"
  725. "\"announced_by\": [],"
  726. "\"version\": \"" USER_AGENT "\","
  727. "\"referrer\": null,"
  728. "\"parent\": null"
  729. "}");
  730. if (!xs_is_null(parent))
  731. md = xs_dict_set(md, "parent", parent);
  732. if (!xs_is_null(referrer))
  733. md = xs_dict_set(md, "referrer", referrer);
  734. msg = xs_dict_set(msg, "_snac", md);
  735. if ((ret = _timeline_write(snac, id, msg, parent, referrer))) {
  736. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  737. object_add(id, o_msg);
  738. timeline_update_indexes(snac, id);
  739. }
  740. return ret;
  741. }
  742. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  743. /* updates a timeline entry with a new admiration */
  744. {
  745. xs *ofn = _timeline_find_fn(snac, id);
  746. FILE *f;
  747. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  748. xs *j1 = xs_readall(f);
  749. fclose(f);
  750. xs *msg = xs_json_loads(j1);
  751. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  752. xs *list;
  753. if (like)
  754. list = xs_dup(xs_dict_get(meta, "liked_by"));
  755. else
  756. list = xs_dup(xs_dict_get(meta, "announced_by"));
  757. /* add the admirer if it's not already there */
  758. if (xs_list_in(list, admirer) == -1)
  759. list = xs_list_append(list, admirer);
  760. /* set the admirer as the referrer (if not already set or it's us) */
  761. if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
  762. strcmp(admirer, snac->actor) == 0))
  763. meta = xs_dict_set(meta, "referrer", admirer);
  764. /* re-store */
  765. if (like)
  766. meta = xs_dict_set(meta, "liked_by", list);
  767. else
  768. meta = xs_dict_set(meta, "announced_by", list);
  769. msg = xs_dict_set(msg, "_snac", meta);
  770. unlink(ofn);
  771. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  772. unlink(ofn);
  773. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  774. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  775. like ? "Like" : "Announce", id, admirer));
  776. }
  777. else
  778. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  779. object_admire(id, admirer, like);
  780. }
  781. d_char *_following_fn(snac *snac, char *actor)
  782. {
  783. xs *md5 = xs_md5_hex(actor, strlen(actor));
  784. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  785. }
  786. int following_add(snac *snac, char *actor, char *msg)
  787. /* adds to the following list */
  788. {
  789. int ret = 201; /* created */
  790. xs *fn = _following_fn(snac, actor);
  791. FILE *f;
  792. if ((f = fopen(fn, "w")) != NULL) {
  793. xs *j = xs_json_dumps_pp(msg, 4);
  794. fwrite(j, 1, strlen(j), f);
  795. fclose(f);
  796. }
  797. else
  798. ret = 500;
  799. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  800. return ret;
  801. }
  802. int following_del(snac *snac, char *actor)
  803. /* we're not following this actor any longer */
  804. {
  805. xs *fn = _following_fn(snac, actor);
  806. unlink(fn);
  807. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  808. return 200;
  809. }
  810. int following_check(snac *snac, char *actor)
  811. /* checks if we are following this actor */
  812. {
  813. xs *fn = _following_fn(snac, actor);
  814. return !!(mtime(fn) != 0.0);
  815. }
  816. int following_get(snac *snac, char *actor, d_char **data)
  817. /* returns the 'Follow' object */
  818. {
  819. xs *fn = _following_fn(snac, actor);
  820. FILE *f;
  821. int status = 200;
  822. if ((f = fopen(fn, "r")) != NULL) {
  823. xs *j = xs_readall(f);
  824. fclose(f);
  825. *data = xs_json_loads(j);
  826. }
  827. else
  828. status = 404;
  829. return status;
  830. }
  831. d_char *following_list(snac *snac)
  832. /* returns the list of people being followed */
  833. {
  834. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  835. xs *glist = xs_glob(spec, 0, 0);
  836. char *p, *v;
  837. d_char *list = xs_list_new();
  838. /* iterate the list of files */
  839. p = glist;
  840. while (xs_list_iter(&p, &v)) {
  841. FILE *f;
  842. /* load the follower data */
  843. if ((f = fopen(v, "r")) != NULL) {
  844. xs *j = xs_readall(f);
  845. fclose(f);
  846. if (j != NULL) {
  847. xs *o = xs_json_loads(j);
  848. if (o != NULL) {
  849. char *type = xs_dict_get(o, "type");
  850. if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
  851. list = xs_list_append(list, o);
  852. }
  853. }
  854. }
  855. }
  856. return list;
  857. }
  858. d_char *_muted_fn(snac *snac, char *actor)
  859. {
  860. xs *md5 = xs_md5_hex(actor, strlen(actor));
  861. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  862. }
  863. void mute(snac *snac, char *actor)
  864. /* mutes a moron */
  865. {
  866. xs *fn = _muted_fn(snac, actor);
  867. FILE *f;
  868. if ((f = fopen(fn, "w")) != NULL) {
  869. fprintf(f, "%s\n", actor);
  870. fclose(f);
  871. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  872. }
  873. }
  874. void unmute(snac *snac, char *actor)
  875. /* actor is no longer a moron */
  876. {
  877. xs *fn = _muted_fn(snac, actor);
  878. unlink(fn);
  879. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  880. }
  881. int is_muted(snac *snac, char *actor)
  882. /* check if someone is muted */
  883. {
  884. xs *fn = _muted_fn(snac, actor);
  885. return !!(mtime(fn) != 0.0);
  886. }
  887. d_char *_hidden_fn(snac *snac, const char *id)
  888. {
  889. xs *md5 = xs_md5_hex(id, strlen(id));
  890. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  891. }
  892. void hide(snac *snac, const char *id)
  893. /* hides a message tree */
  894. {
  895. xs *fn = _hidden_fn(snac, id);
  896. FILE *f;
  897. if ((f = fopen(fn, "w")) != NULL) {
  898. fprintf(f, "%s\n", id);
  899. fclose(f);
  900. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  901. /* hide all the children */
  902. xs *chld = object_children(id);
  903. char *p, *v;
  904. p = chld;
  905. while (xs_list_iter(&p, &v)) {
  906. xs *co = NULL;
  907. /* resolve to get the id */
  908. if (valid_status(object_get_by_md5(v, &co, NULL))) {
  909. if ((v = xs_dict_get(co, "id")) != NULL)
  910. hide(snac, v);
  911. }
  912. }
  913. }
  914. }
  915. int is_hidden(snac *snac, const char *id)
  916. /* check is id is hidden */
  917. {
  918. xs *fn = _hidden_fn(snac, id);
  919. return !!(mtime(fn) != 0.0);
  920. }
  921. int actor_add(snac *snac, const char *actor, d_char *msg)
  922. /* adds an actor */
  923. {
  924. return object_add_ow(actor, msg);
  925. }
  926. int actor_get(snac *snac, const char *actor, d_char **data)
  927. /* returns an already downloaded actor */
  928. {
  929. int status = 200;
  930. char *d;
  931. if (strcmp(actor, snac->actor) == 0) {
  932. /* this actor */
  933. if (data)
  934. *data = msg_actor(snac);
  935. return status;
  936. }
  937. /* read the object */
  938. if (!valid_status(status = object_get(actor, &d, NULL)))
  939. return status;
  940. if (data)
  941. *data = d;
  942. xs *fn = _object_fn(actor);
  943. double max_time;
  944. /* maximum time for the actor data to be considered stale */
  945. max_time = 3600.0 * 36.0;
  946. if (mtime(fn) + max_time < (double) time(NULL)) {
  947. /* actor data exists but also stinks */
  948. FILE *f;
  949. if ((f = fopen(fn, "a")) != NULL) {
  950. /* write a blank at the end to 'touch' the file */
  951. fwrite(" ", 1, 1, f);
  952. fclose(f);
  953. }
  954. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  955. }
  956. return status;
  957. }
  958. d_char *_static_fn(snac *snac, const char *id)
  959. /* gets the filename for a static file */
  960. {
  961. return xs_fmt("%s/static/%s", snac->basedir, id);
  962. }
  963. int static_get(snac *snac, const char *id, d_char **data, int *size)
  964. /* returns static content */
  965. {
  966. xs *fn = _static_fn(snac, id);
  967. FILE *f;
  968. int status = 404;
  969. *size = XS_ALL;
  970. if ((f = fopen(fn, "rb")) != NULL) {
  971. *data = xs_read(f, size);
  972. fclose(f);
  973. status = 200;
  974. }
  975. return status;
  976. }
  977. void static_put(snac *snac, const char *id, const char *data, int size)
  978. /* writes status content */
  979. {
  980. xs *fn = _static_fn(snac, id);
  981. FILE *f;
  982. if ((f = fopen(fn, "wb")) != NULL) {
  983. fwrite(data, size, 1, f);
  984. fclose(f);
  985. }
  986. }
  987. d_char *_history_fn(snac *snac, char *id)
  988. /* gets the filename for the history */
  989. {
  990. return xs_fmt("%s/history/%s", snac->basedir, id);
  991. }
  992. double history_mtime(snac *snac, char * id)
  993. {
  994. double t = 0.0;
  995. xs *fn = _history_fn(snac, id);
  996. if (fn != NULL)
  997. t = mtime(fn);
  998. return t;
  999. }
  1000. void history_add(snac *snac, char *id, char *content, int size)
  1001. /* adds something to the history */
  1002. {
  1003. xs *fn = _history_fn(snac, id);
  1004. FILE *f;
  1005. if ((f = fopen(fn, "w")) != NULL) {
  1006. fwrite(content, size, 1, f);
  1007. fclose(f);
  1008. }
  1009. }
  1010. d_char *history_get(snac *snac, char *id)
  1011. {
  1012. d_char *content = NULL;
  1013. xs *fn = _history_fn(snac, id);
  1014. FILE *f;
  1015. if ((f = fopen(fn, "r")) != NULL) {
  1016. content = xs_readall(f);
  1017. fclose(f);
  1018. }
  1019. return content;
  1020. }
  1021. int history_del(snac *snac, char *id)
  1022. {
  1023. xs *fn = _history_fn(snac, id);
  1024. return unlink(fn);
  1025. }
  1026. d_char *history_list(snac *snac)
  1027. {
  1028. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  1029. return xs_glob(spec, 1, 0);
  1030. }
  1031. /** the queue **/
  1032. static int _enqueue_put(char *fn, char *msg)
  1033. /* writes safely to the queue */
  1034. {
  1035. int ret = 1;
  1036. xs *tfn = xs_fmt("%s.tmp", fn);
  1037. FILE *f;
  1038. if ((f = fopen(tfn, "w")) != NULL) {
  1039. xs *j = xs_json_dumps_pp(msg, 4);
  1040. fwrite(j, strlen(j), 1, f);
  1041. fclose(f);
  1042. rename(tfn, fn);
  1043. }
  1044. else
  1045. ret = 0;
  1046. return ret;
  1047. }
  1048. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  1049. /* enqueues an input message */
  1050. {
  1051. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1052. xs *ntid = tid(retries * 60 * qrt);
  1053. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1054. xs *qmsg = xs_dict_new();
  1055. xs *rn = xs_number_new(retries);
  1056. qmsg = xs_dict_append(qmsg, "type", "input");
  1057. qmsg = xs_dict_append(qmsg, "object", msg);
  1058. qmsg = xs_dict_append(qmsg, "req", req);
  1059. qmsg = xs_dict_append(qmsg, "retries", rn);
  1060. _enqueue_put(fn, qmsg);
  1061. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  1062. }
  1063. void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
  1064. /* enqueues an output message to an inbox */
  1065. {
  1066. if (xs_startswith(inbox, snac->actor)) {
  1067. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  1068. return;
  1069. }
  1070. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1071. xs *ntid = tid(retries * 60 * qrt);
  1072. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1073. xs *qmsg = xs_dict_new();
  1074. xs *rn = xs_number_new(retries);
  1075. qmsg = xs_dict_append(qmsg, "type", "output");
  1076. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1077. qmsg = xs_dict_append(qmsg, "object", msg);
  1078. qmsg = xs_dict_append(qmsg, "retries", rn);
  1079. _enqueue_put(fn, qmsg);
  1080. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1081. }
  1082. void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
  1083. /* enqueues an output message for an actor */
  1084. {
  1085. xs *inbox = get_actor_inbox(snac, actor);
  1086. if (!xs_is_null(inbox))
  1087. enqueue_output(snac, msg, inbox, retries);
  1088. else
  1089. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1090. }
  1091. void enqueue_email(snac *snac, char *msg, int retries)
  1092. /* enqueues an email message to be sent */
  1093. {
  1094. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1095. xs *ntid = tid(retries * 60 * qrt);
  1096. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1097. xs *qmsg = xs_dict_new();
  1098. xs *rn = xs_number_new(retries);
  1099. qmsg = xs_dict_append(qmsg, "type", "email");
  1100. qmsg = xs_dict_append(qmsg, "message", msg);
  1101. qmsg = xs_dict_append(qmsg, "retries", rn);
  1102. _enqueue_put(fn, qmsg);
  1103. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  1104. }
  1105. d_char *queue(snac *snac)
  1106. /* returns a list with filenames that can be dequeued */
  1107. {
  1108. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1109. d_char *list = xs_list_new();
  1110. time_t t = time(NULL);
  1111. char *p, *v;
  1112. xs *fns = xs_glob(spec, 0, 0);
  1113. p = fns;
  1114. while (xs_list_iter(&p, &v)) {
  1115. /* get the retry time from the basename */
  1116. char *bn = strrchr(v, '/');
  1117. time_t t2 = atol(bn + 1);
  1118. if (t2 > t)
  1119. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1120. else {
  1121. list = xs_list_append(list, v);
  1122. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  1123. }
  1124. }
  1125. return list;
  1126. }
  1127. d_char *dequeue(snac *snac, char *fn)
  1128. /* dequeues a message */
  1129. {
  1130. FILE *f;
  1131. d_char *obj = NULL;
  1132. if ((f = fopen(fn, "r")) != NULL) {
  1133. /* delete right now */
  1134. unlink(fn);
  1135. xs *j = xs_readall(f);
  1136. obj = xs_json_loads(j);
  1137. fclose(f);
  1138. }
  1139. return obj;
  1140. }
  1141. /** the purge **/
  1142. static void _purge_file(const char *fn, time_t mt)
  1143. /* purge fn if it's older than days */
  1144. {
  1145. if (mtime(fn) < mt) {
  1146. /* older than the minimum time: delete it */
  1147. unlink(fn);
  1148. srv_debug(1, xs_fmt("purged %s", fn));
  1149. }
  1150. }
  1151. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1152. /* purges all files in subdir older than days */
  1153. {
  1154. if (days) {
  1155. time_t mt = time(NULL) - days * 24 * 3600;
  1156. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1157. xs *list = xs_glob(spec, 0, 0);
  1158. char *p, *v;
  1159. p = list;
  1160. while (xs_list_iter(&p, &v))
  1161. _purge_file(v, mt);
  1162. }
  1163. }
  1164. void purge_server(void)
  1165. /* purge global server data */
  1166. {
  1167. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1168. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1169. xs *dirs = xs_glob(spec, 0, 0);
  1170. char *p, *v;
  1171. time_t mt = time(NULL) - tpd * 24 * 3600;
  1172. p = dirs;
  1173. while (xs_list_iter(&p, &v)) {
  1174. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1175. xs *files = xs_glob(spec2, 0, 0);
  1176. char *p2, *v2;
  1177. p2 = files;
  1178. while (xs_list_iter(&p2, &v2)) {
  1179. int n_link;
  1180. /* old and with no hard links? */
  1181. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1182. xs *s1 = xs_replace(v2, ".json", "");
  1183. xs *l = xs_split(s1, "/");
  1184. char *md5 = xs_list_get(l, -1);
  1185. object_del_by_md5(md5);
  1186. }
  1187. }
  1188. }
  1189. }
  1190. void purge_user(snac *snac)
  1191. /* do the purge for this user */
  1192. {
  1193. int days;
  1194. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1195. _purge_subdir(snac, "timeline", days);
  1196. _purge_subdir(snac, "hidden", days);
  1197. _purge_subdir(snac, "private", days);
  1198. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1199. _purge_subdir(snac, "local", days);
  1200. _purge_subdir(snac, "public", days);
  1201. }
  1202. void purge_all(void)
  1203. /* purge all users */
  1204. {
  1205. snac snac;
  1206. xs *list = user_list();
  1207. char *p, *uid;
  1208. p = list;
  1209. while (xs_list_iter(&p, &uid)) {
  1210. if (user_open(&snac, uid)) {
  1211. purge_user(&snac);
  1212. user_free(&snac);
  1213. }
  1214. }
  1215. purge_server();
  1216. }