data.c 30 KB

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