data.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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.7;
  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. xs_free(snac->md5);
  93. }
  94. int user_open(snac *snac, char *uid)
  95. /* opens a user */
  96. {
  97. int ret = 0;
  98. memset(snac, '\0', sizeof(struct _snac));
  99. if (validate_uid(uid)) {
  100. xs *cfg_file;
  101. FILE *f;
  102. snac->uid = xs_str_new(uid);
  103. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  104. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  105. if ((f = fopen(cfg_file, "r")) != NULL) {
  106. xs *cfg_data;
  107. /* read full config file */
  108. cfg_data = xs_readall(f);
  109. fclose(f);
  110. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  111. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  112. if ((f = fopen(key_file, "r")) != NULL) {
  113. xs *key_data;
  114. key_data = xs_readall(f);
  115. fclose(f);
  116. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  117. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  118. snac->md5 = xs_md5_hex(snac->actor, strlen(snac->actor));
  119. ret = 1;
  120. }
  121. else
  122. srv_log(xs_fmt("cannot parse '%s'", key_file));
  123. }
  124. else
  125. srv_log(xs_fmt("error opening '%s'", key_file));
  126. }
  127. else
  128. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  129. }
  130. else
  131. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  132. }
  133. else
  134. srv_log(xs_fmt("invalid user '%s'", uid));
  135. if (!ret)
  136. user_free(snac);
  137. return ret;
  138. }
  139. d_char *user_list(void)
  140. /* returns the list of user ids */
  141. {
  142. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  143. return xs_glob(spec, 1, 0);
  144. }
  145. double mtime_nl(const char *fn, int *n_link)
  146. /* returns the mtime and number of links of a file or directory, or 0.0 */
  147. {
  148. struct stat st;
  149. double r = 0.0;
  150. int n = 0;
  151. if (fn && stat(fn, &st) != -1) {
  152. r = (double) st.st_mtim.tv_sec;
  153. n = st.st_nlink;
  154. }
  155. if (n_link)
  156. *n_link = n;
  157. return r;
  158. }
  159. /** database 2.1+ **/
  160. /** indexes **/
  161. int index_add_md5(const char *fn, const char *md5)
  162. /* adds an md5 to an index */
  163. {
  164. int status = 201; /* Created */
  165. FILE *f;
  166. if ((f = fopen(fn, "a")) != NULL) {
  167. flock(fileno(f), LOCK_EX);
  168. /* ensure the position is at the end after getting the lock */
  169. fseek(f, 0, SEEK_END);
  170. fprintf(f, "%s\n", md5);
  171. fclose(f);
  172. }
  173. else
  174. status = 500;
  175. return status;
  176. }
  177. int index_add(const char *fn, const char *id)
  178. /* adds an id to an index */
  179. {
  180. xs *md5 = xs_md5_hex(id, strlen(id));
  181. return index_add_md5(fn, md5);
  182. }
  183. int index_del(const char *fn, const char *md5)
  184. /* deletes an md5 from an index */
  185. {
  186. int status = 404;
  187. FILE *i, *o;
  188. if ((i = fopen(fn, "r")) != NULL) {
  189. flock(fileno(i), LOCK_EX);
  190. xs *nfn = xs_fmt("%s.new", fn);
  191. char line[256];
  192. if ((o = fopen(nfn, "w")) != NULL) {
  193. while (fgets(line, sizeof(line), i) != NULL) {
  194. line[32] = '\0';
  195. if (memcmp(line, md5, 32) != 0)
  196. fprintf(o, "%s\n", line);
  197. }
  198. fclose(o);
  199. xs *ofn = xs_fmt("%s.bak", fn);
  200. link(fn, ofn);
  201. rename(nfn, fn);
  202. }
  203. else
  204. status = 500;
  205. fclose(i);
  206. }
  207. else
  208. status = 500;
  209. return status;
  210. }
  211. int index_in_md5(const char *fn, const char *md5)
  212. /* checks if the md5 is already in the index */
  213. {
  214. FILE *f;
  215. int ret = 0;
  216. if ((f = fopen(fn, "r")) != NULL) {
  217. flock(fileno(f), LOCK_SH);
  218. char line[256];
  219. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  220. line[32] = '\0';
  221. if (strcmp(line, md5) == 0)
  222. ret = 1;
  223. }
  224. fclose(f);
  225. }
  226. return ret;
  227. }
  228. int index_in(const char *fn, const char *id)
  229. /* checks if the object id is already in the index */
  230. {
  231. xs *md5 = xs_md5_hex(id, strlen(id));
  232. return index_in_md5(fn, md5);
  233. }
  234. int index_first(const char *fn, char *line, int size)
  235. /* reads the first entry of an index */
  236. {
  237. FILE *f;
  238. int ret = 0;
  239. if ((f = fopen(fn, "r")) != NULL) {
  240. flock(fileno(f), LOCK_SH);
  241. if (fgets(line, size, f) != NULL) {
  242. line[32] = '\0';
  243. ret = 1;
  244. }
  245. fclose(f);
  246. }
  247. return ret;
  248. }
  249. d_char *index_list(const char *fn, int max)
  250. /* returns an index as a list */
  251. {
  252. d_char *list = NULL;
  253. FILE *f;
  254. int n = 0;
  255. if ((f = fopen(fn, "r")) != NULL) {
  256. flock(fileno(f), LOCK_SH);
  257. char line[256];
  258. list = xs_list_new();
  259. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  260. line[32] = '\0';
  261. list = xs_list_append(list, line);
  262. n++;
  263. }
  264. fclose(f);
  265. }
  266. return list;
  267. }
  268. d_char *index_list_desc(const char *fn, int max)
  269. /* returns an index as a list, in reverse order */
  270. {
  271. d_char *list = NULL;
  272. FILE *f;
  273. int n = 0;
  274. if ((f = fopen(fn, "r")) != NULL) {
  275. flock(fileno(f), LOCK_SH);
  276. char line[256];
  277. list = xs_list_new();
  278. /* move to the end minus one entry */
  279. if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
  280. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  281. line[32] = '\0';
  282. list = xs_list_append(list, line);
  283. n++;
  284. /* move backwards 2 entries */
  285. if (fseek(f, -66, SEEK_CUR) == -1)
  286. break;
  287. }
  288. }
  289. fclose(f);
  290. }
  291. return list;
  292. }
  293. /** objects **/
  294. d_char *_object_fn_by_md5(const char *md5)
  295. {
  296. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  297. mkdir(bfn, 0755);
  298. return xs_fmt("%s/%s.json", bfn, md5);
  299. }
  300. d_char *_object_fn(const char *id)
  301. {
  302. xs *md5 = xs_md5_hex(id, strlen(id));
  303. return _object_fn_by_md5(md5);
  304. }
  305. int object_here_by_md5(char *id)
  306. /* checks if an object is already downloaded */
  307. {
  308. xs *fn = _object_fn_by_md5(id);
  309. return mtime(fn) > 0.0;
  310. }
  311. int object_here(char *id)
  312. /* checks if an object is already downloaded */
  313. {
  314. xs *fn = _object_fn(id);
  315. return mtime(fn) > 0.0;
  316. }
  317. int object_get_by_md5(const char *md5, d_char **obj, const char *type)
  318. /* returns a stored object, optionally of the requested type */
  319. {
  320. int status = 404;
  321. xs *fn = _object_fn_by_md5(md5);
  322. FILE *f;
  323. if ((f = fopen(fn, "r")) != NULL) {
  324. flock(fileno(f), LOCK_SH);
  325. xs *j = xs_readall(f);
  326. fclose(f);
  327. *obj = xs_json_loads(j);
  328. if (*obj) {
  329. status = 200;
  330. /* specific type requested? */
  331. if (!xs_is_null(type)) {
  332. char *v = xs_dict_get(*obj, "type");
  333. if (xs_is_null(v) || strcmp(v, type) != 0) {
  334. status = 404;
  335. *obj = xs_free(*obj);
  336. }
  337. }
  338. }
  339. }
  340. else
  341. *obj = NULL;
  342. return status;
  343. }
  344. int object_get(const char *id, d_char **obj, const char *type)
  345. /* returns a stored object, optionally of the requested type */
  346. {
  347. xs *md5 = xs_md5_hex(id, strlen(id));
  348. return object_get_by_md5(md5, obj, type);
  349. }
  350. int _object_add(const char *id, d_char *obj, int ow)
  351. /* stores an object */
  352. {
  353. int status = 201; /* Created */
  354. xs *fn = _object_fn(id);
  355. FILE *f;
  356. if (!ow && mtime(fn) > 0.0) {
  357. /* object already here */
  358. srv_debug(0, xs_fmt("object_add object already here %s", id));
  359. return 204; /* No content */
  360. }
  361. if ((f = fopen(fn, "w")) != NULL) {
  362. flock(fileno(f), LOCK_EX);
  363. xs *j = xs_json_dumps_pp(obj, 4);
  364. fwrite(j, strlen(j), 1, f);
  365. fclose(f);
  366. /* does this object has a parent? */
  367. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  368. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  369. /* update the children index of the parent */
  370. xs *c_idx = _object_fn(in_reply_to);
  371. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  372. if (!index_in(c_idx, id)) {
  373. index_add(c_idx, id);
  374. srv_debug(0, xs_fmt("object_add added child %s to %s", id, c_idx));
  375. }
  376. else
  377. srv_debug(0, xs_fmt("object_add %s child already in %s", id, c_idx));
  378. /* create a one-element index with the parent */
  379. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  380. index_add(p_idx, in_reply_to);
  381. srv_debug(0, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  382. }
  383. }
  384. else
  385. status = 500;
  386. srv_debug(0, xs_fmt("object_add %s %s %d", id, fn, status));
  387. return status;
  388. }
  389. int object_add(const char *id, d_char *obj)
  390. /* stores an object */
  391. {
  392. return _object_add(id, obj, 0);
  393. }
  394. int object_add_ow(const char *id, d_char *obj)
  395. /* stores an object (overwriting allowed) */
  396. {
  397. return _object_add(id, obj, 1);
  398. }
  399. int object_del_by_md5(const char *md5)
  400. /* deletes an object by its md5 */
  401. {
  402. int status = 404;
  403. xs *fn = _object_fn_by_md5(md5);
  404. if (fn != NULL && unlink(fn) != -1) {
  405. status = 200;
  406. /* also delete associated indexes */
  407. xs *spec = xs_dup(fn);
  408. spec = xs_replace_i(spec, ".json", "*.idx");
  409. xs *files = xs_glob(spec, 0, 0);
  410. char *p, *v;
  411. p = files;
  412. while (xs_list_iter(&p, &v)) {
  413. srv_debug(0, xs_fmt("object_del index %s", v));
  414. unlink(v);
  415. }
  416. }
  417. srv_debug(0, xs_fmt("object_del %s %d", fn, status));
  418. return status;
  419. }
  420. int object_del(const char *id)
  421. /* deletes an object */
  422. {
  423. xs *md5 = xs_md5_hex(id, strlen(id));
  424. return object_del_by_md5(md5);
  425. }
  426. int object_del_if_unref(const char *id)
  427. /* deletes an object if its n_links < 2 */
  428. {
  429. xs *fn = _object_fn(id);
  430. int n_links;
  431. int ret = 0;
  432. if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
  433. ret = object_del(id);
  434. return ret;
  435. }
  436. d_char *_object_metadata(const char *id, const char *idxsfx)
  437. /* returns the content of a metadata index */
  438. {
  439. xs *fn = _object_fn(id);
  440. fn = xs_replace_i(fn, ".json", idxsfx);
  441. return index_list(fn, XS_ALL);
  442. }
  443. d_char *object_children(const char *id)
  444. /* returns the list of an object's children */
  445. {
  446. return _object_metadata(id, "_c.idx");
  447. }
  448. d_char *object_likes(const char *id)
  449. {
  450. return _object_metadata(id, "_l.idx");
  451. }
  452. d_char *object_announces(const char *id)
  453. {
  454. return _object_metadata(id, "_a.idx");
  455. }
  456. int object_parent(const char *id, char *buf, int size)
  457. /* returns the object parent, if any */
  458. {
  459. xs *fn = _object_fn_by_md5(id);
  460. fn = xs_replace_i(fn, ".json", "_p.idx");
  461. return index_first(fn, buf, size);
  462. }
  463. int object_admire(const char *id, const char *actor, int like)
  464. /* actor likes or announces this object */
  465. {
  466. int status = 200;
  467. xs *fn = _object_fn(id);
  468. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  469. if (!index_in(fn, actor)) {
  470. status = index_add(fn, actor);
  471. srv_debug(0, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  472. }
  473. return status;
  474. }
  475. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  476. /* adds or deletes from a user cache */
  477. {
  478. xs *ofn = _object_fn(id);
  479. xs *l = xs_split(ofn, "/");
  480. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  481. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  482. int ret;
  483. if (del) {
  484. if ((ret = unlink(cfn)) != -1)
  485. index_del(idx, id);
  486. }
  487. else {
  488. index_add(idx, id);
  489. ret = link(ofn, cfn);
  490. }
  491. return ret;
  492. }
  493. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  494. /* caches an object into a user cache */
  495. {
  496. return _object_user_cache(snac, id, cachedir, 0);
  497. }
  498. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  499. /* deletes an object from a user cache */
  500. {
  501. return _object_user_cache(snac, id, cachedir, 1);
  502. }
  503. int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
  504. /* checks if an object is stored in a cache */
  505. {
  506. xs *md5 = xs_md5_hex(id, strlen(id));
  507. xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
  508. return !!(mtime(cfn) != 0.0);
  509. }
  510. d_char *object_user_cache_list(snac *snac, const char *cachedir, int max)
  511. /* returns the objects in a cache as a list */
  512. {
  513. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  514. return index_list(idx, max);
  515. }
  516. /** specialized functions **/
  517. /** followers **/
  518. int follower_add(snac *snac, const char *actor)
  519. /* adds a follower */
  520. {
  521. int ret = object_user_cache_add(snac, actor, "followers");
  522. snac_debug(snac, 2, xs_fmt("follower_add %s %s", actor));
  523. return ret == -1 ? 500 : 200;
  524. }
  525. int follower_del(snac *snac, const char *actor)
  526. /* deletes a follower */
  527. {
  528. int ret = object_user_cache_del(snac, actor, "followers");
  529. snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor));
  530. return ret == -1 ? 404 : 200;
  531. }
  532. int follower_check(snac *snac, const char *actor)
  533. /* checks if someone is a follower */
  534. {
  535. return object_user_cache_in(snac, actor, "followers");
  536. }
  537. d_char *follower_list(snac *snac)
  538. /* returns the list of followers */
  539. {
  540. xs *list = object_user_cache_list(snac, "followers", XS_ALL);
  541. d_char *fwers = xs_list_new();
  542. char *p, *v;
  543. /* resolve the list of md5 to be a list of actors */
  544. p = list;
  545. while (xs_list_iter(&p, &v)) {
  546. xs *a_obj = NULL;
  547. if (valid_status(object_get_by_md5(v, &a_obj, NULL))) {
  548. char *actor = xs_dict_get(a_obj, "id");
  549. if (!xs_is_null(actor))
  550. fwers = xs_list_append(fwers, actor);
  551. }
  552. }
  553. return fwers;
  554. }
  555. /** timeline **/
  556. double timeline_mtime(snac *snac)
  557. {
  558. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  559. return mtime(fn);
  560. }
  561. int timeline_del(snac *snac, char *id)
  562. /* deletes a message from the timeline */
  563. {
  564. /* delete from the user's caches */
  565. object_user_cache_del(snac, id, "public");
  566. object_user_cache_del(snac, id, "private");
  567. /* try to delete the object if it's not used elsewhere */
  568. return object_del_if_unref(id);
  569. }
  570. void timeline_update_indexes(snac *snac, const char *id)
  571. /* updates the indexes */
  572. {
  573. object_user_cache_add(snac, id, "private");
  574. if (xs_startswith(id, snac->actor)) {
  575. xs *msg = NULL;
  576. if (valid_status(object_get(id, &msg, NULL))) {
  577. /* if its ours and is public, also store in public */
  578. if (is_msg_public(snac, msg))
  579. object_user_cache_add(snac, id, "public");
  580. }
  581. }
  582. }
  583. int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
  584. /* adds a message to the timeline */
  585. {
  586. int ret = object_add(id, o_msg);
  587. timeline_update_indexes(snac, id);
  588. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  589. return ret;
  590. }
  591. void timeline_admire(snac *snac, char *o_msg, char *id, char *admirer, int like)
  592. /* updates a timeline entry with a new admiration */
  593. {
  594. /* if we are admiring this, add to the public timeline */
  595. if (!like && strcmp(admirer, snac->actor) == 0)
  596. object_user_cache_add(snac, id, "public");
  597. object_admire(id, admirer, like);
  598. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  599. like ? "Like" : "Announce", id, admirer));
  600. }
  601. d_char *timeline_top_level(d_char *list)
  602. /* returns the top level md5 entries from this index */
  603. {
  604. xs_set seen;
  605. char *p, *v;
  606. xs_set_init(&seen);
  607. p = list;
  608. while (xs_list_iter(&p, &v)) {
  609. char line[256] = "";
  610. strcpy(line, v);
  611. for (;;) {
  612. char line2[256];
  613. /* if it doesn't have a parent, use this */
  614. if (!object_parent(line, line2, sizeof(line2)))
  615. break;
  616. /* well, there is a parent... but if it's not there, use this */
  617. if (!object_here_by_md5(line2))
  618. break;
  619. /* it's here! try again with its own parent */
  620. strcpy(line, line2);
  621. }
  622. xs_set_add(&seen, line);
  623. }
  624. return xs_set_result(&seen);
  625. }
  626. d_char *timeline_simple_list(snac *snac, const char *idx_name, int max)
  627. /* returns a timeline (with all entries) */
  628. {
  629. int c_max;
  630. /* maximum number of items in the timeline */
  631. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  632. /* never more timeline entries than the configured maximum */
  633. if (max > c_max)
  634. max = c_max;
  635. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
  636. return index_list_desc(idx, max);
  637. }
  638. d_char *timeline_list(snac *snac, const char *idx_name, int max)
  639. /* returns a timeline (only top level entries) */
  640. {
  641. xs *list = timeline_simple_list(snac, idx_name, max);
  642. return timeline_top_level(list);
  643. }
  644. /** following **/
  645. /* this needs special treatment and cannot use the object db as is,
  646. with a link to a cached author, because we need the Follow object
  647. in case we need to unfollow (Undo + original Follow) */
  648. d_char *_following_fn(snac *snac, char *actor)
  649. {
  650. xs *md5 = xs_md5_hex(actor, strlen(actor));
  651. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  652. }
  653. int following_add(snac *snac, char *actor, char *msg)
  654. /* adds to the following list */
  655. {
  656. int ret = 201; /* created */
  657. xs *fn = _following_fn(snac, actor);
  658. FILE *f;
  659. if ((f = fopen(fn, "w")) != NULL) {
  660. xs *j = xs_json_dumps_pp(msg, 4);
  661. fwrite(j, 1, strlen(j), f);
  662. fclose(f);
  663. }
  664. else
  665. ret = 500;
  666. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  667. return ret;
  668. }
  669. int following_del(snac *snac, char *actor)
  670. /* we're not following this actor any longer */
  671. {
  672. xs *fn = _following_fn(snac, actor);
  673. unlink(fn);
  674. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  675. return 200;
  676. }
  677. int following_check(snac *snac, char *actor)
  678. /* checks if we are following this actor */
  679. {
  680. xs *fn = _following_fn(snac, actor);
  681. return !!(mtime(fn) != 0.0);
  682. }
  683. int following_get(snac *snac, char *actor, d_char **data)
  684. /* returns the 'Follow' object */
  685. {
  686. xs *fn = _following_fn(snac, actor);
  687. FILE *f;
  688. int status = 200;
  689. if ((f = fopen(fn, "r")) != NULL) {
  690. xs *j = xs_readall(f);
  691. fclose(f);
  692. *data = xs_json_loads(j);
  693. }
  694. else
  695. status = 404;
  696. return status;
  697. }
  698. d_char *following_list(snac *snac)
  699. /* returns the list of people being followed */
  700. {
  701. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  702. xs *glist = xs_glob(spec, 0, 0);
  703. char *p, *v;
  704. d_char *list = xs_list_new();
  705. /* iterate the list of files */
  706. p = glist;
  707. while (xs_list_iter(&p, &v)) {
  708. FILE *f;
  709. /* load the follower data */
  710. if ((f = fopen(v, "r")) != NULL) {
  711. xs *j = xs_readall(f);
  712. fclose(f);
  713. if (j != NULL) {
  714. xs *o = xs_json_loads(j);
  715. if (o != NULL) {
  716. char *type = xs_dict_get(o, "type");
  717. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  718. char *actor = xs_dict_get(o, "actor");
  719. if (!xs_is_null(actor))
  720. list = xs_list_append(list, actor);
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return list;
  727. }
  728. d_char *_muted_fn(snac *snac, char *actor)
  729. {
  730. xs *md5 = xs_md5_hex(actor, strlen(actor));
  731. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  732. }
  733. void mute(snac *snac, char *actor)
  734. /* mutes a moron */
  735. {
  736. xs *fn = _muted_fn(snac, actor);
  737. FILE *f;
  738. if ((f = fopen(fn, "w")) != NULL) {
  739. fprintf(f, "%s\n", actor);
  740. fclose(f);
  741. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  742. }
  743. }
  744. void unmute(snac *snac, char *actor)
  745. /* actor is no longer a moron */
  746. {
  747. xs *fn = _muted_fn(snac, actor);
  748. unlink(fn);
  749. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  750. }
  751. int is_muted(snac *snac, char *actor)
  752. /* check if someone is muted */
  753. {
  754. xs *fn = _muted_fn(snac, actor);
  755. return !!(mtime(fn) != 0.0);
  756. }
  757. d_char *_hidden_fn(snac *snac, const char *id)
  758. {
  759. xs *md5 = xs_md5_hex(id, strlen(id));
  760. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  761. }
  762. void hide(snac *snac, const char *id)
  763. /* hides a message tree */
  764. {
  765. xs *fn = _hidden_fn(snac, id);
  766. FILE *f;
  767. if ((f = fopen(fn, "w")) != NULL) {
  768. fprintf(f, "%s\n", id);
  769. fclose(f);
  770. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  771. /* hide all the children */
  772. xs *chld = object_children(id);
  773. char *p, *v;
  774. p = chld;
  775. while (xs_list_iter(&p, &v)) {
  776. xs *co = NULL;
  777. /* resolve to get the id */
  778. if (valid_status(object_get_by_md5(v, &co, NULL))) {
  779. if ((v = xs_dict_get(co, "id")) != NULL)
  780. hide(snac, v);
  781. }
  782. }
  783. }
  784. }
  785. int is_hidden(snac *snac, const char *id)
  786. /* check is id is hidden */
  787. {
  788. xs *fn = _hidden_fn(snac, id);
  789. return !!(mtime(fn) != 0.0);
  790. }
  791. int actor_add(snac *snac, const char *actor, d_char *msg)
  792. /* adds an actor */
  793. {
  794. return object_add_ow(actor, msg);
  795. }
  796. int actor_get(snac *snac, const char *actor, d_char **data)
  797. /* returns an already downloaded actor */
  798. {
  799. int status = 200;
  800. char *d;
  801. if (strcmp(actor, snac->actor) == 0) {
  802. /* this actor */
  803. if (data)
  804. *data = msg_actor(snac);
  805. return status;
  806. }
  807. /* read the object */
  808. if (!valid_status(status = object_get(actor, &d, NULL)))
  809. return status;
  810. if (data)
  811. *data = d;
  812. xs *fn = _object_fn(actor);
  813. double max_time;
  814. /* maximum time for the actor data to be considered stale */
  815. max_time = 3600.0 * 36.0;
  816. if (mtime(fn) + max_time < (double) time(NULL)) {
  817. /* actor data exists but also stinks */
  818. FILE *f;
  819. if ((f = fopen(fn, "a")) != NULL) {
  820. /* write a blank at the end to 'touch' the file */
  821. fwrite(" ", 1, 1, f);
  822. fclose(f);
  823. }
  824. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  825. }
  826. return status;
  827. }
  828. d_char *_static_fn(snac *snac, const char *id)
  829. /* gets the filename for a static file */
  830. {
  831. return xs_fmt("%s/static/%s", snac->basedir, id);
  832. }
  833. int static_get(snac *snac, const char *id, d_char **data, int *size)
  834. /* returns static content */
  835. {
  836. xs *fn = _static_fn(snac, id);
  837. FILE *f;
  838. int status = 404;
  839. *size = XS_ALL;
  840. if ((f = fopen(fn, "rb")) != NULL) {
  841. *data = xs_read(f, size);
  842. fclose(f);
  843. status = 200;
  844. }
  845. return status;
  846. }
  847. void static_put(snac *snac, const char *id, const char *data, int size)
  848. /* writes status content */
  849. {
  850. xs *fn = _static_fn(snac, id);
  851. FILE *f;
  852. if ((f = fopen(fn, "wb")) != NULL) {
  853. fwrite(data, size, 1, f);
  854. fclose(f);
  855. }
  856. }
  857. d_char *_history_fn(snac *snac, char *id)
  858. /* gets the filename for the history */
  859. {
  860. return xs_fmt("%s/history/%s", snac->basedir, id);
  861. }
  862. double history_mtime(snac *snac, char * id)
  863. {
  864. double t = 0.0;
  865. xs *fn = _history_fn(snac, id);
  866. if (fn != NULL)
  867. t = mtime(fn);
  868. return t;
  869. }
  870. void history_add(snac *snac, char *id, char *content, int size)
  871. /* adds something to the history */
  872. {
  873. xs *fn = _history_fn(snac, id);
  874. FILE *f;
  875. if ((f = fopen(fn, "w")) != NULL) {
  876. fwrite(content, size, 1, f);
  877. fclose(f);
  878. }
  879. }
  880. d_char *history_get(snac *snac, char *id)
  881. {
  882. d_char *content = NULL;
  883. xs *fn = _history_fn(snac, id);
  884. FILE *f;
  885. if ((f = fopen(fn, "r")) != NULL) {
  886. content = xs_readall(f);
  887. fclose(f);
  888. }
  889. return content;
  890. }
  891. int history_del(snac *snac, char *id)
  892. {
  893. xs *fn = _history_fn(snac, id);
  894. return unlink(fn);
  895. }
  896. d_char *history_list(snac *snac)
  897. {
  898. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  899. return xs_glob(spec, 1, 0);
  900. }
  901. /** the queue **/
  902. static int _enqueue_put(char *fn, char *msg)
  903. /* writes safely to the queue */
  904. {
  905. int ret = 1;
  906. xs *tfn = xs_fmt("%s.tmp", fn);
  907. FILE *f;
  908. if ((f = fopen(tfn, "w")) != NULL) {
  909. xs *j = xs_json_dumps_pp(msg, 4);
  910. fwrite(j, strlen(j), 1, f);
  911. fclose(f);
  912. rename(tfn, fn);
  913. }
  914. else
  915. ret = 0;
  916. return ret;
  917. }
  918. void enqueue_input(snac *snac, char *msg, char *req, int retries)
  919. /* enqueues an input message */
  920. {
  921. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  922. xs *ntid = tid(retries * 60 * qrt);
  923. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  924. xs *qmsg = xs_dict_new();
  925. xs *rn = xs_number_new(retries);
  926. qmsg = xs_dict_append(qmsg, "type", "input");
  927. qmsg = xs_dict_append(qmsg, "object", msg);
  928. qmsg = xs_dict_append(qmsg, "req", req);
  929. qmsg = xs_dict_append(qmsg, "retries", rn);
  930. _enqueue_put(fn, qmsg);
  931. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  932. }
  933. void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
  934. /* enqueues an output message to an inbox */
  935. {
  936. if (xs_startswith(inbox, snac->actor)) {
  937. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  938. return;
  939. }
  940. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  941. xs *ntid = tid(retries * 60 * qrt);
  942. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  943. xs *qmsg = xs_dict_new();
  944. xs *rn = xs_number_new(retries);
  945. qmsg = xs_dict_append(qmsg, "type", "output");
  946. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  947. qmsg = xs_dict_append(qmsg, "object", msg);
  948. qmsg = xs_dict_append(qmsg, "retries", rn);
  949. _enqueue_put(fn, qmsg);
  950. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  951. }
  952. void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
  953. /* enqueues an output message for an actor */
  954. {
  955. xs *inbox = get_actor_inbox(snac, actor);
  956. if (!xs_is_null(inbox))
  957. enqueue_output(snac, msg, inbox, retries);
  958. else
  959. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  960. }
  961. void enqueue_email(snac *snac, char *msg, int retries)
  962. /* enqueues an email message to be sent */
  963. {
  964. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  965. xs *ntid = tid(retries * 60 * qrt);
  966. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  967. xs *qmsg = xs_dict_new();
  968. xs *rn = xs_number_new(retries);
  969. qmsg = xs_dict_append(qmsg, "type", "email");
  970. qmsg = xs_dict_append(qmsg, "message", msg);
  971. qmsg = xs_dict_append(qmsg, "retries", rn);
  972. _enqueue_put(fn, qmsg);
  973. snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
  974. }
  975. d_char *queue(snac *snac)
  976. /* returns a list with filenames that can be dequeued */
  977. {
  978. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  979. d_char *list = xs_list_new();
  980. time_t t = time(NULL);
  981. char *p, *v;
  982. xs *fns = xs_glob(spec, 0, 0);
  983. p = fns;
  984. while (xs_list_iter(&p, &v)) {
  985. /* get the retry time from the basename */
  986. char *bn = strrchr(v, '/');
  987. time_t t2 = atol(bn + 1);
  988. if (t2 > t)
  989. snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  990. else {
  991. list = xs_list_append(list, v);
  992. snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
  993. }
  994. }
  995. return list;
  996. }
  997. d_char *dequeue(snac *snac, char *fn)
  998. /* dequeues a message */
  999. {
  1000. FILE *f;
  1001. d_char *obj = NULL;
  1002. if ((f = fopen(fn, "r")) != NULL) {
  1003. /* delete right now */
  1004. unlink(fn);
  1005. xs *j = xs_readall(f);
  1006. obj = xs_json_loads(j);
  1007. fclose(f);
  1008. }
  1009. return obj;
  1010. }
  1011. /** the purge **/
  1012. static void _purge_file(const char *fn, time_t mt)
  1013. /* purge fn if it's older than days */
  1014. {
  1015. if (mtime(fn) < mt) {
  1016. /* older than the minimum time: delete it */
  1017. unlink(fn);
  1018. srv_debug(1, xs_fmt("purged %s", fn));
  1019. }
  1020. }
  1021. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1022. /* purges all files in subdir older than days */
  1023. {
  1024. if (days) {
  1025. time_t mt = time(NULL) - days * 24 * 3600;
  1026. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1027. xs *list = xs_glob(spec, 0, 0);
  1028. char *p, *v;
  1029. p = list;
  1030. while (xs_list_iter(&p, &v))
  1031. _purge_file(v, mt);
  1032. }
  1033. }
  1034. void purge_server(void)
  1035. /* purge global server data */
  1036. {
  1037. int tpd = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1038. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1039. xs *dirs = xs_glob(spec, 0, 0);
  1040. char *p, *v;
  1041. time_t mt = time(NULL) - tpd * 24 * 3600;
  1042. p = dirs;
  1043. while (xs_list_iter(&p, &v)) {
  1044. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1045. xs *files = xs_glob(spec2, 0, 0);
  1046. char *p2, *v2;
  1047. p2 = files;
  1048. while (xs_list_iter(&p2, &v2)) {
  1049. int n_link;
  1050. /* old and with no hard links? */
  1051. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1052. xs *s1 = xs_replace(v2, ".json", "");
  1053. xs *l = xs_split(s1, "/");
  1054. char *md5 = xs_list_get(l, -1);
  1055. object_del_by_md5(md5);
  1056. }
  1057. }
  1058. }
  1059. }
  1060. void purge_user(snac *snac)
  1061. /* do the purge for this user */
  1062. {
  1063. int days;
  1064. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1065. _purge_subdir(snac, "timeline", days);
  1066. _purge_subdir(snac, "hidden", days);
  1067. _purge_subdir(snac, "private", days);
  1068. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1069. _purge_subdir(snac, "local", days);
  1070. _purge_subdir(snac, "public", days);
  1071. }
  1072. void purge_all(void)
  1073. /* purge all users */
  1074. {
  1075. snac snac;
  1076. xs *list = user_list();
  1077. char *p, *uid;
  1078. p = list;
  1079. while (xs_list_iter(&p, &uid)) {
  1080. if (user_open(&snac, uid)) {
  1081. purge_user(&snac);
  1082. user_free(&snac);
  1083. }
  1084. }
  1085. purge_server();
  1086. }