data.c 32 KB

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