data.c 35 KB

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