data.c 39 KB

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