data.c 39 KB

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