data.c 33 KB

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