data.c 30 KB

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