data.c 34 KB

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