data.c 34 KB

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