data.c 38 KB

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