data.c 24 KB

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