data.c 34 KB

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