1
0

data.c 39 KB

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