data.c 32 KB

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