data.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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 "xs_set.h"
  9. #include "snac.h"
  10. #include <time.h>
  11. #include <sys/stat.h>
  12. #include <sys/file.h>
  13. #include <fcntl.h>
  14. double db_layout = 2.4;
  15. int db_upgrade(d_char **error);
  16. int srv_open(char *basedir, int auto_upgrade)
  17. /* opens a server */
  18. {
  19. int ret = 0;
  20. xs *cfg_file = NULL;
  21. FILE *f;
  22. d_char *error = NULL;
  23. srv_basedir = xs_str_new(basedir);
  24. if (xs_endswith(srv_basedir, "/"))
  25. srv_basedir = xs_crop(srv_basedir, 0, -1);
  26. cfg_file = xs_fmt("%s/server.json", basedir);
  27. if ((f = fopen(cfg_file, "r")) == NULL)
  28. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  29. else {
  30. xs *cfg_data;
  31. /* read full config file */
  32. cfg_data = xs_readall(f);
  33. fclose(f);
  34. /* parse */
  35. srv_config = xs_json_loads(cfg_data);
  36. if (srv_config == NULL)
  37. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  38. else {
  39. char *host;
  40. char *prefix;
  41. char *dbglvl;
  42. host = xs_dict_get(srv_config, "host");
  43. prefix = xs_dict_get(srv_config, "prefix");
  44. dbglvl = xs_dict_get(srv_config, "dbglevel");
  45. if (host == NULL || prefix == NULL)
  46. error = xs_str_new("ERROR: cannot get server data");
  47. else {
  48. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  49. dbglevel = (int) xs_number_get(dbglvl);
  50. if ((dbglvl = getenv("DEBUG")) != NULL) {
  51. dbglevel = atoi(dbglvl);
  52. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  53. }
  54. if (auto_upgrade)
  55. ret = db_upgrade(&error);
  56. else {
  57. if (xs_number_get(xs_dict_get(srv_config, "layout")) < db_layout)
  58. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  59. else
  60. ret = 1;
  61. }
  62. }
  63. }
  64. }
  65. if (error != NULL)
  66. srv_log(error);
  67. /* disabled temporarily; messages can't be sent (libcurl issue?) */
  68. #if 0
  69. #ifdef __OpenBSD__
  70. srv_debug(2, xs_fmt("Calling unveil()"));
  71. unveil(basedir, "rwc");
  72. unveil("/usr/sbin", "x");
  73. unveil(NULL, NULL);
  74. #endif /* __OpenBSD__ */
  75. #endif
  76. return ret;
  77. }
  78. void srv_free(void)
  79. {
  80. xs_free(srv_basedir);
  81. xs_free(srv_config);
  82. xs_free(srv_baseurl);
  83. }
  84. void user_free(snac *snac)
  85. /* frees a user snac */
  86. {
  87. xs_free(snac->uid);
  88. xs_free(snac->basedir);
  89. xs_free(snac->config);
  90. xs_free(snac->key);
  91. xs_free(snac->actor);
  92. }
  93. int user_open(snac *snac, char *uid)
  94. /* opens a user */
  95. {
  96. int ret = 0;
  97. memset(snac, '\0', sizeof(struct _snac));
  98. if (validate_uid(uid)) {
  99. xs *cfg_file;
  100. FILE *f;
  101. snac->uid = xs_str_new(uid);
  102. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  103. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  104. if ((f = fopen(cfg_file, "r")) != NULL) {
  105. xs *cfg_data;
  106. /* read full config file */
  107. cfg_data = xs_readall(f);
  108. fclose(f);
  109. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  110. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  111. if ((f = fopen(key_file, "r")) != NULL) {
  112. xs *key_data;
  113. key_data = xs_readall(f);
  114. fclose(f);
  115. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  116. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  117. ret = 1;
  118. }
  119. else
  120. srv_log(xs_fmt("cannot parse '%s'", key_file));
  121. }
  122. else
  123. srv_log(xs_fmt("error opening '%s'", key_file));
  124. }
  125. else
  126. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  127. }
  128. else
  129. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  130. }
  131. else
  132. srv_log(xs_fmt("invalid user '%s'", uid));
  133. if (!ret)
  134. user_free(snac);
  135. return ret;
  136. }
  137. d_char *user_list(void)
  138. /* returns the list of user ids */
  139. {
  140. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  141. return xs_glob(spec, 1, 0);
  142. }
  143. double mtime_nl(const char *fn, int *n_link)
  144. /* returns the mtime and number of links of a file or directory, or 0.0 */
  145. {
  146. struct stat st;
  147. double r = 0.0;
  148. int n = 0;
  149. if (fn && stat(fn, &st) != -1) {
  150. r = (double) st.st_mtim.tv_sec;
  151. n = st.st_nlink;
  152. }
  153. if (n_link)
  154. *n_link = n;
  155. return r;
  156. }
  157. /** database 2.1+ **/
  158. /** indexes **/
  159. int index_add_md5(const char *fn, const char *md5)
  160. /* adds an md5 to an index */
  161. {
  162. int status = 201; /* Created */
  163. FILE *f;
  164. if ((f = fopen(fn, "a")) != NULL) {
  165. flock(fileno(f), LOCK_EX);
  166. fprintf(f, "%s\n", md5);
  167. fclose(f);
  168. }
  169. else
  170. status = 500;
  171. return status;
  172. }
  173. int index_add(const char *fn, const char *id)
  174. /* adds an id to an index */
  175. {
  176. xs *md5 = xs_md5_hex(id, strlen(id));
  177. return index_add_md5(fn, md5);
  178. }
  179. int index_del(const char *fn, const char *md5)
  180. /* deletes an md5 from an index */
  181. {
  182. int status = 404;
  183. FILE *i, *o;
  184. if ((i = fopen(fn, "r")) != NULL) {
  185. flock(fileno(i), LOCK_EX);
  186. xs *nfn = xs_fmt("%s.new", fn);
  187. char line[256];
  188. if ((o = fopen(nfn, "w")) != NULL) {
  189. while (fgets(line, sizeof(line), i) != NULL) {
  190. line[32] = '\0';
  191. if (memcmp(line, md5, 32) != 0)
  192. fprintf(o, "%s\n", line);
  193. }
  194. fclose(o);
  195. xs *ofn = xs_fmt("%s.bak", fn);
  196. link(fn, ofn);
  197. rename(nfn, fn);
  198. }
  199. else
  200. status = 500;
  201. fclose(i);
  202. }
  203. else
  204. status = 500;
  205. return status;
  206. }
  207. int index_in_md5(const char *fn, const char *md5)
  208. /* checks if the md5 is already in the index */
  209. {
  210. FILE *f;
  211. int ret = 0;
  212. if ((f = fopen(fn, "r")) != NULL) {
  213. flock(fileno(f), LOCK_SH);
  214. char line[256];
  215. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  216. line[32] = '\0';
  217. if (strcmp(line, md5) == 0)
  218. ret = 1;
  219. }
  220. fclose(f);
  221. }
  222. return ret;
  223. }
  224. int index_in(const char *fn, const char *id)
  225. /* checks if the object id is already in the index */
  226. {
  227. xs *md5 = xs_md5_hex(id, strlen(id));
  228. return index_in_md5(fn, md5);
  229. }
  230. int index_first(const char *fn, char *line, int size)
  231. /* reads the first entry of an index */
  232. {
  233. FILE *f;
  234. int ret = 0;
  235. if ((f = fopen(fn, "r")) != NULL) {
  236. if (fgets(line, size, f) != NULL) {
  237. line[32] = '\0';
  238. ret = 1;
  239. }
  240. fclose(f);
  241. }
  242. return ret;
  243. }
  244. d_char *index_list(const char *fn, int max)
  245. /* returns an index as a list */
  246. {
  247. d_char *list = NULL;
  248. FILE *f;
  249. int n = 0;
  250. if ((f = fopen(fn, "r")) != NULL) {
  251. flock(fileno(f), LOCK_SH);
  252. char line[256];
  253. list = xs_list_new();
  254. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  255. line[32] = '\0';
  256. list = xs_list_append(list, line);
  257. n++;
  258. }
  259. fclose(f);
  260. }
  261. return list;
  262. }
  263. d_char *index_list_desc(const char *fn, int max)
  264. /* returns an index as a list, in reverse order */
  265. {
  266. d_char *list = NULL;
  267. FILE *f;
  268. int n = 0;
  269. if ((f = fopen(fn, "r")) != NULL) {
  270. flock(fileno(f), LOCK_SH);
  271. char line[256];
  272. list = xs_list_new();
  273. /* move to the end minus one entry */
  274. if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
  275. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  276. line[32] = '\0';
  277. list = xs_list_append(list, line);
  278. n++;
  279. /* move backwards 2 entries */
  280. if (fseek(f, -66, SEEK_CUR) == -1)
  281. break;
  282. }
  283. }
  284. fclose(f);
  285. }
  286. return list;
  287. }
  288. /** objects **/
  289. d_char *_object_fn_by_md5(const char *md5)
  290. {
  291. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  292. mkdir(bfn, 0755);
  293. return xs_fmt("%s/%s.json", bfn, md5);
  294. }
  295. d_char *_object_fn(const char *id)
  296. {
  297. xs *md5 = xs_md5_hex(id, strlen(id));
  298. return _object_fn_by_md5(md5);
  299. }
  300. int object_get_by_md5(const char *md5, d_char **obj, const char *type)
  301. /* returns a stored object, optionally of the requested type */
  302. {
  303. int status = 404;
  304. xs *fn = _object_fn_by_md5(md5);
  305. FILE *f;
  306. if ((f = fopen(fn, "r")) != NULL) {
  307. flock(fileno(f), LOCK_SH);
  308. xs *j = xs_readall(f);
  309. fclose(f);
  310. *obj = xs_json_loads(j);
  311. if (*obj) {
  312. status = 200;
  313. /* specific type requested? */
  314. if (!xs_is_null(type)) {
  315. char *v = xs_dict_get(*obj, "type");
  316. if (xs_is_null(v) || strcmp(v, type) != 0) {
  317. status = 404;
  318. *obj = xs_free(*obj);
  319. }
  320. }
  321. }
  322. }
  323. else
  324. *obj = NULL;
  325. return status;
  326. }
  327. int object_get(const char *id, d_char **obj, const char *type)
  328. /* returns a stored object, optionally of the requested type */
  329. {
  330. xs *md5 = xs_md5_hex(id, strlen(id));
  331. return object_get_by_md5(md5, obj, type);
  332. }
  333. int _object_add(const char *id, d_char *obj, int ow)
  334. /* stores an object */
  335. {
  336. int status = 201; /* Created */
  337. xs *fn = _object_fn(id);
  338. FILE *f;
  339. if (!ow && mtime(fn) > 0.0) {
  340. /* object already here */
  341. srv_debug(0, xs_fmt("object_add object already here %s", id));
  342. return 204; /* No content */
  343. }
  344. if ((f = fopen(fn, "w")) != NULL) {
  345. flock(fileno(f), LOCK_EX);
  346. xs *j = xs_json_dumps_pp(obj, 4);
  347. fwrite(j, strlen(j), 1, f);
  348. fclose(f);
  349. /* does this object has a parent? */
  350. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  351. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  352. /* update the children index of the parent */
  353. xs *c_idx = _object_fn(in_reply_to);
  354. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  355. index_add(c_idx, id);
  356. srv_debug(0, xs_fmt("object_add added child %s to %s", id, c_idx));
  357. /* create a one-element index with the parent */
  358. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  359. index_add(p_idx, in_reply_to);
  360. srv_debug(0, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  361. }
  362. }
  363. else
  364. status = 500;
  365. srv_debug(0, xs_fmt("object_add %s %s %d", id, fn, status));
  366. return status;
  367. }
  368. int object_add(const char *id, d_char *obj)
  369. /* stores an object */
  370. {
  371. return _object_add(id, obj, 0);
  372. }
  373. int object_add_ow(const char *id, d_char *obj)
  374. /* stores an object (overwriting allowed) */
  375. {
  376. return _object_add(id, obj, 1);
  377. }
  378. int object_del_by_md5(const char *md5)
  379. /* deletes an object by its md5 */
  380. {
  381. int status = 404;
  382. xs *fn = _object_fn_by_md5(md5);
  383. if (fn != NULL && unlink(fn) != -1) {
  384. status = 200;
  385. /* also delete associated indexes */
  386. xs *spec = xs_dup(fn);
  387. spec = xs_replace_i(spec, ".json", "*.idx");
  388. xs *files = xs_glob(spec, 0, 0);
  389. char *p, *v;
  390. p = files;
  391. while (xs_list_iter(&p, &v)) {
  392. srv_debug(0, xs_fmt("object_del index %s", v));
  393. unlink(v);
  394. }
  395. }
  396. srv_debug(0, xs_fmt("object_del %s %d", fn, status));
  397. return status;
  398. }
  399. int object_del(const char *id)
  400. /* deletes an object */
  401. {
  402. xs *md5 = xs_md5_hex(id, strlen(id));
  403. return object_del_by_md5(md5);
  404. }
  405. d_char *object_children(const char *id)
  406. /* returns the list of an object's children */
  407. {
  408. xs *fn = _object_fn(id);
  409. fn = xs_replace_i(fn, ".json", "_c.idx");
  410. return index_list(fn, XS_ALL);
  411. }
  412. int object_admire(const char *id, const char *actor, int like)
  413. /* actor likes or announces this object */
  414. {
  415. int status = 200;
  416. xs *fn = _object_fn(id);
  417. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  418. if (!index_in(fn, actor)) {
  419. status = index_add(fn, actor);
  420. srv_debug(0, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  421. }
  422. return status;
  423. }
  424. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  425. /* adds or deletes from a user cache */
  426. {
  427. xs *ofn = _object_fn(id);
  428. xs *l = xs_split(ofn, "/");
  429. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  430. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  431. int ret;
  432. if (del) {
  433. if ((ret = unlink(cfn)) != -1)
  434. index_del(idx, id);
  435. }
  436. else {
  437. index_add(idx, id);
  438. ret = link(ofn, cfn);
  439. }
  440. return ret;
  441. }
  442. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  443. /* caches an object into a user cache */
  444. {
  445. return _object_user_cache(snac, id, cachedir, 0);
  446. }
  447. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  448. /* deletes an object from a user cache */
  449. {
  450. return _object_user_cache(snac, id, cachedir, 1);
  451. }
  452. /** specialized functions **/
  453. d_char *_follower_fn(snac *snac, char *actor)
  454. {
  455. xs *md5 = xs_md5_hex(actor, strlen(actor));
  456. return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
  457. }
  458. int follower_add(snac *snac, char *actor, char *msg)
  459. /* adds a follower */
  460. {
  461. int ret = 201; /* created */
  462. xs *fn = _follower_fn(snac, actor);
  463. FILE *f;
  464. if ((f = fopen(fn, "w")) != NULL) {
  465. xs *j = xs_json_dumps_pp(msg, 4);
  466. fwrite(j, 1, strlen(j), f);
  467. fclose(f);
  468. }
  469. else
  470. ret = 500;
  471. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor, fn));
  472. return ret;
  473. }
  474. int follower_del(snac *snac, char *actor)
  475. /* deletes a follower */
  476. {
  477. int status = 200;
  478. xs *fn = _follower_fn(snac, actor);
  479. if (fn != NULL)
  480. unlink(fn);
  481. else
  482. status = 404;
  483. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
  484. return status;
  485. }
  486. int follower_check(snac *snac, char *actor)
  487. /* checks if someone is a follower */
  488. {
  489. xs *fn = _follower_fn(snac, actor);
  490. return !!(mtime(fn) != 0.0);
  491. }
  492. d_char *follower_list(snac *snac)
  493. /* returns the list of followers */
  494. {
  495. xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
  496. xs *glist = xs_glob(spec, 0, 0);
  497. char *p, *v;
  498. d_char *list = xs_list_new();
  499. /* iterate the list of files */
  500. p = glist;
  501. while (xs_list_iter(&p, &v)) {
  502. FILE *f;
  503. /* load the follower data */
  504. if ((f = fopen(v, "r")) != NULL) {
  505. xs *j = xs_readall(f);
  506. fclose(f);
  507. if (j != NULL) {
  508. xs *o = xs_json_loads(j);
  509. if (o != NULL)
  510. list = xs_list_append(list, o);
  511. }
  512. }
  513. }
  514. return list;
  515. }
  516. double timeline_mtime(snac *snac)
  517. {
  518. xs *fn = xs_fmt("%s/timeline", snac->basedir);
  519. return mtime(fn);
  520. }
  521. d_char *_timeline_find_fn(snac *snac, char *id)
  522. /* returns the file name of a timeline entry by its id */
  523. {
  524. xs *md5 = xs_md5_hex(id, strlen(id));
  525. xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
  526. xs *list = NULL;
  527. d_char *fn = NULL;
  528. int l;
  529. list = xs_glob(spec, 0, 0);
  530. l = xs_list_len(list);
  531. /* if there is something, get the first one */
  532. if (l > 0) {
  533. fn = xs_str_new(xs_list_get(list, 0));
  534. if (l > 1)
  535. snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
  536. }
  537. return fn;
  538. }
  539. int timeline_here(snac *snac, char *id)
  540. /* checks if an object is already downloaded */
  541. {
  542. xs *fn = _timeline_find_fn(snac, id);
  543. return fn != NULL;
  544. }
  545. d_char *timeline_find(snac *snac, char *id)
  546. /* gets a message from the timeline by id */
  547. {
  548. xs *fn = _timeline_find_fn(snac, id);
  549. d_char *msg = NULL;
  550. if (fn != NULL) {
  551. FILE *f;
  552. if ((f = fopen(fn, "r")) != NULL) {
  553. xs *j = xs_readall(f);
  554. msg = xs_json_loads(j);
  555. fclose(f);
  556. }
  557. }
  558. return msg;
  559. }
  560. int timeline_del(snac *snac, char *id)
  561. /* deletes a message from the timeline */
  562. {
  563. int ret = 404;
  564. xs *fn = _timeline_find_fn(snac, id);
  565. if (fn != NULL) {
  566. xs *lfn = NULL;
  567. unlink(fn);
  568. snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
  569. /* try to delete also from the local timeline */
  570. lfn = xs_replace(fn, "/timeline/", "/local/");
  571. if (unlink(lfn) != -1)
  572. snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
  573. ret = 200;
  574. }
  575. /* delete from the user's caches */
  576. object_user_cache_del(snac, id, "public");
  577. object_user_cache_del(snac, id, "private");
  578. return ret;
  579. }
  580. d_char *timeline_get(snac *snac, char *fn)
  581. /* gets a timeline entry by file name */
  582. {
  583. d_char *d = NULL;
  584. FILE *f;
  585. if ((f = fopen(fn, "r")) != NULL) {
  586. xs *j = xs_readall(f);
  587. d = xs_json_loads(j);
  588. fclose(f);
  589. }
  590. return d;
  591. }
  592. d_char *_timeline_list(snac *snac, char *directory, int max)
  593. /* returns a list of the timeline filenames */
  594. {
  595. xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
  596. int c_max;
  597. /* maximum number of items in the timeline */
  598. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  599. /* never more timeline entries than the configured maximum */
  600. if (max > c_max)
  601. max = c_max;
  602. return xs_glob_n(spec, 0, 1, max);
  603. }
  604. d_char *timeline_list(snac *snac, int max)
  605. {
  606. return _timeline_list(snac, "timeline", max);
  607. }
  608. d_char *local_list(snac *snac, int max)
  609. {
  610. return _timeline_list(snac, "local", max);
  611. }
  612. d_char *_timeline_new_fn(snac *snac, char *id)
  613. /* creates a new filename */
  614. {
  615. xs *ntid = tid(0);
  616. xs *md5 = xs_md5_hex(id, strlen(id));
  617. return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
  618. }
  619. int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
  620. /* writes a timeline entry and refreshes the ancestors */
  621. {
  622. xs *fn = _timeline_new_fn(snac, id);
  623. xs *pfn = NULL;
  624. xs *p_msg = NULL;
  625. FILE *f;
  626. if (!xs_is_null(parent)) {
  627. /* get the parent */
  628. pfn = _timeline_find_fn(snac, parent);
  629. if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
  630. xs *j;
  631. j = xs_readall(f);
  632. fclose(f);
  633. p_msg = xs_json_loads(j);
  634. }
  635. }
  636. /* write the message */
  637. if ((f = fopen(fn, "w")) != NULL) {
  638. xs *j = xs_json_dumps_pp(msg, 4);
  639. fwrite(j, strlen(j), 1, f);
  640. fclose(f);
  641. snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
  642. }
  643. /* related to this user? link to local timeline */
  644. if (xs_startswith(id, snac->actor) ||
  645. (!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
  646. (!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
  647. xs *lfn = xs_replace(fn, "/timeline/", "/local/");
  648. link(fn, lfn);
  649. snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
  650. }
  651. if (p_msg != NULL) {
  652. /* update the parent, adding this id to its children list */
  653. xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
  654. xs *children = xs_dup(xs_dict_get(meta, "children"));
  655. /* add the child if it's not already there */
  656. if (xs_list_in(children, id) == -1)
  657. children = xs_list_append(children, id);
  658. /* re-store */
  659. meta = xs_dict_set(meta, "children", children);
  660. p_msg = xs_dict_set(p_msg, "_snac", meta);
  661. xs *nfn = _timeline_new_fn(snac, parent);
  662. if ((f = fopen(nfn, "w")) != NULL) {
  663. xs *j = xs_json_dumps_pp(p_msg, 4);
  664. fwrite(j, strlen(j), 1, f);
  665. fclose(f);
  666. unlink(pfn);
  667. snac_debug(snac, 1,
  668. xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
  669. /* try to do the same with the local */
  670. xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
  671. if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
  672. xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
  673. link(nfn, nlfn);
  674. snac_debug(snac, 1,
  675. xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
  676. }
  677. }
  678. else
  679. return 0;
  680. /* now iterate all parents up, just renaming the files */
  681. xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
  682. int max_levels = 20;
  683. while (!xs_is_null(grampa)) {
  684. xs *gofn = _timeline_find_fn(snac, grampa);
  685. if (gofn == NULL)
  686. break;
  687. /* create the new filename */
  688. xs *gnfn = _timeline_new_fn(snac, grampa);
  689. rename(gofn, gnfn);
  690. snac_debug(snac, 1,
  691. xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
  692. /* try to do the same with the local */
  693. xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
  694. if (unlink(golfn) != -1) {
  695. xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
  696. link(gnfn, gnlfn);
  697. snac_debug(snac, 1,
  698. xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
  699. }
  700. /* now open it and get its own parent */
  701. if ((f = fopen(gnfn, "r")) != NULL) {
  702. xs *j = xs_readall(f);
  703. fclose(f);
  704. xs *g_msg = xs_json_loads(j);
  705. char *meta = xs_dict_get(g_msg, "_snac");
  706. char *p = xs_dict_get(meta, "parent");
  707. xs_free(grampa);
  708. grampa = xs_dup(p);
  709. }
  710. else
  711. break;
  712. if (--max_levels == 0) {
  713. snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached"));
  714. break;
  715. }
  716. }
  717. }
  718. return 1;
  719. }
  720. void timeline_update_indexes(snac *snac, const char *id)
  721. /* updates the indexes */
  722. {
  723. object_user_cache_add(snac, id, "private");
  724. if (xs_startswith(id, snac->actor))
  725. object_user_cache_add(snac, id, "public");
  726. }
  727. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  728. /* adds a message to the timeline */
  729. {
  730. xs *pfn = _timeline_find_fn(snac, id);
  731. int ret = 0;
  732. if (pfn != NULL) {
  733. snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
  734. return 0;
  735. }
  736. xs *msg = xs_dup(o_msg);
  737. xs *md;
  738. /* add new metadata */
  739. md = xs_json_loads("{"
  740. "\"children\": [],"
  741. "\"liked_by\": [],"
  742. "\"announced_by\": [],"
  743. "\"version\": \"" USER_AGENT "\","
  744. "\"referrer\": null,"
  745. "\"parent\": null"
  746. "}");
  747. if (!xs_is_null(parent))
  748. md = xs_dict_set(md, "parent", parent);
  749. if (!xs_is_null(referrer))
  750. md = xs_dict_set(md, "referrer", referrer);
  751. msg = xs_dict_set(msg, "_snac", md);
  752. if ((ret = _timeline_write(snac, id, msg, parent, referrer))) {
  753. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  754. object_add(id, o_msg);
  755. timeline_update_indexes(snac, id);
  756. }
  757. return ret;
  758. }
  759. d_char *timeline_top_level(snac *snac, const char *index)
  760. /* returns the top level entries from this index */
  761. {
  762. int max = 256;
  763. xs *list = index_list_desc(index, max);
  764. xs *tl = xs_list_new();
  765. xs_set seen;
  766. char *p, *v;
  767. xs_set_init(&seen);
  768. p = list;
  769. while (xs_list_iter(&p, &v)) {
  770. char line[256] = "";
  771. strcpy(line, v);
  772. for (;;) {
  773. char line2[256];
  774. xs *fn = _object_fn_by_md5(line);
  775. fn = xs_replace_i(fn, ".json", "_p.idx");
  776. /* if it doesn't have a parent, we got it */
  777. if (index_first(fn, line2, strlen(line2)) == 0) {
  778. strcpy(line, line2);
  779. break;
  780. }
  781. xs *pfn = _object_fn_by_md5(line2);
  782. /* well, there is a parent... if it's not here, we're done */
  783. if (mtime(pfn) == 0.0)
  784. break;
  785. /* it's here! try again with its own parent */
  786. strcpy(line, line2);
  787. }
  788. if (xs_set_add(&seen, line) == 1) {
  789. xs *obj = NULL;
  790. if (valid_status(object_get(line, &obj, NULL)))
  791. tl = xs_list_append(tl, obj);
  792. }
  793. }
  794. xs_set_free(&seen);
  795. return tl;
  796. }
  797. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  798. /* updates a timeline entry with a new admiration */
  799. {
  800. xs *ofn = _timeline_find_fn(snac, id);
  801. FILE *f;
  802. if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
  803. xs *j1 = xs_readall(f);
  804. fclose(f);
  805. xs *msg = xs_json_loads(j1);
  806. xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
  807. xs *list;
  808. if (like)
  809. list = xs_dup(xs_dict_get(meta, "liked_by"));
  810. else
  811. list = xs_dup(xs_dict_get(meta, "announced_by"));
  812. /* add the admirer if it's not already there */
  813. if (xs_list_in(list, admirer) == -1)
  814. list = xs_list_append(list, admirer);
  815. /* set the admirer as the referrer (if not already set or it's us) */
  816. if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
  817. strcmp(admirer, snac->actor) == 0))
  818. meta = xs_dict_set(meta, "referrer", admirer);
  819. /* re-store */
  820. if (like)
  821. meta = xs_dict_set(meta, "liked_by", list);
  822. else
  823. meta = xs_dict_set(meta, "announced_by", list);
  824. msg = xs_dict_set(msg, "_snac", meta);
  825. unlink(ofn);
  826. ofn = xs_replace_i(ofn, "/timeline/", "/local/");
  827. unlink(ofn);
  828. _timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
  829. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  830. like ? "Like" : "Announce", id, admirer));
  831. }
  832. else
  833. snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
  834. object_admire(id, admirer, like);
  835. }
  836. d_char *_following_fn(snac *snac, char *actor)
  837. {
  838. xs *md5 = xs_md5_hex(actor, strlen(actor));
  839. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  840. }
  841. int following_add(snac *snac, char *actor, char *msg)
  842. /* adds to the following list */
  843. {
  844. int ret = 201; /* created */
  845. xs *fn = _following_fn(snac, actor);
  846. FILE *f;
  847. if ((f = fopen(fn, "w")) != NULL) {
  848. xs *j = xs_json_dumps_pp(msg, 4);
  849. fwrite(j, 1, strlen(j), f);
  850. fclose(f);
  851. }
  852. else
  853. ret = 500;
  854. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  855. return ret;
  856. }
  857. int following_del(snac *snac, char *actor)
  858. /* we're not following this actor any longer */
  859. {
  860. xs *fn = _following_fn(snac, actor);
  861. unlink(fn);
  862. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  863. return 200;
  864. }
  865. int following_check(snac *snac, char *actor)
  866. /* checks if we are following this actor */
  867. {
  868. xs *fn = _following_fn(snac, actor);
  869. return !!(mtime(fn) != 0.0);
  870. }
  871. int following_get(snac *snac, char *actor, d_char **data)
  872. /* returns the 'Follow' object */
  873. {
  874. xs *fn = _following_fn(snac, actor);
  875. FILE *f;
  876. int status = 200;
  877. if ((f = fopen(fn, "r")) != NULL) {
  878. xs *j = xs_readall(f);
  879. fclose(f);
  880. *data = xs_json_loads(j);
  881. }
  882. else
  883. status = 404;
  884. return status;
  885. }
  886. d_char *following_list(snac *snac)
  887. /* returns the list of people being followed */
  888. {
  889. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  890. xs *glist = xs_glob(spec, 0, 0);
  891. char *p, *v;
  892. d_char *list = xs_list_new();
  893. /* iterate the list of files */
  894. p = glist;
  895. while (xs_list_iter(&p, &v)) {
  896. FILE *f;
  897. /* load the follower data */
  898. if ((f = fopen(v, "r")) != NULL) {
  899. xs *j = xs_readall(f);
  900. fclose(f);
  901. if (j != NULL) {
  902. xs *o = xs_json_loads(j);
  903. if (o != NULL) {
  904. char *type = xs_dict_get(o, "type");
  905. if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
  906. list = xs_list_append(list, o);
  907. }
  908. }
  909. }
  910. }
  911. return list;
  912. }
  913. d_char *_muted_fn(snac *snac, char *actor)
  914. {
  915. xs *md5 = xs_md5_hex(actor, strlen(actor));
  916. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  917. }
  918. void mute(snac *snac, char *actor)
  919. /* mutes a moron */
  920. {
  921. xs *fn = _muted_fn(snac, actor);
  922. FILE *f;
  923. if ((f = fopen(fn, "w")) != NULL) {
  924. fprintf(f, "%s\n", actor);
  925. fclose(f);
  926. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  927. }
  928. }
  929. void unmute(snac *snac, char *actor)
  930. /* actor is no longer a moron */
  931. {
  932. xs *fn = _muted_fn(snac, actor);
  933. unlink(fn);
  934. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  935. }
  936. int is_muted(snac *snac, char *actor)
  937. /* check if someone is muted */
  938. {
  939. xs *fn = _muted_fn(snac, actor);
  940. return !!(mtime(fn) != 0.0);
  941. }
  942. d_char *_hidden_fn(snac *snac, const char *id)
  943. {
  944. xs *md5 = xs_md5_hex(id, strlen(id));
  945. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  946. }
  947. void hide(snac *snac, const char *id)
  948. /* hides a message tree */
  949. {
  950. xs *fn = _hidden_fn(snac, id);
  951. FILE *f;
  952. if ((f = fopen(fn, "w")) != NULL) {
  953. fprintf(f, "%s\n", id);
  954. fclose(f);
  955. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  956. /* hide all the children */
  957. xs *chld = object_children(id);
  958. char *p, *v;
  959. p = chld;
  960. while (xs_list_iter(&p, &v)) {
  961. xs *co = NULL;
  962. /* resolve to get the id */
  963. if (valid_status(object_get_by_md5(v, &co, NULL))) {
  964. if ((v = xs_dict_get(co, "id")) != NULL)
  965. hide(snac, v);
  966. }
  967. }
  968. }
  969. }
  970. int is_hidden(snac *snac, const char *id)
  971. /* check is id is hidden */
  972. {
  973. xs *fn = _hidden_fn(snac, id);
  974. return !!(mtime(fn) != 0.0);
  975. }
  976. int actor_add(snac *snac, const char *actor, d_char *msg)
  977. /* adds an actor */
  978. {
  979. return object_add_ow(actor, msg);
  980. }
  981. int actor_get(snac *snac, const char *actor, d_char **data)
  982. /* returns an already downloaded actor */
  983. {
  984. int status = 200;
  985. char *d;
  986. if (strcmp(actor, snac->actor) == 0) {
  987. /* this actor */
  988. if (data)
  989. *data = msg_actor(snac);
  990. return status;
  991. }
  992. /* read the object */
  993. if (!valid_status(status = object_get(actor, &d, NULL)))
  994. return status;
  995. if (data)
  996. *data = d;
  997. xs *fn = _object_fn(actor);
  998. double max_time;
  999. /* maximum time for the actor data to be considered stale */
  1000. max_time = 3600.0 * 36.0;
  1001. if (mtime(fn) + max_time < (double) time(NULL)) {
  1002. /* actor data exists but also stinks */
  1003. FILE *f;
  1004. if ((f = fopen(fn, "a")) != NULL) {
  1005. /* write a blank at the end to 'touch' the file */
  1006. fwrite(" ", 1, 1, f);
  1007. fclose(f);
  1008. }
  1009. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  1010. }
  1011. return status;
  1012. }
  1013. d_char *_static_fn(snac *snac, const char *id)
  1014. /* gets the filename for a static file */
  1015. {
  1016. return xs_fmt("%s/static/%s", snac->basedir, id);
  1017. }
  1018. int static_get(snac *snac, const char *id, d_char **data, int *size)
  1019. /* returns static content */
  1020. {
  1021. xs *fn = _static_fn(snac, id);
  1022. FILE *f;
  1023. int status = 404;
  1024. *size = XS_ALL;
  1025. if ((f = fopen(fn, "rb")) != NULL) {
  1026. *data = xs_read(f, size);
  1027. fclose(f);
  1028. status = 200;
  1029. }
  1030. return status;
  1031. }
  1032. void static_put(snac *snac, const char *id, const char *data, int size)
  1033. /* writes status content */
  1034. {
  1035. xs *fn = _static_fn(snac, id);
  1036. FILE *f;
  1037. if ((f = fopen(fn, "wb")) != NULL) {
  1038. fwrite(data, size, 1, f);
  1039. fclose(f);
  1040. }
  1041. }
  1042. d_char *_history_fn(snac *snac, char *id)
  1043. /* gets the filename for the history */
  1044. {
  1045. return xs_fmt("%s/history/%s", snac->basedir, id);
  1046. }
  1047. double history_mtime(snac *snac, char * id)
  1048. {
  1049. double t = 0.0;
  1050. xs *fn = _history_fn(snac, id);
  1051. if (fn != NULL)
  1052. t = mtime(fn);
  1053. return t;
  1054. }
  1055. void history_add(snac *snac, char *id, char *content, int size)
  1056. /* adds something to the history */
  1057. {
  1058. xs *fn = _history_fn(snac, id);
  1059. FILE *f;
  1060. if ((f = fopen(fn, "w")) != NULL) {
  1061. fwrite(content, size, 1, f);
  1062. fclose(f);
  1063. }
  1064. }
  1065. d_char *history_get(snac *snac, char *id)
  1066. {
  1067. d_char *content = NULL;
  1068. xs *fn = _history_fn(snac, id);
  1069. FILE *f;
  1070. if ((f = fopen(fn, "r")) != NULL) {
  1071. content = xs_readall(f);
  1072. fclose(f);
  1073. }
  1074. return content;
  1075. }
  1076. int history_del(snac *snac, char *id)
  1077. {
  1078. xs *fn = _history_fn(snac, id);
  1079. return unlink(fn);
  1080. }
  1081. d_char *history_list(snac *snac)
  1082. {
  1083. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  1084. return xs_glob(spec, 1, 0);
  1085. }
  1086. /** the queue **/
  1087. static int _enqueue_put(char *fn, char *msg)
  1088. /* writes safely to the queue */
  1089. {
  1090. int ret = 1;
  1091. xs *tfn = xs_fmt("%s.tmp", fn);
  1092. FILE *f;
  1093. if ((f = fopen(tfn, "w")) != NULL) {
  1094. xs *j = xs_json_dumps_pp(msg, 4);
  1095. fwrite(j, strlen(j), 1, f);
  1096. fclose(f);
  1097. rename(tfn, fn);
  1098. }
  1099. else
  1100. ret = 0;
  1101. return ret;
  1102. }
  1103. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  1104. /* enqueues an input message */
  1105. {
  1106. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1107. xs *ntid = tid(retries * 60 * qrt);
  1108. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1109. xs *qmsg = xs_dict_new();
  1110. xs *rn = xs_number_new(retries);
  1111. qmsg = xs_dict_append(qmsg, "type", "input");
  1112. qmsg = xs_dict_append(qmsg, "object", msg);
  1113. qmsg = xs_dict_append(qmsg, "req", req);
  1114. qmsg = xs_dict_append(qmsg, "retries", rn);
  1115. _enqueue_put(fn, qmsg);
  1116. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  1117. }
  1118. void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
  1119. /* enqueues an output message to an inbox */
  1120. {
  1121. if (xs_startswith(inbox, snac->actor)) {
  1122. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  1123. return;
  1124. }
  1125. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1126. xs *ntid = tid(retries * 60 * qrt);
  1127. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1128. xs *qmsg = xs_dict_new();
  1129. xs *rn = xs_number_new(retries);
  1130. qmsg = xs_dict_append(qmsg, "type", "output");
  1131. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1132. qmsg = xs_dict_append(qmsg, "object", msg);
  1133. qmsg = xs_dict_append(qmsg, "retries", rn);
  1134. _enqueue_put(fn, qmsg);
  1135. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1136. }
  1137. void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
  1138. /* enqueues an output message for an actor */
  1139. {
  1140. xs *inbox = get_actor_inbox(snac, actor);
  1141. if (!xs_is_null(inbox))
  1142. enqueue_output(snac, msg, inbox, retries);
  1143. else
  1144. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1145. }
  1146. void enqueue_email(snac *snac, char *msg, int retries)
  1147. /* enqueues an email message to be sent */
  1148. {
  1149. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1150. xs *ntid = tid(retries * 60 * qrt);
  1151. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1152. xs *qmsg = xs_dict_new();
  1153. xs *rn = xs_number_new(retries);
  1154. qmsg = xs_dict_append(qmsg, "type", "email");
  1155. qmsg = xs_dict_append(qmsg, "message", msg);
  1156. qmsg = xs_dict_append(qmsg, "retries", rn);
  1157. _enqueue_put(fn, qmsg);
  1158. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  1159. }
  1160. d_char *queue(snac *snac)
  1161. /* returns a list with filenames that can be dequeued */
  1162. {
  1163. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1164. d_char *list = xs_list_new();
  1165. time_t t = time(NULL);
  1166. char *p, *v;
  1167. xs *fns = xs_glob(spec, 0, 0);
  1168. p = fns;
  1169. while (xs_list_iter(&p, &v)) {
  1170. /* get the retry time from the basename */
  1171. char *bn = strrchr(v, '/');
  1172. time_t t2 = atol(bn + 1);
  1173. if (t2 > t)
  1174. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1175. else {
  1176. list = xs_list_append(list, v);
  1177. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  1178. }
  1179. }
  1180. return list;
  1181. }
  1182. d_char *dequeue(snac *snac, char *fn)
  1183. /* dequeues a message */
  1184. {
  1185. FILE *f;
  1186. d_char *obj = NULL;
  1187. if ((f = fopen(fn, "r")) != NULL) {
  1188. /* delete right now */
  1189. unlink(fn);
  1190. xs *j = xs_readall(f);
  1191. obj = xs_json_loads(j);
  1192. fclose(f);
  1193. }
  1194. return obj;
  1195. }
  1196. /** the purge **/
  1197. static void _purge_file(const char *fn, time_t mt)
  1198. /* purge fn if it's older than days */
  1199. {
  1200. if (mtime(fn) < mt) {
  1201. /* older than the minimum time: delete it */
  1202. unlink(fn);
  1203. srv_debug(1, xs_fmt("purged %s", fn));
  1204. }
  1205. }
  1206. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1207. /* purges all files in subdir older than days */
  1208. {
  1209. if (days) {
  1210. time_t mt = time(NULL) - days * 24 * 3600;
  1211. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1212. xs *list = xs_glob(spec, 0, 0);
  1213. char *p, *v;
  1214. p = list;
  1215. while (xs_list_iter(&p, &v))
  1216. _purge_file(v, mt);
  1217. }
  1218. }
  1219. void purge_server(void)
  1220. /* purge global server data */
  1221. {
  1222. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1223. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1224. xs *dirs = xs_glob(spec, 0, 0);
  1225. char *p, *v;
  1226. time_t mt = time(NULL) - tpd * 24 * 3600;
  1227. p = dirs;
  1228. while (xs_list_iter(&p, &v)) {
  1229. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1230. xs *files = xs_glob(spec2, 0, 0);
  1231. char *p2, *v2;
  1232. p2 = files;
  1233. while (xs_list_iter(&p2, &v2)) {
  1234. int n_link;
  1235. /* old and with no hard links? */
  1236. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1237. xs *s1 = xs_replace(v2, ".json", "");
  1238. xs *l = xs_split(s1, "/");
  1239. char *md5 = xs_list_get(l, -1);
  1240. object_del_by_md5(md5);
  1241. }
  1242. }
  1243. }
  1244. }
  1245. void purge_user(snac *snac)
  1246. /* do the purge for this user */
  1247. {
  1248. int days;
  1249. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1250. _purge_subdir(snac, "timeline", days);
  1251. _purge_subdir(snac, "hidden", days);
  1252. _purge_subdir(snac, "private", days);
  1253. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1254. _purge_subdir(snac, "local", days);
  1255. _purge_subdir(snac, "public", days);
  1256. }
  1257. void purge_all(void)
  1258. /* purge all users */
  1259. {
  1260. snac snac;
  1261. xs *list = user_list();
  1262. char *p, *uid;
  1263. p = list;
  1264. while (xs_list_iter(&p, &uid)) {
  1265. if (user_open(&snac, uid)) {
  1266. purge_user(&snac);
  1267. user_free(&snac);
  1268. }
  1269. }
  1270. purge_server();
  1271. }