data.c 34 KB

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