data.c 39 KB

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