data.c 32 KB

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