data.c 34 KB

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