data.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 "snac.h"
  8. #include <time.h>
  9. #include <glob.h>
  10. #include <sys/stat.h>
  11. int srv_open(char *basedir)
  12. /* opens a server */
  13. {
  14. int ret = 0;
  15. xs *cfg_file = NULL;
  16. FILE *f;
  17. d_char *error = NULL;
  18. srv_basedir = xs_str_new(basedir);
  19. if (xs_endswith(srv_basedir, "/"))
  20. srv_basedir = xs_crop(srv_basedir, 0, -1);
  21. cfg_file = xs_fmt("%s/server.json", basedir);
  22. if ((f = fopen(cfg_file, "r")) == NULL)
  23. error = xs_fmt("error opening '%s'", cfg_file);
  24. else {
  25. xs *cfg_data;
  26. /* read full config file */
  27. cfg_data = xs_readall(f);
  28. /* parse */
  29. srv_config = xs_json_loads(cfg_data);
  30. if (srv_config == NULL)
  31. error = xs_fmt("cannot parse '%s'", cfg_file);
  32. else {
  33. char *host;
  34. char *prefix;
  35. char *dbglvl;
  36. host = xs_dict_get(srv_config, "host");
  37. prefix = xs_dict_get(srv_config, "prefix");
  38. dbglvl = xs_dict_get(srv_config, "dbglevel");
  39. if (host == NULL || prefix == NULL)
  40. error = xs_str_new("cannot get server data");
  41. else {
  42. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  43. dbglevel = (int) xs_number_get(dbglvl);
  44. if ((dbglvl = getenv("DEBUG")) != NULL) {
  45. dbglevel = atoi(dbglvl);
  46. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  47. }
  48. ret = 1;
  49. }
  50. }
  51. }
  52. if (ret == 0 && error != NULL)
  53. srv_log(error);
  54. return ret;
  55. }
  56. void user_free(snac *snac)
  57. /* frees a user snac */
  58. {
  59. free(snac->uid);
  60. free(snac->basedir);
  61. free(snac->config);
  62. free(snac->key);
  63. free(snac->actor);
  64. }
  65. int user_open(snac *snac, char *uid)
  66. /* opens a user */
  67. {
  68. int ret = 0;
  69. memset(snac, '\0', sizeof(struct _snac));
  70. if (validate_uid(uid)) {
  71. xs *cfg_file;
  72. FILE *f;
  73. snac->uid = xs_str_new(uid);
  74. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  75. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  76. if ((f = fopen(cfg_file, "r")) != NULL) {
  77. xs *cfg_data;
  78. /* read full config file */
  79. cfg_data = xs_readall(f);
  80. fclose(f);
  81. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  82. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  83. if ((f = fopen(key_file, "r")) != NULL) {
  84. xs *key_data;
  85. key_data = xs_readall(f);
  86. fclose(f);
  87. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  88. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  89. ret = 1;
  90. }
  91. else
  92. srv_log(xs_fmt("cannot parse '%s'", key_file));
  93. }
  94. else
  95. srv_log(xs_fmt("error opening '%s'", key_file));
  96. }
  97. else
  98. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  99. }
  100. else
  101. srv_log(xs_fmt("error opening '%s'", cfg_file));
  102. }
  103. else
  104. srv_log(xs_fmt("invalid user '%s'", uid));
  105. if (!ret)
  106. user_free(snac);
  107. return ret;
  108. }
  109. d_char *user_list(void)
  110. /* returns the list of user ids */
  111. {
  112. d_char *list;
  113. xs *spec;
  114. glob_t globbuf;
  115. globbuf.gl_offs = 1;
  116. list = xs_list_new();
  117. spec = xs_fmt("%s/user/" "*", srv_basedir);
  118. if (glob(spec, 0, NULL, &globbuf) == 0) {
  119. int n;
  120. char *p;
  121. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  122. if ((p = strrchr(p, '/')) != NULL)
  123. list = xs_list_append(list, p + 1);
  124. }
  125. }
  126. globfree(&globbuf);
  127. return list;
  128. }
  129. float mtime(char *fn)
  130. /* returns the mtime of a file or directory, or 0.0 */
  131. {
  132. struct stat st;
  133. float r = 0.0;
  134. if (stat(fn, &st) != -1)
  135. r = (float)st.st_mtim.tv_sec;
  136. return r;
  137. }
  138. d_char *_follower_fn(snac *snac, char *actor)
  139. {
  140. xs *md5 = xs_md5_hex(actor, strlen(actor));
  141. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  142. }
  143. int follower_add(snac *snac, char *actor, char *msg)
  144. /* adds a follower */
  145. {
  146. int ret = 201; /* created */
  147. xs *fn = _follower_fn(snac, actor);
  148. FILE *f;
  149. if ((f = fopen(fn, "w")) != NULL) {
  150. xs *j = xs_json_dumps_pp(msg, 4);
  151. fwrite(j, 1, strlen(j), f);
  152. fclose(f);
  153. }
  154. else
  155. ret = 500;
  156. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  157. return ret;
  158. }
  159. int follower_del(snac *snac, char *actor)
  160. /* deletes a follower */
  161. {
  162. xs *fn = _follower_fn(snac, actor);
  163. unlink(fn);
  164. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  165. return 200;
  166. }
  167. int follower_check(snac *snac, char *actor)
  168. /* checks if someone is a follower */
  169. {
  170. xs *fn = _follower_fn(snac, actor);
  171. return !!(mtime(fn) != 0.0);
  172. }
  173. d_char *follower_list(snac *snac)
  174. /* returns the list of followers */
  175. {
  176. d_char *list;
  177. xs *spec;
  178. glob_t globbuf;
  179. list = xs_list_new();
  180. spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  181. if (glob(spec, 0, NULL, &globbuf) == 0) {
  182. int n;
  183. char *fn;
  184. for (n = 0; (fn = globbuf.gl_pathv[n]) != NULL; n++) {
  185. FILE *f;
  186. if ((f = fopen(fn, "r")) != NULL) {
  187. xs *j = xs_readall(f);
  188. xs *o = xs_json_loads(j);
  189. if (o != NULL)
  190. list = xs_list_append(list, o);
  191. fclose(f);
  192. }
  193. }
  194. }
  195. globfree(&globbuf);
  196. return list;
  197. }
  198. d_char *_timeline_find_fn(snac *snac, char *id)
  199. /* returns the file name of a timeline entry by its id */
  200. {
  201. xs *md5 = xs_md5_hex(id, strlen(id));
  202. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  203. glob_t globbuf;
  204. d_char *fn = NULL;
  205. if (glob(spec, 0, NULL, &globbuf) == 0 && globbuf.gl_pathc) {
  206. /* get just the first file */
  207. fn = xs_str_new(globbuf.gl_pathv[0]);
  208. }
  209. globfree(&globbuf);
  210. return fn;
  211. }
  212. d_char *timeline_find(snac *snac, char *id)
  213. /* gets a message from the timeline by id */
  214. {
  215. xs *fn = _timeline_find_fn(snac, id);
  216. xs *msg = NULL;
  217. if (fn != NULL) {
  218. FILE *f;
  219. if ((f = fopen(fn, "r")) != NULL) {
  220. xs *j = xs_readall(f);
  221. msg = xs_json_loads(j);
  222. fclose(f);
  223. }
  224. }
  225. return msg;
  226. }
  227. void timeline_del(snac *snac, char *id)
  228. /* deletes a message from the timeline */
  229. {
  230. xs *fn = _timeline_find_fn(snac, id);
  231. if (fn != NULL) {
  232. xs *lfn = NULL;
  233. unlink(fn);
  234. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  235. /* try to delete also from the local timeline */
  236. lfn = xs_replace(fn, "/timeline/", "/local/");
  237. if (unlink(lfn) != -1)
  238. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  239. }
  240. }
  241. d_char *timeline_get(snac *snac, char *fn)
  242. /* gets a timeline entry by file name */
  243. {
  244. d_char *d = NULL;
  245. FILE *f;
  246. if ((f = fopen(fn, "r")) != NULL) {
  247. xs *j = xs_readall(f);
  248. d = xs_json_loads(j);
  249. fclose(f);
  250. }
  251. return d;
  252. }
  253. d_char *timeline_list(snac *snac)
  254. /* returns a list of the timeline filenames */
  255. {
  256. d_char *list;
  257. xs *spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
  258. glob_t globbuf;
  259. int max;
  260. /* maximum number of items in the timeline */
  261. max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  262. list = xs_list_new();
  263. /* get the list in reverse order */
  264. if (glob(spec, 0, NULL, &globbuf) == 0) {
  265. int n;
  266. if (max > globbuf.gl_pathc)
  267. max = globbuf.gl_pathc;
  268. for (n = 0; n < max; n++) {
  269. char *fn = globbuf.gl_pathv[globbuf.gl_pathc - n - 1];
  270. list = xs_list_append(list, fn);
  271. }
  272. }
  273. globfree(&globbuf);
  274. return list;
  275. }
  276. void _timeline_parent(snac *snac, char *parent, char *child)
  277. /* add child to parent's children list */
  278. {
  279. if (parent != NULL) {
  280. xs *pfn = _timeline_find_fn(snac, parent);
  281. if (pfn != NULL) {
  282. FILE *f;
  283. if ((f = fopen(pfn, "r")) != NULL) {
  284. xs *ji, *msg;
  285. ji = xs_readall(f);
  286. fclose(f);
  287. msg = xs_json_loads(ji);
  288. if (msg != NULL) {
  289. xs *meta;
  290. xs *children;
  291. /* get the children list */
  292. meta = xs_dict_get(msg, "_snac");
  293. children = xs_dict_get(meta, "children");
  294. /* add */
  295. children = xs_list_append(children, child);
  296. /* re-store */
  297. meta = xs_dict_set(meta, "children", children);
  298. msg = xs_dict_set(msg, "_snac", meta);
  299. xs *jo = xs_json_dumps_pp(msg, 4);
  300. xs *ntid = tid(0);
  301. xs *md5 = xs_md5_hex(parent, strlen(parent));
  302. xs *nfn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  303. if ((f = fopen(nfn, "w")) != NULL) {
  304. fwrite(jo, strlen(jo), 1, f);
  305. fclose(f);
  306. snac_debug(snac, 1,
  307. xs_fmt("_timeline_parent updated %s %s", parent, nfn));
  308. unlink(pfn);
  309. /* generated by this user? link to local timeline */
  310. if (xs_startswith(parent, snac->actor)) {
  311. xs *lfn = xs_replace(nfn, "/timeline/", "/local/");
  312. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  313. link(nfn, lfn);
  314. unlink(olfn);
  315. snac_debug(snac, 1,
  316. xs_fmt("_timeline_parent (local) updated %s %s", parent, lfn));
  317. }
  318. /* retry with grampa */
  319. _timeline_parent(snac, xs_dict_get(meta, "parent"), parent);
  320. }
  321. else
  322. snac_log(snac, xs_fmt("_timeline_parent error writing %s %s", parent, nfn));
  323. }
  324. else
  325. snac_log(snac, xs_fmt("_timeline_parent error reading %s %s", parent, pfn));
  326. }
  327. else
  328. snac_log(snac, xs_fmt("_timeline_parent error opening %s %s", parent, pfn));
  329. }
  330. }
  331. }
  332. void timeline_add(snac *snac, char *id, char *msg, char *parent)
  333. /* adds a message to the timeline */
  334. {
  335. xs *pfn = _timeline_find_fn(snac, id);
  336. FILE *f;
  337. if (pfn != NULL) {
  338. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  339. return;
  340. }
  341. /* build the new filename */
  342. xs *ntid = tid(0);
  343. xs *md5 = xs_md5_hex(id, strlen(id));
  344. xs *fn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  345. xs *md;
  346. /* add metadata */
  347. md = xs_json_loads("{"
  348. "\"children\": [],"
  349. "\"liked_by\": [],"
  350. "\"announced_by\": [],"
  351. "\"parent\": null"
  352. "}");
  353. if (parent != NULL)
  354. md = xs_dict_set(md, "parent", parent);
  355. msg = xs_dict_set(msg, "_snac", md);
  356. if ((f = fopen(fn, "w")) != NULL) {
  357. xs *j = xs_json_dumps_pp(msg, 4);
  358. fwrite(j, strlen(j), 1, f);
  359. fclose(f);
  360. snac_debug(snac, 1, xs_fmt("timeline_add %s %s", id, fn));
  361. }
  362. /* generated by this user? link to local timeline */
  363. if (xs_startswith(id, snac->actor)) {
  364. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  365. link(fn, lfn);
  366. snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn));
  367. }
  368. /* relink the parent */
  369. _timeline_parent(snac, parent, id);
  370. }
  371. d_char *_following_fn(snac *snac, char *actor)
  372. {
  373. xs *md5 = xs_md5_hex(actor, strlen(actor));
  374. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  375. }
  376. int following_add(snac *snac, char *actor, char *msg)
  377. /* adds to the following list */
  378. {
  379. int ret = 201; /* created */
  380. xs *fn = _following_fn(snac, actor);
  381. FILE *f;
  382. if ((f = fopen(fn, "w")) != NULL) {
  383. xs *j = xs_json_dumps_pp(msg, 4);
  384. fwrite(j, 1, strlen(j), f);
  385. fclose(f);
  386. }
  387. else
  388. ret = 500;
  389. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  390. return ret;
  391. }
  392. int following_del(snac *snac, char *actor)
  393. /* someone is no longer following us */
  394. {
  395. xs *fn = _following_fn(snac, actor);
  396. unlink(fn);
  397. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  398. return 200;
  399. }
  400. int following_check(snac *snac, char *actor)
  401. /* checks if someone is following us */
  402. {
  403. xs *fn = _following_fn(snac, actor);
  404. return !!(mtime(fn) != 0.0);
  405. }
  406. d_char *_muted_fn(snac *snac, char *actor)
  407. {
  408. xs *md5 = xs_md5_hex(actor, strlen(actor));
  409. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  410. }
  411. void mute(snac *snac, char *actor)
  412. /* mutes a moron */
  413. {
  414. xs *fn = _muted_fn(snac, actor);
  415. FILE *f;
  416. if ((f = fopen(fn, "w")) != NULL) {
  417. fprintf(f, "%s\n", actor);
  418. fclose(f);
  419. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  420. }
  421. }
  422. void unmute(snac *snac, char *actor)
  423. /* actor is no longer a moron */
  424. {
  425. xs *fn = _muted_fn(snac, actor);
  426. unlink(fn);
  427. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  428. }
  429. int is_muted(snac *snac, char *actor)
  430. /* check if someone is muted */
  431. {
  432. xs *fn = _muted_fn(snac, actor);
  433. return !!(mtime(fn) != 0.0);
  434. }
  435. void enqueue_output(snac *snac, char *actor, char *msg, int retries)
  436. /* enqueues an output message for an actor */
  437. {
  438. if (strcmp(actor, snac->actor) == 0) {
  439. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  440. return;
  441. }
  442. int qrt = xs_number_get(xs_dict_get(srv_config, "query_retry_minutes"));
  443. xs *ntid = tid(retries * 60 * qrt);
  444. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  445. xs *tfn = xs_str_cat(fn, ".tmp");
  446. FILE *f;
  447. if ((f = fopen(tfn, "w")) != NULL) {
  448. xs *qmsg = xs_dict_new();
  449. xs *rn = xs_number_new(retries);
  450. xs *j;
  451. qmsg = xs_dict_append(qmsg, "type", "output");
  452. qmsg = xs_dict_append(qmsg, "actor", actor);
  453. qmsg = xs_dict_append(qmsg, "object", msg);
  454. qmsg = xs_dict_append(qmsg, "retries", rn);
  455. j = xs_json_dumps_pp(qmsg, 4);
  456. fwrite(j, strlen(j), 1, f);
  457. fclose(f);
  458. rename(tfn, fn);
  459. snac_debug(snac, 2, xs_fmt("enqueue %s %s %d", actor, fn, retries));
  460. }
  461. }
  462. d_char *queue(snac *snac)
  463. /* returns a list with filenames that can be dequeued */
  464. {
  465. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  466. d_char *list = xs_list_new();
  467. glob_t globbuf;
  468. time_t t = time(NULL);
  469. if (glob(spec, 0, NULL, &globbuf) == 0) {
  470. int n;
  471. char *p;
  472. for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
  473. /* get the retry time from the basename */
  474. char *bn = strrchr(p, '/');
  475. time_t t2 = atol(bn + 1);
  476. if (t2 > t)
  477. snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
  478. else {
  479. list = xs_list_append(list, p);
  480. snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
  481. }
  482. }
  483. }
  484. globfree(&globbuf);
  485. return list;
  486. }
  487. d_char *dequeue(snac *snac, char *fn)
  488. /* dequeues a message */
  489. {
  490. FILE *f;
  491. d_char *obj = NULL;
  492. if ((f = fopen(fn, "r")) != NULL) {
  493. /* delete right now */
  494. unlink(fn);
  495. xs *j = xs_readall(f);
  496. obj = xs_json_loads(j);
  497. fclose(f);
  498. }
  499. return obj;
  500. }