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