data.c 33 KB

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