data.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  416. int ret;
  417. if (del) {
  418. if ((ret = unlink(cfn)) != -1)
  419. index_del(idx, id);
  420. }
  421. else {
  422. index_add(idx, id);
  423. ret = link(ofn, cfn);
  424. }
  425. return ret;
  426. }
  427. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  428. /* caches an object into a user cache */
  429. {
  430. return _object_user_cache(snac, id, cachedir, 0);
  431. }
  432. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  433. /* deletes an object from a user cache */
  434. {
  435. return _object_user_cache(snac, id, cachedir, 1);
  436. }
  437. /** specialized functions **/
  438. d_char *_follower_fn(snac *snac, char *actor)
  439. {
  440. xs *md5 = xs_md5_hex(actor, strlen(actor));
  441. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  442. }
  443. int follower_add(snac *snac, char *actor, char *msg)
  444. /* adds a follower */
  445. {
  446. int ret = 201; /* created */
  447. xs *fn = _follower_fn(snac, actor);
  448. FILE *f;
  449. if ((f = fopen(fn, "w")) != NULL) {
  450. xs *j = xs_json_dumps_pp(msg, 4);
  451. fwrite(j, 1, strlen(j), f);
  452. fclose(f);
  453. }
  454. else
  455. ret = 500;
  456. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  457. return ret;
  458. }
  459. int follower_del(snac *snac, char *actor)
  460. /* deletes a follower */
  461. {
  462. int status = 200;
  463. xs *fn = _follower_fn(snac, actor);
  464. if (fn != NULL)
  465. unlink(fn);
  466. else
  467. status = 404;
  468. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  469. return status;
  470. }
  471. int follower_check(snac *snac, char *actor)
  472. /* checks if someone is a follower */
  473. {
  474. xs *fn = _follower_fn(snac, actor);
  475. return !!(mtime(fn) != 0.0);
  476. }
  477. d_char *follower_list(snac *snac)
  478. /* returns the list of followers */
  479. {
  480. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  481. xs *glist = xs_glob(spec, 0, 0);
  482. char *p, *v;
  483. d_char *list = xs_list_new();
  484. /* iterate the list of files */
  485. p = glist;
  486. while (xs_list_iter(&p, &v)) {
  487. FILE *f;
  488. /* load the follower data */
  489. if ((f = fopen(v, "r")) != NULL) {
  490. xs *j = xs_readall(f);
  491. fclose(f);
  492. if (j != NULL) {
  493. xs *o = xs_json_loads(j);
  494. if (o != NULL)
  495. list = xs_list_append(list, o);
  496. }
  497. }
  498. }
  499. return list;
  500. }
  501. double timeline_mtime(snac *snac)
  502. {
  503. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  504. return mtime(fn);
  505. }
  506. d_char *_timeline_find_fn(snac *snac, char *id)
  507. /* returns the file name of a timeline entry by its id */
  508. {
  509. xs *md5 = xs_md5_hex(id, strlen(id));
  510. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  511. xs *list = NULL;
  512. d_char *fn = NULL;
  513. int l;
  514. list = xs_glob(spec, 0, 0);
  515. l = xs_list_len(list);
  516. /* if there is something, get the first one */
  517. if (l > 0) {
  518. fn = xs_str_new(xs_list_get(list, 0));
  519. if (l > 1)
  520. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  521. }
  522. return fn;
  523. }
  524. int timeline_here(snac *snac, char *id)
  525. /* checks if an object is already downloaded */
  526. {
  527. xs *fn = _timeline_find_fn(snac, id);
  528. return fn != NULL;
  529. }
  530. d_char *timeline_find(snac *snac, char *id)
  531. /* gets a message from the timeline by id */
  532. {
  533. xs *fn = _timeline_find_fn(snac, id);
  534. d_char *msg = NULL;
  535. if (fn != NULL) {
  536. FILE *f;
  537. if ((f = fopen(fn, "r")) != NULL) {
  538. xs *j = xs_readall(f);
  539. msg = xs_json_loads(j);
  540. fclose(f);
  541. }
  542. }
  543. return msg;
  544. }
  545. int timeline_del(snac *snac, char *id)
  546. /* deletes a message from the timeline */
  547. {
  548. int ret = 404;
  549. xs *fn = _timeline_find_fn(snac, id);
  550. if (fn != NULL) {
  551. xs *lfn = NULL;
  552. unlink(fn);
  553. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  554. /* try to delete also from the local timeline */
  555. lfn = xs_replace(fn, "/timeline/", "/local/");
  556. if (unlink(lfn) != -1)
  557. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  558. ret = 200;
  559. }
  560. /* delete from the user's caches */
  561. object_user_cache_del(snac, id, "public");
  562. object_user_cache_del(snac, id, "private");
  563. return ret;
  564. }
  565. d_char *timeline_get(snac *snac, char *fn)
  566. /* gets a timeline entry by file name */
  567. {
  568. d_char *d = NULL;
  569. FILE *f;
  570. if ((f = fopen(fn, "r")) != NULL) {
  571. xs *j = xs_readall(f);
  572. d = xs_json_loads(j);
  573. fclose(f);
  574. }
  575. return d;
  576. }
  577. d_char *_timeline_list(snac *snac, char *directory, int max)
  578. /* returns a list of the timeline filenames */
  579. {
  580. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  581. int c_max;
  582. /* maximum number of items in the timeline */
  583. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  584. /* never more timeline entries than the configured maximum */
  585. if (max > c_max)
  586. max = c_max;
  587. return xs_glob_n(spec, 0, 1, max);
  588. }
  589. d_char *timeline_list(snac *snac, int max)
  590. {
  591. return _timeline_list(snac, "timeline", max);
  592. }
  593. d_char *local_list(snac *snac, int max)
  594. {
  595. return _timeline_list(snac, "local", max);
  596. }
  597. d_char *_timeline_new_fn(snac *snac, char *id)
  598. /* creates a new filename */
  599. {
  600. xs *ntid = tid(0);
  601. xs *md5 = xs_md5_hex(id, strlen(id));
  602. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  603. }
  604. int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  605. /* writes a timeline entry and refreshes the ancestors */
  606. {
  607. xs *fn = _timeline_new_fn(snac, id);
  608. xs *pfn = NULL;
  609. xs *p_msg = NULL;
  610. FILE *f;
  611. if (!xs_is_null(parent)) {
  612. /* get the parent */
  613. pfn = _timeline_find_fn(snac, parent);
  614. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  615. xs *j;
  616. j = xs_readall(f);
  617. fclose(f);
  618. p_msg = xs_json_loads(j);
  619. }
  620. }
  621. /* write the message */
  622. if ((f = fopen(fn, "w")) != NULL) {
  623. xs *j = xs_json_dumps_pp(msg, 4);
  624. fwrite(j, strlen(j), 1, f);
  625. fclose(f);
  626. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  627. }
  628. /* related to this user? link to local timeline */
  629. if (xs_startswith(id, snac->actor) ||
  630. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  631. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  632. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  633. link(fn, lfn);
  634. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  635. }
  636. if (p_msg != NULL) {
  637. /* update the parent, adding this id to its children list */
  638. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  639. xs *children = xs_dup(xs_dict_get(meta, "children"));
  640. /* add the child if it's not already there */
  641. if (xs_list_in(children, id) == -1)
  642. children = xs_list_append(children, id);
  643. /* re-store */
  644. meta = xs_dict_set(meta, "children", children);
  645. p_msg = xs_dict_set(p_msg, "_snac", meta);
  646. xs *nfn = _timeline_new_fn(snac, parent);
  647. if ((f = fopen(nfn, "w")) != NULL) {
  648. xs *j = xs_json_dumps_pp(p_msg, 4);
  649. fwrite(j, strlen(j), 1, f);
  650. fclose(f);
  651. unlink(pfn);
  652. snac_debug(snac, 1,
  653. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  654. /* try to do the same with the local */
  655. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  656. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  657. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  658. link(nfn, nlfn);
  659. snac_debug(snac, 1,
  660. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  661. }
  662. }
  663. else
  664. return 0;
  665. /* now iterate all parents up, just renaming the files */
  666. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  667. int max_levels = 20;
  668. while (!xs_is_null(grampa)) {
  669. xs *gofn = _timeline_find_fn(snac, grampa);
  670. if (gofn == NULL)
  671. break;
  672. /* create the new filename */
  673. xs *gnfn = _timeline_new_fn(snac, grampa);
  674. rename(gofn, gnfn);
  675. snac_debug(snac, 1,
  676. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  677. /* try to do the same with the local */
  678. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  679. if (unlink(golfn) != -1) {
  680. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  681. link(gnfn, gnlfn);
  682. snac_debug(snac, 1,
  683. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  684. }
  685. /* now open it and get its own parent */
  686. if ((f = fopen(gnfn, "r")) != NULL) {
  687. xs *j = xs_readall(f);
  688. fclose(f);
  689. xs *g_msg = xs_json_loads(j);
  690. char *meta = xs_dict_get(g_msg, "_snac");
  691. char *p = xs_dict_get(meta, "parent");
  692. xs_free(grampa);
  693. grampa = xs_dup(p);
  694. }
  695. else
  696. break;
  697. if (--max_levels == 0) {
  698. snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached"));
  699. break;
  700. }
  701. }
  702. }
  703. return 1;
  704. }
  705. void timeline_update_indexes(snac *snac, const char *id)
  706. /* updates the indexes */
  707. {
  708. object_user_cache_add(snac, id, "private");
  709. if (xs_startswith(id, snac->actor))
  710. object_user_cache_add(snac, id, "public");
  711. }
  712. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  713. /* adds a message to the timeline */
  714. {
  715. xs *pfn = _timeline_find_fn(snac, id);
  716. int ret = 0;
  717. if (pfn != NULL) {
  718. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  719. return 0;
  720. }
  721. xs *msg = xs_dup(o_msg);
  722. xs *md;
  723. /* add new metadata */
  724. md = xs_json_loads("{"
  725. "\"children\": [],"
  726. "\"liked_by\": [],"
  727. "\"announced_by\": [],"
  728. "\"version\": \"" USER_AGENT "\","
  729. "\"referrer\": null,"
  730. "\"parent\": null"
  731. "}");
  732. if (!xs_is_null(parent))
  733. md = xs_dict_set(md, "parent", parent);
  734. if (!xs_is_null(referrer))
  735. md = xs_dict_set(md, "referrer", referrer);
  736. msg = xs_dict_set(msg, "_snac", md);
  737. if ((ret = _timeline_write(snac, id, msg, parent, referrer))) {
  738. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  739. object_add(id, o_msg);
  740. timeline_update_indexes(snac, id);
  741. }
  742. return ret;
  743. }
  744. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  745. /* updates a timeline entry with a new admiration */
  746. {
  747. xs *ofn = _timeline_find_fn(snac, id);
  748. FILE *f;
  749. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  750. xs *j1 = xs_readall(f);
  751. fclose(f);
  752. xs *msg = xs_json_loads(j1);
  753. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  754. xs *list;
  755. if (like)
  756. list = xs_dup(xs_dict_get(meta, "liked_by"));
  757. else
  758. list = xs_dup(xs_dict_get(meta, "announced_by"));
  759. /* add the admirer if it's not already there */
  760. if (xs_list_in(list, admirer) == -1)
  761. list = xs_list_append(list, admirer);
  762. /* set the admirer as the referrer (if not already set or it's us) */
  763. if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
  764. strcmp(admirer, snac->actor) == 0))
  765. meta = xs_dict_set(meta, "referrer", admirer);
  766. /* re-store */
  767. if (like)
  768. meta = xs_dict_set(meta, "liked_by", list);
  769. else
  770. meta = xs_dict_set(meta, "announced_by", list);
  771. msg = xs_dict_set(msg, "_snac", meta);
  772. unlink(ofn);
  773. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  774. unlink(ofn);
  775. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  776. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  777. like ? "Like" : "Announce", id, admirer));
  778. }
  779. else
  780. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  781. object_admire(id, admirer, like);
  782. }
  783. d_char *_following_fn(snac *snac, char *actor)
  784. {
  785. xs *md5 = xs_md5_hex(actor, strlen(actor));
  786. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  787. }
  788. int following_add(snac *snac, char *actor, char *msg)
  789. /* adds to the following list */
  790. {
  791. int ret = 201; /* created */
  792. xs *fn = _following_fn(snac, actor);
  793. FILE *f;
  794. if ((f = fopen(fn, "w")) != NULL) {
  795. xs *j = xs_json_dumps_pp(msg, 4);
  796. fwrite(j, 1, strlen(j), f);
  797. fclose(f);
  798. }
  799. else
  800. ret = 500;
  801. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  802. return ret;
  803. }
  804. int following_del(snac *snac, char *actor)
  805. /* we're not following this actor any longer */
  806. {
  807. xs *fn = _following_fn(snac, actor);
  808. unlink(fn);
  809. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  810. return 200;
  811. }
  812. int following_check(snac *snac, char *actor)
  813. /* checks if we are following this actor */
  814. {
  815. xs *fn = _following_fn(snac, actor);
  816. return !!(mtime(fn) != 0.0);
  817. }
  818. int following_get(snac *snac, char *actor, d_char **data)
  819. /* returns the 'Follow' object */
  820. {
  821. xs *fn = _following_fn(snac, actor);
  822. FILE *f;
  823. int status = 200;
  824. if ((f = fopen(fn, "r")) != NULL) {
  825. xs *j = xs_readall(f);
  826. fclose(f);
  827. *data = xs_json_loads(j);
  828. }
  829. else
  830. status = 404;
  831. return status;
  832. }
  833. d_char *following_list(snac *snac)
  834. /* returns the list of people being followed */
  835. {
  836. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  837. xs *glist = xs_glob(spec, 0, 0);
  838. char *p, *v;
  839. d_char *list = xs_list_new();
  840. /* iterate the list of files */
  841. p = glist;
  842. while (xs_list_iter(&p, &v)) {
  843. FILE *f;
  844. /* load the follower data */
  845. if ((f = fopen(v, "r")) != NULL) {
  846. xs *j = xs_readall(f);
  847. fclose(f);
  848. if (j != NULL) {
  849. xs *o = xs_json_loads(j);
  850. if (o != NULL) {
  851. char *type = xs_dict_get(o, "type");
  852. if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
  853. list = xs_list_append(list, o);
  854. }
  855. }
  856. }
  857. }
  858. return list;
  859. }
  860. d_char *_muted_fn(snac *snac, char *actor)
  861. {
  862. xs *md5 = xs_md5_hex(actor, strlen(actor));
  863. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  864. }
  865. void mute(snac *snac, char *actor)
  866. /* mutes a moron */
  867. {
  868. xs *fn = _muted_fn(snac, actor);
  869. FILE *f;
  870. if ((f = fopen(fn, "w")) != NULL) {
  871. fprintf(f, "%s\n", actor);
  872. fclose(f);
  873. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  874. }
  875. }
  876. void unmute(snac *snac, char *actor)
  877. /* actor is no longer a moron */
  878. {
  879. xs *fn = _muted_fn(snac, actor);
  880. unlink(fn);
  881. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  882. }
  883. int is_muted(snac *snac, char *actor)
  884. /* check if someone is muted */
  885. {
  886. xs *fn = _muted_fn(snac, actor);
  887. return !!(mtime(fn) != 0.0);
  888. }
  889. d_char *_hidden_fn(snac *snac, const char *id)
  890. {
  891. xs *md5 = xs_md5_hex(id, strlen(id));
  892. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  893. }
  894. void hide(snac *snac, const char *id)
  895. /* hides a message tree */
  896. {
  897. xs *fn = _hidden_fn(snac, id);
  898. FILE *f;
  899. if ((f = fopen(fn, "w")) != NULL) {
  900. fprintf(f, "%s\n", id);
  901. fclose(f);
  902. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  903. /* hide all the children */
  904. xs *chld = object_children(id);
  905. char *p, *v;
  906. p = chld;
  907. while (xs_list_iter(&p, &v)) {
  908. xs *co = NULL;
  909. /* resolve to get the id */
  910. if (valid_status(object_get_by_md5(v, &co, NULL))) {
  911. if ((v = xs_dict_get(co, "id")) != NULL)
  912. hide(snac, v);
  913. }
  914. }
  915. }
  916. }
  917. int is_hidden(snac *snac, const char *id)
  918. /* check is id is hidden */
  919. {
  920. xs *fn = _hidden_fn(snac, id);
  921. return !!(mtime(fn) != 0.0);
  922. }
  923. int actor_add(snac *snac, const char *actor, d_char *msg)
  924. /* adds an actor */
  925. {
  926. return object_add_ow(actor, msg);
  927. }
  928. int actor_get(snac *snac, const char *actor, d_char **data)
  929. /* returns an already downloaded actor */
  930. {
  931. int status = 200;
  932. char *d;
  933. if (strcmp(actor, snac->actor) == 0) {
  934. /* this actor */
  935. if (data)
  936. *data = msg_actor(snac);
  937. return status;
  938. }
  939. /* read the object */
  940. if (!valid_status(status = object_get(actor, &d, NULL)))
  941. return status;
  942. if (data)
  943. *data = d;
  944. xs *fn = _object_fn(actor);
  945. double max_time;
  946. /* maximum time for the actor data to be considered stale */
  947. max_time = 3600.0 * 36.0;
  948. if (mtime(fn) + max_time < (double) time(NULL)) {
  949. /* actor data exists but also stinks */
  950. FILE *f;
  951. if ((f = fopen(fn, "a")) != NULL) {
  952. /* write a blank at the end to 'touch' the file */
  953. fwrite(" ", 1, 1, f);
  954. fclose(f);
  955. }
  956. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  957. }
  958. return status;
  959. }
  960. d_char *_static_fn(snac *snac, const char *id)
  961. /* gets the filename for a static file */
  962. {
  963. return xs_fmt("%s/static/%s", snac->basedir, id);
  964. }
  965. int static_get(snac *snac, const char *id, d_char **data, int *size)
  966. /* returns static content */
  967. {
  968. xs *fn = _static_fn(snac, id);
  969. FILE *f;
  970. int status = 404;
  971. *size = XS_ALL;
  972. if ((f = fopen(fn, "rb")) != NULL) {
  973. *data = xs_read(f, size);
  974. fclose(f);
  975. status = 200;
  976. }
  977. return status;
  978. }
  979. void static_put(snac *snac, const char *id, const char *data, int size)
  980. /* writes status content */
  981. {
  982. xs *fn = _static_fn(snac, id);
  983. FILE *f;
  984. if ((f = fopen(fn, "wb")) != NULL) {
  985. fwrite(data, size, 1, f);
  986. fclose(f);
  987. }
  988. }
  989. d_char *_history_fn(snac *snac, char *id)
  990. /* gets the filename for the history */
  991. {
  992. return xs_fmt("%s/history/%s", snac->basedir, id);
  993. }
  994. double history_mtime(snac *snac, char * id)
  995. {
  996. double t = 0.0;
  997. xs *fn = _history_fn(snac, id);
  998. if (fn != NULL)
  999. t = mtime(fn);
  1000. return t;
  1001. }
  1002. void history_add(snac *snac, char *id, char *content, int size)
  1003. /* adds something to the history */
  1004. {
  1005. xs *fn = _history_fn(snac, id);
  1006. FILE *f;
  1007. if ((f = fopen(fn, "w")) != NULL) {
  1008. fwrite(content, size, 1, f);
  1009. fclose(f);
  1010. }
  1011. }
  1012. d_char *history_get(snac *snac, char *id)
  1013. {
  1014. d_char *content = NULL;
  1015. xs *fn = _history_fn(snac, id);
  1016. FILE *f;
  1017. if ((f = fopen(fn, "r")) != NULL) {
  1018. content = xs_readall(f);
  1019. fclose(f);
  1020. }
  1021. return content;
  1022. }
  1023. int history_del(snac *snac, char *id)
  1024. {
  1025. xs *fn = _history_fn(snac, id);
  1026. return unlink(fn);
  1027. }
  1028. d_char *history_list(snac *snac)
  1029. {
  1030. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  1031. return xs_glob(spec, 1, 0);
  1032. }
  1033. /** the queue **/
  1034. static int _enqueue_put(char *fn, char *msg)
  1035. /* writes safely to the queue */
  1036. {
  1037. int ret = 1;
  1038. xs *tfn = xs_fmt("%s.tmp", fn);
  1039. FILE *f;
  1040. if ((f = fopen(tfn, "w")) != NULL) {
  1041. xs *j = xs_json_dumps_pp(msg, 4);
  1042. fwrite(j, strlen(j), 1, f);
  1043. fclose(f);
  1044. rename(tfn, fn);
  1045. }
  1046. else
  1047. ret = 0;
  1048. return ret;
  1049. }
  1050. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  1051. /* enqueues an input message */
  1052. {
  1053. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1054. xs *ntid = tid(retries * 60 * qrt);
  1055. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1056. xs *qmsg = xs_dict_new();
  1057. xs *rn = xs_number_new(retries);
  1058. qmsg = xs_dict_append(qmsg, "type", "input");
  1059. qmsg = xs_dict_append(qmsg, "object", msg);
  1060. qmsg = xs_dict_append(qmsg, "req", req);
  1061. qmsg = xs_dict_append(qmsg, "retries", rn);
  1062. _enqueue_put(fn, qmsg);
  1063. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  1064. }
  1065. void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
  1066. /* enqueues an output message to an inbox */
  1067. {
  1068. if (xs_startswith(inbox, snac->actor)) {
  1069. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  1070. return;
  1071. }
  1072. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1073. xs *ntid = tid(retries * 60 * qrt);
  1074. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1075. xs *qmsg = xs_dict_new();
  1076. xs *rn = xs_number_new(retries);
  1077. qmsg = xs_dict_append(qmsg, "type", "output");
  1078. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1079. qmsg = xs_dict_append(qmsg, "object", msg);
  1080. qmsg = xs_dict_append(qmsg, "retries", rn);
  1081. _enqueue_put(fn, qmsg);
  1082. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1083. }
  1084. void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
  1085. /* enqueues an output message for an actor */
  1086. {
  1087. xs *inbox = get_actor_inbox(snac, actor);
  1088. if (!xs_is_null(inbox))
  1089. enqueue_output(snac, msg, inbox, retries);
  1090. else
  1091. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1092. }
  1093. void enqueue_email(snac *snac, char *msg, int retries)
  1094. /* enqueues an email message to be sent */
  1095. {
  1096. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1097. xs *ntid = tid(retries * 60 * qrt);
  1098. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1099. xs *qmsg = xs_dict_new();
  1100. xs *rn = xs_number_new(retries);
  1101. qmsg = xs_dict_append(qmsg, "type", "email");
  1102. qmsg = xs_dict_append(qmsg, "message", msg);
  1103. qmsg = xs_dict_append(qmsg, "retries", rn);
  1104. _enqueue_put(fn, qmsg);
  1105. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  1106. }
  1107. d_char *queue(snac *snac)
  1108. /* returns a list with filenames that can be dequeued */
  1109. {
  1110. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1111. d_char *list = xs_list_new();
  1112. time_t t = time(NULL);
  1113. char *p, *v;
  1114. xs *fns = xs_glob(spec, 0, 0);
  1115. p = fns;
  1116. while (xs_list_iter(&p, &v)) {
  1117. /* get the retry time from the basename */
  1118. char *bn = strrchr(v, '/');
  1119. time_t t2 = atol(bn + 1);
  1120. if (t2 > t)
  1121. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1122. else {
  1123. list = xs_list_append(list, v);
  1124. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  1125. }
  1126. }
  1127. return list;
  1128. }
  1129. d_char *dequeue(snac *snac, char *fn)
  1130. /* dequeues a message */
  1131. {
  1132. FILE *f;
  1133. d_char *obj = NULL;
  1134. if ((f = fopen(fn, "r")) != NULL) {
  1135. /* delete right now */
  1136. unlink(fn);
  1137. xs *j = xs_readall(f);
  1138. obj = xs_json_loads(j);
  1139. fclose(f);
  1140. }
  1141. return obj;
  1142. }
  1143. /** the purge **/
  1144. static void _purge_file(const char *fn, time_t mt)
  1145. /* purge fn if it's older than days */
  1146. {
  1147. if (mtime(fn) < mt) {
  1148. /* older than the minimum time: delete it */
  1149. unlink(fn);
  1150. srv_debug(1, xs_fmt("purged %s", fn));
  1151. }
  1152. }
  1153. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1154. /* purges all files in subdir older than days */
  1155. {
  1156. if (days) {
  1157. time_t mt = time(NULL) - days * 24 * 3600;
  1158. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1159. xs *list = xs_glob(spec, 0, 0);
  1160. char *p, *v;
  1161. p = list;
  1162. while (xs_list_iter(&p, &v))
  1163. _purge_file(v, mt);
  1164. }
  1165. }
  1166. void purge_server(void)
  1167. /* purge global server data */
  1168. {
  1169. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1170. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1171. xs *dirs = xs_glob(spec, 0, 0);
  1172. char *p, *v;
  1173. time_t mt = time(NULL) - tpd * 24 * 3600;
  1174. p = dirs;
  1175. while (xs_list_iter(&p, &v)) {
  1176. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1177. xs *files = xs_glob(spec2, 0, 0);
  1178. char *p2, *v2;
  1179. p2 = files;
  1180. while (xs_list_iter(&p2, &v2)) {
  1181. int n_link;
  1182. /* old and with no hard links? */
  1183. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1184. xs *s1 = xs_replace(v2, ".json", "");
  1185. xs *l = xs_split(s1, "/");
  1186. char *md5 = xs_list_get(l, -1);
  1187. object_del_by_md5(md5);
  1188. }
  1189. }
  1190. }
  1191. }
  1192. void purge_user(snac *snac)
  1193. /* do the purge for this user */
  1194. {
  1195. int days;
  1196. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1197. _purge_subdir(snac, "timeline", days);
  1198. _purge_subdir(snac, "hidden", days);
  1199. _purge_subdir(snac, "private", days);
  1200. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1201. _purge_subdir(snac, "local", days);
  1202. _purge_subdir(snac, "public", days);
  1203. }
  1204. void purge_all(void)
  1205. /* purge all users */
  1206. {
  1207. snac snac;
  1208. xs *list = user_list();
  1209. char *p, *uid;
  1210. p = list;
  1211. while (xs_list_iter(&p, &uid)) {
  1212. if (user_open(&snac, uid)) {
  1213. purge_user(&snac);
  1214. user_free(&snac);
  1215. }
  1216. }
  1217. purge_server();
  1218. }