data.c 40 KB

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