1
0

data.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 <glob.h>
  11. #include <sys/stat.h>
  12. int srv_open(char *basedir)
  13. /* opens a server */
  14. {
  15. int ret = 0;
  16. xs *cfg_file = NULL;
  17. FILE *f;
  18. d_char *error = NULL;
  19. srv_basedir = xs_str_new(basedir);
  20. if (xs_endswith(srv_basedir, "/"))
  21. srv_basedir = xs_crop(srv_basedir, 0, -1);
  22. cfg_file = xs_fmt("%s/server.json", basedir);
  23. if ((f = fopen(cfg_file, "r")) == NULL)
  24. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  25. else {
  26. xs *cfg_data;
  27. /* read full config file */
  28. cfg_data = xs_readall(f);
  29. fclose(f);
  30. /* parse */
  31. srv_config = xs_json_loads(cfg_data);
  32. if (srv_config == NULL)
  33. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  34. else {
  35. char *host;
  36. char *prefix;
  37. char *dbglvl;
  38. char *layout;
  39. double f = 0.0;
  40. host = xs_dict_get(srv_config, "host");
  41. prefix = xs_dict_get(srv_config, "prefix");
  42. dbglvl = xs_dict_get(srv_config, "dbglevel");
  43. layout = xs_dict_get(srv_config, "layout");
  44. if (host == NULL || prefix == NULL)
  45. error = xs_str_new("ERROR: cannot get server data");
  46. else {
  47. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  48. dbglevel = (int) xs_number_get(dbglvl);
  49. if ((dbglvl = getenv("DEBUG")) != NULL) {
  50. dbglevel = atoi(dbglvl);
  51. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  52. }
  53. if (!layout || (f = xs_number_get(layout)) != 2.0)
  54. error = xs_fmt("ERROR: unsupported old disk layout %f\n", f);
  55. else
  56. ret = 1;
  57. }
  58. }
  59. }
  60. if (ret == 0 && error != NULL)
  61. srv_log(error);
  62. return ret;
  63. }
  64. void srv_free(void)
  65. {
  66. xs_free(srv_basedir);
  67. xs_free(srv_config);
  68. xs_free(srv_baseurl);
  69. }
  70. void user_free(snac *snac)
  71. /* frees a user snac */
  72. {
  73. xs_free(snac->uid);
  74. xs_free(snac->basedir);
  75. xs_free(snac->config);
  76. xs_free(snac->key);
  77. xs_free(snac->actor);
  78. }
  79. int user_open(snac *snac, char *uid)
  80. /* opens a user */
  81. {
  82. int ret = 0;
  83. memset(snac, '\0', sizeof(struct _snac));
  84. if (validate_uid(uid)) {
  85. xs *cfg_file;
  86. FILE *f;
  87. snac->uid = xs_str_new(uid);
  88. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  89. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  90. if ((f = fopen(cfg_file, "r")) != NULL) {
  91. xs *cfg_data;
  92. /* read full config file */
  93. cfg_data = xs_readall(f);
  94. fclose(f);
  95. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  96. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  97. if ((f = fopen(key_file, "r")) != NULL) {
  98. xs *key_data;
  99. key_data = xs_readall(f);
  100. fclose(f);
  101. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  102. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  103. ret = 1;
  104. }
  105. else
  106. srv_log(xs_fmt("cannot parse '%s'", key_file));
  107. }
  108. else
  109. srv_log(xs_fmt("error opening '%s'", key_file));
  110. }
  111. else
  112. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  113. }
  114. else
  115. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  116. }
  117. else
  118. srv_log(xs_fmt("invalid user '%s'", uid));
  119. if (!ret)
  120. user_free(snac);
  121. return ret;
  122. }
  123. d_char *user_list(void)
  124. /* returns the list of user ids */
  125. {
  126. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  127. return xs_glob(spec, 1, 0);
  128. }
  129. double mtime(char *fn)
  130. /* returns the mtime of a file or directory, or 0.0 */
  131. {
  132. struct stat st;
  133. double r = 0.0;
  134. if (fn && stat(fn, &st) != -1)
  135. r = (double)st.st_mtim.tv_sec;
  136. return r;
  137. }
  138. d_char *_follower_fn(snac *snac, char *actor)
  139. {
  140. xs *md5 = xs_md5_hex(actor, strlen(actor));
  141. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  142. }
  143. int follower_add(snac *snac, char *actor, char *msg)
  144. /* adds a follower */
  145. {
  146. int ret = 201; /* created */
  147. xs *fn = _follower_fn(snac, actor);
  148. FILE *f;
  149. if ((f = fopen(fn, "w")) != NULL) {
  150. xs *j = xs_json_dumps_pp(msg, 4);
  151. fwrite(j, 1, strlen(j), f);
  152. fclose(f);
  153. }
  154. else
  155. ret = 500;
  156. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  157. return ret;
  158. }
  159. int follower_del(snac *snac, char *actor)
  160. /* deletes a follower */
  161. {
  162. int status = 200;
  163. xs *fn = _follower_fn(snac, actor);
  164. if (fn != NULL)
  165. unlink(fn);
  166. else
  167. status = 404;
  168. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  169. return status;
  170. }
  171. int follower_check(snac *snac, char *actor)
  172. /* checks if someone is a follower */
  173. {
  174. xs *fn = _follower_fn(snac, actor);
  175. return !!(mtime(fn) != 0.0);
  176. }
  177. d_char *follower_list(snac *snac)
  178. /* returns the list of followers */
  179. {
  180. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  181. xs *glist = xs_glob(spec, 0, 0);
  182. char *p, *v;
  183. d_char *list = xs_list_new();
  184. /* iterate the list of files */
  185. p = glist;
  186. while (xs_list_iter(&p, &v)) {
  187. FILE *f;
  188. /* load the follower data */
  189. if ((f = fopen(v, "r")) != NULL) {
  190. xs *j = xs_readall(f);
  191. fclose(f);
  192. if (j != NULL) {
  193. xs *o = xs_json_loads(j);
  194. if (o != NULL)
  195. list = xs_list_append(list, o);
  196. }
  197. }
  198. }
  199. return list;
  200. }
  201. double timeline_mtime(snac *snac)
  202. {
  203. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  204. return mtime(fn);
  205. }
  206. d_char *_timeline_find_fn(snac *snac, char *id)
  207. /* returns the file name of a timeline entry by its id */
  208. {
  209. xs *md5 = xs_md5_hex(id, strlen(id));
  210. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  211. xs *list = NULL;
  212. d_char *fn = NULL;
  213. int l;
  214. list = xs_glob(spec, 0, 0);
  215. l = xs_list_len(list);
  216. /* if there is something, get the first one */
  217. if (l > 0) {
  218. fn = xs_str_new(xs_list_get(list, 0));
  219. if (l > 1)
  220. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  221. }
  222. return fn;
  223. }
  224. int timeline_here(snac *snac, char *id)
  225. /* checks if an object is already downloaded */
  226. {
  227. xs *fn = _timeline_find_fn(snac, id);
  228. return fn != NULL;
  229. }
  230. d_char *timeline_find(snac *snac, char *id)
  231. /* gets a message from the timeline by id */
  232. {
  233. xs *fn = _timeline_find_fn(snac, id);
  234. d_char *msg = NULL;
  235. if (fn != NULL) {
  236. FILE *f;
  237. if ((f = fopen(fn, "r")) != NULL) {
  238. xs *j = xs_readall(f);
  239. msg = xs_json_loads(j);
  240. fclose(f);
  241. }
  242. }
  243. return msg;
  244. }
  245. int timeline_del(snac *snac, char *id)
  246. /* deletes a message from the timeline */
  247. {
  248. int ret = 404;
  249. xs *fn = _timeline_find_fn(snac, id);
  250. if (fn != NULL) {
  251. xs *lfn = NULL;
  252. unlink(fn);
  253. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  254. /* try to delete also from the local timeline */
  255. lfn = xs_replace(fn, "/timeline/", "/local/");
  256. if (unlink(lfn) != -1)
  257. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  258. ret = 200;
  259. }
  260. return ret;
  261. }
  262. d_char *timeline_get(snac *snac, char *fn)
  263. /* gets a timeline entry by file name */
  264. {
  265. d_char *d = NULL;
  266. FILE *f;
  267. if ((f = fopen(fn, "r")) != NULL) {
  268. xs *j = xs_readall(f);
  269. d = xs_json_loads(j);
  270. fclose(f);
  271. }
  272. return d;
  273. }
  274. d_char *_timeline_list(snac *snac, char *directory, int max)
  275. /* returns a list of the timeline filenames */
  276. {
  277. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  278. int c_max;
  279. /* maximum number of items in the timeline */
  280. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  281. /* never more timeline entries than the configured maximum */
  282. if (max > c_max)
  283. max = c_max;
  284. return xs_glob_n(spec, 0, 1, max);
  285. }
  286. d_char *timeline_list(snac *snac, int max)
  287. {
  288. return _timeline_list(snac, "timeline", max);
  289. }
  290. d_char *local_list(snac *snac, int max)
  291. {
  292. return _timeline_list(snac, "local", max);
  293. }
  294. d_char *_timeline_new_fn(snac *snac, char *id)
  295. /* creates a new filename */
  296. {
  297. xs *ntid = tid(0);
  298. xs *md5 = xs_md5_hex(id, strlen(id));
  299. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  300. }
  301. void _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  302. /* writes a timeline entry and refreshes the ancestors */
  303. {
  304. xs *fn = _timeline_new_fn(snac, id);
  305. FILE *f;
  306. if ((f = fopen(fn, "w")) != NULL) {
  307. xs *j = xs_json_dumps_pp(msg, 4);
  308. fwrite(j, strlen(j), 1, f);
  309. fclose(f);
  310. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  311. }
  312. /* related to this user? link to local timeline */
  313. if (xs_startswith(id, snac->actor) ||
  314. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  315. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  316. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  317. link(fn, lfn);
  318. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  319. }
  320. if (!xs_is_null(parent)) {
  321. /* update the parent, adding this id to its children list */
  322. xs *pfn = _timeline_find_fn(snac, parent);
  323. xs *p_msg = NULL;
  324. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  325. xs *j;
  326. j = xs_readall(f);
  327. fclose(f);
  328. p_msg = xs_json_loads(j);
  329. }
  330. if (p_msg == NULL)
  331. return;
  332. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  333. xs *children = xs_dup(xs_dict_get(meta, "children"));
  334. /* add the child if it's not already there */
  335. if (xs_list_in(children, id) == -1)
  336. children = xs_list_append(children, id);
  337. /* re-store */
  338. meta = xs_dict_set(meta, "children", children);
  339. p_msg = xs_dict_set(p_msg, "_snac", meta);
  340. xs *nfn = _timeline_new_fn(snac, parent);
  341. if ((f = fopen(nfn, "w")) != NULL) {
  342. xs *j = xs_json_dumps_pp(p_msg, 4);
  343. fwrite(j, strlen(j), 1, f);
  344. fclose(f);
  345. unlink(pfn);
  346. snac_debug(snac, 1,
  347. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  348. /* try to do the same with the local */
  349. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  350. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  351. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  352. link(nfn, nlfn);
  353. snac_debug(snac, 1,
  354. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  355. }
  356. }
  357. else
  358. return;
  359. /* now iterate all parents up, just renaming the files */
  360. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  361. while (!xs_is_null(grampa)) {
  362. xs *gofn = _timeline_find_fn(snac, grampa);
  363. if (gofn == NULL)
  364. break;
  365. /* create the new filename */
  366. xs *gnfn = _timeline_new_fn(snac, grampa);
  367. rename(gofn, gnfn);
  368. snac_debug(snac, 1,
  369. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  370. /* try to do the same with the local */
  371. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  372. if (unlink(golfn) != -1) {
  373. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  374. link(gnfn, gnlfn);
  375. snac_debug(snac, 1,
  376. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  377. }
  378. /* now open it and get its own parent */
  379. if ((f = fopen(gnfn, "r")) != NULL) {
  380. xs *j = xs_readall(f);
  381. fclose(f);
  382. xs *g_msg = xs_json_loads(j);
  383. d_char *meta = xs_dict_get(g_msg, "_snac");
  384. d_char *p = xs_dict_get(meta, "parent");
  385. free(grampa);
  386. grampa = xs_dup(p);
  387. }
  388. }
  389. }
  390. }
  391. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  392. /* adds a message to the timeline */
  393. {
  394. xs *pfn = _timeline_find_fn(snac, id);
  395. if (pfn != NULL) {
  396. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  397. return 0;
  398. }
  399. xs *msg = xs_dup(o_msg);
  400. xs *md;
  401. /* add new metadata */
  402. md = xs_json_loads("{"
  403. "\"children\": [],"
  404. "\"liked_by\": [],"
  405. "\"announced_by\": [],"
  406. "\"version\": \"" USER_AGENT "\","
  407. "\"referrer\": null,"
  408. "\"parent\": null"
  409. "}");
  410. if (!xs_is_null(parent))
  411. md = xs_dict_set(md, "parent", parent);
  412. if (!xs_is_null(referrer))
  413. md = xs_dict_set(md, "referrer", referrer);
  414. msg = xs_dict_set(msg, "_snac", md);
  415. _timeline_write(snac, id, msg, parent, referrer);
  416. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  417. return 1;
  418. }
  419. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  420. /* updates a timeline entry with a new admiration */
  421. {
  422. xs *ofn = _timeline_find_fn(snac, id);
  423. FILE *f;
  424. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  425. xs *j1 = xs_readall(f);
  426. fclose(f);
  427. xs *msg = xs_json_loads(j1);
  428. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  429. xs *list;
  430. if (like)
  431. list = xs_dup(xs_dict_get(meta, "liked_by"));
  432. else
  433. list = xs_dup(xs_dict_get(meta, "announced_by"));
  434. /* add the admirer if it's not already there */
  435. if (xs_list_in(list, admirer) == -1)
  436. list = xs_list_append(list, admirer);
  437. /* set the admirer as the referrer (if not already set) */
  438. if (!like && xs_is_null(xs_dict_get(meta, "referrer")))
  439. meta = xs_dict_set(meta, "referrer", admirer);
  440. /* re-store */
  441. if (like)
  442. meta = xs_dict_set(meta, "liked_by", list);
  443. else
  444. meta = xs_dict_set(meta, "announced_by", list);
  445. msg = xs_dict_set(msg, "_snac", meta);
  446. unlink(ofn);
  447. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  448. unlink(ofn);
  449. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  450. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  451. like ? "Like" : "Announce", id, admirer));
  452. }
  453. else
  454. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  455. }
  456. d_char *_following_fn(snac *snac, char *actor)
  457. {
  458. xs *md5 = xs_md5_hex(actor, strlen(actor));
  459. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  460. }
  461. int following_add(snac *snac, char *actor, char *msg)
  462. /* adds to the following list */
  463. {
  464. int ret = 201; /* created */
  465. xs *fn = _following_fn(snac, actor);
  466. FILE *f;
  467. if ((f = fopen(fn, "w")) != NULL) {
  468. xs *j = xs_json_dumps_pp(msg, 4);
  469. fwrite(j, 1, strlen(j), f);
  470. fclose(f);
  471. }
  472. else
  473. ret = 500;
  474. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  475. return ret;
  476. }
  477. int following_del(snac *snac, char *actor)
  478. /* someone is no longer following us */
  479. {
  480. xs *fn = _following_fn(snac, actor);
  481. unlink(fn);
  482. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  483. return 200;
  484. }
  485. int following_check(snac *snac, char *actor)
  486. /* checks if someone is following us */
  487. {
  488. xs *fn = _following_fn(snac, actor);
  489. return !!(mtime(fn) != 0.0);
  490. }
  491. int following_get(snac *snac, char *actor, d_char **data)
  492. /* returns the 'Follow' object */
  493. {
  494. xs *fn = _following_fn(snac, actor);
  495. FILE *f;
  496. int status = 200;
  497. if ((f = fopen(fn, "r")) != NULL) {
  498. xs *j = xs_readall(f);
  499. fclose(f);
  500. *data = xs_json_loads(j);
  501. }
  502. else
  503. status = 404;
  504. return status;
  505. }
  506. d_char *_muted_fn(snac *snac, char *actor)
  507. {
  508. xs *md5 = xs_md5_hex(actor, strlen(actor));
  509. return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
  510. }
  511. void mute(snac *snac, char *actor)
  512. /* mutes a moron */
  513. {
  514. xs *fn = _muted_fn(snac, actor);
  515. FILE *f;
  516. if ((f = fopen(fn, "w")) != NULL) {
  517. fprintf(f, "%s\n", actor);
  518. fclose(f);
  519. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  520. }
  521. }
  522. void unmute(snac *snac, char *actor)
  523. /* actor is no longer a moron */
  524. {
  525. xs *fn = _muted_fn(snac, actor);
  526. unlink(fn);
  527. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  528. }
  529. int is_muted(snac *snac, char *actor)
  530. /* check if someone is muted */
  531. {
  532. xs *fn = _muted_fn(snac, actor);
  533. return !!(mtime(fn) != 0.0);
  534. }
  535. d_char *_actor_fn(snac *snac, char *actor)
  536. /* returns the file name for an actor */
  537. {
  538. xs *md5 = xs_md5_hex(actor, strlen(actor));
  539. return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
  540. }
  541. int actor_add(snac *snac, char *actor, char *msg)
  542. /* adds an actor */
  543. {
  544. int ret = 201; /* created */
  545. xs *fn = _actor_fn(snac, actor);
  546. FILE *f;
  547. if ((f = fopen(fn, "w")) != NULL) {
  548. xs *j = xs_json_dumps_pp(msg, 4);
  549. fwrite(j, 1, strlen(j), f);
  550. fclose(f);
  551. }
  552. else
  553. ret = 500;
  554. snac_debug(snac, 2, xs_fmt("actor_add %s %s", actor, fn));
  555. return ret;
  556. }
  557. int actor_get(snac *snac, char *actor, d_char **data)
  558. /* returns an already downloaded actor */
  559. {
  560. xs *fn = _actor_fn(snac, actor);
  561. double t;
  562. double max_time;
  563. int status;
  564. FILE *f;
  565. t = mtime(fn);
  566. /* no mtime? there is nothing here */
  567. if (t == 0.0)
  568. return 404;
  569. /* maximum time for the actor data to be considered stale */
  570. max_time = 3600.0 * 36.0;
  571. if (t + max_time < (double) time(NULL)) {
  572. /* actor data exists but also stinks */
  573. if ((f = fopen(fn, "a")) != NULL) {
  574. /* write a blank at the end to 'touch' the file */
  575. fwrite(" ", 1, 1, f);
  576. fclose(f);
  577. }
  578. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  579. }
  580. else {
  581. /* it's still valid */
  582. status = 200;
  583. }
  584. if (data) {
  585. if ((f = fopen(fn, "r")) != NULL) {
  586. xs *j = xs_readall(f);
  587. fclose(f);
  588. *data = xs_json_loads(j);
  589. }
  590. else
  591. status = 500;
  592. }
  593. return status;
  594. }
  595. d_char *_static_fn(snac *snac, const char *id)
  596. /* gets the filename for a static file */
  597. {
  598. return xs_fmt("%s/static/%s", snac->basedir, id);
  599. }
  600. int static_get(snac *snac, const char *id, d_char **data, int *size)
  601. /* returns static content */
  602. {
  603. xs *fn = _static_fn(snac, id);
  604. FILE *f;
  605. int status = 404;
  606. *size = 0xfffffff;
  607. if ((f = fopen(fn, "rb")) != NULL) {
  608. *data = xs_read(f, size);
  609. fclose(f);
  610. status = 200;
  611. }
  612. return status;
  613. }
  614. void static_put(snac *snac, const char *id, const char *data, int size)
  615. /* writes status content */
  616. {
  617. xs *fn = _static_fn(snac, id);
  618. FILE *f;
  619. if ((f = fopen(fn, "wb")) != NULL) {
  620. fwrite(data, size, 1, f);
  621. fclose(f);
  622. }
  623. }
  624. d_char *_history_fn(snac *snac, char *id)
  625. /* gets the filename for the history */
  626. {
  627. return xs_fmt("%s/history/%s", snac->basedir, id);
  628. }
  629. double history_mtime(snac *snac, char * id)
  630. {
  631. double t = 0.0;
  632. xs *fn = _history_fn(snac, id);
  633. if (fn != NULL)
  634. t = mtime(fn);
  635. return t;
  636. }
  637. void history_add(snac *snac, char *id, char *content, int size)
  638. /* adds something to the history */
  639. {
  640. xs *fn = _history_fn(snac, id);
  641. FILE *f;
  642. if ((f = fopen(fn, "w")) != NULL) {
  643. fwrite(content, size, 1, f);
  644. fclose(f);
  645. }
  646. }
  647. d_char *history_get(snac *snac, char *id)
  648. {
  649. d_char *content = NULL;
  650. xs *fn = _history_fn(snac, id);
  651. FILE *f;
  652. if ((f = fopen(fn, "r")) != NULL) {
  653. content = xs_readall(f);
  654. fclose(f);
  655. }
  656. return content;
  657. }
  658. int history_del(snac *snac, char *id)
  659. {
  660. xs *fn = _history_fn(snac, id);
  661. return unlink(fn);
  662. }
  663. d_char *history_list(snac *snac)
  664. {
  665. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  666. return xs_glob(spec, 1, 0);
  667. }
  668. static int _enqueue_put(char *fn, char *msg)
  669. /* writes safely to the queue */
  670. {
  671. int ret = 1;
  672. xs *tfn = xs_fmt("%s.tmp", fn);
  673. FILE *f;
  674. if ((f = fopen(tfn, "w")) != NULL) {
  675. xs *j = xs_json_dumps_pp(msg, 4);
  676. fwrite(j, strlen(j), 1, f);
  677. fclose(f);
  678. rename(tfn, fn);
  679. }
  680. else
  681. ret = 0;
  682. return ret;
  683. }
  684. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  685. /* enqueues an input message */
  686. {
  687. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  688. xs *ntid = tid(retries * 60 * qrt);
  689. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  690. xs *qmsg = xs_dict_new();
  691. xs *rn = xs_number_new(retries);
  692. qmsg = xs_dict_append(qmsg, "type", "input");
  693. qmsg = xs_dict_append(qmsg, "object", msg);
  694. qmsg = xs_dict_append(qmsg, "req", req);
  695. qmsg = xs_dict_append(qmsg, "retries", rn);
  696. _enqueue_put(fn, qmsg);
  697. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  698. }
  699. void enqueue_output(snac *snac, char *msg, char *actor, int retries)
  700. /* enqueues an output message for an actor */
  701. {
  702. if (strcmp(actor, snac->actor) == 0) {
  703. snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
  704. return;
  705. }
  706. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  707. xs *ntid = tid(retries * 60 * qrt);
  708. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  709. xs *qmsg = xs_dict_new();
  710. xs *rn = xs_number_new(retries);
  711. qmsg = xs_dict_append(qmsg, "type", "output");
  712. qmsg = xs_dict_append(qmsg, "actor", actor);
  713. qmsg = xs_dict_append(qmsg, "object", msg);
  714. qmsg = xs_dict_append(qmsg, "retries", rn);
  715. _enqueue_put(fn, qmsg);
  716. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
  717. }
  718. void enqueue_email(snac *snac, char *msg, int retries)
  719. /* enqueues an email message to be sent */
  720. {
  721. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  722. xs *ntid = tid(retries * 60 * qrt);
  723. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  724. xs *qmsg = xs_dict_new();
  725. xs *rn = xs_number_new(retries);
  726. qmsg = xs_dict_append(qmsg, "type", "email");
  727. qmsg = xs_dict_append(qmsg, "message", msg);
  728. qmsg = xs_dict_append(qmsg, "retries", rn);
  729. _enqueue_put(fn, qmsg);
  730. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  731. }
  732. d_char *queue(snac *snac)
  733. /* returns a list with filenames that can be dequeued */
  734. {
  735. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  736. d_char *list = xs_list_new();
  737. time_t t = time(NULL);
  738. char *p, *v;
  739. xs *fns = xs_glob(spec, 0, 0);
  740. p = fns;
  741. while (xs_list_iter(&p, &v)) {
  742. /* get the retry time from the basename */
  743. char *bn = strrchr(v, '/');
  744. time_t t2 = atol(bn + 1);
  745. if (t2 > t)
  746. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  747. else {
  748. list = xs_list_append(list, v);
  749. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  750. }
  751. }
  752. return list;
  753. }
  754. d_char *dequeue(snac *snac, char *fn)
  755. /* dequeues a message */
  756. {
  757. FILE *f;
  758. d_char *obj = NULL;
  759. if ((f = fopen(fn, "r")) != NULL) {
  760. /* delete right now */
  761. unlink(fn);
  762. xs *j = xs_readall(f);
  763. obj = xs_json_loads(j);
  764. fclose(f);
  765. }
  766. return obj;
  767. }
  768. void purge(snac *snac)
  769. /* do the purge */
  770. {
  771. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  772. /* purge days set to 0? disable purging */
  773. if (tpd == 0) {
  774. /* well, enjoy your data drive exploding */
  775. return;
  776. }
  777. time_t mt = time(NULL) - tpd * 24 * 3600;
  778. xs *t_spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
  779. xs *t_list = xs_glob(t_spec, 0, 0);
  780. char *p, *v;
  781. p = t_list;
  782. while (xs_list_iter(&p, &v)) {
  783. if (mtime(v) < mt) {
  784. /* older than the minimum time: delete it */
  785. unlink(v);
  786. snac_debug(snac, 1, xs_fmt("purged %s", v));
  787. }
  788. }
  789. xs *a_spec = xs_fmt("%s/actors/" "*.json", snac->basedir);
  790. xs *a_list = xs_glob(a_spec, 0, 0);
  791. p = a_list;
  792. while (xs_list_iter(&p, &v)) {
  793. if (mtime(v) < mt) {
  794. /* older than the minimum time: delete it */
  795. unlink(v);
  796. snac_debug(snac, 1, xs_fmt("purged %s", v));
  797. }
  798. }
  799. }
  800. void purge_all(void)
  801. /* purge all users */
  802. {
  803. snac snac;
  804. xs *list = user_list();
  805. char *p, *uid;
  806. p = list;
  807. while (xs_list_iter(&p, &uid)) {
  808. if (user_open(&snac, uid)) {
  809. purge(&snac);
  810. user_free(&snac);
  811. }
  812. }
  813. }