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