data.c 13 KB

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